Repository navigation
CI: reject unmasked credential writes to $GITHUB_ENV / $GITHUB_OUTPUT - #2347
Conversation
Add a static, line-based check (.github/scripts/check-secret-writes.sh)
that scans .github/**/*.sh and workflow YAML. It fails when a line writes
a credential-looking variable or ${{ expression }} to $GITHUB_ENV or
$GITHUB_OUTPUT without an earlier ::add-mask:: of the same reference in
the same file. A trailing "# secret-write-ok: <reason>" allows justified
exceptions.
Tests use fixtures, including the pre-fix jfrog-oidc-token.sh (must be
flagged) and the current masked one (must pass), and scan the repository.
They run as a new step of the existing secret masking workflow.
DECO-29706
Co-authored-by: Isaac <no-reply@databricks.com>
|
🤖 Integration tests ❌ failed for |
louis-johnston-db
left a comment
There was a problem hiding this comment.
AI automated review. The comments in this review were written by an automated AI reviewer.
| while IFS= read -r line || [[ -n "$line" ]]; do | ||
| n=$((n + 1)) | ||
| trimmed="${line#"${line%%[![:space:]]*}"}" | ||
| [[ "$trimmed" == \#* ]] && continue |
There was a problem hiding this comment.
AI automated review: Any line containing ::add-mask:: is excluded from sink analysis, regardless of command order or which value is masked.
Trigger: A one-line command such as echo "TOKEN=$TOKEN" >> "$GITHUB_ENV"; echo "::add-mask::$TOKEN" passes even though the write precedes the mask.
There was a problem hiding this comment.
Fixed in c2bc691. Each line is now read as a sequence of commands split on ;, && and || (outside ${{ }}), so a mask later on the same line no longer covers an earlier write, and a mask of one value no longer exempts the rest of the line. Covered by review-findings.yml lines 15 and 16.
|
|
||
| check_file() { | ||
| local file="$1" n=0 line trimmed key ref flagged sink | ||
| local masked=$'\n' |
There was a problem hiding this comment.
AI automated review: Mask state is accumulated across the entire workflow file rather than per job, although GitHub masks are job-scoped.
Trigger: If an earlier job masks $TOKEN and a later job writes $TOKEN unmasked, the checker accepts the later write even though it runs on a separate runner.
There was a problem hiding this comment.
Fixed in c2bc691. In workflow files, masks are now forgotten at each job key under jobs:. Covered by review-findings.yml line 14.
| } | ||
|
|
||
| auto_masked() { | ||
| [[ "$1" == '${{secrets.'* || "$1" == '${{github.token}}' ]] |
There was a problem hiding this comment.
AI automated review: The automatic-mask exemption accepts every compound expression beginning with secrets., not only a direct secret reference.
Trigger: ${{ secrets.OPTIONAL_TOKEN || steps.login.outputs.token }} is skipped; when the secret is absent, the unmasked step output is written.
There was a problem hiding this comment.
Fixed in c2bc691. Only a bare ${{ secrets.NAME }} or ${{ github.token }} is skipped now; any other expression is checked like a variable. Covered by review-findings.yml line 20. The same commit also counts a mask only when it masks exactly the written reference, so ::add-mask::prefix-$TOKEN no longer covers $TOKEN (line 19).
- Read each line as commands split on ;, && and ||, so a mask later on the
same line no longer covers an earlier write, and a mask of one value no
longer exempts the whole line.
- Count a mask only when it masks exactly the reference that is written;
masking prefix-$TOKEN does not cover $TOKEN.
- Forget masks at each job boundary in workflow files, since masks are
scoped to a job.
- Skip only a bare ${{ secrets.NAME }} or ${{ github.token }}; an
expression that can fall back to a step output is checked.
Adds a fixture covering each case.
Co-authored-by: Isaac <no-reply@databricks.com>
louis-johnston-db
left a comment
There was a problem hiding this comment.
AI automated review. The comments in this review were written by an automated AI reviewer.
| grep -qE "$ALLOW_RE" <<< "$line" && allowed=true | ||
|
|
||
| while IFS= read -r cmd; do | ||
| if [[ "$cmd" == *"::add-mask::"* ]]; then |
There was a problem hiding this comment.
AI automated review: Any textual occurrence is treated as an executed mask. For example, MASK_TEMPLATE="::add-mask::$TOKEN" records TOKEN even though the workflow command is never emitted, so a later unmasked write passes.
Trigger: A script assigns or otherwise uses an ::add-mask:: string without printing it, then writes the referenced credential to a GitHub environment file.
| continue | ||
| elif [[ "$line" =~ ^[A-Za-z_] ]]; then | ||
| in_jobs=false | ||
| elif [[ "$in_jobs" == true && "$line" =~ ^([[:space:]]+)[A-Za-z0-9_-]+:[[:space:]]*(#.*)?$ ]]; then |
There was a problem hiding this comment.
AI automated review: The job-boundary matcher excludes valid quoted YAML job IDs. Mask state therefore carries into the next quoted job even though GitHub runs it with a fresh mask scope.
Trigger: One job masks $TOKEN, and a later job declared as "second": writes a different $TOKEN without masking it.
| grep -oE "$REF_RE" <<< "$1" | while IFS= read -r ref; do | ||
| case "$ref" in | ||
| '${{'*) echo "${ref//[[:space:]]/}" ;; | ||
| *) ref="${ref#\$}"; echo "${ref#\{}" ;; |
There was a problem hiding this comment.
AI automated review: Shell parameter transformations are reduced to the underlying variable name. A raw mask for $TOKEN therefore incorrectly covers ${TOKEN^^} even though GitHub masks values, not variable names.
Trigger: With TOKEN=abc, a script masks $TOKEN and then writes API_TOKEN=${TOKEN^^}; the checker passes, but ABC remains unmasked.
|
🤖 Integration tests ✅ passed for |
…OUTPUT (#2350) ## Why SEC-35859: `.github/scripts/jfrog-oidc-token.sh` wrote the JFrog token to `$GITHUB_ENV` without `::add-mask::`, so it showed up in public run logs. #2291 masked it, and #2334 added a runtime test (`secret-masking.sh`) for the existing credential scripts. Nothing stopped a new script or workflow step from making the same mistake. This adds a static check that covers every script and workflow under `.github/` (DECO-29706). ## What - `.github/scripts/check-secret-writes.sh`: a dependency-free bash check. It scans `.github/**/*.sh`, `*.yml` and `*.yaml`. It checks every non-comment line that writes to `$GITHUB_ENV` or `$GITHUB_OUTPUT`. The line fails when either: - it references a variable or `${{ expression }}` whose name looks like a credential (`token`, `secret`, `passw(or)d`, `credential`, `api_key`, `private_key`, or a `key` / `auth` name segment, case-insensitive), or - the key it writes looks like a credential and its value is not a literal, and that same reference was not passed to `::add-mask::` on an earlier line of the same file. - Exceptions: `${{ secrets.* }}` and `${{ github.token }}` are skipped, because Actions masks them automatically. A justified write can end with `# secret-write-ok: <reason>`. - `.github/scripts/tests/check-secret-writes.sh` and `fixtures/secret-writes/`: tests for the check. They run as a new step of the existing `secret-masking-test.yml` job, so there is no new workflow. ## What the heuristic catches and misses It is a line-based heuristic, not a shell parser. Catches: - the pre-fix JFrog pattern - a mask placed after the write - unmasked step outputs in `${{ }}` - a credential-named key filled from `$(...)` Misses: - Multi-line writes: heredocs into `$GITHUB_ENV`, `{ ...; } >> "$GITHUB_ENV"`. - Credentials behind a non-credential name. For example, `PIP_INDEX_URL` embeds the token. The runtime test still covers that case for the existing scripts. - Masks done in another file or another step's script. The mask must appear earlier in the same file. - Writes to rc files such as `~/.npmrc`. `has_token`-style outputs with literal values do not trip it, and neither do env mappings like `GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}`. Neither one writes a non-literal credential value to a sink. A name like `KEY_ID` is flagged; the allow comment is the escape hatch for it. ## Tests `bash .github/scripts/tests/check-secret-writes.sh` checks that: - `jfrog-oidc-token.prefix.sh`, the pre-fix script taken from before b4cec4e, is flagged at the write line. - `mask-after-write.sh` and `unmasked-workflow.yml` are flagged at the expected lines. - `jfrog-oidc-token.masked.sh`, a copy of the current script, and `clean-workflow.yml` pass. `clean-workflow.yml` covers masked, auto-masked, literal and allow-listed writes. - Removing the allow comment from `clean-workflow.yml` makes it fail. - The repository itself passes, with fixtures excluded. Run locally on this branch: all pass, and `secret-masking.sh` still passes. A copy of `jfrog-oidc-token.sh` with its `::add-mask::` line deleted is flagged. This is the `release/3.x` counterpart of #2347. The scripts are the same. The fixture YAML uses the 4-space style that `oxfmt` enforces on this branch, and the repository scan here covers the `release/3.x` workflows. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
|
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
|
🤖 Integration tests running for |
| exprs+=("${BASH_REMATCH[1]}") | ||
| s="${s/"${BASH_REMATCH[1]}"/$'\x01'$((${#exprs[@]} - 1))$'\x01'}" | ||
| done | ||
| s="${s//&&/$'\n'}" |
There was a problem hiding this comment.
[blocking] commands() also splits on &&, || and ; inside $(...), so a command substitution like echo "JFROG_TOKEN=$(jq -r .access_token resp.json || true)" >> "$GITHUB_ENV" gets cut into two pieces. The piece with the sink has no KEY= and no credential ref, so the write passes, even though the first version of the script flagged it. Could you protect $(...) and backticks from splitting the same way ${{ }} is protected, and add a fixture line for it?
Why
SEC-35859:
.github/scripts/jfrog-oidc-token.shwrote the JFrog token to$GITHUB_ENVwithout::add-mask::, so it showed up in public run logs. #2291 masked it, and #2334 added a runtime test (secret-masking.sh) for the existing credential scripts. Nothing stopped a new script or workflow step from making the same mistake. This adds a static check that covers every script and workflow under.github/(DECO-29706).What
.github/scripts/check-secret-writes.sh: a dependency-free bash check. It scans.github/**/*.sh,*.ymland*.yaml. It checks every non-comment line that writes to$GITHUB_ENVor$GITHUB_OUTPUT. The line fails when either:${{ expression }}whose name looks like a credential (token,secret,passw(or)d,credential,api_key,private_key, or akey/authname segment, case-insensitive), orand that same reference was not passed to
::add-mask::on an earlier line of the same file.Exceptions:
${{ secrets.* }}and${{ github.token }}are skipped, because Actions masks them automatically. A justified write can end with# secret-write-ok: <reason>..github/scripts/tests/check-secret-writes.shandfixtures/secret-writes/: tests for the check. They run as a new step of the existingsecret-masking-test.ymljob, so there is no new workflow.What the heuristic catches and misses
It is a line-based heuristic, not a shell parser.
Catches:
${{ }}$(...)Misses:
$GITHUB_ENV,{ ...; } >> "$GITHUB_ENV".PIP_INDEX_URLembeds the token. The runtime test still covers that case for the existing scripts.~/.npmrc.has_token-style outputs with literal values do not trip it, and neither do env mappings likeGITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}. Neither one writes a non-literal credential value to a sink. A name likeKEY_IDis flagged; the allow comment is the escape hatch for it.Tests
bash .github/scripts/tests/check-secret-writes.shchecks that:jfrog-oidc-token.prefix.sh, the pre-fix script taken from before b4cec4e, is flagged at the write line.mask-after-write.shandunmasked-workflow.ymlare flagged at the expected lines.jfrog-oidc-token.masked.sh, a copy of the current script, andclean-workflow.ymlpass.clean-workflow.ymlcovers masked, auto-masked, literal and allow-listed writes.clean-workflow.ymlmakes it fail.Run locally on this branch: all pass, and
secret-masking.shstill passes. A copy ofjfrog-oidc-token.shwith its::add-mask::line deleted is flagged.This pull request and its description were written by Isaac.