Skip to content

fix(deploy): remove the restore block column key on the refusal path - #1211

Open
HugoXK wants to merge 2 commits into
mainfrom
dev3/1192-restore-key-on-disk
Open

HugoXK wants to merge 2 commits into
mainfrom
dev3/1192-restore-key-on-disk

Conversation

@HugoXK

@HugoXK HugoXK commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Written-by: HugoXK
Author: Fledge Platform Dev-3
Commit identity: commit.author and commit.committer are both Fledge Platform Dev-3 <dev3@fledge.invalid>, read back from GET /repos/HugoXK/fledge/commits/e6b205ad500ee8fdf2fa2c9becc2ceae6559bcee after the push.

Fixes #1192

Current vs expected

Current. A restore-for-real run that the drill refuses leaves /var/tmp/fledge-restore/column.key on disk: the block's only rm -f sits after fi, and exit 1 in the else branch returns before it. The block then tells the operator to sudo rm -rf the directory only "after /healthz answers and one page loads", which a refused restore never reaches.

Expected. The key file is gone on every exit of the block, and a refused drill still stops the block before anything is copied in.

What changed

  • docs/deploy.md: the else branch now runs sudo rm -f /var/tmp/fledge-restore/column.key before exit 1. exit 1 stays in the same shell as the if, so the block still stops; the success path keeps its own removal and its comment.
  • test/columnCheck.test.ts: the guard reads each documented key fence for structure instead of accepting a rm -f anywhere in it. A block passes when the removal sits inside the else branch that stops it, or when a trap removes the file and the block propagates the subshell status before it copies anything in. A subshell trap whose exit 1 ends only the subshell is rejected with that reason.
  • changelog.d/1192.security.md: one fragment.

Shape chosen, and the shape rejected

The card allows two shapes. I took the first — remove the key before exit 1, keeping exit 1 in the block's own shell — and the ratchet still accepts the second (trap plus an explicit $? -ne 0 stop) so a later rewrite can use it. The rejections are the point:

  • A plain ( trap … EXIT; … ) with the drill inside and no status check is rejected: the trap removes the key, but exit 1 inside the subshell no longer stops the block, so sudo cp … restored.db "$DB_PATH" runs after a refusal — a key leak turned into copying an unverified database over the live one.
  • A rm -f only on the success path is rejected, which is the defect itself.

Red first

At the base 044c7515 with the committed test file copied in (test/columnCheck.test.ts sha256 95ababcd780c289f25f3d9e8d056013b681d6d553bd71739e0fd6effae291ce4), npx tsx --test test/columnCheck.test.ts gives 13 tests, 12 pass, 1 fail:

not ok 7 - every runbook block that hands the drill a column key removes it on the refusal path
  the block that passes /var/tmp/fledge-restore/column.key must remove it on every exit, got:
  the key is removed only outside the failure branch, so a refused drill leaves it on disk

The same file at this tip is 13/13.

The ratchet is a text check over the two documented fences, not shell static analysis, as the card asks. It also pins the pre-fix fence (REFUSAL_LEAK_BLOCK) and asserts that text fails, so a later rewrite cannot quietly restore the success-path-only shape; and it asserts the rejected trap shape fails while the accepted trap-plus-status shape passes.

Uncertainty and residual risk

  • A signal that kills the drill (SIGKILL, a closed terminal) still skips both removals: this fence has no trap. This slice fixes the refusal path the card names; trap-based cleanup needs the propagated-status shape to stay safe and is a different change.
  • The ratchet reads else and fi as whole lines. A one-line if …; then …; else …; fi fence would be reported as a failure rather than silently accepted, so the failure mode is a false alarm, not a false green.
  • The key file is one file in a root-0700 directory. The refusal path still leaves the drill's scratch files there by design — they are the evidence of the refusal — and the block already tells the operator to remove the directory after a successful restore.
  • Not touched: #1187's drill block, the /admin drill row, the status file, and src/jobs/service.ts:3807 (the card's recorded residual, src/jobs/**, a different slice).

Ran:

  • npx tsx --test test/columnCheck.test.ts at the base 044c7515 with the committed test file copied in — 13 tests, 12 pass, 1 fail (the refusal-path guard).
  • npx tsx --test test/columnCheck.test.ts at this tip — 13/13.
  • npm run check at this tip — exit 0: lint 11 warning(s) in 8 entries match the baseline, tsc --noEmit clean, 3294 tests / 3294 pass / 0 fail / 0 skipped, build clean.
  • node scripts/changelog.mjs check — changelog: 4 fragment(s) ok; node scripts/version.mjs check — version: 0.4.5 ok.
  • node scripts/risk-gate.mjs --base 044c7515a6111560bf96cdd20e8c724535cefc0a at this tip — output below.

Not run:

  • No shell execution of the fence itself: the guard is the text ratchet the card specifies, so the exit 1 behaviour is argued from the block's own text rather than from running it.
  • No drill against a real backup copy, no operator runbook rehearsal, no deployment.

Risk

Fledge risk tier
base: 044c7515a6111560bf96cdd20e8c724535cefc0a
head: HEAD
changed files: 3

T1: test/columnCheck.test.ts
T0: changelog.d/1192.security.md, docs/deploy.md
limits: 25 files / 500 lines (advisory)

risk-tier: T1; basis: test/columnCheck.test.ts

risk-tier: T1 is computed from the paths the gate reads (docs/** T0, test/** T1), which is also how the card describes this slice; no src/** or deploy/** file changes. The gate decides the requirement, not this paragraph — I am asking for a PASS at the tip anyway, and the Lead routes the Gate Security read.

Docs and changelog

The runbook is the code here, so docs/deploy.md changes in the same commit as the guard that reads it. Changelog: changelog.d/1192.security.md.

@HugoXK
HugoXK requested a review from HugoXK-bot November 21, 2026 18:16
@HugoXK

HugoXK commented Nov 21, 2026

Copy link
Copy Markdown
Owner Author

Written-by: Fledge Platform Dev-3

Tip e6b205ad500ee8fdf2fa2c9becc2ceae6559bcee: all 21 required checks are green, ci-ok 136939171394.

review-gate reads risk-tier: T1 for this diff (test/** T1, docs/** and the changelog T0, no src/** or deploy/**), so the gate itself is green at this tip and a PASS is not required by it; I have still asked for one at the tip, and the Security read the Lead routes sits on top of that.

Red first on the same commit: test/columnCheck.test.ts at 044c7515 with the committed file copied in gives 13 tests, 12 pass, 1 fail on the restore fence; 13/13 at this tip, and npm run check is 3294/3294.

@HugoXK

HugoXK commented Nov 21, 2026

Copy link
Copy Markdown
Owner Author

Security: PASS at e6b205a

  • docs/deploy.md:1541-1546: the else branch removes the key at :1544 before exit 1 at :1545, and that exit 1 stays in the block's own shell (no subshell wraps the if), so a refused drill stops before sudo cp at :1552.
  • docs/deploy.md:1548,1551-1552: the success path still removes the key before the wal/shm delete and the copy; I ran the fence text with the scratch dir remapped and sudo/npm/systemctl/curl stubbed — refusal: exit 1, key gone, only restored.db.failed, no copy; success: key gone at the moment cp runs.
  • docs/deploy.md:1544: the new removal is a hardcoded literal inside the root-0700 scratch dir and the same sudo rm -f shape as the pre-existing cleanup at :1548 — no variable, glob, or new privilege.
  • test/columnCheck.test.ts:318-337: the ratchet is materially tighter — it rejects the pinned pre-fix fence (removal only outside the failure branch) and a subshell trap without $? -ne 0, where the old check accepted rm -f anywhere in the block.
  • test/columnCheck.test.ts:358-375: both keyed fences are still read (count 2) and every --secret-key-file path must pass the structural check; no assertion was removed.
  • Delta is three files with no src/**, deploy/** or workflow surface, and the suite is green at this tip (ci-ok 136939171394).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security(deploy): the restore-for-real block leaves the column key on disk when the drill refuses

1 participant