Skip to content

Commit

Permalink
Try another validation approach
Browse files Browse the repository at this point in the history
  • Loading branch information
tw4l committed Nov 5, 2024
1 parent 6dcd101 commit fe2403f
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions pywb/apps/static_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
from pywb.utils.wbexception import NotFoundException


class PathValidationError(Exception):
"""Path validation exception"""


#=================================================================
# Static Content Handler
#=================================================================
Expand All @@ -33,14 +29,12 @@ def __call__(self, environ, url_str):
# url = sanitize_filepath(url)

static_path_to_validate = None
full_path = None

full_path = environ.get('pywb.static_dir')
if full_path:
static_path_to_validate = full_path
full_path = os.path.join(full_path, url)
if not os.path.isfile(full_path):
static_path_to_validate = None
full_path = None

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

try:
validate_requested_file_path(static_path_to_validate, full_path)
except PathValidationError:
except ValueError:
raise NotFoundException('Static File Not Found: ' +
url_str)

Expand Down Expand Up @@ -87,12 +81,12 @@ def __call__(self, environ, url_str):
url_str)

def validate_requested_file_path(self, static_dir, requested_path):
"""Validate that requested file path is within static dir"""
static_dir = Path(static_dir)
requested_path = Path(requested_path)
"""Validate that requested file path is within static dir.
if static_dir.resolve() not in requested_path.resolve().parents:
raise PathValidationError('Requested path forbidden')
Returns relative path starting from static_dir or raises ValueError if
requested path is not in the static directory.
"""
return Path(static_dir).joinpath(requested_path).resolve().relative_to(static_dir.resolve())



0 comments on commit fe2403f

Please sign in to comment.