Skip to content

Commit 99358c6

Browse files
author
Alvaro Muñoz
committed
Extend CFG to reach env expressions
1 parent 4b57cee commit 99358c6

File tree

3 files changed

+53
-35
lines changed

3 files changed

+53
-35
lines changed

ql/lib/codeql/actions/Ast.qll

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,6 @@ class StepStmt extends Statement instanceof Actions::Step {
184184
string getId() { result = super.getId() }
185185

186186
JobStmt getJobStmt() { result = super.getJob() }
187-
188-
/**
189-
* Gets a environment variable expression by name in the scope of the current step.
190-
*/
191-
Expression getEnvExpr(string name) {
192-
exists(Actions::StepEnv env |
193-
env.getStep() = this and
194-
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
195-
)
196-
or
197-
exists(Actions::JobEnv env |
198-
env.getJob() = this.getJobStmt() and
199-
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
200-
)
201-
or
202-
exists(Actions::WorkflowEnv env |
203-
env.getWorkflow() = this.getJobStmt().getWorkflowStmt() and
204-
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
205-
)
206-
}
207187
}
208188

209189
/**
@@ -238,7 +218,25 @@ class StepUsesExpr extends StepStmt, UsesExpr {
238218
)
239219
}
240220

241-
override Expression getEnvExpr(string name) { result = this.(StepStmt).getEnvExpr(name) }
221+
/**
222+
* Gets a environment variable expression by name in the scope of the current step.
223+
*/
224+
override Expression getEnvExpr(string name) {
225+
exists(Actions::StepEnv env |
226+
env.getStep() = this and
227+
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
228+
)
229+
or
230+
exists(Actions::JobEnv env |
231+
env.getJob() = this.getJobStmt() and
232+
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
233+
)
234+
or
235+
exists(Actions::WorkflowEnv env |
236+
env.getWorkflow() = this.getJobStmt().getWorkflowStmt() and
237+
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
238+
)
239+
}
242240
}
243241

244242
/**
@@ -317,6 +315,26 @@ class RunExpr extends StepStmt, Expression {
317315
Expression getScriptExpr() { result = scriptExpr }
318316

319317
string getScript() { result = scriptExpr.getValue() }
318+
319+
/**
320+
* Gets a environment variable expression by name in the scope of the current node.
321+
*/
322+
Expression getEnvExpr(string name) {
323+
exists(Actions::StepEnv env |
324+
env.getStep() = this and
325+
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
326+
)
327+
or
328+
exists(Actions::JobEnv env |
329+
env.getJob() = this.getJobStmt() and
330+
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
331+
)
332+
or
333+
exists(Actions::WorkflowEnv env |
334+
env.getWorkflow() = this.getJobStmt().getWorkflowStmt() and
335+
env.(YamlMapping).maps(any(YamlScalar s | s.getValue() = name), result)
336+
)
337+
}
320338
}
321339

322340
/**
@@ -420,5 +438,11 @@ class EnvAccessExpr extends ExprAccessExpr {
420438

421439
EnvAccessExpr() { varName = this.getExpression().regexpCapture("env\\.([A-Za-z0-9_-]+)", 1) }
422440

423-
override Expression getRefExpr() { exists(RunExpr s | s.getEnvExpr(varName) = result) }
441+
override Expression getRefExpr() {
442+
exists(JobUsesExpr s | s.getEnvExpr(varName) = result)
443+
or
444+
exists(StepUsesExpr s | s.getEnvExpr(varName) = result)
445+
or
446+
exists(RunExpr s | s.getEnvExpr(varName) = result)
447+
}
424448
}

ql/lib/codeql/actions/controlflow/internal/Cfg.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ private class StepUsesTree extends StandardPreOrderTree instanceof StepUsesExpr
227227
override ControlFlowTree getChildNode(int i) {
228228
result =
229229
rank[i](Expression child, Location l |
230-
child = super.getArgumentExpr(_) and l = child.getLocation()
230+
(child = super.getArgumentExpr(_) or child = super.getEnvExpr(_)) and
231+
l = child.getLocation()
231232
|
232233
child
233234
order by
@@ -240,7 +241,8 @@ private class JobUsesTree extends StandardPreOrderTree instanceof JobUsesExpr {
240241
override ControlFlowTree getChildNode(int i) {
241242
result =
242243
rank[i](Expression child, Location l |
243-
child = super.getArgumentExpr(_) and l = child.getLocation()
244+
(child = super.getArgumentExpr(_) or child = super.getEnvExpr(_)) and
245+
l = child.getLocation()
244246
|
245247
child
246248
order by

ql/lib/test/test.ql

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ query predicate runStepChildren(RunExpr run, AstNode child) { child.getParentNod
3131

3232
query predicate varAccesses(ExprAccessExpr ea, string expr) { expr = ea.getExpression() }
3333

34-
query predicate outputAccesses(StepOutputAccessExpr va, string id, string var) {
35-
id = va.getStepId() and var = va.getVarName()
36-
}
37-
3834
query predicate orphanVarAccesses(ExprAccessExpr va, string var) {
3935
var = va.getExpression() and
4036
not exists(AstNode n | n = va.getParentNode())
@@ -53,25 +49,21 @@ query predicate cfgNodes(Cfg::Node n) {
5349
}
5450

5551
query predicate dfNodes(DataFlow::Node e) {
56-
e.getLocation().getFile().getBaseName() = "simple1.yml"
52+
e.getLocation().getFile().getBaseName() = "argus_case_study.yml"
5753
}
5854

5955
query predicate exprNodes(DataFlow::ExprNode e) { any() }
6056

6157
query predicate argumentNodes(DataFlow::ArgumentNode e) { any() }
6258

63-
query predicate localFlow(StepUsesExpr s, StepOutputAccessExpr o) { s.getId() = o.getStepId() }
64-
6559
query predicate usesIds(StepUsesExpr s, string a) { s.getId() = a }
6660

67-
query predicate varIds(StepOutputAccessExpr s, string a) { s.getStepId() = a }
68-
6961
query predicate nodeLocations(DataFlow::Node n, Location l) { n.getLocation() = l }
7062

7163
query predicate scopes(Cfg::CfgScope c) { any() }
7264

73-
query predicate sources(string action, string version, string output, string kind) {
74-
sourceModel(action, version, output, kind)
65+
query predicate sources(string action, string version, string output, string trigger, string kind) {
66+
sourceModel(action, version, output, trigger, kind)
7567
}
7668

7769
query predicate summaries(string action, string version, string input, string output, string kind) {

0 commit comments

Comments
 (0)