Sub topic 17
Security practices and limits
This chapter assumes you can write a workflow that targets an environment, and that you have configured required reviewers and branch restrictions. It covers how to combine those controls, and where they stop protecting you.
What environment protections guarantee
A job that names an environment gets that environment's secrets only after every protection rule passes. That is the whole guarantee. The rules decide whether a job may start. They do not control what the job does once it is running.
Practices that work
- Restrict branches first. Without a branch or tag rule, anyone with write access can push a branch whose workflow targets
production. Reviewers would still be asked to approve, but the approval request itself is attacker-controlled. Limit deployments tomainor to release tags. - Protect the workflow files. A person who can change the workflow on an allowed branch can change what runs with the secrets. Use branch protection or rulesets that require pull request review, and add a CODEOWNERS entry for
.github/workflows/. - Prefer OIDC to stored secrets. A long-lived cloud key in an environment secret can be copied by any step that runs after approval. An OIDC token is short-lived. Restrict the cloud role to the subject claim for the environment, for example
repo:OWNER/REPO:environment:production. - Keep production secrets out of other scopes. Store them only in the production environment. If a repository secret has the same name as an environment secret, the environment value wins for jobs in that environment. A repository-level copy would still be readable by jobs that don't use the environment, so avoid creating one.
- Prevent self-review so the person who triggered the run cannot also approve it.
- Tell reviewers what to check. An approval applies to one specific commit and run. Reviewers should open the run and confirm the commit before they approve.
Limits to remember
| Limit | Consequence |
|---|---|
| Rules gate job start, not job behavior | After approval, any step can print, transmit, or misuse the secrets. Third-party actions run with the same access. |
| Administrators can bypass rules by default | An admin can deploy without review unless you clear "Allow administrators to bypass configured protection rules". |
| One approval is enough | With several reviewers listed, a single approval releases the deployment. |
| Masking is best effort | Log masking only matches exact secret values. Transformed values, such as base64-encoded ones, can leak. |
| Plan restrictions | Some rules are unavailable for private repositories on lower plans. |
Audit what happened
Review the deployment history for each environment and the repository audit log. They show who approved, who bypassed, and which commit was deployed. Do this regularly, because most of the limits above are not blocked by GitHub. You find them only by looking.
Exercise
A teammate with write access pushes a branch containing a workflow that targets production and runs curl with the deploy token. Reviewers are required, and no branch rule is set. Name two controls that reduce this risk.
Show answer
Add a deployment branch rule so only main (or release tags) can deploy to production. Protect .github/workflows/ with required pull request review or CODEOWNERS so workflow changes get reviewed before they reach an allowed branch. Using OIDC with a role limited to the production environment claim also reduces the damage a copied token can do.