Skip to content

Merge the old secret into an existing record that has none - #1

Merged
mgilardi merged 2 commits into
mainfrom
co/move-keeps-old-secret
Nov 16, 2026
Merged

mgilardi merged 2 commits into
mainfrom
co/move-keeps-old-secret

Conversation

@mgilardi

Copy link
Copy Markdown
Contributor

Captive Origins task K5009. Follows the 2026-11-15 Security Analyst read of the first site, and the contribution rules (D5001).

Problem. moveLegacyState counted an account with an mfa-state record as already moved and removed its old twoStep group, even when the record had no secret. A reset (email-code block) or a started setup can make such a record between deploy and move. The old secret was lost and two-step turned off.

Fix.

  • Record with no secret (null or empty) and an old secret: the old fields fill what the record leaves empty (secret, enabled date, recovery codes, last step). The record's own fields stay (email-code block, reset date). The old group is removed only after the write succeeds, otherwise the move throws.
  • Record that already has a secret: kept. A different old secret puts the account id in differentSecret; the old group is still removed, as before, so the account is not locked out.
  • No record: moved as before.
  • The result gains merged and differentSecret.

Tested. 3 new tests, 49 pass, in-memory MongoDB: null and empty secret records merged (fields kept, old group gone, two-step on, sign-in works, rerun no-op); same and different secret kept and reported; no record moved as before. Leak check passes locally.

Not tested. A real site database, and a merge write losing a race. CI not checked yet. No version bump or release.

README move section, CHANGELOG (Unreleased) and decisions updated.

🤖 Generated with Claude Code

moveLegacyState treated any account with an mfa-state record as already
moved and removed the old group, even when the record had no secret. A reset
or a started setup can create such a record between deploy and move, so the
old secret was lost and two-step silently turned off.

Now the old fields fill what the record leaves empty (secret, enabled date,
recovery codes), the record's own fields stay, and the old group is removed
only after the merge is written. A record with its own secret is kept, and a
different old secret is listed in differentSecret. The result gains merged
and differentSecret.

Tested: 3 new tests, 49 pass, against an in-memory MongoDB. Not tested: a
real site database, or a merge write losing a race.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mgilardi

Copy link
Copy Markdown
Contributor Author

Review from EXTANT APE (same-day, per the contribution rules). Tests pass locally (49/49), and CI and the leak check are green. One change is needed before merge, and I agree with the differentSecret call.

Change needed: the merge can bring back a factor that was turned off or reset on purpose.

A record with no secret isn't only an unconfirmed setup. turnOff (src/api.ts) and the maintainer reset (src/api.ts, the cleared state with enrollUntil and resetAt) both write a record whose secret is null and whose recovery is empty. If an account still carries the old twoStep field when either of those happens, moveLegacyState now fills the empty fields from the old field. That puts the old secret and the old recovery codes back. After a reset made because a phone was lost or a code leaked, that restores the very factor the reset was meant to revoke.

Suggested rule, failing closed:

  • Merge only when the record shows no deliberate clear: no resetAt, no enrollUntil, and a pending setup, or a record that only carries emailBlockedUntil from a blocked email-code flow, if you can show that's the only other way such a record arises.
  • Otherwise don't merge. Leave the old field in place and name the account in a new needsReview result. hasUnmovedState keeps that account at the two-step step until the site resolves it, which is the safe outcome.
  • Never copy recovery from the old field into a record that already exists.
  • Tests: a turn-off record plus an old field, and a maintainer-reset record plus an old field. Neither gets the old secret, both are listed in needsReview, and the old field stays.

differentSecret: keep it as written. The record's secret is the newer factor and the one sign-in reads. The old secret stays recoverable from the backup the move requires. Naming the account is the right amount of noise.

Small: the CHANGELOG Tested line should say this ran against fixtures only, not a copy of the site's data, if that's the case.

A secret-less state record also comes from turnOff and from a maintainer
reset, so merging the old field into one would restore a factor that was
revoked on purpose. moveLegacyState now merges only when the record shows no
clear: turnOff writes a new turnedOffAt date, and resetAt, enrollUntil, an
enabled date, recovery codes, a last step or a verified date also count. A
started setup and an email-code block still merge. Otherwise the old field
stays and the account is listed in the new needsReview result. Recovery codes
are never copied into an existing record.

Tested: 50 tests pass on in-memory MongoDB with made-up accounts: turnOff and
reset records not merged and listed (also on rerun), plain case merged
without recovery codes, differentSecret as before. Not tested: a real site
database, a merge write losing a race, or a mutation check of the guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mgilardi
mgilardi merged commit f8b8d5e into main Nov 16, 2026
3 checks passed
@mgilardi
mgilardi deleted the co/move-keeps-old-secret branch November 16, 2026 04:17
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.

1 participant