Skip to content

Commit 5692eb0

Browse files
committed
PS: Add read step for 'foreach' statements.
1 parent e39d9f9 commit 5692eb0

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

powershell/ql/lib/semmle/code/powershell/dataflow/internal/DataFlowPrivate.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,17 @@ predicate readStep(Node node1, ContentSet c, Node node2) {
10781078
node2 = TProcessPropertyByNameNode(p, true)
10791079
)
10801080
or
1081+
// Read from a collection into a `foreach` loop
1082+
exists(
1083+
CfgNodes::StmtNodes::ForEachStmtCfgNode forEach, Content::KnownElementContent ec, BasicBlock bb,
1084+
int i
1085+
|
1086+
c.isKnownOrUnknownElement(ec) and
1087+
node1.asExpr() = forEach.getIterableExpr() and
1088+
bb.getNode(i) = forEach.getVarAccess() and
1089+
node2.asDefinition().definesAt(_, bb, i)
1090+
)
1091+
or
10811092
// Summary read steps
10821093
FlowSummaryImpl::Private::Steps::summaryReadStep(node1.(FlowSummaryNode).getSummaryNode(), c,
10831094
node2.(FlowSummaryNode).getSummaryNode())

powershell/ql/lib/semmle/code/powershell/dataflow/internal/DataFlowPublic.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
private import powershell
22
private import DataFlowDispatch
33
private import DataFlowPrivate
4+
private import semmle.code.powershell.dataflow.Ssa
45
private import semmle.code.powershell.typetracking.internal.TypeTrackingImpl
56
private import semmle.code.powershell.ApiGraphs
67
private import semmle.code.powershell.Cfg
@@ -13,6 +14,9 @@ class Node extends TNode {
1314
/** Gets the expression corresponding to this node, if any. */
1415
CfgNodes::ExprCfgNode asExpr() { result = this.(ExprNode).getExprNode() }
1516

17+
/** Gets the definition corresponding to this node, if any. */
18+
Ssa::Definition asDefinition() { result = this.(SsaDefinitionNodeImpl).getDefinition() }
19+
1620
ScriptBlock asCallable() { result = this.(CallableNode).asCallableAstNode() }
1721

1822
/** Gets the parameter corresponding to this node, if any. */

powershell/ql/test/library-tests/dataflow/fields/test.expected

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ edges
8383
| test.ps1:88:1:88:5 | [post] hash [b] | test.ps1:89:6:89:10 | hash [b] | provenance | |
8484
| test.ps1:88:11:88:21 | Call to source | test.ps1:88:1:88:5 | [post] hash [b] | provenance | |
8585
| test.ps1:89:6:89:10 | hash [b] | test.ps1:89:6:89:12 | b | provenance | |
86+
| test.ps1:91:9:91:10 | a | test.ps1:92:10:92:11 | a | provenance | |
87+
| test.ps1:91:15:91:36 | ...,... [element 2] | test.ps1:91:9:91:10 | a | provenance | |
88+
| test.ps1:91:21:91:33 | (...) | test.ps1:91:15:91:36 | ...,... [element 2] | provenance | |
89+
| test.ps1:91:22:91:32 | Call to source | test.ps1:91:21:91:33 | (...) | provenance | |
8690
nodes
8791
| test.ps1:3:1:3:2 | [post] a [f] | semmle.label | [post] a [f] |
8892
| test.ps1:3:8:3:17 | Call to source | semmle.label | Call to source |
@@ -179,9 +183,13 @@ nodes
179183
| test.ps1:88:11:88:21 | Call to source | semmle.label | Call to source |
180184
| test.ps1:89:6:89:10 | hash [b] | semmle.label | hash [b] |
181185
| test.ps1:89:6:89:12 | b | semmle.label | b |
186+
| test.ps1:91:9:91:10 | a | semmle.label | a |
187+
| test.ps1:91:15:91:36 | ...,... [element 2] | semmle.label | ...,... [element 2] |
188+
| test.ps1:91:21:91:33 | (...) | semmle.label | (...) |
189+
| test.ps1:91:22:91:32 | Call to source | semmle.label | Call to source |
190+
| test.ps1:92:10:92:11 | a | semmle.label | a |
182191
subpaths
183192
testFailures
184-
| test.ps1:92:13:92:31 | # $ hasValueFlow=18 | Missing result: hasValueFlow=18 |
185193
#select
186194
| test.ps1:4:6:4:9 | f | test.ps1:3:8:3:17 | Call to source | test.ps1:4:6:4:9 | f | $@ | test.ps1:3:8:3:17 | Call to source | Call to source |
187195
| test.ps1:11:6:11:13 | ...[...] | test.ps1:10:12:10:21 | Call to source | test.ps1:11:6:11:13 | ...[...] | $@ | test.ps1:10:12:10:21 | Call to source | Call to source |
@@ -209,3 +217,4 @@ testFailures
209217
| test.ps1:83:6:83:15 | ...[...] | test.ps1:79:7:79:17 | Call to source | test.ps1:83:6:83:15 | ...[...] | $@ | test.ps1:79:7:79:17 | Call to source | Call to source |
210218
| test.ps1:87:6:87:15 | ...[...] | test.ps1:79:7:79:17 | Call to source | test.ps1:87:6:87:15 | ...[...] | $@ | test.ps1:79:7:79:17 | Call to source | Call to source |
211219
| test.ps1:89:6:89:12 | b | test.ps1:88:11:88:21 | Call to source | test.ps1:89:6:89:12 | b | $@ | test.ps1:88:11:88:21 | Call to source | Call to source |
220+
| test.ps1:92:10:92:11 | a | test.ps1:91:22:91:32 | Call to source | test.ps1:92:10:92:11 | a | $@ | test.ps1:91:22:91:32 | Call to source | Call to source |

0 commit comments

Comments
 (0)