Sub topic 12
Permissions, secrets, and masked output
This chapter assumes you can read a workflow log, find the first real error in a failed step, and edit a workflow YAML file. It covers three things that often cause confusing failures: the GITHUB_TOKEN permissions, secrets that are missing or empty, and the masking that hides secret values in logs.
Why these failures look different
Most failures show a command's own error, such as a failing test or a missing file. Permission and secret failures come from the platform boundary. The command is fine, but the credential it uses is too weak, absent, or hidden.
Three signs suggest this category:
- The log contains
403,Resource not accessible by integration,permission denied, orBad credentials. - The step passes on your machine, where you are logged in with broader rights.
- The step fails only on pull requests from forks, or only on a particular branch.
Confirm this using the method from the earlier chapters. Find the first error, then ask whether the command ran with the right identity.
The GITHUB_TOKEN and its permissions
Each workflow run gets a temporary token named GITHUB_TOKEN. It is available as ${{ github.token }} or ${{ secrets.GITHUB_TOKEN }}. It expires when the job ends. The actions/checkout action uses it by default to fetch your code.
You control what the token may do with a permissions block. Listing any scope in the block sets every scope you did not list to none. That is the most common surprise: adding one permission removes the others.
permissions:
contents: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- run: gh pr comment "$PR_NUMBER" --body "Checks finished"
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}Here contents: read lets the job check out code, and pull-requests: write lets gh post a comment. Without the second line, the gh step fails with a 403 error.
Where permissions can be set
| Level | Where it is set | Effect |
|---|---|---|
| Workflow | `permissions:` at the top of the file | Applies to every job without its own block |
| Job | `permissions:` inside a job | Overrides the workflow-level block for that job |
| Repository or organization | Settings, then Actions, then General | Sets the default token access when no block exists |
A job-level block replaces the workflow-level block. It does not merge with it. If a job defines permissions: { issues: write }, that job loses contents: read unless you list it again.
The repository default may be read-only or read and write, depending on settings and when the repository was created. Do not depend on the default. State what each workflow needs.
Reading a permission failure
A typical failure looks like this:
HttpError: Resource not accessible by integration
gh: Resource not accessible by integration (HTTP 403)The step started and the command ran, but the API refused it. To fix it:
- Identify the API call that failed. The step name and the command usually tell you.
- Find which scope that call needs in the GitHub documentation for the REST endpoint.
- Add that scope at the narrowest level that works, preferably on the one job.
- Re-run and check that the 403 is gone.
The log for each job also begins with a "Set up job" step. Expanding it shows a "GITHUB_TOKEN Permissions" section that lists the effective scopes. Check this section before guessing. It shows what the token actually received, not what you meant to write.
Pull requests from forks
For pull_request events from a fork, the token is read-only. Secrets other than GITHUB_TOKEN are not passed to the workflow. A step that comments on the PR or pushes a branch can fail only for outside contributors. Editing permissions does not grant write access in this case.
Do not work around this by switching to pull_request_target without understanding the risk. That event runs with write access and secrets, and checking out and running untrusted fork code there can leak them. Prefer a design where the untrusted build runs without secrets, and a separate privileged workflow acts on its results.
Secrets
A secret is an encrypted value stored in repository, environment, or organization settings. You read it in a workflow with ${{ secrets.NAME }}.
steps:
- name: Deploy
run: ./deploy.sh
env:
API_TOKEN: ${{ secrets.API_TOKEN }}Missing secrets fail quietly
If secrets.API_TOKEN does not exist, GitHub does not raise an error. The expression evaluates to an empty string. The failure then appears later, inside the command, as an authentication error or a strange message about an empty value.
Common causes:
- The name is misspelled. Secret names are case-insensitive, but a typo still creates a different name.
- The secret lives in an environment, but the job does not declare
environment: production. - The secret is defined at the organization level but not shared with this repository.
- The run is from a fork, or was started by Dependabot, which uses a separate Dependabot secrets store.
To test whether a secret reached the step without printing it, check for emptiness:
steps:
- name: Check secret is set
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: |
if [ -z "$API_TOKEN" ]; then
echo "::error::API_TOKEN is empty or not available to this run"
exit 1
fiThis prints a clear annotation and stops the job at the real cause, rather than letting a later command fail in a confusing way.
Pass secrets through env
Put secrets in env and refer to them as shell variables. Writing ${{ secrets.NAME }} directly inside a run script inserts the value into the script text before the shell starts. A value containing quotes or special characters can then break the command or change what it does. An environment variable avoids this.
Masked output
GitHub hides registered secret values in logs. When a secret's exact value would appear, the log shows *** instead.
Run ./deploy.sh
Authenticating with token ***
Error: 401 UnauthorizedMasking is useful, but it has limits that affect diagnosis:
- It matches exact strings. If a script transforms the secret (base64 encoding, URL encoding, splitting it into pieces), the transformed value is not masked.
- Very short or common values are poor secrets. If a secret is
1, every1in the log may be hidden, which makes the log unreadable. - Structured secrets such as a JSON blob are masked as a whole. Fields inside the JSON are not masked on their own unless you register them.
- Masking applies to the log. It does not stop a step from sending the value elsewhere.
When the log shows *** in an unexpected place, the cause is usually a value that equals a secret. This can hide real information, such as a region name or a username that was stored as a secret. Consider storing non-sensitive settings as plain variables (vars.NAME) so they stay readable.
Masking your own values
If a step generates a sensitive value at runtime, register it so later log lines hide it:
steps:
- name: Create temporary credential
run: |
TOKEN=$(./get-temp-token.sh)
echo "::add-mask::$TOKEN"
echo "TEMP_TOKEN=$TOKEN" >> "$GITHUB_ENV"Call ::add-mask:: before the value is printed anywhere else. Masking does not apply retroactively to lines already written.
Debug logging and secrets
Debug logging (covered in an earlier chapter) adds detail, and extra detail raises the chance of exposure. Masking still applies, but derived values are not covered. Review a debug log before sharing it outside your team, and delete runs that exposed something real. If a real secret appears in a log, treat it as leaked and rotate it.
A diagnosis flow
The diagram shows how to separate these failures from others.
Worked example
A workflow posts a comment on each pull request. It fails with this log:
Run gh pr comment "$PR_NUMBER" --body "Checks finished"
GraphQL: Resource not accessible by integration (addComment)
Error: Process completed with exit code 1.The final line is the follow-on error. The first real error is the line above it. The "Set up job" step shows Contents: read and Metadata: read only. The workflow has no permissions block, and the repository default is read-only.
The fix is to add pull-requests: write to the job, as in the first example in this chapter. Then re-run the failed job with gh run rerun <run-id> --failed and confirm the comment appears. If the PR came from a fork, the comment still fails, and the workflow needs the redesign described above.
Common mistakes
- Granting
write-allto make an error go away. This hides the missing scope and gives every step more power than it needs. - Adding one scope and losing
contents: read, which then breaks checkout in a private repository. - Printing a secret to check it. Test for emptiness or length instead.
- Assuming a masked
***means the secret was correct. Masking says nothing about validity. - Putting
permissionsat the workflow level when only one job needs write access.
Exercise
A job has permissions: issues: write at the job level and the workflow also has permissions: contents: read at the top. The checkout step in that job fails with a "not found" error on a private repository. Why, and what is the fix?
Show answer
A job-level permissions block replaces the workflow-level block instead of merging with it. The job's token has issues: write and every other scope set to none, including contents. Checkout needs contents: read. The fix is to add contents: read to the job's own permissions block.
Exercise
A deploy step prints "401 Unauthorized". The workflow uses ${{ secrets.DEPLOY_KEY }}, and the secret exists in the repository's production environment. The job does not set environment. What is the most likely cause, and how can you confirm it without printing the secret?
Show answer
Environment secrets are available only to jobs that declare that environment, so the expression evaluates to an empty string. Add environment: production to the job. To confirm, pass the secret through env and test it with [ -z "$DEPLOY_KEY" ], emitting an ::error:: annotation if it is empty.