Skip to content

Python: Should the py/tarslip GOOD example and sanitizer account for link members? #23132

Open

Description

@wildoranges

Description of the issue

The py/tarslip query help's GOOD example checks entry.name with os.path.isabs(entry.name) or ".." in entry.name. In the Python 3.10, 3.12 and 3.13 versions tested below, that example still writes outside the destination directory when the archive contains symbolic or hard links. I'm wondering whether the help should account for link members, and whether this case falls within the intended scope of TarFileInfoSanitizer.

I understand that this sanitizer is deliberately heuristic. The review of #9551 already noted that matching path-check names might be too broad, and the implementation retains that caveat. The case here is a concrete link bypass of the check recommended in the help: a symbolic link link → .. followed by link/pwned.txt, or a hard link hl → ../victim.txt followed by a regular member hl. Neither archive has an absolute member name or .. in any member name; the escaping target is in linkname.

Affected sanitizer

TarFileInfoSanitizer recognizes a call with info.name as an argument when a name or attribute within the call expression matches %path, and uses its false branch as a barrier guard for the member. This includes os.path.isabs(info.name) through the path attribute. The query help's GOOD example is tarslip_good.py, and the query test uses the same check in safemembers, with the comment "safe -- we assume safemembers makes up for the unsafe filter" at L115-L116.

Steps to reproduce

The help's GOOD example, unchanged:

import sys
import tarfile
import os.path

with tarfile.open(sys.argv[1]) as tar:
    for entry in tar:
        #GOOD: Check that entry is safe
        if os.path.isabs(entry.name) or ".." in entry.name:
            raise ValueError("Illegal tar archive entry")
        tar.extract(entry, "/tmp/unpack/")

The safemembers check from the query test:

# The `safemembers` pattern from python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py (lines 47-54)
import sys
import tarfile
import os.path

def safemembers(members):
    for info in members:
        if os.path.isabs(info.name) or ".." in info.name:
            raise
        yield info

tar = tarfile.open(sys.argv[1])
tar.extractall("/tmp/unpack/", members=safemembers(tar))

py/tarslip (and python-security-extended.qls) reports neither file; it reports only the help's BAD example. I ran both files unchanged except for the destination directory, on the two archives above. For the hard-link case, I first created victim.txt in the parent of the destination directory; extracting hl creates a hard link to that existing file, and the following regular member overwrites it. The results were:

Python symlink, help example symlink, safemembers hard link, help example hard link, safemembers DeprecationWarning
3.10.12 ../pwned.txt written ../pwned.txt written ../victim.txt overwritten ../victim.txt overwritten no
3.12.3 ../pwned.txt written ../pwned.txt written ../victim.txt overwritten ../victim.txt overwritten yes
3.13.16 ../pwned.txt written ../pwned.txt written ../victim.txt overwritten ../victim.txt overwritten yes
3.14.8 LinkOutsideDestinationError, nothing written LinkOutsideDestinationError, nothing written LinkOutsideDestinationError, nothing written LinkOutsideDestinationError, nothing written no

On the tested Python 3.10, 3.12 and 3.13 versions, the GOOD example writes outside the destination. On Python 3.14.8, the default "data" filter rejects both archives; the member-name check itself still permits their names.

The same name-only checks have led to real vulnerabilities recently: GHSA-9q28-ghcr-c4x3 (PraisonAI, CVE-2026-44340), GHSA-9xq3-3fqg-4vg7 (proot-distro, CVE-2026-54574) and GHSA-m6w7-qv66-g3mf (BentoML, CVE-2026-27905).

Question

Would it be reasonable to update the help's GOOD example and recommendation to account for link members, for example by recommending filter="data" where supported (already recognized by the query since #14918), and explaining the limitations of checking only member names?

Separately, is suppressing alerts for these link-capable members an accepted limitation of the sanitizer heuristic, or would it be reasonable to narrow the sanitizer so that a member-name check alone is insufficient unless links are also rejected or safely handled? I understand that recognizing custom validation involves a precision/coverage trade-off; even if the sanitizer remains unchanged, clarifying the help would address the demonstrated bypass of its GOOD example.

Environment

  • CodeQL CLI 2.27.4 (codeql/python-queries 1.9.0); CodeQL CLI 2.27.2 gives the same results
  • CodeQL repository commit for the links above: fbf9fca5ce7c3b64c2f5d719ea0444577850cb4a (the files above are identical in 2.27.2, 2.27.4 and this commit)
  • Python 3.10.12, 3.12.3 (Ubuntu), 3.13.16 and 3.14.8 on Linux/amd64

Activity

  1. jketema commented on Nov 16, 2026

    @jketema
    Contributor

    Hi @wildoranges,

    Thanks for your report. You're right that they should probably account for hard links and symlinks. I'm doubtful this can easily be fixed on the query-side.

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

Metadata

Metadata

Assignees

No one assigned

    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