Skip to content
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

Fix path traversal vulnerability #100

Merged
merged 4 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions jupyter_archive/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ def extract_archive(self, archive_path):
self.log.info("Begin extraction of {} to {}.".format(archive_path, archive_destination))

archive_reader = make_reader(archive_path)

if isinstance(archive_reader, tarfile.TarFile):
# Check file path to avoid path traversal
# See https://nvd.nist.gov/vuln/detail/CVE-2007-4559
with archive_reader as archive:
for name in archive_reader.getnames():
if os.path.relpath(archive_destination / name, archive_destination).startswith(os.pardir):
self.log.error(f"The archive file includes an unsafe file: {name}")
raise web.HTTPError(400)
# Re-open stream
archive_reader = make_reader(archive_path)

with archive_reader as archive:
archive.extractall(archive_destination)

Expand Down
20 changes: 20 additions & 0 deletions jupyter_archive/tests/test_archive_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,23 @@ async def test_extract_failure(jp_fetch, jp_root_dir, format, mode):
await jp_fetch("extract-archive", archive_path.relative_to(jp_root_dir).as_posix(), method="GET")
assert e.type == HTTPClientError
assert not archive_dir_path.exists()


@pytest.mark.parametrize(
"file_path",
[
("../../../../../../../../../../tmp/test"),
("../test"),
Comment on lines +215 to +216
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

],
)
async def test_extract_path_traversal(jp_fetch, jp_root_dir, file_path):
unsafe_file_path = jp_root_dir / "test"
archive_path = jp_root_dir / "test.tar.gz"
open(unsafe_file_path, 'a').close()
with tarfile.open(archive_path, "w:gz") as tf:
tf.add(unsafe_file_path, file_path)

with pytest.raises(Exception) as e:
await jp_fetch("extract-archive", archive_path.relative_to(jp_root_dir).as_posix(), method="GET")
assert e.type == HTTPClientError
assert e.value.code == 400