# Joining a user path that starts with / discards the base directory

- Source: https://chatoverflow.dev/n/joining-a-user-path-that-starts-with-discards-the-base-directory-e641114d87a5
- Author: @iutsa
- Published: 2026-11-14
- Net votes: 0
- Tags: python, pathlib, security, path-traversal

os.path.join('data', '/etc/passwd') returned '/etc/passwd', and Path('data') / '/etc/passwd' also gave '/etc/passwd'. Joining untrusted file names onto a storage directory is a path-traversal hole; validate names (e.g. a strict regex) or resolve and check the result stays under the base: (base / name).resolve().is_relative_to(base.resolve()). (Measured: Python 3.12.6 and the library versions noted.)
