Skip to content

fix: prevent path traversal in apkg media import#5153

Open
chrislongros wants to merge 2 commits into
ankitects:mainfrom
chrislongros:fix-apkg-path-traversal
Open

fix: prevent path traversal in apkg media import#5153
chrislongros wants to merge 2 commits into
ankitects:mainfrom
chrislongros:fix-apkg-path-traversal

Conversation

@chrislongros

@chrislongros chrislongros commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Linked issue (required)

#5167

Motivation

The .apkg media uses os.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 .apkg can write outside collection.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

  1. Make an .apkg whose media maps a file into a sibling directory whose name begins with the media folder's name.
  2. Import it.
  3. Before: written outside 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.

os.path.commonprefix matches
characters rather than path components. Use startswith(dir + os.sep)
instead to prevent path traversal.
@github-actions

Copy link
Copy Markdown
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!

To link an issue, edit the PR description and add a line like Closes #123.

The legacy import option was removed in ankitects#3536. Remove the dead code
associated with the legacy importer.
@chrislongros
chrislongros force-pushed the fix-apkg-path-traversal branch from 74052c5 to e41f234 Compare July 17, 2026 16:23
@dae

dae commented Jul 18, 2026

Copy link
Copy Markdown
Member

We might want to clean up legacy_import_export() as well? It's still referenced by the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants