Skip to content

feat: repair mismatch primary srp backup - #11766

Open
lwin-kyaw wants to merge 9 commits into
feat/seedless-repair-missing-primary-srp-backupfrom
feat/seedless-repair-mismatch-primary-srp-backup
Open

lwin-kyaw wants to merge 9 commits into
feat/seedless-repair-missing-primary-srp-backupfrom
feat/seedless-repair-mismatch-primary-srp-backup

Conversation

@lwin-kyaw

@lwin-kyaw lwin-kyaw commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explanation

#11748 writes a missing primary SRP and only reports a mismatch. A mismatch leaves the wrong mnemonic as the remote primary, so a restore on another device rehydrates the wrong wallet. That PR also keeps a v1 backup on the legacy schema.

This PR repairs both a missing and a mismatched primary SRP in PrimarySrpBackupRepairService. The service is still internal to the controller. One run plans from a single remote fetch and a single local export, emits the analytics event, then writes in two steps:

  1. Retag every secret except the remote copy of the local primary SRP. A primary candidate becomes ImportedSrp, which demotes the old primary. An untagged private key becomes ImportedPrivateKey. Items that are already imported are left alone. An update stores the item as v2 and does not change the encrypted data.
  2. Promote that remote copy to PrimarySrp, or add the local primary SRP as a new v2 PrimarySrp item when there is no copy.

The two steps cannot be one request. The metadata service rejects a new PrimarySrp while another PrimarySrp row is stored, and that check ignores a demotion in the same batch. See metadata-service src/routes/encAccountData.ts.

submitPassword does not await the repair, so a fast second unlock can start another run. Runs for the same user share one in-memory pending promise, so the second call does not fetch or write again. A different user gets a new run, and the first run aborts if the identity changed. No controller state is added.

A failed retag writes nothing. A failed primary write leaves the backup with no primary SRP, and the next unlock repairs that as a missing primary. There is no public API change.

The behaviour is described in docs/seedless-onboarding-remediation-implementation-design.md.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Changes which mnemonic is labeled PrimarySrp in remote backups (wallet restore), though writes are best-effort, identity-guarded, and heavily tested with no public API surface change.

Overview
Extends post-unlock primary SRP backup remediation so it fixes mismatches as well as missing primaries, not just telemetry for mismatches. PrimarySrpBackupRepair is replaced by PrimarySrpBackupRepairService, wired from submitPassword and identifyIncompleteMetadataBackup.

When the remote primary does not match the local one, repair runs in two metadata steps: retag every other secret as imported (demotes the wrong primary and upgrades legacy items to v2 without re-encrypting), then promote the remote copy of the local SRP to PrimarySrp or add it as a new v2 item. Concurrent runs for the same user share one in-flight promise; identity changes abort the write.

Planning moves from exported planPrimarySrpRepair into the service; changelog and a new remediation design doc describe the flow and edge cases.

Reviewed by Cursor Bugbot for commit 54d4bc7. Bugbot is set up for automated code reviews on this repo. Configure here.

@lwin-kyaw
lwin-kyaw added this pull request to stack #11767 November 25, 2026 18:29
@lwin-kyaw
lwin-kyaw requested a review from a team as a code owner November 25, 2026 18:29
@lwin-kyaw
lwin-kyaw deployed to default-branch November 25, 2026 18:29 — with GitHub Actions Active
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

🛑 Files requiring codeowner review

To merge this PR, you'll need to get approvals from the following owners.

@MetaMask/web3auth (14 files)
  • packages/seedless-onboarding-controller/CHANGELOG.md
  • packages/seedless-onboarding-controller/docs/seedless-onboarding-remediation-implementation-design.md
  • packages/seedless-onboarding-controller/src/backup-repair/PrimarySrpBackupRepair.test.ts
  • packages/seedless-onboarding-controller/src/backup-repair/PrimarySrpBackupRepair.ts
  • packages/seedless-onboarding-controller/src/backup-repair/PrimarySrpBackupRepairService.test.ts
  • packages/seedless-onboarding-controller/src/backup-repair/PrimarySrpBackupRepairService.ts
  • packages/seedless-onboarding-controller/src/SeedlessOnboardingController.test.ts
  • packages/seedless-onboarding-controller/src/SeedlessOnboardingController.ts
  • packages/seedless-onboarding-controller/src/types.ts
  • packages/seedless-onboarding-controller/src/utils/analytics.test.ts
  • packages/seedless-onboarding-controller/src/utils/analytics.ts
  • packages/seedless-onboarding-controller/src/utils/index.ts
  • packages/seedless-onboarding-controller/src/utils/secret-data-utils.test.ts
  • packages/seedless-onboarding-controller/src/utils/secret-data-utils.ts

ℹ️ Files co-owned by @MetaMask/core-platform

For convenience in maintaining this repo, this team co-owns the following files and thus may approve changes to them.

Note

You do not need to get approval from @MetaMask/core-platform to merge this PR.

(Expand to view list of files)
  • packages/seedless-onboarding-controller/CHANGELOG.md

The mismatch repair migrates every secret to v2, so the changelog no longer says a v1 backup is rewritten as v1.

Co-authored-by: Cursor <cursoragent@cursor.com>
@lwin-kyaw
lwin-kyaw requested a review from a team as a code owner November 25, 2026 18:33
@lwin-kyaw lwin-kyaw mentioned this pull request Nov 25, 2026
3 of 4 tasks
…zero-arg function

The metadata-service fake's fetch method takes the SDK parameters, so it cannot be the implementation of the service's parameterless fetch callback.

Co-authored-by: Cursor <cursoragent@cursor.com>
},
});
this.#primarySrpBackupRepair = new PrimarySrpBackupRepair({
this.#primarySrpBackupRepairService = new PrimarySrpBackupRepairService({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same vault-cache concern as #11748 (still on this stack)

#planRepair still calls fetchAllSecretData, which reads #cachedDecryptedVaultData as DeserializedVaultData outside withRepairContext (constructor options just below).

#pendingRun fixes same-client overlap, but not “cache cleared / locked mid-plan.” Can planning re-unlock (or share the write-path vault resolution) before fetch + export?


const toprfClient = this.#getToprfClient();

if (retag.length === 1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Partial failure window: demoted primary, no new primary

If step 1 (retag / demote) succeeds and step 2 (promote/add, ~L337–355) fails, the remote backup has no PrimarySrp until the next unlock repairs it as Missing.

That’s worse than mismatch for any cross-device restore in that window.

The design doc calls this out — thanks. A few asks:

  1. Immediate in-process retry of step 2 before giving up?
  2. Distinct analytics for retag_ok_primary_write_failed?
  3. Any client guidance if restore is attempted in that window?

this.#assertIsUnlocked();

await this.#primarySrpBackupRepair.run(password);
await this.#primarySrpBackupRepairService.run(password);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Public method name vs behavior

identifyIncompleteMetadataBackup now identifies and repairs (awaits run(), which writes). Clients may still treat this as telemetry-only.

Options:

  • Rename / add an explicit repairPrimarySrpBackup and keep identify read-only, or
  • Document this as an intentional breaking behavior change in the changelog and ping mobile/extension callers.

I’d lean toward a clear repair API so “identify” stays non-mutating.


const retag: PrimarySrpRepairWrites['retag'] = [];
for (const secret of secretMetadata) {
if (secret === localCopy || !secret.itemId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rows without itemId are skipped on retag

if (secret === localCopy || !secret.itemId) {
  continue;
}

If the current remote primary (or another primary candidate) has no itemId, we never demote it, then step 2 add (~L350) can fail forever with “Only one PRIMARY_SRP.”

Can we log/count these, fail the plan explicitly, or otherwise surface “unrepairable without itemId” instead of silently no-opping the demotion?

await this.#withRepairContext(
async ({ toprfEncryptionKey, toprfAuthKeyPair }) => {
// The identity may have changed since the repair was planned.
assertIdentityUnchanged(this.#getIdentity(), expectedIdentity);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Under the write lock we only assert identity, not that the plan is still valid

Between #planRepair and #repairPrimarySrpBackup, another client can change metadata. promote especially can target a stale itemId.

Suggestion: under withRepairContext, cheap re-check (or re-plan) before retag/promote — e.g. confirm mismatch/missing still holds and primary.itemId still exists when promoting.

…eat/seedless-repair-mismatch-primary-srp-backup

Keep the repair service test in place of the removed repair test, and await the vault callback so the return-await lint stays fixed.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1b17f33. Configure here.

itemId: secret.itemId,
dataType: EncAccountDataType.ImportedPrivateKey,
});
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Repair retags password-backup rows

Medium Severity

#getWrites retags every primary candidate and every untagged non-candidate, including PW_BACKUP and unknown-type rows. getDataTypeMigrationUpdates skips those items. A PW_BACKUP row tagged PrimarySrp sorts first, so a healthy backup is planned as a mismatch and those rows are rewritten as imported secrets.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 1b17f33. Configure here.

lwin-kyaw and others added 2 commits November 27, 2026 16:39
…epair run

Co-authored-by: Cursor <cursoragent@cursor.com>
…eat/seedless-repair-mismatch-primary-srp-backup

This branch was successfully deployed

1 active (outdated) deployment
default-branch — 6f02efd1 Deployed Nov 25, 2026 by lwin-kyaw via Determine whether this PR is a release PR #7639
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants