Secrets for Every Fork — JavaScript Bug Hunt
Modelled on Travis CI's September 2021 security bulletin (CVE-2021-41077): for several days, the secure environment variables of public repositories —…
- Language: JavaScript
- Layer: Backend
- Difficulty: Easy
- Concepts: Security, Config
- Modelled on: Travis CI · CVE-2021-41077
- Visible tests: a push gets its secrets; a fork pull request gets no secrets
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-1 (priority: Critical (secret exposure))
- Ticket: BUG-FORKENV
- Reported by: security
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- a fork pull request gets no secrets — secrets must not reach a fork: expected {"CI":"true","TRAVIS_SECURE_ENV_VARS":"false"} but got {"CI":"true","DEPLOY_KEY":"k1","NPM_TOKEN":"t1","TRAVIS_SECURE_ENV_VARS":"true"}
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Modelled on Travis CI's September 2021 security bulletin (CVE-2021-41077): for several days, the secure environment variables of public repositories — signing keys, access tokens — were made available to builds of pull requests opened from forks. Anyone able to open a pull request could have a build read them.
env.js builds a job's environment. It is meant to hold secrets back from fork pull requests, but its check reads a field the build payload does not carry.
Fix buildEnv so secrets reach only pushes and pull requests opened from the same repository.
Bug report
BUG-FORKENV · Priority: Critical (secret exposure) · Reported by: security
buildEnv(build, settings) returns the job environment as an object:
- every settings.plain variable, in order
- then settings.secure variables ONLY when the build is trusted: build.event === "push", or build.event === "pull_request" with a headRepo equal to its baseRepo. A pull request whose headRepo differs (a fork) or is missing is untrusted.
- then TRAVIS_SECURE_ENV_VARS: "true" when secrets were added, else "false"
Observed: pull requests from forks receive DEPLOY_KEY and NPM_TOKEN.
Logs
[worker] job 88213 pull_request base=acme/site head=stranger/site
[worker] env: CI, DEPLOY_KEY, NPM_TOKEN, TRAVIS_SECURE_ENV_VARS=trueThe code as shipped
src/ci/env.js (editable)
// Builds the environment a CI job runs with.
exports.buildEnv = function (build, settings) {
var env = {};
Object.keys(settings.plain).forEach(function (k) { env[k] = settings.plain[k]; });
var trusted = build.event === "push" || !build.fork;
if (trusted) {
Object.keys(settings.secure).forEach(function (k) { env[k] = settings.secure[k]; });
}
env.TRAVIS_SECURE_ENV_VARS = trusted ? "true" : "false";
return env;
};
Read-only context: src/ci/BUILD.js.
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.