|
| 1 | +private import actions |
| 2 | +private import codeql.actions.TaintTracking |
| 3 | +private import codeql.actions.dataflow.ExternalFlow |
| 4 | +import codeql.actions.dataflow.FlowSources |
| 5 | +private import codeql.actions.security.ArtifactPoisoningQuery |
| 6 | +import codeql.actions.DataFlow |
| 7 | + |
| 8 | +predicate envPathInjectionFromExprSink(DataFlow::Node sink) { |
| 9 | + exists(Expression expr, Run run, string value | |
| 10 | + Utils::writeToGitHubPath(run, value) and |
| 11 | + expr = sink.asExpr() and |
| 12 | + run.getAnScriptExpr() = expr and |
| 13 | + value.indexOf(expr.getExpression()) > 0 |
| 14 | + ) |
| 15 | +} |
| 16 | + |
| 17 | +predicate envPathInjectionFromFileSink(DataFlow::Node sink) { |
| 18 | + exists(Run run, UntrustedArtifactDownloadStep step, string value | |
| 19 | + sink.asExpr() = run and |
| 20 | + step.getAFollowingStep() = run and |
| 21 | + Utils::writeToGitHubPath(run, value) and |
| 22 | + // TODO: add support for other commands like `<`, `jq`, ... |
| 23 | + value.regexpMatch(["\\$\\(", "`"] + ["cat\\s+", "<"] + ".*" + ["`", "\\)"]) |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Holds if a Run step declares an environment variable, uses it to declare a PATH env var. |
| 29 | + * e.g. |
| 30 | + * env: |
| 31 | + * BODY: ${{ github.event.comment.body }} |
| 32 | + * run: | |
| 33 | + * echo "$BODY" >> $GITHUB_PATH |
| 34 | + */ |
| 35 | +predicate envPathInjectionFromEnvSink(DataFlow::Node sink) { |
| 36 | + exists(Run run, Expression expr, string varname, string value | |
| 37 | + sink.asExpr().getInScopeEnvVarExpr(varname) = expr and |
| 38 | + run = sink.asExpr() and |
| 39 | + Utils::writeToGitHubPath(run, value) and |
| 40 | + ( |
| 41 | + value = ["$" + varname, "${" + varname + "}", "$ENV{" + varname + "}"] |
| 42 | + or |
| 43 | + value.matches("$(echo %") and value.indexOf(varname) > 0 |
| 44 | + ) |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +private class EnvPathInjectionSink extends DataFlow::Node { |
| 49 | + EnvPathInjectionSink() { |
| 50 | + envPathInjectionFromExprSink(this) or |
| 51 | + envPathInjectionFromFileSink(this) or |
| 52 | + envPathInjectionFromEnvSink(this) or |
| 53 | + externallyDefinedSink(this, "envpath-injection") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * A taint-tracking configuration for unsafe user input |
| 59 | + * that is used to construct and evaluate an environment variable. |
| 60 | + */ |
| 61 | +private module EnvPathInjectionConfig implements DataFlow::ConfigSig { |
| 62 | + predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 63 | + |
| 64 | + predicate isSink(DataFlow::Node sink) { sink instanceof EnvPathInjectionSink } |
| 65 | +} |
| 66 | + |
| 67 | +/** Tracks flow of unsafe user input that is used to construct and evaluate the PATH environment variable. */ |
| 68 | +module EnvPathInjectionFlow = TaintTracking::Global<EnvPathInjectionConfig>; |
0 commit comments