MSBA-308 · Lesson 7 of 12 · 21 min
Domain review: securing automation
▶ Video coming soon A video version of this lesson is on the way.
The last domain review fell on the second Monday, and Priya opened it with the March 2025 incident everyone in the Actions world remembers: a popular third-party action had its version tags moved to malicious code, and workflows that used it by tag printed their secrets into logs. "Every bullet in this domain is a defense against something like that," she said. The fifth domain, Secure and optimize automation, carries 10 to 15 of the exam weight, but as lesson 1 noted, its ideas appear across the other four. It has two subgroups: security best practices, and workflow performance and cost. Managing and Securing GitHub Actions in the Enterprise taught both.
Environment protections and approval gates
An environment is a named deployment target, such as staging or production, with protection rules. Required reviewers must approve before a job that references the environment can start. A wait timer delays it. Deployment branch and tag rules limit which refs can deploy. Custom protection rules can call an external system. Environment secrets and variables are released only after the rules pass. The plan matters: on GitHub Team, required reviewers and wait timers apply to public repositories; private repositories need GitHub Enterprise. A scenario that requires a human approval before production on a private repository in a Team organization therefore needs either a plan change or another gate.
Trustworthy actions and pinning
The outline asks you to identify trustworthy Marketplace actions, enforce usage policies with allow and deny lists, and pin third-party actions to full commit SHAs. Signs of a trustworthy action include a verified creator badge, an active repository with recent releases and responses to issues, a clear permissions footprint, pinned dependencies of its own, and immutable releases. None of these is proof. Pinning is what makes your choice stick: a full commit SHA always refers to the same code, while a tag or branch can be moved. The usual form adds the version as a comment so readers and Dependabot know what the SHA means. Dependabot version updates for the github-actions ecosystem keep pins current, and since October 2026 actions lockfiles can pin the actions that your actions use, too.
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
# pinned to a full commit SHA, version in a comment (SHAs illustrative)
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
with:
node-version: 24
- run: npm ci && npm testScript injection
Script injection happens when untrusted text, such as a pull request title, branch name or issue comment, is placed directly into a run script with an expression. The expression is expanded before the shell runs, so text crafted to look like shell syntax becomes part of the command. The outline lists the defenses: validate and sanitize inputs, use least-privilege permissions, avoid putting untrusted data straight into run, quote correctly in the shell, and prefer vetted actions to inline scripts.
# Risky: the title is pasted into the script before the shell runs
# run: echo "Checking ${{ github.event.pull_request.title }}"
# Safer: pass it as an environment variable and quote it in the shell
- name: Check title
env:
TITLE: ${{ github.event.pull_request.title }}
run: |
echo "Checking: $TITLE"The environment variable pattern works because the value reaches the shell as data in a variable, not as text inside the script. The same rule applies inside actions: inputs should be passed through env, not interpolated into run lines. Triggers that run with elevated access on untrusted code, especially pull_request_target combined with checking out the fork's head, are the other classic risk. From December 2026, GitHub blocks pull_request_target by default in public repositories.
GITHUB_TOKEN versus personal access tokens
The GITHUB_TOKEN is created for each job, scoped to the repository, and expires when the job finishes or after 24 hours at most. Its permissions come from the repository or organization default, which GitHub recommends setting to read-only, and can be narrowed or widened with a permissions key at the workflow or job level. Any permission not listed in a permissions key is set to none. A personal access token, by contrast, belongs to a person, lasts until it expires or is revoked, and often has far wider reach. The outline asks you to contrast the two and restrict write scopes. In exam terms: use GITHUB_TOKEN with the narrowest permissions; use a GitHub App installation token when you need access across repositories; use a personal token only when nothing else works. Events created with GITHUB_TOKEN, apart from workflow_dispatch and repository_dispatch, do not trigger new workflow runs, which prevents accidental loops.
| GITHUB_TOKEN | Personal access token | GitHub App token | |
|---|---|---|---|
| Owner | The workflow run | A user | An app installation |
| Lifetime | Until the job ends, 24 hours at most | Until expiry or revocation | About an hour, renewable |
| Scope | One repository, set by permissions | Whatever the user granted | Repositories the app is installed on |
| Triggers new runs | No, except dispatch events | Yes | Yes |
| Best use | Almost everything inside one repository | Last resort | Cross-repository automation |
OIDC for cloud access
OpenID Connect lets a job request a short-lived identity token from GitHub and exchange it with a cloud provider for temporary credentials, so no long-lived cloud key is stored as a secret. The job needs permissions: id-token: write. On the cloud side, a trust policy accepts tokens only when their claims match, for example the repository, branch or environment in the subject claim. A question asking how to remove a stored cloud access key from a deployment workflow points at OIDC. A question about a token being rejected after a workflow moved to a new environment points at the subject claim no longer matching the trust policy. Since mid-2026, new repositories get immutable subject claims based on IDs rather than names, so renaming a repository does not silently change who the cloud trusts.
Artifact attestations
The last security bullet asks you to generate and verify artifact attestations and provenance and to use them in deployment verification. An attestation is a signed statement, made in the workflow with GitHub's attestation action, that links an artifact's digest to the workflow, commit and repository that built it. The job needs id-token: write and attestations: write. Anyone can then verify the artifact with gh attestation verify before deploying or running it. Attestations from public repositories are signed through the public Sigstore instance; private repositories use GitHub's own signing instance and need GitHub Enterprise Cloud. Attestations alone meet SLSA Build Level 2; building in an isolated reusable workflow can reach Level 3.
Worked problem
Choosing the right control
A team stores a cloud access key as a repository secret, deploys from any branch, and uses a community action by @main to upload files. Which three changes would the outline recommend first?
Show solution
- Replace the stored key with OIDC, adding id-token: write and a trust policy limited to the repository and production environment.
- Deploy through a production environment with deployment branch rules and, where the plan allows, required reviewers.
- Pin the community action to a full commit SHA, check its trustworthiness, and add it to the allow list.
Answer: OIDC instead of the stored key, a protected production environment, and a pinned, reviewed action.
Optimizing performance and cost
The second subgroup is short: configure caching and artifact retention for efficiency, apply retention policies through the REST API, and recommend strategies for scaling and optimizing workflows. In practice the exam asks you to pick the change that saves the most time or minutes in a described situation.
| Waste | Fix |
|---|---|
| Dependencies downloaded on every run | Cache by a lock-file hash, or use a setup action's built-in caching |
| Workflows run for changes that do not affect them | paths and paths-ignore filters |
| Old runs keep building after a new push | concurrency with cancel-in-progress |
| A full matrix for every pull request | A smaller matrix on pull requests, the full grid nightly or on main |
| Large artifacts kept for months | Shorter retention-days, or retention policies set through the API |
| Slow builds on standard runners | Larger runners for the heavy job only |
| Jobs waiting on each other unnecessarily | Remove needs that are not real dependencies |
Pricing moved in 2026 as well: GitHub-hosted runner prices fell by up to 39 percent from January 1, 2026, and a planned per-minute platform charge for self-hosted runners was postponed. The exam does not test prices. It tests the habits above, and knowing that minutes on private repositories are billed while public repositories on standard hosted runners are free.
Worked problem
The cheapest fix
A docs-only change triggers a 40-minute test matrix of 6 jobs on every push. What single change saves the most?
Show solution
- The matrix runs even though no code changed.
- A paths-ignore filter for the docs folder on the test workflow stops it running for docs-only pushes.
- Caching would only shorten each run; the filter removes the run entirely.
Answer: Add paths-ignore for documentation to the test workflow's triggers.
Keeping dependencies current
Pinning actions to commit SHAs solves one problem and creates another: pins go stale. Dependabot can keep them current. A dependabot.yml entry with the package ecosystem github-actions makes Dependabot open pull requests when a pinned action has a new release, updating the SHA and the version comment together. Reviewers then read the change log before merging, which is the point at which a supply chain problem can be caught. Dependabot alerts also cover actions listed in the GitHub Advisory Database, so a vulnerable version in a workflow can raise an alert.
Code owners add a second line of defense. A CODEOWNERS entry for the .github/workflows folder means that any pull request changing a workflow needs review from the platform or security team, so a contributor cannot quietly widen permissions or add a new action. Combined with branch protection or a ruleset that requires code owner review, this keeps workflow changes visible.
Secrets in logs
GitHub masks the values of secrets in logs, but masking only works for the exact string. A secret that is transformed, for example encoded or split, is not recognized. When a workflow derives a sensitive value at runtime, such as a token returned by a script, it should register the value with the add-mask workflow command before printing anything that might include it. Questions about a token appearing in a log usually have this as the answer, along with rotating the exposed credential.
Fork pull requests and untrusted code
Many security questions describe a public repository receiving pull requests from forks. By default, workflows triggered by pull_request from a fork run with a read-only GITHUB_TOKEN and without access to secrets, and first-time contributors may need approval before their workflows run at all. These limits exist because the workflow file and code in the pull request come from someone outside the project. The pull_request_target event behaves differently: it runs in the context of the base repository, with its secrets and a token that can write, which makes it dangerous if the workflow checks out and runs the fork's code. The safe pattern is to keep untrusted code in a pull_request workflow and do anything that needs secrets in a separate workflow that only reads results, such as one triggered by workflow_run.
When an option in a question uses pull_request_target with a checkout of the pull request's head and then runs a build, it is almost always the vulnerable choice. GitHub has also announced tighter defaults for this event in 2026, which is another reason to read such options with suspicion.
Lena summarized the security domain on one card with five questions to ask of any workflow: who can change it, what code it runs, what its token can do, how it reaches the cloud and how its outputs can be verified. Code owners and rulesets answer the first; allowed actions and pinning answer the second; permissions answer the third; OIDC answers the fourth; attestations answer the fifth. Script injection cuts across them all, because untrusted text can turn a well-configured workflow into a tool for someone else.
Common mistakes
MistakePasting github.event text directly into a run script.
InsteadPass it through env and quote it in the shell.
MistakeGranting write-all to fix a permissions error.
InsteadAdd only the specific permission the job needs.
MistakeStoring a long-lived cloud key as a secret.
InsteadUse OIDC with a trust policy that checks claims.
MistakePinning to @v4 and calling it pinned.
InsteadOnly a full commit SHA is fixed.
MistakeForgetting attestations: write and id-token: write when generating attestations.
InsteadBoth permissions are needed for the signing step.
Lena's homework for the domain was to rebuild TrailCheck's deploy job from memory on a blank page: permissions, environment, OIDC, pinned actions, an attestation for the tile bundle and a verify step before upload. She got everything except the attestations permission, which went straight into her error log.
Summary and key terms
- Security and optimization is 10 to 15 of the exam, but its ideas appear in every domain.
- Environments add reviewers, wait timers and branch rules; on Team, reviewers apply to public repositories.
- Pin third-party actions to full commit SHAs, keep them updated, and restrict them by policy.
- Avoid script injection by passing untrusted data through env and quoting it.
- Use GITHUB_TOKEN with least privilege; it expires with the job and does not trigger new runs.
- Use OIDC with id-token: write instead of stored cloud keys.
- Generate attestations with id-token and attestations permissions and verify them before deploying.
- Save time and minutes with path filters, caching, concurrency, smaller matrices and shorter retention.
- Environment protection rule
- A requirement, such as reviewers or a wait timer, that must pass before a job using the environment runs.
- Script injection
- Untrusted text becoming part of a shell command because it was expanded into a script.
- Least privilege
- Granting only the permissions a job needs.
- OIDC
- OpenID Connect, used to exchange a short-lived GitHub token for temporary cloud credentials.
- Subject claim
- The part of an OIDC token that identifies the repository and context, checked by the cloud trust policy.
- Artifact attestation
- A signed statement linking an artifact to the workflow and commit that built it.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Which permission does a job need to request an OIDC token?
Show answer
id-token: write.
Why does the environment variable pattern prevent script injection?
Show answer
The value reaches the shell as data in a variable rather than as text inside the script.
Which command verifies an artifact's attestation?
Show answer
gh attestation verify.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.