Skip to content

Build: Fix exceptions #10616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions readthedocs/core/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def safe_open(
)

if path.exists() and not path.is_file():
raise FileIsNotRegularFile(path)
raise FileIsNotRegularFile()

if not allow_symlinks and path.is_symlink():
log.info("Skipping file becuase it's a symlink.")
raise UnsupportedSymlinkFileError(path)
raise UnsupportedSymlinkFileError()

# Expand symlinks.
resolved_path = path.resolve()
Expand All @@ -89,17 +89,21 @@ def safe_open(
file_size = resolved_path.stat().st_size
if file_size > max_size_bytes:
log.info("File is too large.", size_bytes=file_size)
raise FileTooLarge(path)
raise FileTooLarge()

if allow_symlinks and base_path:
base_path = Path(base_path).absolute()
if not resolved_path.is_relative_to(base_path):
# Trying to path traversal via a symlink, sneaky!
log.info("Path traversal via symlink.")
raise SymlinkOutsideBasePath(path)
raise SymlinkOutsideBasePath()

_assert_path_is_inside_docroot(resolved_path)

# The encoding is valid only if the file opened is a text file,
# this functions is used to read both types of files (text and binary),
# so we can't specify the encoding here.
# pylint: disable=unspecified-encoding
return resolved_path.open(*args, **kwargs)


Expand Down