Skip to content

Commit 4a2fd52

Browse files
committed
PS: Add reads of environment variables as local flow sources.
1 parent 9f0f0ca commit 4a2fd52

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ class LocalVariable extends AbstractLocalScopeVariable, TLocalVariable {
273273
final override Scope getDeclaringScope() { result = scope }
274274
}
275275

276+
/**
277+
* A variable of the form `$Env:HOME`.
278+
*/
279+
class EnvVariable extends Variable {
280+
string var;
281+
282+
EnvVariable() { this.getName() = ["env:", "Env:"] + var }
283+
284+
/**
285+
* Gets the part of the variable name that represens which environment
286+
* variable.
287+
*/
288+
string getEnvironmentVariable() { result = var }
289+
}
290+
276291
class Parameter extends AbstractLocalScopeVariable, TParameter {
277292
ParameterImpl p;
278293

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@ class VarWriteAccess extends VarAccess {
4848

4949
predicate isImplicit() { isImplicitVariableWriteAccess(this) }
5050
}
51+
52+
/** An access to an environment variable such as `$Env:PATH` */
53+
class EnvVarAccess extends VarAccess {
54+
EnvVarAccess() { super.getVariable() instanceof EnvVariable }
55+
56+
override EnvVariable getVariable() { result = super.getVariable() }
57+
58+
string getEnvironmentVariable() { result = this.getVariable().getEnvironmentVariable() }
59+
}

powershell/ql/lib/semmle/code/powershell/dataflow/flowsources/Local.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ abstract class EnvironmentVariableSource extends LocalFlowSource {
3030
override string getSourceType() { result = "environment variable" }
3131
}
3232

33+
private class EnvironmentVariableEnv extends EnvironmentVariableSource {
34+
EnvironmentVariableEnv() {
35+
this.asExpr().getExpr().(VarReadAccess).getVariable() instanceof EnvVariable
36+
}
37+
}
38+
3339
private class ExternalEnvironmentVariableSource extends EnvironmentVariableSource {
3440
ExternalEnvironmentVariableSource() {
3541
this = ModelOutput::getASourceNode("environment", _).asSource()

0 commit comments

Comments
 (0)