fix: replace string-prefix check in safe_extractall with commonpath - #324
Merged
Gi-ron merged 1 commit intoJul 28, 2026
Merged
Conversation
`startswith` is the wrong primitive for "is target inside directory base": once a trailing separator drops anywhere along the way, sibling directories whose names extend base match. We could spot-fix by re-appending the separator before the check, but `commonpath` makes the directory-boundary intent explicit and removes the failure mode entirely. Fixes GHSA-6cmm-8875-5pcw. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> (cherry picked from commit 97de058)
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
_is_bad_pathwithos.path.commonpath, closing a path traversal insafe_extractallContext
The official fix landed on Ulmo, Verawood and master but did not include Teak. Since
extract_archive.pyis unchanged in our Teak branch relative to upstream, this cherry-pick applies cleanly with no conflicts._is_bad_pathvalidated extraction targets withresolved(joinpath(base, path)).startswith(base).safe_extractallappends a trailing separator tooutput_path, but_checkmembersimmediately callsresolved(base), which normalizes it away. The check then matched/data/<base64>as a string prefix of/data/<base64>evil/..., letting a crafted.tar.gzentry such as../<base64>evil/filewrite outside the intended extraction directory.Reached via the Studio course/library import flow (
import_olx), which requires theCOURSES_IMPORT_COURSEpermission — so the attacker must be a course author or staff. Only.tar.gzis practically exploitable, sinceZipFile.extractallstrips..segments independently. Realistic impact is limited filesystem corruption and cross-tenant pollution of course import staging directories.The fix uses
commonpathrather than re-appending the separator, making the directory-boundary intent explicit and removing the failure mode entirely. AValueErrorguard treats incomparable paths (mixed absolute/relative, different drives) as bad.Cherry-picked with
-x; applied cleanly with no conflicts