Skip to content

test-determinism: keep the issue token out of every process that runs project code - #2797

Merged
bbondy merged 2 commits into
brave:mainfrom
netzenbot:fix-measure-job-token-exposure
Oct 20, 2026
Merged

bbondy merged 2 commits into
brave:mainfrom
netzenbot:fix-measure-job-token-exposure

Conversation

@netzenbot

Copy link
Copy Markdown
Collaborator

Closes #2789

User impact: none -- CI hardening; the weekly flake report and the filed issues read the same.

The problem

The weekly Test determinism job holds a token that can create, edit, close and label issues and pull requests. It passes that token to contrib/measure-flakes.py, which gives its whole environment to cargo, so every build script, proc macro and test of every crate in Cargo.lock can read it. The checkout also leaves the job token in the working tree. The security check that exists to catch a privileged job running third-party code reads only npm, pnpm and yarn, so it reports nothing here.

Reproduce

  1. In a checkout of upstream/main, run sed -n 137,146p .github/workflows/test-determinism.yml and sed -n 477,481p contrib/measure-flakes.py: the Measure step sets GH_TOKEN, and cargo_env copies os.environ.
  2. Run GH_TOKEN=x python3 -c "import runpy;m=runpy.run_path('contrib/measure-flakes.py');print('GH_TOKEN' in m['cargo_env']())": prints True.
  3. Run the security audit's check_privileged_job_runs_only_its_own_code() against that workflow: it returns [].

Expected: no process cargo starts sees the token, and the check names the step. After this change, step 2 prints False and the check names measure-flakes.py on the old workflow (selftest fixture) and returns nothing on the new one. The tests are test_privileged_job_runs_project_code in agents/skills/security-audit/selftest.py and the measure-flakes.py --selftest check.

The fix

measure now holds only contents: read, checks out with persist-credentials: false, and runs measure-flakes.py --save, so no token is in it at all. A new file job (the only one holding issues: write, ubuntu, no toolchain) downloads the saved measurement for the two filing legs and runs measure-flakes.py --load, which runs no cargo and so no project code. The machine description is saved with the measurement so issues still name the host that ran the suite. cargo_env also drops GH_TOKEN and GITHUB_TOKEN. The security check now reports cargo build|test|clippy|run|nextest|install|check|bench|doc and contrib/measure-flakes.py (not in --load mode) in a step that holds a secret, a job-level secret, id-token: write, or a write grant with a checkout that persists credentials.

Test plan

  • contrib/measure-flakes.py --selftest — passed
  • python3 agents/skills/security-audit/selftest.py — passed
  • BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 make check-affected — scripts, spec, security, locales, versions, narration, reviewdog, docs, npm, deps passed. One Rust test, a_replaced_submodule_a_smudged_entry_and_a_socket_read_as_git_reads_them, failed here only because this host's /tmp and the worktree are on different filesystems (CrossesDevices); it passes with TMPDIR on the worktree's filesystem. Untouched by this diff.
  • The two workflow legs (measure, file) have not run; they run only on the schedule or a dispatch. Docker gates skipped: host-only.
  • CI passes cleanly
Test evidence
Behaviour Test Fault it rejects Evidence
cargo never inherits the token --selftest "no token reaches the environment cargo and the tests inherit" cargo_env returning dict(os.environ) demonstrated: restored that line, the check failed, restored
a cargo step holding the token is reported test_privileged_job_runs_project_code check reading only npm/pnpm/yarn demonstrated: the pre-change workflow yields a finding naming measure-flakes.py at line 144; the old check returned []
split design and contents: read builds are clean same test check over-reporting, which would fail CI on the fix reasoned plus the real-tree assertion that the tree reports nothing
--load files without cargo, naming the measuring host hand run with --dry-run issue body crediting the filing host's CPUs reasoned only

… project code

The measure job held issues: write and passed GH_TOKEN to measure-flakes.py,
which gave its whole environment to cargo, every build script, proc macro and
test, and left the job token in the checkout. Measuring now holds no grant
beyond contents: read and saves what it found with --save; a separate file job
holds issues: write, loads that file with --load and runs only gh. cargo_env
also drops GH_TOKEN and GITHUB_TOKEN, and every checkout here sets
persist-credentials: false.

check_privileged_job_runs_only_its_own_code now reads cargo commands and
measure-flakes.py beside a token, a job-level secret or a persisted write
credential.

Closes brave#2789
Comment thread contrib/measure-flakes.py Resolved
@netzenbot

Copy link
Copy Markdown
Collaborator Author

Fixed: the selftest now drives --save then --load through main() (no cargo, dry-run filing) and checks the restored threads, the saved machine, and that the filing step reports only its own section. Reverting the load of either machine or threads makes it fail.

@bbondy
bbondy merged commit ce6fb9d into brave:main Oct 20, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants