Guard tar/zip extraction against path-traversal (CVE-2007-4559)#2458
Open
dsblank wants to merge 2 commits into
Open
Guard tar/zip extraction against path-traversal (CVE-2007-4559)#2458dsblank wants to merge 2 commits into
dsblank wants to merge 2 commits into
Conversation
Gramps package (.gpkg) import extracted archive members with TarFile.extract() without checking their paths. A crafted .gpkg file could use an absolute path, a "../" path-traversal name, or a symlink/hardlink to create or overwrite files outside the intended media directory. Each archive member's resolved path is now validated before extraction, and extraction is aborted if any member would escape the target directory. Fixes #12911.
Contributor
|
@DavidMStraub |
Member
|
.gpkg is disallowed in Gramps Web API precisely because it's not safe. There was a path traversal vulnerability in the media importer though, see GHSA-m5gr-86j6-99jp and gramps-project/gramps-web-api@3ed4342. |
The addon/plugin installer (load_addon_file() in gramps/gen/plug/utils.py) downloads or opens a .zip or .tar.gz addon package and extracts it into the user's plugin directory. Neither the custom Zipfile.extractall() wrapper nor the tarfile.TarFile.extractall() call validated member paths, so a crafted or MITM'd addon package could use an absolute path, a "../" path-traversal name, or a symlink/hardlink to create or overwrite files outside the intended plugin directory (the same class of bug as CVE-2007-4559, aka "Zip Slip"). The path-traversal checks added for the .gpkg importer in the previous commit have been factored out into a shared gramps.gen.utils.safearchive module and are now also applied here before any zip or tar member is extracted. Fixes #12911.
Member
Author
|
Thank you @emyoulation for mentioning zip importer (in general). I have updated the PR to fix all known security issues in gramps zip/tar importing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
.gpkgimporter (gramps/plugins/importer/importgpkg.py) extracted archive members withtarfile.TarFile.extract()without validating their paths, vulnerable to CVE-2007-4559 / "Zip Slip" (see also python/cpython#73974).load_addon_file()ingramps/gen/plug/utils.py) has the same class of bug in two places: the customZipfile.extractall()wrapper (used for.zipaddon packages) and a baretarfile.TarFile.extractall()call (used for.tar.gz/.tgzaddon packages). This is arguably higher risk than the.gpkgcase since addon packages are fetched from a URL (the Gramps addon server or a configured mirror) rather than a file the user deliberately imports.../path-traversal member name, or a symlink/hardlink to create or overwrite files outside the intended target directory (media directory for.gpkg, plugin directory for addons).gramps/gen/utils/safearchive.pymodule (is_safe_archive_member(),is_safe_tar_member()) and applied it at all three extraction sites before any member is written to disk. This is a self-contained workaround that does not rely on Python 3.12+'sfilter=extraction parameter (PEP 706), so it works across all Python versions Gramps supports (3.10+).Fixes #12911.
Test plan
gramps/gen/utils/test/safearchive_test.py— unit tests for the shared helpers (absolute paths,../traversal, symlink/hardlink escapes).gramps/plugins/importer/test/importgpkg_test.py— integration-style test against a crafted malicious tar archive.Zipfile.extractall()(raisesValueError) and via the addon-installer's tar-member pre-check; a benign zip with a real directory entry still extracts correctly (no regression).python3 -m unittest gramps.gen.utils.test.safearchive_test gramps.plugins.importer.test.importgpkg_test -v— all pass.black --checkon all changed files.mypy(full project) — no issues.🤖 Generated with Claude Code