fix: inside_base_dir raises TypeError on a non-string path instead of failing closed (#1619)
This commit is contained in:
@@ -22,6 +22,8 @@ def abs_join(base_dir: str, rel: str) -> str:
|
|||||||
|
|
||||||
def inside_base_dir(base_dir: str, path: str) -> bool:
|
def inside_base_dir(base_dir: str, path: str) -> bool:
|
||||||
"""Check if path is inside base directory."""
|
"""Check if path is inside base directory."""
|
||||||
|
if not isinstance(base_dir, str) or not isinstance(path, str):
|
||||||
|
return False
|
||||||
base = os.path.realpath(base_dir)
|
base = os.path.realpath(base_dir)
|
||||||
p = os.path.realpath(path)
|
p = os.path.realpath(path)
|
||||||
try:
|
try:
|
||||||
|
|||||||
19
tests/test_inside_base_dir_nonstring.py
Normal file
19
tests/test_inside_base_dir_nonstring.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
"""Regression: inside_base_dir must fail closed on a non-string input.
|
||||||
|
|
||||||
|
The `os.path.realpath(path)` calls run before the try/except (which only wraps
|
||||||
|
commonpath), so a None / non-string path raised TypeError out of this
|
||||||
|
path-safety check instead of returning False.
|
||||||
|
"""
|
||||||
|
from src.app_helpers import inside_base_dir
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_string_fails_closed():
|
||||||
|
assert inside_base_dir("/tmp", None) is False
|
||||||
|
assert inside_base_dir("/tmp", 123) is False
|
||||||
|
assert inside_base_dir(None, "/tmp/x") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_real_containment_still_works(tmp_path):
|
||||||
|
base = str(tmp_path)
|
||||||
|
assert inside_base_dir(base, str(tmp_path / "a.txt")) is True
|
||||||
|
assert inside_base_dir(base, "/etc/passwd") is False
|
||||||
Reference in New Issue
Block a user