From d773505c4360148b22b428e3cafb2a7add216080 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 19 Nov 2026 11:16:26 +0000 Subject: [PATCH 1/3] Python: Add tests for tar extraction with None members Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Security/CWE-022-TarSlip/tarslip.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py b/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py index de87f02351fe..e8be2e007d5d 100644 --- a/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py +++ b/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py @@ -126,3 +126,32 @@ def safemembers(members): tar = tarfile.open(unsafe_filename_tar) tar.extractall("/tmp/unpack/", safemembers(tar)) # safe + +# `None` selects all archive members, just like an omitted `members` argument. +tar = tarfile.open(unsafe_filename_tar) # $ MISSING: Source +tar.extractall("/tmp/unpack/", None) # $ MISSING: Alert +tar.extractall("/tmp/unpack/", members=None) # $ MISSING: Alert + +members = None +tar.extractall("/tmp/unpack/", members) # $ MISSING: Alert + +tar.extractall("/tmp/unpack/", None, filter=None) # $ MISSING: Alert +tar.extractall("/tmp/unpack/", members=None, filter="fully_trusted") # $ MISSING: Alert +tar.extractall("/tmp/unpack/", members=None, filter="data") # safe +tar.extractall("/tmp/unpack/", []) # safe +tar.extractall("/tmp/unpack/", members=[]) # safe + + +def extract_with_default_members(tar, path=".", members=None, *, numeric_owner=False): + tar.extractall(path, members, numeric_owner=numeric_owner) # $ MISSING: Alert + + +def extract_with_default_members_kw(tar, path=".", *, members=None, numeric_owner=False): + tar.extractall(path, members=members, numeric_owner=numeric_owner) # $ MISSING: Alert + + +tar = tarfile.open(unsafe_filename_tar) # $ MISSING: Source +extract_with_default_members(tar, "/tmp/unpack/") + +tar = tarfile.open(unsafe_filename_tar) # $ MISSING: Source +extract_with_default_members_kw(tar, "/tmp/unpack/") From ab8d84792be8fea5c252df2b36cbf351218b6c69 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 19 Nov 2026 11:17:12 +0000 Subject: [PATCH 2/3] Python: Detect tar extraction with None members Treat extractall members values that may be None like an omitted members argument, while retaining extraction filter checks. Fixes github/codeql#23153. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../dataflow/TarSlipCustomizations.qll | 8 +++- .../Security/CWE-022-TarSlip/TarSlip.expected | 38 +++++++++++++++++++ .../Security/CWE-022-TarSlip/tarslip.py | 20 +++++----- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll b/python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll index 675f48a9442a..ac79d214c904 100644 --- a/python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll +++ b/python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll @@ -84,7 +84,7 @@ module TarSlip { * A sink capturing method calls to `extractall`. * * For a call to `file.extractall`, `file` is considered a sink if - * there is no `members` argument and the extraction filter is unsafe. + * the `members` argument is omitted or may be `None`, and the extraction filter is unsafe. */ class ExtractAllSink extends Sink { ExtractAllSink() { @@ -96,7 +96,11 @@ module TarSlip { .getMember("extractall") .getACall() and hasUnsafeFilter(call) and - not exists(call.getParameter(1, "members")) and + ( + not exists(call.getParameter(1, "members")) + or + call.getParameter(1, "members").getAValueReachingSink().asExpr() instanceof None + ) and this = call.(DataFlow::MethodCallNode).getObject() ) } diff --git a/python/ql/test/query-tests/Security/CWE-022-TarSlip/TarSlip.expected b/python/ql/test/query-tests/Security/CWE-022-TarSlip/TarSlip.expected index 734b85d69236..14b253cf92a1 100644 --- a/python/ql/test/query-tests/Security/CWE-022-TarSlip/TarSlip.expected +++ b/python/ql/test/query-tests/Security/CWE-022-TarSlip/TarSlip.expected @@ -10,6 +10,13 @@ | tarslip.py:113:24:113:26 | ControlFlowNode for tar | tarslip.py:112:7:112:39 | ControlFlowNode for Attribute() | tarslip.py:113:24:113:26 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:112:7:112:39 | ControlFlowNode for Attribute() | potentially untrusted source | | tarslip.py:121:24:121:28 | ControlFlowNode for entry | tarslip.py:119:7:119:39 | ControlFlowNode for Attribute() | tarslip.py:121:24:121:28 | ControlFlowNode for entry | This file extraction depends on a $@. | tarslip.py:119:7:119:39 | ControlFlowNode for Attribute() | potentially untrusted source | | tarslip.py:125:32:125:34 | ControlFlowNode for tar | tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | tarslip.py:125:32:125:34 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:132:1:132:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:132:1:132:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:133:1:133:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:133:1:133:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:136:1:136:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:136:1:136:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:138:1:138:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:138:1:138:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:139:1:139:3 | ControlFlowNode for tar | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:139:1:139:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:146:5:146:7 | ControlFlowNode for tar | tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | tarslip.py:146:5:146:7 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | potentially untrusted source | +| tarslip.py:150:5:150:7 | ControlFlowNode for tar | tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | tarslip.py:150:5:150:7 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | potentially untrusted source | edges | tarslip.py:14:1:14:3 | ControlFlowNode for tar | tarslip.py:15:1:15:3 | ControlFlowNode for tar | provenance | | | tarslip.py:14:7:14:39 | ControlFlowNode for Attribute() | tarslip.py:14:1:14:3 | ControlFlowNode for tar | provenance | | @@ -38,6 +45,20 @@ edges | tarslip.py:120:5:120:9 | ControlFlowNode for entry | tarslip.py:121:24:121:28 | ControlFlowNode for entry | provenance | | | tarslip.py:124:1:124:3 | ControlFlowNode for tar | tarslip.py:125:32:125:34 | ControlFlowNode for tar | provenance | | | tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | tarslip.py:124:1:124:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:132:1:132:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:133:1:133:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:136:1:136:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:138:1:138:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:131:1:131:3 | ControlFlowNode for tar | tarslip.py:139:1:139:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | tarslip.py:131:1:131:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:145:34:145:36 | ControlFlowNode for tar | tarslip.py:146:5:146:7 | ControlFlowNode for tar | provenance | | +| tarslip.py:149:37:149:39 | ControlFlowNode for tar | tarslip.py:150:5:150:7 | ControlFlowNode for tar | provenance | | +| tarslip.py:153:1:153:3 | ControlFlowNode for tar | tarslip.py:154:30:154:32 | ControlFlowNode for tar | provenance | | +| tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | tarslip.py:153:1:153:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:154:30:154:32 | ControlFlowNode for tar | tarslip.py:145:34:145:36 | ControlFlowNode for tar | provenance | | +| tarslip.py:156:1:156:3 | ControlFlowNode for tar | tarslip.py:157:33:157:35 | ControlFlowNode for tar | provenance | | +| tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | tarslip.py:156:1:156:3 | ControlFlowNode for tar | provenance | | +| tarslip.py:157:33:157:35 | ControlFlowNode for tar | tarslip.py:149:37:149:39 | ControlFlowNode for tar | provenance | | nodes | tarslip.py:14:1:14:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | | tarslip.py:14:7:14:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() | @@ -77,4 +98,21 @@ nodes | tarslip.py:124:1:124:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | | tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() | | tarslip.py:125:32:125:34 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:131:1:131:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:131:7:131:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() | +| tarslip.py:132:1:132:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:133:1:133:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:136:1:136:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:138:1:138:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:139:1:139:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:145:34:145:36 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:146:5:146:7 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:149:37:149:39 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:150:5:150:7 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:153:1:153:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:153:7:153:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() | +| tarslip.py:154:30:154:32 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:156:1:156:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | +| tarslip.py:156:7:156:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() | +| tarslip.py:157:33:157:35 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar | subpaths diff --git a/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py b/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py index e8be2e007d5d..a1540dea1783 100644 --- a/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py +++ b/python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py @@ -128,30 +128,30 @@ def safemembers(members): tar.extractall("/tmp/unpack/", safemembers(tar)) # safe # `None` selects all archive members, just like an omitted `members` argument. -tar = tarfile.open(unsafe_filename_tar) # $ MISSING: Source -tar.extractall("/tmp/unpack/", None) # $ MISSING: Alert -tar.extractall("/tmp/unpack/", members=None) # $ MISSING: Alert +tar = tarfile.open(unsafe_filename_tar) # $ Source +tar.extractall("/tmp/unpack/", None) # $ Alert +tar.extractall("/tmp/unpack/", members=None) # $ Alert members = None -tar.extractall("/tmp/unpack/", members) # $ MISSING: Alert +tar.extractall("/tmp/unpack/", members) # $ Alert -tar.extractall("/tmp/unpack/", None, filter=None) # $ MISSING: Alert -tar.extractall("/tmp/unpack/", members=None, filter="fully_trusted") # $ MISSING: Alert +tar.extractall("/tmp/unpack/", None, filter=None) # $ Alert +tar.extractall("/tmp/unpack/", members=None, filter="fully_trusted") # $ Alert tar.extractall("/tmp/unpack/", members=None, filter="data") # safe tar.extractall("/tmp/unpack/", []) # safe tar.extractall("/tmp/unpack/", members=[]) # safe def extract_with_default_members(tar, path=".", members=None, *, numeric_owner=False): - tar.extractall(path, members, numeric_owner=numeric_owner) # $ MISSING: Alert + tar.extractall(path, members, numeric_owner=numeric_owner) # $ Alert def extract_with_default_members_kw(tar, path=".", *, members=None, numeric_owner=False): - tar.extractall(path, members=members, numeric_owner=numeric_owner) # $ MISSING: Alert + tar.extractall(path, members=members, numeric_owner=numeric_owner) # $ Alert -tar = tarfile.open(unsafe_filename_tar) # $ MISSING: Source +tar = tarfile.open(unsafe_filename_tar) # $ Source extract_with_default_members(tar, "/tmp/unpack/") -tar = tarfile.open(unsafe_filename_tar) # $ MISSING: Source +tar = tarfile.open(unsafe_filename_tar) # $ Source extract_with_default_members_kw(tar, "/tmp/unpack/") From 0606b59cbb19fad0678476618962584cff4dbe9c Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 19 Nov 2026 11:17:28 +0000 Subject: [PATCH 3/3] Python: Document the tar extraction None-members fix Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- python/ql/lib/change-notes/2026-11-19-tarslip-none-members.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 python/ql/lib/change-notes/2026-11-19-tarslip-none-members.md diff --git a/python/ql/lib/change-notes/2026-11-19-tarslip-none-members.md b/python/ql/lib/change-notes/2026-11-19-tarslip-none-members.md new file mode 100644 index 000000000000..b682f5f695ba --- /dev/null +++ b/python/ql/lib/change-notes/2026-11-19-tarslip-none-members.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The `py/tarslip` query now detects unsafe `TarFile.extractall` calls where the `members` argument may be `None`, including calls through wrappers with a `None` default.