|
| 1 | +import actions |
| 2 | +import codeql.actions.DataFlow |
| 3 | + |
| 4 | +bindingset[s] |
| 5 | +predicate containsPullRequestNumber(string s) { |
| 6 | + exists( |
| 7 | + Utils::normalizeExpr(s) |
| 8 | + .regexpFind([ |
| 9 | + "\\bgithub\\.event\\.number\\b", "\\bgithub\\.event\\.issue\\.number\\b", |
| 10 | + "\\bgithub\\.event\\.pull_request\\.id\\b", |
| 11 | + "\\bgithub\\.event\\.pull_request\\.number\\b", |
| 12 | + "\\bgithub\\.event\\.check_suite\\.pull_requests\\[\\d+\\]\\.id\\b", |
| 13 | + "\\bgithub\\.event\\.check_suite\\.pull_requests\\[\\d+\\]\\.number\\b", |
| 14 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.pull_requests\\[\\d+\\]\\.id\\b", |
| 15 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.pull_requests\\[\\d+\\]\\.number\\b", |
| 16 | + "\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.id\\b", |
| 17 | + "\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.number\\b", |
| 18 | + // heuristics |
| 19 | + "\\bpr_number\\b", "\\bpr_id\\b" |
| 20 | + ], _, _) |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +bindingset[s] |
| 25 | +predicate containsHeadSHA(string s) { |
| 26 | + exists( |
| 27 | + Utils::normalizeExpr(s) |
| 28 | + .regexpFind([ |
| 29 | + "\\bgithub\\.event\\.pull_request\\.head\\.sha\\b", |
| 30 | + "\\bgithub\\.event\\.pull_request\\.merge_commit_sha\\b", |
| 31 | + "\\bgithub\\.event\\.workflow_run\\.head_commit\\.id\\b", |
| 32 | + "\\bgithub\\.event\\.workflow_run\\.head_sha\\b", |
| 33 | + "\\bgithub\\.event\\.check_suite\\.after\\b", |
| 34 | + "\\bgithub\\.event\\.check_suite\\.head_commit\\.id\\b", |
| 35 | + "\\bgithub\\.event\\.check_suite\\.head_sha\\b", |
| 36 | + "\\bgithub\\.event\\.check_suite\\.pull_requests\\[\\d+\\]\\.head\\.sha\\b", |
| 37 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.after\\b", |
| 38 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.head_commit\\.id\\b", |
| 39 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.head_sha\\b", |
| 40 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.pull_requests\\[\\d+\\]\\.head\\.sha\\b", |
| 41 | + "\\bgithub\\.event\\.check_run\\.head_sha\\b", |
| 42 | + "\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.head\\.sha\\b", |
| 43 | + // heuristics |
| 44 | + "\\bhead\\.sha\\b", "\\bhead_sha\\b", "\\bpr_head_sha\\b" |
| 45 | + ], _, _) |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +bindingset[s] |
| 50 | +predicate containsHeadRef(string s) { |
| 51 | + exists( |
| 52 | + Utils::normalizeExpr(s) |
| 53 | + .regexpFind([ |
| 54 | + "\\bgithub\\.event\\.pull_request\\.head\\.ref\\b", "\\bgithub\\.head_ref\\b", |
| 55 | + "\\bgithub\\.event\\.workflow_run\\.head_branch\\b", |
| 56 | + "\\bgithub\\.event\\.check_suite\\.pull_requests\\[\\d+\\]\\.head\\.ref\\b", |
| 57 | + "\\bgithub\\.event\\.check_run\\.check_suite\\.pull_requests\\[\\d+\\]\\.head\\.ref\\b", |
| 58 | + "\\bgithub\\.event\\.check_run\\.pull_requests\\[\\d+\\]\\.head\\.ref\\b", |
| 59 | + // heuristics |
| 60 | + "\\bhead\\.ref\\b", "\\bhead_ref\\b", "\\bpr_head_ref\\b", |
| 61 | + // env vars |
| 62 | + "\\benv\\.GITHUB_HEAD_REF\\b", |
| 63 | + ], _, _) |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +/** Checkout of a Pull Request HEAD ref */ |
| 68 | +abstract class PRHeadCheckoutStep extends Step { } |
| 69 | + |
| 70 | +/** Checkout of a Pull Request HEAD ref using actions/checkout action */ |
| 71 | +class ActionsMutableRefCheckout extends PRHeadCheckoutStep instanceof UsesStep { |
| 72 | + ActionsMutableRefCheckout() { |
| 73 | + this.getCallee() = "actions/checkout" and |
| 74 | + ( |
| 75 | + // ref argument contains the PR id/number or head ref/sha |
| 76 | + exists(Expression e | |
| 77 | + ( |
| 78 | + containsHeadRef(e.getExpression()) or |
| 79 | + containsPullRequestNumber(e.getExpression()) |
| 80 | + ) and |
| 81 | + DataFlow::hasLocalFlowExpr(e, this.getArgumentExpr("ref")) |
| 82 | + ) |
| 83 | + or |
| 84 | + // 3rd party actions returning the PR head sha/ref |
| 85 | + exists(UsesStep step | |
| 86 | + step.getCallee() = ["eficode/resolve-pr-refs", "xt0rted/pull-request-comment-branch"] and |
| 87 | + // TODO: This should be read step of the head_sha or head_ref output vars |
| 88 | + this.getArgument("ref").regexpMatch(".*head_ref.*") and |
| 89 | + DataFlow::hasLocalFlowExpr(step, this.getArgumentExpr("ref")) |
| 90 | + ) |
| 91 | + or |
| 92 | + // heuristic base on the step id and field name |
| 93 | + exists(StepsExpression e | |
| 94 | + this.getArgumentExpr("ref") = e and |
| 95 | + ( |
| 96 | + e.getStepId().matches(["%ref%", "%branch%"]) or |
| 97 | + e.getFieldName().matches(["%ref%", "%branch%"]) |
| 98 | + ) |
| 99 | + ) |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** Checkout of a Pull Request HEAD ref using actions/checkout action */ |
| 105 | +class ActionsSHACheckout extends PRHeadCheckoutStep instanceof UsesStep { |
| 106 | + ActionsSHACheckout() { |
| 107 | + this.getCallee() = "actions/checkout" and |
| 108 | + ( |
| 109 | + // ref argument contains the PR id/number or head ref/sha |
| 110 | + exists(Expression e | |
| 111 | + containsHeadSHA(e.getExpression()) and |
| 112 | + DataFlow::hasLocalFlowExpr(e, this.getArgumentExpr("ref")) |
| 113 | + ) |
| 114 | + or |
| 115 | + // 3rd party actions returning the PR head sha/ref |
| 116 | + exists(UsesStep step | |
| 117 | + step.getCallee() = ["eficode/resolve-pr-refs", "xt0rted/pull-request-comment-branch"] and |
| 118 | + this.getArgument("ref").regexpMatch(".*head_sha.*") and |
| 119 | + DataFlow::hasLocalFlowExpr(step, this.getArgumentExpr("ref")) |
| 120 | + ) |
| 121 | + or |
| 122 | + // heuristic base on the step id and field name |
| 123 | + exists(StepsExpression e | |
| 124 | + this.getArgumentExpr("ref") = e and |
| 125 | + ( |
| 126 | + e.getStepId().matches(["%sha%", "%commit%"]) or |
| 127 | + e.getFieldName().matches(["%sha%", "%commit%"]) |
| 128 | + ) |
| 129 | + ) |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** Checkout of a Pull Request HEAD ref using git within a Run step */ |
| 135 | +class GitMutableRefCheckout extends PRHeadCheckoutStep instanceof Run { |
| 136 | + GitMutableRefCheckout() { |
| 137 | + exists(string line | |
| 138 | + this.getScript().splitAt("\n") = line and |
| 139 | + line.regexpMatch(".*git\\s+(fetch|pull).*") and |
| 140 | + ( |
| 141 | + (containsHeadRef(line) or containsPullRequestNumber(line)) |
| 142 | + or |
| 143 | + exists(string varname, string expr | |
| 144 | + expr = this.getInScopeEnvVarExpr(varname).getExpression() and |
| 145 | + ( |
| 146 | + containsHeadRef(expr) or |
| 147 | + containsPullRequestNumber(expr) |
| 148 | + ) and |
| 149 | + exists(line.regexpFind(varname, _, _)) |
| 150 | + ) |
| 151 | + ) |
| 152 | + ) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** Checkout of a Pull Request HEAD ref using git within a Run step */ |
| 157 | +class GitSHACheckout extends PRHeadCheckoutStep instanceof Run { |
| 158 | + GitSHACheckout() { |
| 159 | + exists(string line | |
| 160 | + this.getScript().splitAt("\n") = line and |
| 161 | + line.regexpMatch(".*git\\s+(fetch|pull).*") and |
| 162 | + ( |
| 163 | + containsHeadSHA(line) |
| 164 | + or |
| 165 | + exists(string varname, string expr | |
| 166 | + expr = this.getInScopeEnvVarExpr(varname).getExpression() and |
| 167 | + containsHeadSHA(expr) and |
| 168 | + exists(line.regexpFind(varname, _, _)) |
| 169 | + ) |
| 170 | + ) |
| 171 | + ) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/** Checkout of a Pull Request HEAD ref using gh within a Run step */ |
| 176 | +class GhMutableRefCheckout extends PRHeadCheckoutStep instanceof Run { |
| 177 | + GhMutableRefCheckout() { |
| 178 | + exists(string line | |
| 179 | + this.getScript().splitAt("\n") = line and |
| 180 | + line.regexpMatch(".*gh\\s+pr\\s+checkout.*") and |
| 181 | + ( |
| 182 | + (containsHeadRef(line) or containsPullRequestNumber(line)) |
| 183 | + or |
| 184 | + exists(string varname | |
| 185 | + ( |
| 186 | + containsHeadRef(this.getInScopeEnvVarExpr(varname).getExpression()) or |
| 187 | + containsPullRequestNumber(this.getInScopeEnvVarExpr(varname).getExpression()) |
| 188 | + ) and |
| 189 | + exists(line.regexpFind(varname, _, _)) |
| 190 | + ) |
| 191 | + ) |
| 192 | + ) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +/** Checkout of a Pull Request HEAD ref using gh within a Run step */ |
| 197 | +class GhSHACheckout extends PRHeadCheckoutStep instanceof Run { |
| 198 | + GhSHACheckout() { |
| 199 | + exists(string line | |
| 200 | + this.getScript().splitAt("\n") = line and |
| 201 | + line.regexpMatch(".*gh\\s+pr\\s+checkout.*") and |
| 202 | + ( |
| 203 | + containsHeadSHA(line) |
| 204 | + or |
| 205 | + exists(string varname | |
| 206 | + containsHeadSHA(this.getInScopeEnvVarExpr(varname).getExpression()) and |
| 207 | + exists(line.regexpFind(varname, _, _)) |
| 208 | + ) |
| 209 | + ) |
| 210 | + ) |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/** An If node that contains an actor, user or label check */ |
| 215 | +class ControlCheck extends If { |
| 216 | + ControlCheck() { |
| 217 | + exists( |
| 218 | + Utils::normalizeExpr(this.getCondition()) |
| 219 | + .regexpFind([ |
| 220 | + "\\bgithub\\.actor\\b", // actor |
| 221 | + "\\bgithub\\.triggering_actor\\b", // actor |
| 222 | + "\\bgithub\\.event\\.comment\\.user\\.login\\b", //user |
| 223 | + "\\bgithub\\.event\\.pull_request\\.user\\.login\\b", //user |
| 224 | + "\\bgithub\\.event\\.pull_request\\.labels\\b", // label |
| 225 | + "\\bgithub\\.event\\.label\\.name\\b" // label |
| 226 | + ], _, _) |
| 227 | + ) |
| 228 | + } |
| 229 | +} |
0 commit comments