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. 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 de87f02351fe..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 @@ -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) # $ Source +tar.extractall("/tmp/unpack/", None) # $ Alert +tar.extractall("/tmp/unpack/", members=None) # $ Alert + +members = None +tar.extractall("/tmp/unpack/", members) # $ 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) # $ Alert + + +def extract_with_default_members_kw(tar, path=".", *, members=None, numeric_owner=False): + tar.extractall(path, members=members, numeric_owner=numeric_owner) # $ Alert + + +tar = tarfile.open(unsafe_filename_tar) # $ Source +extract_with_default_members(tar, "/tmp/unpack/") + +tar = tarfile.open(unsafe_filename_tar) # $ Source +extract_with_default_members_kw(tar, "/tmp/unpack/")