Skip to content

Commit

Permalink
PS: Add reads of environment variables as local flow sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasVP committed Nov 8, 2024
1 parent 9f0f0ca commit 4a2fd52
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Variable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ class LocalVariable extends AbstractLocalScopeVariable, TLocalVariable {
final override Scope getDeclaringScope() { result = scope }
}

/**
* A variable of the form `$Env:HOME`.
*/
class EnvVariable extends Variable {
string var;

EnvVariable() { this.getName() = ["env:", "Env:"] + var }

/**
* Gets the part of the variable name that represens which environment
* variable.
*/
string getEnvironmentVariable() { result = var }
}

class Parameter extends AbstractLocalScopeVariable, TParameter {
ParameterImpl p;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ class VarWriteAccess extends VarAccess {

predicate isImplicit() { isImplicitVariableWriteAccess(this) }
}

/** An access to an environment variable such as `$Env:PATH` */
class EnvVarAccess extends VarAccess {
EnvVarAccess() { super.getVariable() instanceof EnvVariable }

override EnvVariable getVariable() { result = super.getVariable() }

string getEnvironmentVariable() { result = this.getVariable().getEnvironmentVariable() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ abstract class EnvironmentVariableSource extends LocalFlowSource {
override string getSourceType() { result = "environment variable" }
}

private class EnvironmentVariableEnv extends EnvironmentVariableSource {
EnvironmentVariableEnv() {
this.asExpr().getExpr().(VarReadAccess).getVariable() instanceof EnvVariable
}
}

private class ExternalEnvironmentVariableSource extends EnvironmentVariableSource {
ExternalEnvironmentVariableSource() {
this = ModelOutput::getASourceNode("environment", _).asSource()
Expand Down

0 comments on commit 4a2fd52

Please sign in to comment.