Skip to content

Commit b72af27

Browse files
committed
PS: Add tests showing that there is no flow starting at environment variables, but we still have flow through them.
1 parent a95f3b3 commit b72af27

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

powershell/ql/lib/semmle/code/powershell/security/CommandInjectionCustomizations.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ module CommandInjection {
3131
abstract class Sanitizer extends DataFlow::Node { }
3232

3333
/** A source of user input, considered as a flow source for command injection. */
34-
class FlowSourceAsSource extends Source instanceof SourceNode {
34+
class FlowSourceAsSource extends Source {
35+
FlowSourceAsSource() {
36+
this instanceof SourceNode and
37+
not this instanceof EnvironmentVariableSource
38+
}
39+
3540
override string getSourceType() { result = "user-provided value" }
3641
}
3742

powershell/ql/test/query-tests/security/cwe-078/CommandInjection/CommandInjection.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ edges
5454
| test.ps1:172:42:172:47 | input | test.ps1:136:11:136:20 | userinput | provenance | |
5555
| test.ps1:173:42:173:47 | input | test.ps1:144:11:144:20 | userinput | provenance | |
5656
| test.ps1:214:10:214:32 | Call to read-host | test.ps1:217:7:217:10 | $o | provenance | Src:MaD:0 |
57+
| test.ps1:225:14:225:36 | Call to read-host | test.ps1:229:7:229:10 | $y | provenance | Src:MaD:0 |
5758
nodes
5859
| test.ps1:3:11:3:20 | userinput | semmle.label | userinput |
5960
| test.ps1:4:23:4:52 | Get-Process -Name $UserInput | semmle.label | Get-Process -Name $UserInput |
@@ -112,6 +113,8 @@ nodes
112113
| test.ps1:173:42:173:47 | input | semmle.label | input |
113114
| test.ps1:214:10:214:32 | Call to read-host | semmle.label | Call to read-host |
114115
| test.ps1:217:7:217:10 | $o | semmle.label | $o |
116+
| test.ps1:225:14:225:36 | Call to read-host | semmle.label | Call to read-host |
117+
| test.ps1:229:7:229:10 | $y | semmle.label | $y |
115118
subpaths
116119
#select
117120
| test.ps1:4:23:4:52 | Get-Process -Name $UserInput | test.ps1:152:10:152:32 | Call to read-host | test.ps1:4:23:4:52 | Get-Process -Name $UserInput | This command depends on a $@. | test.ps1:152:10:152:32 | Call to read-host | user-provided value |
@@ -133,3 +136,4 @@ subpaths
133136
| test.ps1:139:50:139:59 | UserInput | test.ps1:152:10:152:32 | Call to read-host | test.ps1:139:50:139:59 | UserInput | This command depends on a $@. | test.ps1:152:10:152:32 | Call to read-host | user-provided value |
134137
| test.ps1:147:63:147:72 | UserInput | test.ps1:152:10:152:32 | Call to read-host | test.ps1:147:63:147:72 | UserInput | This command depends on a $@. | test.ps1:152:10:152:32 | Call to read-host | user-provided value |
135138
| test.ps1:217:7:217:10 | $o | test.ps1:214:10:214:32 | Call to read-host | test.ps1:217:7:217:10 | $o | This command depends on a $@. | test.ps1:214:10:214:32 | Call to read-host | user-provided value |
139+
| test.ps1:229:7:229:10 | $y | test.ps1:225:14:225:36 | Call to read-host | test.ps1:229:7:229:10 | $y | This command depends on a $@. | test.ps1:225:14:225:36 | Call to read-host | user-provided value |

powershell/ql/test/query-tests/security/cwe-078/CommandInjection/test.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,16 @@ function false-positive-in-call-operator($d)
215215
& unzip -o "$o" -d $d # GOOD
216216

217217
. "$o" # BAD
218+
}
219+
220+
function flow-through-env-var() {
221+
$x = $env:foo
222+
223+
. "$x" # GOOD # we don't consider environment vars flow sources
224+
225+
$input = Read-Host "enter input"
226+
$env:bar = $input
227+
228+
$y = $env:bar
229+
. "$y" # BAD # but we have flow through them
218230
}

0 commit comments

Comments
 (0)