Skip to content

Commit 79218a3

Browse files
committed
Use YamlMapping for modeling Env
1 parent dd52ef8 commit 79218a3

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

javascript/ql/lib/semmle/javascript/Actions.qll

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module Actions {
7575
YamlMapping getJobs() { result = this.lookup("jobs") }
7676

7777
/** Gets the 'global' `env` mapping in this workflow. */
78-
YamlMapping getEnv() { result = this.lookup("env") }
78+
WorkflowEnv getEnv() { result = this.lookup("env") }
7979

8080
/** Gets the name of the workflow. */
8181
string getName() { result = this.lookup("name").(YamlString).getValue() }
@@ -103,52 +103,36 @@ module Actions {
103103
Workflow getWorkflow() { result = workflow }
104104
}
105105

106-
/** An environment variable in 'env:' */
107-
abstract class EnvVariable extends YamlNode, YamlString {
108-
/** Gets the name of this environment variable. */
109-
abstract string getName();
110-
}
106+
abstract class Env extends YamlNode, YamlMapping { }
111107

112-
/** A workflow level 'global' environment variable. */
113-
class WorkflowEnvVariable extends EnvVariable {
114-
string envName;
108+
/** A workflow level `env` mapping. */
109+
class WorkflowEnv extends Env {
115110
Workflow workflow;
116111

117-
WorkflowEnvVariable() { this = workflow.getEnv().lookup(envName) }
112+
WorkflowEnv() { workflow.lookup("env") = this }
118113

119114
/** Gets the workflow this field belongs to. */
120115
Workflow getWorkflow() { result = workflow }
121-
122-
/** Gets the name of this environment variable. */
123-
override string getName() { result = envName }
124116
}
125117

126-
/** A job level environment variable. */
127-
class JobEnvVariable extends EnvVariable {
128-
string envName;
118+
/** A job level `env` mapping. */
119+
class JobEnv extends Env {
129120
Job job;
130121

131-
JobEnvVariable() { this = job.getEnv().lookup(envName) }
122+
JobEnv() { job.lookup("env") = this }
132123

133124
/** Gets the job this field belongs to. */
134125
Job getJob() { result = job }
135-
136-
/** Gets the name of this environment variable. */
137-
override string getName() { result = envName }
138126
}
139127

140-
/** A step level environment variable. */
141-
class StepEnvVariable extends EnvVariable {
142-
string envName;
128+
/** A step level `env` mapping. */
129+
class StepEnv extends Env {
143130
Step step;
144131

145-
StepEnvVariable() { this = step.getEnv().lookup(envName) }
132+
StepEnv() { step.lookup("env") = this }
146133

147134
/** Gets the step this field belongs to. */
148135
Step getStep() { result = step }
149-
150-
/** Gets the name of this environment variable. */
151-
override string getName() { result = envName }
152136
}
153137

154138
/**
@@ -183,7 +167,7 @@ module Actions {
183167
Step getStep(int index) { result.getJob() = this and result.getIndex() = index }
184168

185169
/** Gets the `env` mapping in this job. */
186-
YamlMapping getEnv() { result = this.lookup("env") }
170+
JobEnv getEnv() { result = this.lookup("env") }
187171

188172
/** Gets the workflow this job belongs to. */
189173
Workflow getWorkflow() { result = workflow }
@@ -250,7 +234,7 @@ module Actions {
250234
StepIf getIf() { result.getStep() = this }
251235

252236
/** Gets the value of the `env` field in this step, if any. */
253-
YamlMapping getEnv() { result = this.lookup("env") }
237+
StepEnv getEnv() { result = this.lookup("env") }
254238

255239
/** Gets the ID of this step, if any. */
256240
string getId() { result = this.lookup("id").(YamlString).getValue() }

javascript/ql/src/Security/CWE-094/ExpressionInjection.ql

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ private predicate isExternalUserControlledWorkflowRun(string context) {
108108
* is where the external user controlled value was assigned to.
109109
*/
110110
bindingset[injection]
111-
predicate isEnvTainted(Actions::EnvVariable env, string injection, string context) {
112-
Actions::getEnvName(injection) = env.getName() and
113-
Actions::getASimpleReferenceExpression(env) = context
111+
predicate isEnvTainted(string injection, string context) {
112+
exists(Actions::Env env, string envName, YamlString envValue |
113+
envValue = env.lookup(envName) and
114+
Actions::getEnvName(injection) = envName and
115+
Actions::getASimpleReferenceExpression(envValue) = context
116+
)
114117
}
115118

116119
/**
@@ -122,7 +125,7 @@ predicate isRunInjectable(Actions::Run run, string injection, string context) {
122125
(
123126
injection = context
124127
or
125-
isEnvTainted(_, injection, context)
128+
isEnvTainted(injection, context)
126129
)
127130
}
128131

@@ -139,7 +142,7 @@ predicate isScriptInjectable(Actions::Script script, string injection, string co
139142
(
140143
injection = context
141144
or
142-
isEnvTainted(_, injection, context)
145+
isEnvTainted(injection, context)
143146
)
144147
)
145148
}

0 commit comments

Comments
 (0)