|
| 1 | +/** |
| 2 | + * @name Checkout of untrusted code in trusted context |
| 3 | + * @description Workflows triggered on `pull_request_target` have read/write access to the base repository and access to secrets. |
| 4 | + * By explicitly checking out and running the build script from a fork the untrusted code is running in an environment |
| 5 | + * that is able to push to the base repository and to access secrets. |
| 6 | + * @kind problem |
| 7 | + * @problem.severity warning |
| 8 | + * @precision low |
| 9 | + * @id actions/untrusted-checkout |
| 10 | + * @tags actions |
| 11 | + * security |
| 12 | + * external/cwe/cwe-094 |
| 13 | + */ |
| 14 | + |
| 15 | +import actions |
| 16 | + |
| 17 | +/** |
| 18 | + * An If node that contains an `actor` check |
| 19 | + */ |
| 20 | +class ActorCheckStmt extends IfStmt { |
| 21 | + ActorCheckStmt() { this.getCondition().regexpMatch(".*github\\.(triggering_)?actor.*") } |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * An If node that contains a `label` check |
| 26 | + */ |
| 27 | +class LabelCheckStmt extends IfStmt { |
| 28 | + LabelCheckStmt() { this.getCondition().regexpMatch(".*github\\.event\\.pull_request\\.labels.*") } |
| 29 | +} |
| 30 | + |
| 31 | +from WorkflowStmt w, JobStmt job, StepUsesExpr checkoutStep |
| 32 | +where |
| 33 | + w.hasTriggerEvent("pull_request_target") and |
| 34 | + w.getAJobStmt() = job and |
| 35 | + job.getAStepStmt() = checkoutStep and |
| 36 | + checkoutStep.getCallee() = "actions/checkout" and |
| 37 | + checkoutStep |
| 38 | + .getArgumentExpr("ref") |
| 39 | + .(ExprAccessExpr) |
| 40 | + .getExpression() |
| 41 | + .matches([ |
| 42 | + "%github.event.pull_request.head.ref%", "%github.event.pull_request.head.sha%", |
| 43 | + "%github.event.pull_request.number%", "%github.event.number%", "%github.head_ref%" |
| 44 | + ]) and |
| 45 | + not exists(ActorCheckStmt check | job.getIfStmt() = check or checkoutStep.getIfStmt() = check) and |
| 46 | + not exists(LabelCheckStmt check | job.getIfStmt() = check or checkoutStep.getIfStmt() = check) |
| 47 | +select checkoutStep, "Potential unsafe checkout of untrusted pull request on 'pull_request_target'." |
0 commit comments