Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: patch

- Prevent output paths from escaping the configured output directory.
4 changes: 4 additions & 0 deletions pelican/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,10 @@ def test_detect_root_breakout(self):
): # (.*?:)? accounts for Windows root
utils.sanitised_join("/foo/bar", "/test")

def test_reject_sibling_prefix(self):
with self.assertRaises(RuntimeError):
utils.sanitised_join("/foo/bar", "../barley/test")

def test_pass_deep_subpaths(self):
self.assertEqual(
utils.sanitised_join("/foo/bar", "test"),
Expand Down
19 changes: 15 additions & 4 deletions pelican/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@


def sanitised_join(base_directory: str, *parts: str) -> str:
joined = posixize_path(os.path.abspath(os.path.join(base_directory, *parts)))
base = posixize_path(os.path.abspath(base_directory))
if not joined.startswith(base):
joined_path = os.path.abspath(os.path.join(base_directory, *parts))
base_path = os.path.abspath(base_directory)
real_joined_path = os.path.realpath(joined_path)
real_base_path = os.path.realpath(base_path)
try:
is_within_base = (
os.path.commonpath((real_base_path, real_joined_path)) == real_base_path
)
except ValueError:
# On Windows, paths on different drives have no common path.
is_within_base = False

if not is_within_base:
joined = posixize_path(joined_path)
raise RuntimeError(f"Attempted to break out of output directory to {joined}")

return joined
return posixize_path(joined_path)


def strftime(date: datetime.datetime, date_format: str) -> str:
Expand Down