Skip to content

Python: py/tarslip misses TarFile.extractall calls whose members argument is None, including the CVE-2007-4559 safe_extract helper #23153

Backlog

Description

@wildoranges

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

  • ExtractAllSink requires not exists(call.getParameter(1, "members")), so any members argument, including one that is always None, removes the sink.
  • ExtractMembersSink only applies when tainted data reaches members.
  • For comparison, hasUnsafeFilter already treats a None value that reaches filter like a missing filter.

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 argument
codeql 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-queries 1.9.0)
  • CodeQL main 57bace85f49150248166ec49587bcbb1fa53c440 for 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

  1. jketema commented on Nov 17, 2026

    @jketema
    Contributor

    Hi @wildoranges,

    Thanks. I've relayed this to the responsible team.

  2. self-assigned this
    on Nov 19, 2026
  3. owen-mc commented on Nov 19, 2026

    @owen-mc
    Contributor

    @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 None default on a wrapper parameter therefore appears as a reaching None literal. The separate ParameterNode / getDefault() check isn't needed.

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

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions