Skip to content

Commit b54316f

Browse files
author
Alvaro Muñoz
committed
Refactor CfgScopes and Ast predicate names
1 parent 9c6fd20 commit b54316f

File tree

6 files changed

+94
-96
lines changed

6 files changed

+94
-96
lines changed

ql/lib/codeql/actions/Ast.qll

+27-26
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ class Expression extends Statement { }
3131
* A Github Actions Workflow
3232
*/
3333
class WorkflowStmt extends Statement instanceof Actions::Workflow {
34-
JobStmt getAJob() { result = super.getJob(_) }
34+
JobStmt getAJobStmt() { result = super.getJob(_) }
3535

36-
JobStmt getJob(string id) { result = super.getJob(id) }
37-
38-
predicate isReusable() { this instanceof ReusableWorkflowStmt }
36+
JobStmt getJobStmt(string id) { result = super.getJob(id) }
3937
}
4038

4139
class ReusableWorkflowStmt extends WorkflowStmt {
@@ -45,15 +43,19 @@ class ReusableWorkflowStmt extends WorkflowStmt {
4543
this.(Actions::Workflow).getOn().getNode("workflow_call") = workflow_call
4644
}
4745

48-
InputsStmt getInputs() { result = workflow_call.(YamlMapping).lookup("inputs") }
46+
ReusableWorkflowInputsStmt getInputsStmt() {
47+
result = workflow_call.(YamlMapping).lookup("inputs")
48+
}
4949

50-
OutputsStmt getOutputs() { result = workflow_call.(YamlMapping).lookup("outputs") }
50+
ReusableWorkflowOutputsStmt getOutputsStmt() {
51+
result = workflow_call.(YamlMapping).lookup("outputs")
52+
}
5153

5254
string getName() { result = this.getLocation().getFile().getRelativePath() }
5355
}
5456

55-
class InputsStmt extends Statement instanceof YamlMapping {
56-
InputsStmt() {
57+
class ReusableWorkflowInputsStmt extends Statement instanceof YamlMapping {
58+
ReusableWorkflowInputsStmt() {
5759
exists(Actions::On on | on.getNode("workflow_call").(YamlMapping).lookup("inputs") = this)
5860
}
5961

@@ -70,16 +72,16 @@ class InputsStmt extends Statement instanceof YamlMapping {
7072
* token:
7173
* required: true
7274
*/
73-
InputExpr getInputExpr(string name) {
75+
ReusableWorkflowInputExpr getInputExpr(string name) {
7476
result.(YamlString).getValue() = name and
7577
this.(YamlMapping).maps(result, _)
7678
}
7779
}
7880

79-
class InputExpr extends Expression instanceof YamlString { }
81+
class ReusableWorkflowInputExpr extends Expression instanceof YamlString { }
8082

81-
class OutputsStmt extends Statement instanceof YamlMapping {
82-
OutputsStmt() {
83+
class ReusableWorkflowOutputsStmt extends Statement instanceof YamlMapping {
84+
ReusableWorkflowOutputsStmt() {
8385
exists(Actions::On on | on.getNode("workflow_call").(YamlMapping).lookup("outputs") = this)
8486
}
8587

@@ -96,12 +98,12 @@ class OutputsStmt extends Statement instanceof YamlMapping {
9698
* description: "The second output string"
9799
* value: ${{ jobs.example_job.outputs.output2 }}
98100
*/
99-
OutputExpr getOutputExpr(string name) {
101+
ReusableWorkflowOutputExpr getOutputExpr(string name) {
100102
this.(YamlMapping).lookup(name).(YamlMapping).lookup("value") = result
101103
}
102104
}
103105

104-
class OutputExpr extends Expression instanceof YamlString { }
106+
class ReusableWorkflowOutputExpr extends Expression instanceof YamlString { }
105107

106108
/**
107109
* A Job is a collection of steps that run in an execution environment.
@@ -114,10 +116,10 @@ class JobStmt extends Statement instanceof Actions::Job {
114116
string getId() { result = super.getId() }
115117

116118
/** Gets the step at the given index within this job. */
117-
StepStmt getStep(int index) { result = super.getStep(index) }
119+
StepStmt getStepStmt(int index) { result = super.getStep(index) }
118120

119121
/** Gets any steps that are defined within this job. */
120-
StepStmt getAStep() { result = super.getStep(_) }
122+
StepStmt getAStepStmt() { result = super.getStep(_) }
121123

122124
/**
123125
* Gets a needed job.
@@ -147,7 +149,7 @@ class JobStmt extends Statement instanceof Actions::Job {
147149
* with:
148150
* arg1: value1
149151
*/
150-
JobUsesExpr getUsesExpr() { result.getJob() = this }
152+
JobUsesExpr getUsesExpr() { result.getJobStmt() = this }
151153
}
152154

153155
/**
@@ -178,7 +180,7 @@ class JobOutputStmt extends Statement instanceof YamlMapping {
178180
class StepStmt extends Statement instanceof Actions::Step {
179181
string getId() { result = super.getId() }
180182

181-
JobStmt getJob() { result = super.getJob() }
183+
JobStmt getJobStmt() { result = super.getJob() }
182184
}
183185

184186
/**
@@ -189,7 +191,7 @@ abstract class UsesExpr extends Expression {
189191

190192
abstract string getVersion();
191193

192-
abstract Expression getArgument(string key);
194+
abstract Expression getArgumentExpr(string key);
193195
}
194196

195197
/**
@@ -204,7 +206,7 @@ class StepUsesExpr extends StepStmt, UsesExpr {
204206

205207
override string getVersion() { result = uses.getVersion() }
206208

207-
override Expression getArgument(string key) {
209+
override Expression getArgumentExpr(string key) {
208210
exists(Actions::With with |
209211
with.getStep() = this and
210212
result = with.lookup(key)
@@ -220,7 +222,7 @@ class JobUsesExpr extends UsesExpr instanceof YamlMapping {
220222
this instanceof JobStmt and this.maps(any(YamlString s | s.getValue() = "uses"), _)
221223
}
222224

223-
JobStmt getJob() { result = this }
225+
JobStmt getJobStmt() { result = this }
224226

225227
/**
226228
* Gets a regular expression that parses an `owner/repo@version` reference within a `uses` field in an Actions job step.
@@ -255,7 +257,7 @@ class JobUsesExpr extends UsesExpr instanceof YamlMapping {
255257
)
256258
}
257259

258-
override Expression getArgument(string key) {
260+
override Expression getArgumentExpr(string key) {
259261
this.(YamlMapping).lookup("with").(YamlMapping).lookup(key) = result
260262
}
261263
}
@@ -290,8 +292,7 @@ class ExprAccessExpr extends Expression instanceof YamlString {
290292

291293
string getExpression() { result = expr }
292294

293-
JobStmt getJob() { result.getAChildNode*() = this }
294-
//override string toString() { result = expr }
295+
JobStmt getJobStmt() { result.getAChildNode*() = this }
295296
}
296297

297298
/**
@@ -313,7 +314,7 @@ class StepOutputAccessExpr extends ExprAccessExpr {
313314

314315
string getVarName() { result = varName }
315316

316-
StepStmt getStep() { result.getId() = stepId }
317+
StepStmt getStepStmt() { result.getId() = stepId }
317318
}
318319

319320
/**
@@ -368,7 +369,7 @@ class ReusableWorkflowInputAccessExpr extends ExprAccessExpr {
368369
Expression getInputExpr() {
369370
exists(ReusableWorkflowStmt w |
370371
w.getLocation().getFile() = this.getLocation().getFile() and
371-
w.getInputs().getInputExpr(paramName) = result
372+
w.getInputsStmt().getInputExpr(paramName) = result
372373
)
373374
}
374375
}

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

+42-28
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ module Completion {
8787
module CfgScope {
8888
abstract class CfgScope extends AstNode { }
8989

90-
class ReusableWorkflowScope extends CfgScope instanceof ReusableWorkflowStmt { }
91-
92-
class JobScope extends CfgScope instanceof JobStmt { }
90+
class WorkflowScope extends CfgScope instanceof WorkflowStmt { }
9391
}
9492

9593
private module Implementation implements CfgShared::InputSig<Location> {
@@ -122,15 +120,9 @@ private module Implementation implements CfgShared::InputSig<Location> {
122120

123121
int maxSplits() { result = 0 }
124122

125-
predicate scopeFirst(CfgScope scope, AstNode e) {
126-
first(scope.(ReusableWorkflowStmt).getInputs(), e) or
127-
first(scope.(JobStmt), e)
128-
}
123+
predicate scopeFirst(CfgScope scope, AstNode e) { first(scope.(WorkflowStmt), e) }
129124

130-
predicate scopeLast(CfgScope scope, AstNode e, Completion c) {
131-
last(scope.(ReusableWorkflowStmt), e, c) or
132-
last(scope.(JobStmt), e, c)
133-
}
125+
predicate scopeLast(CfgScope scope, AstNode e, Completion c) { last(scope.(WorkflowStmt), e, c) }
134126

135127
predicate successorTypeIsSimple(SuccessorType t) { t instanceof NormalSuccessor }
136128

@@ -147,21 +139,38 @@ private import CfgImpl
147139
private import Completion
148140
private import CfgScope
149141

150-
private class ReusableWorkflowTree extends StandardPreOrderTree instanceof ReusableWorkflowStmt {
142+
private class WorkflowTree extends StandardPreOrderTree instanceof WorkflowStmt {
151143
override ControlFlowTree getChildNode(int i) {
152-
result =
153-
rank[i](Expression child, Location l |
154-
(child = super.getInputs() or child = super.getOutputs()) and
155-
l = child.getLocation()
156-
|
157-
child
158-
order by
159-
l.getStartLine(), l.getStartColumn(), l.getEndColumn(), l.getEndLine(), child.toString()
160-
)
144+
if this instanceof ReusableWorkflowStmt
145+
then
146+
result =
147+
rank[i](Expression child, Location l |
148+
(
149+
child = this.(ReusableWorkflowStmt).getInputsStmt() or
150+
child = this.(ReusableWorkflowStmt).getOutputsStmt() or
151+
child = this.(ReusableWorkflowStmt).getAJobStmt()
152+
) and
153+
l = child.getLocation()
154+
|
155+
child
156+
order by
157+
l.getStartLine(), l.getStartColumn(), l.getEndColumn(), l.getEndLine(), child.toString()
158+
)
159+
else
160+
result =
161+
rank[i](Expression child, Location l |
162+
child = super.getAJobStmt() and
163+
l = child.getLocation()
164+
|
165+
child
166+
order by
167+
l.getStartLine(), l.getStartColumn(), l.getEndColumn(), l.getEndLine(), child.toString()
168+
)
161169
}
162170
}
163171

164-
private class ReusableWorkflowInputsTree extends StandardPreOrderTree instanceof InputsStmt {
172+
private class ReusableWorkflowInputsTree extends StandardPreOrderTree instanceof ReusableWorkflowInputsStmt
173+
{
165174
override ControlFlowTree getChildNode(int i) {
166175
result =
167176
rank[i](Expression child, Location l |
@@ -174,9 +183,10 @@ private class ReusableWorkflowInputsTree extends StandardPreOrderTree instanceof
174183
}
175184
}
176185

177-
private class InputExprTree extends LeafTree instanceof InputExpr { }
186+
private class InputExprTree extends LeafTree instanceof ReusableWorkflowInputExpr { }
178187

179-
private class ReusableWorkflowOutputsTree extends StandardPreOrderTree instanceof OutputsStmt {
188+
private class ReusableWorkflowOutputsTree extends StandardPreOrderTree instanceof ReusableWorkflowOutputsStmt
189+
{
180190
override ControlFlowTree getChildNode(int i) {
181191
result =
182192
rank[i](Expression child, Location l |
@@ -189,13 +199,17 @@ private class ReusableWorkflowOutputsTree extends StandardPreOrderTree instanceo
189199
}
190200
}
191201

192-
private class OutputExprTree extends LeafTree instanceof OutputExpr { }
202+
private class OutputExprTree extends LeafTree instanceof ReusableWorkflowOutputExpr { }
193203

194204
private class JobTree extends StandardPreOrderTree instanceof JobStmt {
195205
override ControlFlowTree getChildNode(int i) {
196206
result =
197207
rank[i](Expression child, Location l |
198-
(child = super.getAStep() or child = super.getOutputStmt() or child = super.getUsesExpr()) and
208+
(
209+
child = super.getAStepStmt() or
210+
child = super.getOutputStmt() or
211+
child = super.getUsesExpr()
212+
) and
199213
l = child.getLocation()
200214
|
201215
child
@@ -213,7 +227,7 @@ private class StepUsesTree extends StandardPreOrderTree instanceof StepUsesExpr
213227
override ControlFlowTree getChildNode(int i) {
214228
result =
215229
rank[i](Expression child, Location l |
216-
child = super.getArgument(_) and l = child.getLocation()
230+
child = super.getArgumentExpr(_) and l = child.getLocation()
217231
|
218232
child
219233
order by
@@ -226,7 +240,7 @@ private class JobUsesTree extends StandardPreOrderTree instanceof JobUsesExpr {
226240
override ControlFlowTree getChildNode(int i) {
227241
result =
228242
rank[i](Expression child, Location l |
229-
child = super.getArgument(_) and l = child.getLocation()
243+
child = super.getArgumentExpr(_) and l = child.getLocation()
230244
|
231245
child
232246
order by

ql/lib/codeql/actions/dataflow/FlowSteps.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private class ActionsFindAndReplaceStringStep extends AdditionalTaintStep {
2727
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
2828
exists(UsesExpr u |
2929
u.getCallee() = "mad9000/actions-find-and-replace-string" and
30-
pred.asExpr() = u.getArgument(["source", "replace"]) and
30+
pred.asExpr() = u.getArgumentExpr(["source", "replace"]) and
3131
succ.asExpr() = u
3232
)
3333
}

0 commit comments

Comments
 (0)