Closed
Description
Hi,
Currently (python 3.10.6 & 3.11.0):
from pathlib import Path
p = Path('/var/log/../../opt')
p.is_relative_to('/var/log')
>>> True
p = p.resolve()
p.is_relative_to('/var/log')
>>> False
Once you know is_relative_to
uses relative_to
, this makes more sense but it's not obvious from the documentation and the examples given. Also it can easily lead to code that looks secure but isn't. Case in point, I was tasked with reviewing this code today (simplified for illustration purposes):
path = Path(ROOT_PATH, user_input_rel_path)
if path.is_relative_to(ROOT_PATH):
path.unlink()
else:
raise PermissionError('Nope!')
I was unsure if I should open a bug or not because one could easily argue it isn't a bug. I do believe however that a warning in the documentation could save a few devs from making a mistake.