Skip to content

Commit 9f0ef7a

Browse files
authored
Refuse to serve static files that are outside of static_dir (#932)
Prevents the path traversal attack reported in #931
1 parent 0de883e commit 9f0ef7a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pywb/apps/static_handler.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
from pywb.utils.wbexception import NotFoundException
88

99

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+
1017
#=================================================================
1118
# Static Content Handler
1219
#=================================================================
@@ -23,15 +30,26 @@ def __call__(self, environ, url_str):
2330
if url.endswith('/'):
2431
url += 'index.html'
2532

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+
2943
if not os.path.isfile(full_path):
3044
full_path = None
3145

3246
if not full_path:
3347
full_path = os.path.join(self.static_path, url)
3448

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+
3553
try:
3654
data = self.block_loader.load(full_path)
3755

0 commit comments

Comments
 (0)