Skip to content

Commit c93daeb

Browse files
authored
Merge pull request #125 from microsoft/global-parameters
PS: Global parameter support
2 parents 4cd37d6 + dec3e71 commit c93daeb

File tree

8 files changed

+86
-30
lines changed

8 files changed

+86
-30
lines changed

powershell/ql/lib/semmle/code/powershell/Function.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ abstract private class AbstractFunction extends Ast {
6969
EntryBasicBlock getEntryBasicBlock() { result.getScope() = this.getBody() }
7070
}
7171

72+
final class Function = AbstractFunction;
73+
7274
/**
7375
* A function definition.
7476
*/
@@ -114,4 +116,12 @@ class Constructor extends Method {
114116
Constructor() { this.isConstructor() }
115117
}
116118

117-
final class Function = FunctionBase;
119+
class TopLevel extends AbstractFunction instanceof TopLevelScriptBlock {
120+
final override string getName() { result = "toplevel" }
121+
122+
final override ScriptBlock getBody() { result = this }
123+
124+
final override Parameter getFunctionParameter(int i) { none() }
125+
126+
final override Type getDeclaringType() { none() }
127+
}

powershell/ql/lib/semmle/code/powershell/ScriptBlock.qll

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ScriptBlock extends @script_block, Ast {
1010
else result = "{...}"
1111
}
1212

13-
override SourceLocation getLocation() { script_block_location(this, result) }
13+
override Location getLocation() { script_block_location(this, result) }
1414

1515
int getNumUsings() { script_block(this, result, _, _, _, _) }
1616

@@ -51,6 +51,42 @@ class ScriptBlock extends @script_block, Ast {
5151
ModuleSpecification getAModuleSpecification() { result = this.getModuleSpecification(_) }
5252

5353
final override Scope getEnclosingScope() { result = this }
54+
55+
/**
56+
* Gets the `i`'th paramter in this scope.
57+
*
58+
* This may be both function paramters and parameter block parameters.
59+
*/
60+
Parameter getParameter(int i) {
61+
exists(Function func |
62+
func.getBody() = this and
63+
result = func.getParameter(i)
64+
)
65+
or
66+
this.isTopLevel() and
67+
result = this.getParamBlock().getParameter(i)
68+
}
69+
70+
/**
71+
* Gets a paramter in this scope.
72+
*
73+
* This may be both function parameters and parameter block parameters.
74+
*/
75+
Parameter getAParameter() { result = this.getParameter(_) }
76+
77+
Parameter getThisParameter() {
78+
exists(Function func |
79+
func.getBody() = this and
80+
result = func.getThisParameter()
81+
)
82+
}
83+
84+
/** Gets the number of function parameters. */
85+
final int getNumberOfParameters() { result = count(this.getAParameter()) }
86+
87+
final Parameter getParameterExcludingPiplines(int i) {
88+
result = this.getParamBlock().getParameterExcludingPiplines(i)
89+
}
5490
}
5591

5692
/** A `process` block. */
@@ -69,3 +105,7 @@ class ProcessBlock extends NamedBlock {
69105
result = scriptBlock.getEnclosingFunction().getAParameter()
70106
}
71107
}
108+
109+
class TopLevelScriptBlock extends ScriptBlock {
110+
TopLevelScriptBlock() { this.isTopLevel() }
111+
}

powershell/ql/lib/semmle/code/powershell/controlflow/internal/Scope.qll

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,7 @@ Scope scopeOf(Ast n) {
1414
* A variable scope. This is either a top-level (file), a module, a class,
1515
* or a callable.
1616
*/
17-
class Scope extends Ast, @script_block {
17+
class Scope extends Ast, ScriptBlock {
1818
/** Gets the outer scope, if any. */
1919
Scope getOuterScope() { result = scopeOf(this) }
20-
21-
/**
22-
* Gets the `i`'th paramter in this scope.
23-
*
24-
* This may be both function paramters and parameter block parameters.
25-
*/
26-
Parameter getParameter(int i) {
27-
exists(Function func |
28-
func.getBody() = this and
29-
result = func.getParameter(i)
30-
)
31-
}
32-
33-
/**
34-
* Gets a paramter in this scope.
35-
*
36-
* This may be both function paramters and parameter block parameters.
37-
*/
38-
Parameter getAParameter() { result = this.getParameter(_) }
39-
40-
Parameter getThisParameter() {
41-
exists(Function func |
42-
func.getBody() = this and
43-
result = func.getThisParameter()
44-
)
45-
}
4620
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Provides classes for performing local (intra-procedural) and
3+
* global (inter-procedural) taint-tracking analyses.
4+
*/
5+
module TaintTracking {
6+
import semmle.code.powershell.dataflow.internal.TaintTrackingImpl::Public
7+
private import semmle.code.powershell.dataflow.internal.DataFlowImplSpecific
8+
private import semmle.code.powershell.dataflow.internal.TaintTrackingImplSpecific
9+
private import codeql.dataflow.TaintTracking
10+
private import powershell
11+
import TaintFlowMake<Location, PowershellDataFlow, PowershellTaintTracking>
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import semmle.code.powershell.dataflow.internal.TaintTrackingPublic as Public
2+
3+
module Private {
4+
import semmle.code.powershell.dataflow.DataFlow::DataFlow as DataFlow
5+
import semmle.code.powershell.dataflow.internal.DataFlowImpl as DataFlowInternal
6+
import semmle.code.powershell.dataflow.internal.TaintTrackingPrivate
7+
}

powershell/ql/test/TestUtilities/InlineFlowTestUtil.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import semmle.code.powershell.dataflow.DataFlow
77

88
predicate defaultSource(DataFlow::Node src) {
99
src.asStmt().getStmt().(Cmd).getCommandName() = ["Source", "Taint"]
10+
or
11+
src.asParameter().getName().matches(["Source%", "Taint%"])
1012
}
1113

1214
predicate defaultSink(DataFlow::Node sink) {
@@ -15,5 +17,9 @@ predicate defaultSink(DataFlow::Node sink) {
1517

1618
string getSourceArgString(DataFlow::Node src) {
1719
defaultSource(src) and
18-
src.asStmt().getStmt().(Cmd).getAnArgument().(StringConstExpr).getValue().getValue() = result
20+
(
21+
src.asStmt().getStmt().(Cmd).getAnArgument().(StringConstExpr).getValue().getValue() = result
22+
or
23+
src.asParameter().getName().regexpCapture(["Source(.+)", "Taint(.+)"], 1) = result
24+
)
1925
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
param([string]$Source)
2+
3+
Sink $Source # $ hasValueFlow

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
models
22
edges
3+
| global.ps1:1:7:1:22 | Source | global.ps1:3:6:3:13 | Source | provenance | |
34
| test.ps1:1:14:1:16 | a | test.ps1:2:10:2:12 | a | provenance | |
45
| test.ps1:5:6:5:16 | Source | test.ps1:6:5:6:7 | x | provenance | |
56
| test.ps1:6:5:6:7 | x | test.ps1:1:14:1:16 | a | provenance | |
@@ -139,6 +140,8 @@ edges
139140
| test.ps1:39:24:39:31 | second | test.ps1:8:24:8:26 | y | provenance | |
140141
| test.ps1:39:32:39:38 | first | test.ps1:8:20:8:22 | x | provenance | |
141142
nodes
143+
| global.ps1:1:7:1:22 | Source | semmle.label | Source |
144+
| global.ps1:3:6:3:13 | Source | semmle.label | Source |
142145
| test.ps1:1:14:1:16 | a | semmle.label | a |
143146
| test.ps1:2:10:2:12 | a | semmle.label | a |
144147
| test.ps1:5:6:5:16 | Source | semmle.label | Source |
@@ -221,6 +224,7 @@ nodes
221224
subpaths
222225
testFailures
223226
#select
227+
| global.ps1:3:6:3:13 | Source | global.ps1:1:7:1:22 | Source | global.ps1:3:6:3:13 | Source | $@ | global.ps1:1:7:1:22 | Source | Source |
224228
| test.ps1:2:10:2:12 | a | test.ps1:5:6:5:16 | Source | test.ps1:2:10:2:12 | a | $@ | test.ps1:5:6:5:16 | Source | Source |
225229
| test.ps1:9:10:9:12 | x | test.ps1:14:10:14:20 | Source | test.ps1:9:10:9:12 | x | $@ | test.ps1:14:10:14:20 | Source | Source |
226230
| test.ps1:10:10:10:12 | y | test.ps1:15:11:15:21 | Source | test.ps1:10:10:10:12 | y | $@ | test.ps1:15:11:15:21 | Source | Source |

0 commit comments

Comments
 (0)