File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -71,9 +71,9 @@ def safe_web_modules_dir_path(path: str) -> Path:
7171 return traversal_safe_path (IDOM_WEB_MODULES_DIR .current , * path .split ("/" ))
7272
7373
74- def traversal_safe_path (root : Path , * unsafe : str | Path ) -> Path :
74+ def traversal_safe_path (root : str | Path , * unsafe : str | Path ) -> Path :
7575 """Raise a ``ValueError`` if the ``unsafe`` path resolves outside the root dir."""
76- root = root .resolve ()
76+ root = Path ( root ) .resolve ()
7777 # resolve relative paths and symlinks
7878 path = root .joinpath (* unsafe ).resolve ()
7979
Original file line number Diff line number Diff line change 22import threading
33import time
44from contextlib import ExitStack
5+ from pathlib import Path
56
67import pytest
78from playwright .async_api import Page
1011from idom .server import flask as flask_implementation
1112from idom .server .utils import find_available_port
1213from idom .server .utils import run as sync_run
14+ from idom .server .utils import traversal_safe_path
1315from tests .tooling .loop import open_event_loop
1416
1517
@@ -49,3 +51,23 @@ def run_in_thread():
4951
5052 await page .goto (url )
5153 await page .wait_for_selector ("#sample" )
54+
55+
56+ @pytest .mark .parametrize (
57+ "bad_path" ,
58+ [
59+ "../escaped" ,
60+ "ok/../../escaped" ,
61+ "ok/../root/../../escaped" ,
62+ ],
63+ )
64+ def test_catch_unsafe_relative_path_traversal (tmp_path , bad_path ):
65+ with pytest .raises (ValueError , match = "Unsafe path" ):
66+ traversal_safe_path (tmp_path , * bad_path .split ("/" ))
67+
68+
69+ def test_catch_unsafe_symlink_path_traversal (tmp_path ):
70+ symlink : Path = tmp_path / "file.txt"
71+ symlink .symlink_to (tmp_path .parent / "escaped-file.txt" )
72+ with pytest .raises (ValueError , match = "Unsafe path" ):
73+ traversal_safe_path (tmp_path , "file.txt" )
You can’t perform that action at this time.
0 commit comments