Skip to content

Commit fe2403f

Browse files
committed
Try another validation approach
1 parent 6dcd101 commit fe2403f

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

pywb/apps/static_handler.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
from pywb.utils.wbexception import NotFoundException
1111

1212

13-
class PathValidationError(Exception):
14-
"""Path validation exception"""
15-
16-
1713
#=================================================================
1814
# Static Content Handler
1915
#=================================================================
@@ -33,14 +29,12 @@ def __call__(self, environ, url_str):
3329
# url = sanitize_filepath(url)
3430

3531
static_path_to_validate = None
36-
full_path = None
3732

3833
full_path = environ.get('pywb.static_dir')
3934
if full_path:
4035
static_path_to_validate = full_path
4136
full_path = os.path.join(full_path, url)
4237
if not os.path.isfile(full_path):
43-
static_path_to_validate = None
4438
full_path = None
4539

4640
if not full_path:
@@ -49,7 +43,7 @@ def __call__(self, environ, url_str):
4943

5044
try:
5145
validate_requested_file_path(static_path_to_validate, full_path)
52-
except PathValidationError:
46+
except ValueError:
5347
raise NotFoundException('Static File Not Found: ' +
5448
url_str)
5549

@@ -87,12 +81,12 @@ def __call__(self, environ, url_str):
8781
url_str)
8882

8983
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.
9385
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())
9690

9791

9892

0 commit comments

Comments
 (0)