Skip to content

Commit 68991eb

Browse files
committed
fix(python): match argument positions and parameter names in py/tarslip sinks
1 parent 7b9ee04 commit 68991eb

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `py/tarslip` query now uses the correct argument positions and parameter names for `TarFile.extract` and `TarFile.extractall` sinks, reporting `extract(member=...)` calls with untrusted members and no longer falsely reporting `extractall(path, members)` calls when sanitized `members` are passed positionally.

‎python/ql/lib/semmle/python/security/dataflow/TarSlipCustomizations.qll‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module TarSlip {
9696
.getMember("extractall")
9797
.getACall() and
9898
hasUnsafeFilter(call) and
99-
not exists(call.getParameter(2, "members")) and
99+
not exists(call.getParameter(1, "members")) and
100100
this = call.(DataFlow::MethodCallNode).getObject()
101101
)
102102
}
@@ -107,10 +107,10 @@ module TarSlip {
107107
*/
108108
class ExtractSink extends Sink {
109109
ExtractSink() {
110-
exists(DataFlow::CallCfgNode call |
110+
exists(API::CallNode call |
111111
call =
112112
API::moduleImport("tarfile").getMember("open").getReturn().getMember("extract").getACall() and
113-
this = call.getArg(0) and
113+
this = call.getParameter(0, "member").asSink() and
114114
hasUnsafeFilter(call)
115115
)
116116
}
@@ -119,14 +119,14 @@ module TarSlip {
119119
/** The `members` argument `extractall` is considered a sink. */
120120
class ExtractMembersSink extends Sink {
121121
ExtractMembersSink() {
122-
exists(DataFlow::CallCfgNode call |
122+
exists(API::CallNode call |
123123
call =
124124
API::moduleImport("tarfile")
125125
.getMember("open")
126126
.getReturn()
127127
.getMember("extractall")
128128
.getACall() and
129-
this in [call.getArg(0), call.getArgByName("members")] and
129+
this = call.getParameter(1, "members").asSink() and
130130
hasUnsafeFilter(call)
131131
)
132132
}

‎python/ql/test/query-tests/Security/CWE-022-TarSlip/TarSlip.expected‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
| tarslip.py:96:17:96:21 | ControlFlowNode for entry | tarslip.py:94:7:94:39 | ControlFlowNode for Attribute() | tarslip.py:96:17:96:21 | ControlFlowNode for entry | This file extraction depends on a $@. | tarslip.py:94:7:94:39 | ControlFlowNode for Attribute() | potentially untrusted source |
99
| tarslip.py:110:1:110:3 | ControlFlowNode for tar | tarslip.py:109:7:109:39 | ControlFlowNode for Attribute() | tarslip.py:110:1:110:3 | ControlFlowNode for tar | This file extraction depends on a $@. | tarslip.py:109:7:109:39 | ControlFlowNode for Attribute() | potentially untrusted source |
1010
| 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 |
11+
| 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 |
12+
| 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 |
1113
edges
1214
| tarslip.py:14:1:14:3 | ControlFlowNode for tar | tarslip.py:15:1:15:3 | ControlFlowNode for tar | provenance | |
1315
| tarslip.py:14:7:14:39 | ControlFlowNode for Attribute() | tarslip.py:14:1:14:3 | ControlFlowNode for tar | provenance | |
@@ -31,6 +33,11 @@ edges
3133
| tarslip.py:109:7:109:39 | ControlFlowNode for Attribute() | tarslip.py:109:1:109:3 | ControlFlowNode for tar | provenance | |
3234
| tarslip.py:112:1:112:3 | ControlFlowNode for tar | tarslip.py:113:24:113:26 | ControlFlowNode for tar | provenance | |
3335
| tarslip.py:112:7:112:39 | ControlFlowNode for Attribute() | tarslip.py:112:1:112:3 | ControlFlowNode for tar | provenance | |
36+
| tarslip.py:119:1:119:3 | ControlFlowNode for tar | tarslip.py:120:5:120:9 | ControlFlowNode for entry | provenance | |
37+
| tarslip.py:119:7:119:39 | ControlFlowNode for Attribute() | tarslip.py:119:1:119:3 | ControlFlowNode for tar | provenance | |
38+
| tarslip.py:120:5:120:9 | ControlFlowNode for entry | tarslip.py:121:24:121:28 | ControlFlowNode for entry | provenance | |
39+
| tarslip.py:124:1:124:3 | ControlFlowNode for tar | tarslip.py:125:32:125:34 | ControlFlowNode for tar | provenance | |
40+
| tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | tarslip.py:124:1:124:3 | ControlFlowNode for tar | provenance | |
3441
nodes
3542
| tarslip.py:14:1:14:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
3643
| tarslip.py:14:7:14:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
@@ -63,4 +70,11 @@ nodes
6370
| tarslip.py:112:1:112:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
6471
| tarslip.py:112:7:112:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
6572
| tarslip.py:113:24:113:26 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
73+
| tarslip.py:119:1:119:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
74+
| tarslip.py:119:7:119:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
75+
| tarslip.py:120:5:120:9 | ControlFlowNode for entry | semmle.label | ControlFlowNode for entry |
76+
| tarslip.py:121:24:121:28 | ControlFlowNode for entry | semmle.label | ControlFlowNode for entry |
77+
| tarslip.py:124:1:124:3 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
78+
| tarslip.py:124:7:124:39 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
79+
| tarslip.py:125:32:125:34 | ControlFlowNode for tar | semmle.label | ControlFlowNode for tar |
6680
subpaths

‎python/ql/test/query-tests/Security/CWE-022-TarSlip/tarslip.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,15 @@ def safemembers(members):
114114

115115
tar = tarfile.open(unsafe_filename_tar)
116116
tar.extractall(members=safemembers(tar), filter=extraction_filter) # safe -- we assume `safemembers` makes up for the unsafe filter
117+
118+
# Keyword `member` argument to `extract`
119+
tar = tarfile.open(unsafe_filename_tar) # $ Source
120+
for entry in tar:
121+
tar.extract(member=entry, path="/tmp/unpack/") # $ Alert
122+
123+
# Positional `members` argument to `extractall`
124+
tar = tarfile.open(unsafe_filename_tar) # $ Source
125+
tar.extractall("/tmp/unpack/", tar) # $ Alert
126+
127+
tar = tarfile.open(unsafe_filename_tar)
128+
tar.extractall("/tmp/unpack/", safemembers(tar)) # safe

0 commit comments

Comments
 (0)