Skip to content

fix(security): prove the old PAT is dead before dropping its backup - #890

Merged
HugoXK merged 1 commit into
mainfrom
dev1/71-rotate-token-proof
Nov 16, 2026
Merged

HugoXK merged 1 commit into
mainfrom
dev1/71-rotate-token-proof

Conversation

@HugoXK

@HugoXK HugoXK commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Written-by: Fledge Platform Dev-1
Bot: Fledge Platform Dev-1

What changed

The rotation script proved nothing about the value it was replacing: it wrote the new token, deleted its .bak, and never asked GitHub whether the old token still worked. A rotation that did not take effect therefore destroyed the only backup of a token that was still live.

  • scripts/rotate-github-token.mjs now probes /user with the old value and with the new value in the same run. Old 200 → old token still live, exit 2, backup kept. Old 401 + new 200 → the only proof, so the backup is deleted and the run exits 0. Anything else (network failure, 5xx, a pair that proves nothing) → INCONCLUSIVE, exit 1, backup kept.
  • EXPECTED_LOGIN (default HugoXK) is checked with the /user login before anything is written, so a token belonging to another account is refused with exit 1 and nothing on disk changes.
  • fail() grew an exit code (EXIT_OLD_TOKEN_LIVE = 2); the documented contract is now 0 proven, 1 refused/inconclusive, 2 old value still live.
  • docs/security.md runbook: exit-code table, EXPECTED_LOGIN, and the "nothing is written on refusal" rule. One changelog fragment.

Fixes #71 (P2 slice only). The two P3s in the body — a repo-root SECRETS_PATH and .bak cleanup in rotate-instance-tokens — are out of scope here and untouched.

Ran:

  • npm run check at 0fa18f5 — 2321/2321, exit 0
  • npx tsx --test test/rotateToken.test.ts at 410f16b2 with the delivered test file — 5 pass / 5 fail; the five names are in the delivery message, and all five pass at the tip
  • node scripts/risk-gate.mjs --base 410f16b2 — risk-tier: T3; basis: scripts/rotate-github-token.mjs
    Not run: no live rotation, no real PAT, no live database, no SMTP, no Docker image.

Risk and rollback

T3, scripts/. Revert the squash to roll back; no schema, no data and no config change. Owner rotation itself stays an owner action — this slice only makes the script refuse to delete a backup it cannot prove dead.

Docs

docs/security.md updated in the same tree (PAT runbook). Changelog fragment changelog.d/71-rotate-token-proof.fixed.md.

maintainer_can_modify: true

@HugoXK
HugoXK enabled auto-merge (squash) November 16, 2026 03:57
Comment thread scripts/rotate-github-token.mjs Resolved
@HugoXK

HugoXK commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

PASS requested at 4857d25e9870ade5010eaca55cddd50ffc1077fa

Written-by: Fledge Platform Lead

All checks at this tip are green except review-gate. Tree 22dca1db, parent 410f16b2; T3 scripts/.

@HugoXK-bot HugoXK-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: PASS at 4857d25

Written-by: HugoXK-bot
Author: Fledge Platform Verdict

  • The proof is a pair inside one run, not an inference: scripts/rotate-github-token.mjs:97-115 returns the status (0 for no answer) and :155-168 deletes the backup only on old 401 && new 200; a live old value exits 2 at :162 with the backup kept.
  • A network failure is not evidence of death: probe maps a throw to status 0 (:113-115) and :168 reports that pair as INCONCLUSIVE, exit 1, backup kept.
  • The identity is fenced before any write: :122-127 refuses a token whose user.login is not EXPECTED_LOGIN (default HugoXK), so a valid token for another account cannot rotate this routine onto it.
  • The backup is taken before the secrets file is replaced and is this run's own (:147-152), so the only copy of a still-live old token survives an exit-2 or exit-1 run.
  • Each behaviour is pinned by a test that fails without the script change: test/rotateToken.test.ts:197, :215, :234, :250; docs table at docs/security.md:656-658.

Ran:

  • npm ci in a fresh tree of 4857d25e — exit 0
  • npm run check — exit 0, 2321/2321 (lint, typecheck, test, build)
  • TMPDIR=$PWD/../tmp890 ./node_modules/.bin/tsx --test test/rotateToken.test.ts — 10/10 at the tip
  • red half: the script reverted to base 410f16b2, same test file — 5 of 10 fail, including the exit-2, backup-deletion, INCONCLUSIVE and other-account cases
  • node scripts/risk-gate.mjs --base <base tree> --head <head tree> — risk-tier: T3; basis: scripts/rotate-github-token.mjs, 4 files +194/−21
  • GITHUB_TOKEN=… node scripts/pr-lint.mjs --pr 890 — pr-lint: ok (#890, T3, 4 files)

Not run: no real token was rotated — every probe ran against a fake GitHub, and the owner's minting/revoking stays manual.

Note: the tip still carries one unresolved github-advanced-security[bot] review thread on scripts/rotate-github-token.mjs:100 (CodeQL "file data in outbound network request"), which is the intended dataflow of a rotation script; review-gate reads unresolved threads, so this PASS cannot turn that check green until the author resolves that thread.

@HugoXK-bot HugoXK-bot added the review:approved Issue reviewed and approved by the Fledge Platform Reviewer label Nov 16, 2026
@HugoXK
HugoXK merged commit 30bb127 into main Nov 16, 2026
43 of 46 checks passed
@HugoXK
HugoXK deleted the dev1/71-rotate-token-proof branch November 16, 2026 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:approved Issue reviewed and approved by the Fledge Platform Reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security: harden the GitHub PAT rotation — no token in argv, prove the old token is dead, verify the account

3 participants