Repository navigation
Description
Description of the issue
py/tarslip does not report TarFile.extractall when the value of its members argument is None, although the call then extracts every member of the archive. Since #23119 (the fix for #23114, merged and included in the 2.27.5 release branch), this also applies when members is passed positionally, so the safe_extract helper that many projects added for CVE-2007-4559 is no longer reported, including the vulnerable code of two published CVEs.
TarFile.extractall(path=".", members=None, *, numeric_owner=False, filter=None) extracts all members when members is None (docs). On main, ExtractAllSink makes the archive a sink only when there is no members argument, and ExtractMembersSink tracks taint into the members argument. When the value of members is None, for example a wrapper parameter with a None default or a literal None, neither of them applies: the call extracts the whole archive, but there is no member list for taint to reach. The keyword form members=members was already not reported in 2.27.4; after #23119 the positional form behaves the same way.
Affected sinks
ExtractAllSinkrequiresnot exists(call.getParameter(1, "members")), so anymembersargument, including one that is alwaysNone, removes the sink.ExtractMembersSinkonly applies when tainted data reachesmembers.- For comparison,
hasUnsafeFilteralready treats aNonevalue that reachesfilterlike a missingfilter.
Steps to reproduce
unpack.py:
import os
import tarfile
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner=numeric_owner) # (1) positional members, None by default
def safe_extract_kw(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members=members, numeric_owner=numeric_owner) # (2) keyword members, None by default
def unpack_helper(archive, dest):
with tarfile.open(archive) as tar:
safe_extract(tar, dest)
def unpack_helper_kw(archive, dest):
with tarfile.open(archive) as tar:
safe_extract_kw(tar, dest)
def unpack_none(archive, dest):
with tarfile.open(archive) as tar:
tar.extractall(dest, None) # (3) literal None
def unpack_plain(archive, dest):
with tarfile.open(archive) as tar:
tar.extractall(dest) # (4) no members argumentcodeql database create db --language=python --source-root=.
codeql database analyze db codeql/python-queries:Security/CWE-022/TarSlip.ql --format=csv --output=2.27.4.csv
# main: run the query from a checkout of 57bace85f49150248166ec49587bcbb1fa53c440
codeql database analyze db <checkout>/python/ql/src/Security/CWE-022/TarSlip.ql --additional-packs=<checkout> --format=csv --output=main.csv| Line | Call | 2.27.4 | main |
|---|---|---|---|
| 17 | (1) tar.extractall(path, members, ...), members defaults to None |
reported | not reported |
| 25 | (2) tar.extractall(path, members=members, ...), members defaults to None |
not reported | not reported |
| 40 | (3) tar.extractall(dest, None) |
reported | not reported |
| 45 | (4) tar.extractall(dest) |
reported | reported |
I ran all four functions on two archives: one with a member named ../dest-evil/pwned.txt, which passes the commonprefix check, and one with a symlink link -> ../outside followed by link/pwned.txt. On Python 3.10.12, 3.12.3 and 3.13.16, all four functions write pwned.txt outside dest for both archives (3.12 and 3.13 emit the DeprecationWarning about the default filter). On Python 3.14.8, the default data filter rejects both archives in all four functions.
The helper is common. A GitHub code search for the exact line tar.extractall(path, members, numeric_owner=numeric_owner) finds about 1,700 Python files, and the same line with members=members finds 3. Two published CVEs have this form, and in both the helper is called without members:
| Advisory | Vulnerable code | Bypass of the name check | 2.27.4 | main |
|---|---|---|---|---|
| CVE-2026-35592 (pyLoad) | UnTar.py L22, called at L81 |
os.path.commonprefix compares characters |
reported | not reported |
| CVE-2026-104103 (Ludwig) | archives.py L134, called at L136 |
link members are not checked | reported | not reported |
If I delete the members argument from that line in both files (it is always None there, so the same members are extracted), main reports both lines again. So it is only the members argument, not the name check in the helper, that stops the alert.
Expected behavior
A members argument whose value can be None should be handled like a missing one, so that ExtractAllSink applies to the receiver, in the same way that hasUnsafeFilter already handles a None value for filter. In the example above, lines 17, 25 and 40 should be reported like line 45.
Possible change
One possible change: keep the receiver as a sink when the value reaching members is a literal None or a parameter whose default is None.
--- a/python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll
+++ b/python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll
@@ -96,7 +96,16 @@
.getMember("extractall")
.getACall() and
hasUnsafeFilter(call) and
- not exists(call.getParameter(1, "members")) and
+ (
+ not exists(call.getParameter(1, "members"))
+ or
+ // `members` may be `None`: a literal `None`, or a parameter whose default is `None`
+ exists(DataFlow::Node v | v = call.getParameter(1, "members").getAValueReachingSink() |
+ v.asExpr() instanceof None
+ or
+ v.(DataFlow::ParameterNode).getParameter().getDefault() instanceof None
+ )
+ ) and
this = call.(DataFlow::MethodCallNode).getObject()
)
}With this change against current main, all four lines of unpack.py above (17, 25, 40 and 45) are reported, the pyLoad and Ludwig lines are reported again (in both the positional and the members=members form), and python/ql/test/query-tests/Security/CWE-022-TarSlip still passes with an unchanged .expected. A filtered list such as members=safemembers(tar) is not affected, since its value is a call rather than None. A parameter with a None default is treated as possibly None even when every caller passes a list; that seems the safer side here, but it is a choice.
Environment
- CodeQL CLI 2.27.4 (
codeql/python-queries1.9.0) - CodeQL main
57bace85f49150248166ec49587bcbb1fa53c440for the links above and the "main" column (query and library from a checkout of this commit, run with CLI 2.27.4) - Python 3.10.12, 3.12.3 (Ubuntu), 3.13.16 and 3.14.8 on Linux/amd64
Activity
jketema commentedon Nov 17, 2026
Hi @wildoranges,
Thanks. I've relayed this to the responsible team.
owen-mc commentedon Nov 19, 2026
@wildoranges Thanks for spotting this and letting us know. I've prepared a fix based on your suggestion, with one simplification: the existing value tracking already follows parameter defaults, including across function boundaries. A
Nonedefault on a wrapper parameter therefore appears as a reachingNoneliteral. The separateParameterNode/getDefault()check isn't needed.