File tree 2 files changed +24
-2
lines changed
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:
71
71
return traversal_safe_path (IDOM_WEB_MODULES_DIR .current , * path .split ("/" ))
72
72
73
73
74
- def traversal_safe_path (root : Path , * unsafe : str | Path ) -> Path :
74
+ def traversal_safe_path (root : str | Path , * unsafe : str | Path ) -> Path :
75
75
"""Raise a ``ValueError`` if the ``unsafe`` path resolves outside the root dir."""
76
- root = root .resolve ()
76
+ root = Path ( root ) .resolve ()
77
77
# resolve relative paths and symlinks
78
78
path = root .joinpath (* unsafe ).resolve ()
79
79
Original file line number Diff line number Diff line change 2
2
import threading
3
3
import time
4
4
from contextlib import ExitStack
5
+ from pathlib import Path
5
6
6
7
import pytest
7
8
from playwright .async_api import Page
10
11
from idom .server import flask as flask_implementation
11
12
from idom .server .utils import find_available_port
12
13
from idom .server .utils import run as sync_run
14
+ from idom .server .utils import traversal_safe_path
13
15
from tests .tooling .loop import open_event_loop
14
16
15
17
@@ -49,3 +51,23 @@ def run_in_thread():
49
51
50
52
await page .goto (url )
51
53
await page .wait_for_selector ("#sample" )
54
+
55
+
56
+ @pytest .mark .parametrize (
57
+ "bad_path" ,
58
+ [
59
+ "../escaped" ,
60
+ "ok/../../escaped" ,
61
+ "ok/ok-again/../../ok-yet-again/../../../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