7
7
from pywb .utils .wbexception import NotFoundException
8
8
9
9
10
+ # =================================================================
11
+ def is_subpath (parent_path , child_path ):
12
+ parent = os .path .abspath (parent_path )
13
+ child = os .path .abspath (child_path )
14
+ return os .path .commonpath ([parent , child ]) == parent
15
+
16
+
10
17
#=================================================================
11
18
# Static Content Handler
12
19
#=================================================================
@@ -23,15 +30,26 @@ def __call__(self, environ, url_str):
23
30
if url .endswith ('/' ):
24
31
url += 'index.html'
25
32
26
- full_path = environ .get ('pywb.static_dir' )
27
- if full_path :
28
- full_path = os .path .join (full_path , url )
33
+ full_path = None
34
+ env_static_dir = environ .get ('pywb.static_dir' )
35
+
36
+ if env_static_dir :
37
+ full_path = os .path .join (env_static_dir , url )
38
+
39
+ # Prevent path traversal
40
+ if not is_subpath (env_static_dir , full_path ):
41
+ raise NotFoundException ('Requested a static file outside of static_dir' )
42
+
29
43
if not os .path .isfile (full_path ):
30
44
full_path = None
31
45
32
46
if not full_path :
33
47
full_path = os .path .join (self .static_path , url )
34
48
49
+ # Prevent path traversal
50
+ if not is_subpath (self .static_path , full_path ):
51
+ raise NotFoundException ('Requested a static file outside of static_dir' )
52
+
35
53
try :
36
54
data = self .block_loader .load (full_path )
37
55
0 commit comments