Skip to content

Commit ffbddb1

Browse files
author
Alvaro Muñoz
committed
Simplify Callable/call match
1 parent 090d22f commit ffbddb1

File tree

1 file changed

+28
-27
lines changed
  • ql/lib/codeql/actions/ast/internal

1 file changed

+28
-27
lines changed

ql/lib/codeql/actions/ast/internal/Ast.qll

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,22 @@ class CompositeActionImpl extends AstNodeImpl, TCompositeAction {
308308
LocalJobImpl getACallerJob() { result = this.getACallerStep().getEnclosingJob() }
309309

310310
UsesStepImpl getACallerStep() {
311-
exists(UsesStepImpl caller, string gwf_path, string path |
312-
// the workflow files may not be rooted in the parent directory of .github/workflows
313-
// extract the offset so we can remove it from the action path
314-
gwf_path =
315-
caller
316-
.getLocation()
311+
exists(DataFlow::CallNode call |
312+
call.getCalleeNode() = this and
313+
result = call.getCfgNode().getAstNode()
314+
)
315+
}
316+
317+
string getResolvedPath() {
318+
result =
319+
["", "./"] +
320+
this.getLocation()
317321
.getFile()
318322
.getRelativePath()
319-
.prefix(caller.getLocation().getFile().getRelativePath().indexOf(".github/workflows/")) and
320-
path = this.getLocation().getFile().getRelativePath().replaceAll(gwf_path, "") and
321-
caller.getCallee() = ["", "./"] + path.prefix(path.indexOf(["/action.yml", "/action.yaml"])) and
322-
result = caller
323-
)
323+
.replaceAll(getRepoRoot(), "")
324+
.replaceAll("/action.yml", "")
325+
.replaceAll("/action.yaml", "")
326+
.replaceAll(".github/reusable_workflows/", "")
324327
}
325328

326329
private predicate hasExplicitSecretAccess() {
@@ -352,6 +355,8 @@ class CompositeActionImpl extends AstNodeImpl, TCompositeAction {
352355
)
353356
}
354357

358+
EventImpl getATriggerEvent() { result = this.getACallerJob().getATriggerEvent() }
359+
355360
/** Holds if the action is privileged and externally triggerable. */
356361
predicate isPrivilegedExternallyTriggerable() {
357362
// the action is externally triggerable
@@ -447,6 +452,16 @@ class ReusableWorkflowImpl extends AstNodeImpl, WorkflowImpl {
447452
result = call.getCfgNode().getAstNode()
448453
)
449454
}
455+
456+
string getResolvedPath() {
457+
result =
458+
["", "./"] +
459+
this.getLocation()
460+
.getFile()
461+
.getRelativePath()
462+
.replaceAll(getRepoRoot(), "")
463+
.replaceAll(".github/reusable_workflows/", "")
464+
}
450465
}
451466

452467
class InputsImpl extends AstNodeImpl, TInputsNode {
@@ -1229,15 +1244,6 @@ abstract class UsesImpl extends AstNodeImpl {
12291244
}
12301245
}
12311246

1232-
/**
1233-
* Gets a regular expression that parses an `owner/repo@version` reference within a `uses` field in an Actions job step.
1234-
* The capture groups are:
1235-
* 1: The owner of the repository where the Action comes from, e.g. `actions` in `actions/checkout@v2`
1236-
* 2: The name of the repository where the Action comes from, e.g. `checkout` in `actions/checkout@v2`.
1237-
* 3: The version reference used when checking out the Action, e.g. `v2` in `actions/checkout@v2`.
1238-
*/
1239-
private string usesParser() { result = "([^/]+)/([^/@]+)@(.+)" }
1240-
12411247
/** A Uses step represents a call to an action that is defined in a GitHub repository. */
12421248
class UsesStepImpl extends StepImpl, UsesImpl {
12431249
YamlScalar u;
@@ -1249,19 +1255,14 @@ class UsesStepImpl extends StepImpl, UsesImpl {
12491255
/** Gets the owner and name of the repository where the Action comes from, e.g. `actions/checkout` in `actions/checkout@v2`. */
12501256
override string getCallee() {
12511257
if u.getValue().indexOf("@") > 0
1252-
then
1253-
result =
1254-
(
1255-
u.getValue().regexpCapture(usesParser(), 1) + "/" +
1256-
u.getValue().regexpCapture(usesParser(), 2)
1257-
).toLowerCase()
1258+
then result = u.getValue().prefix(u.getValue().indexOf("@"))
12581259
else result = u.getValue()
12591260
}
12601261

12611262
override ScalarValueImpl getCalleeNode() { result.getNode() = u }
12621263

12631264
/** Gets the version reference used when checking out the Action, e.g. `v2` in `actions/checkout@v2`. */
1264-
override string getVersion() { result = u.getValue().regexpCapture(usesParser(), 3) }
1265+
override string getVersion() { result = u.getValue().suffix(u.getValue().indexOf("@") + 1) }
12651266

12661267
override string toString() {
12671268
if exists(this.getId()) then result = "Uses Step: " + this.getId() else result = "Uses Step"

0 commit comments

Comments
 (0)