fix: prevent path traversal in apkg media import#5153
Open
chrislongros wants to merge 2 commits into
Open
Conversation
os.path.commonprefix matches characters rather than path components. Use startswith(dir + os.sep) instead to prevent path traversal.
Contributor
|
Thanks for the PR! To ensure we're solving the right problems, we require all PRs to be linked to an open issue. Please open one describing the problem this PR addresses so we can discuss the implementation there first. You have 4 days to link an issue before this PR is automatically closed, it can be reopened at any time after that. Looking forward to the discussion!
|
Documentation build overview
25 files changed ·
|
The legacy import option was removed in ankitects#3536. Remove the dead code associated with the legacy importer.
chrislongros
force-pushed
the
fix-apkg-path-traversal
branch
from
July 17, 2026 16:23
74052c5 to
e41f234
Compare
Member
|
We might want to clean up legacy_import_export() as well? It's still referenced by the browser. |
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.
Linked issue (required)
#5167
Motivation
The
.apkgmedia usesos.path.commonprefix, which compares characters, not path components. A media filename resolving to a sibling directory that shares the media folder's name prefix passes the check, so a malicious.apkgcan write outsidecollection.media(path traversal / arbitrary file write).A second commit removes the legacy importer entirely. The legacy import option was removed in #3536, leaving pylib/anki/importing, qt/aqt/importing.py and their dialog forms unreachable.
Steps to reproduce
.apkgwhose media maps a file into a sibling directory whose name begins with the media folder's name.collection.media. After: "Invalid file".Testing performed
Tested by importing .apkg and .csv decks via File > Import and drag-and-drop on the main branch and on the PR branch. Cards import completes without problems.