Skip to content

Commit f63244b

Browse files
felickzCopilot
andauthored
[New Query - githubsecuritylab/actions/encoded-secret-in-log] - detect Actions encoded secret in log (#253)
* Actions: add encoded secret in log query Report a secret that is piped through an encoding command (base64, base32, xxd, hexdump, od, rev, uuencode, openssl base64) and printed to the workflow log. The runner masks a secret and a fixed set of its encodings, such as Base64, JSON, URL and XML escapes. It does not recognize other transformations, and Base64 output can still leak through its trailing newline or line wrapping. The query is experimental with medium precision. Decoding, hashing and encrypting before encoding, output redirection, here-document bodies and captured command substitutions are not reported. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 * Do not change the Actions pack version The version changes when a release is cut. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 * Actions: fix redirection, shadowed variable and piped input handling Fix three results of the encoded secret query: - base64 2>> file redirects only standard error, so the encoded secret still reaches the log. It is reported again. - An environment variable that an earlier assignment in the script replaced no longer counts as the secret. - An encoder that reads a redirected input or a file operand (base64 < file, base64 file, openssl base64 -in file) does not read the piped secret. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 * Actions: handle conditional secrets, copied variables and downstream input Address three more findings of the encoded secret query: - A conditional expression such as `github.ref == 'refs/heads/main' && secrets.A || secrets.B` evaluates to a secret, but every expression with && or a comparison was rejected, so the leak was not reported. A secret now counts when the expression can return it: it is not negated, not compared, and not only tested as the left operand of &&. - A copy of an environment variable (T="$TOKEN") resolved to the environment secret even when an earlier assignment had replaced the variable. The copy is now resolved at the line of the assignment. - A downstream stage that reads a redirected input (base64 | tee < /dev/null) prints nothing, but was accepted as a pass-through stage. A quoted angle bracket, as in tr '+' '<', is not a redirection. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
1 parent 54c268b commit f63244b

6 files changed

Lines changed: 778 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Encoded secret written to workflow log
2+
3+
GitHub Actions masks the values of secrets when they appear in workflow logs. The runner masks the secret itself and a fixed set of its encodings, such as Base64 (at three alignments), JSON, URL and XML escapes. It does not recognize other transformations, for example `base32`, `xxd`, `od`, `hexdump` or `rev`, so a secret that is transformed this way and printed is written to the log in clear. Base64 output is also only masked when the encoded text of the whole value appears unbroken. The trailing newline that `echo` adds changes the end of the encoded text, and `base64` wraps long output into lines of 76 characters, so printing the Base64 encoding of a secret can still leave all or part of it readable. Anyone who can read the workflow logs (for public repositories, anyone at all) can decode what is printed and recover the secret.
4+
5+
This query reports `run` steps using a Bash shell where a secret (`${{ secrets.* }}`, `${{ toJSON(secrets) }}` or `${{ github.token }}`, used inline, through an environment variable, or through a shell variable assigned from one) is piped into an encoding command (`base64`, `base32`, `basenc`, `xxd`, `od`, `hexdump`, `rev`, `uuencode`, `openssl base64` or `openssl enc -base64`) whose output reaches the log, that is, it is not redirected to a file (with `>` or an output option such as `base64 -o`), captured in a variable or command substitution, or consumed by another command. An expression whose result is one of these secrets counts, for example `${{ github.ref == 'refs/heads/main' && secrets.PROD || secrets.DEV }}`. An expression that only tests or compares a secret does not, for example `${{ secrets.X != '' }}`, `${{ !secrets.X }}` or `${{ secrets.X && 'yes' }}`. Decoding commands such as `base64 --decode` or `xxd -r` are not reported.
6+
7+
## Known limitations
8+
9+
Variable assignments are ordered by their source line, not by shell control flow. Assignments inside functions or loops may execute in a different order than their source lines. Pipeline source attribution recognizes `curl` as a command that does not forward secret arguments to stdout; the output behavior of other commands is not modeled.
10+
11+
An input redirection (`base64 < file`) that replaces the pipe is recognized for every stage. A file operand that replaces the pipe is recognized only for `base64`, `base32`, `basenc` (`base64 file`) and `openssl` (`-in file`), so for other encoders such as `xxd file` the pipe is assumed to be the input. An assignment to an environment variable earlier in the script replaces its secret value for the statements after it, and for the copies of it made after it (`T="$TOKEN"`), even when the assignment only runs in some branches. Only one level of copies is followed, not a copy of a copy. An expression that compares a combination of secrets, as in `${{ (secrets.A || secrets.B) == '' }}`, is not recognized as a boolean and is reported.
12+
13+
A command between the secret and the encoder that destroys the secret, for example a hash such as `sha256sum` or encryption with a key, stops the query from reporting the statement. Only `tee`, `tr`, `cat` and `fold` are treated as keeping the data recoverable. Redirecting the output of the whole script with `exec >` is recognized, and so is the body of a here-document, which is data and not a command. Other ways of redirecting output are not modeled.
14+
15+
Decoding is not reported because restoring a certificate or key from a secret, as in `echo "$CERT_B64" | base64 --decode -o cert.p12`, is a routine step. Printing the decoded value to the log is a leak that this query does not find.
16+
17+
## Recommendation
18+
19+
Do not print secrets, or values derived from secrets, to the workflow log. If an encoded form of a secret is needed, capture it in a variable and register it with the `::add-mask::` workflow command before using it, and pass it to the consuming command through a file or standard input instead of printing it.
20+
21+
## Example
22+
23+
The following workflow prints the hexadecimal encoding of a secret. Masking does not apply to the encoded value, so the secret is exposed:
24+
25+
```yaml
26+
- name: Get secret
27+
env:
28+
SUPER_SECRET: ${{ secrets.SUPER_SECRET }}
29+
run: echo "$SUPER_SECRET" | xxd -p
30+
```
31+
32+
The encoded value should instead be masked and consumed without printing it:
33+
34+
```yaml
35+
- name: Use secret
36+
env:
37+
SUPER_SECRET: ${{ secrets.SUPER_SECRET }}
38+
run: |
39+
ENCODED=$(echo "$SUPER_SECRET" | base64 -w0)
40+
echo "::add-mask::$ENCODED"
41+
echo "$ENCODED" | docker login -u user --password-stdin
42+
```
43+
44+
## References
45+
46+
- GitHub Docs: [Using secrets in GitHub Actions](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions).
47+
- GitHub Docs: [Masking a value in a log](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#masking-a-value-in-a-log).
48+
- Common Weakness Enumeration: [CWE-532](https://cwe.mitre.org/data/definitions/532.html).

0 commit comments

Comments
 (0)