10
10
from pywb .utils .wbexception import NotFoundException
11
11
12
12
13
- class PathValidationError (Exception ):
14
- """Path validation exception"""
15
-
16
-
17
13
#=================================================================
18
14
# Static Content Handler
19
15
#=================================================================
@@ -33,14 +29,12 @@ def __call__(self, environ, url_str):
33
29
# url = sanitize_filepath(url)
34
30
35
31
static_path_to_validate = None
36
- full_path = None
37
32
38
33
full_path = environ .get ('pywb.static_dir' )
39
34
if full_path :
40
35
static_path_to_validate = full_path
41
36
full_path = os .path .join (full_path , url )
42
37
if not os .path .isfile (full_path ):
43
- static_path_to_validate = None
44
38
full_path = None
45
39
46
40
if not full_path :
@@ -49,7 +43,7 @@ def __call__(self, environ, url_str):
49
43
50
44
try :
51
45
validate_requested_file_path (static_path_to_validate , full_path )
52
- except PathValidationError :
46
+ except ValueError :
53
47
raise NotFoundException ('Static File Not Found: ' +
54
48
url_str )
55
49
@@ -87,12 +81,12 @@ def __call__(self, environ, url_str):
87
81
url_str )
88
82
89
83
def validate_requested_file_path (self , static_dir , requested_path ):
90
- """Validate that requested file path is within static dir"""
91
- static_dir = Path (static_dir )
92
- requested_path = Path (requested_path )
84
+ """Validate that requested file path is within static dir.
93
85
94
- if static_dir .resolve () not in requested_path .resolve ().parents :
95
- raise PathValidationError ('Requested path forbidden' )
86
+ Returns relative path starting from static_dir or raises ValueError if
87
+ requested path is not in the static directory.
88
+ """
89
+ return Path (static_dir ).joinpath (requested_path ).resolve ().relative_to (static_dir .resolve ())
96
90
97
91
98
92
0 commit comments