Skip to content

fix: repair missing Primary SRP - #11748

Open
lwin-kyaw wants to merge 8 commits into
mainfrom
feat/seedless-repair-missing-primary-srp-backup
Open

lwin-kyaw wants to merge 8 commits into
mainfrom
feat/seedless-repair-missing-primary-srp-backup

Conversation

@lwin-kyaw

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

Copy link
Copy Markdown
Contributor

Explanation

A social login user can unlock a wallet whose primary SRP exists only on that device. The remote backup then has no primary SRP, so restoring on another device cannot recover the wallet. Unlock already detects this and emits an analytics event. It does not write the missing item.

submitPassword and identifyIncompleteMetadataBackup now run PrimarySrpBackupRepair. The class is held by the controller and is not a public export. One run fetches the remote backup once and exports the local primary SRP once, then uses that plan for both the analytics event and the write.

The write happens only when no remote mnemonic can be the primary SRP:

  • A v1 backup gets an untagged v1 item. The SDK defaults a new item to v2 and then requires a data type, so the version is passed explicitly.
  • An empty, v2, or mixed backup gets a v2 item with the PrimarySrp data type.
  • A mismatch is reported and left unchanged. Repairing it is #11766.

The write runs under the controller lock, after the unlocked check, a password-sync check, and token refresh. It is aborted if the user identity changed after planning. Failures are logged and never thrown, so unlock is not blocked. submitPassword does not await the repair, because the repair takes the lock that submitPassword is still holding.

There is no public API change.

References

  • Related to #11766, which repairs a mismatched primary SRP on top of this branch

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
Best-effort writes of the primary SRP to remote backup are security-sensitive, but guarded by identity checks, schema rules, and failure isolation that avoids blocking unlock.

Overview
Adds automatic repair when remote TOPRF metadata has no primary SRP but the local keyring does—so social-login wallets can be restored on another device after unlock.

submitPassword and identifyIncompleteMetadataBackup now run a new PrimarySrpBackupRepair flow instead of telemetry-only detection. One pass uses planPrimarySrpRepair (fetch remote items once, export local mnemonic once) for both analytics (missing / mismatch events) and, when the backup is missing, a best-effort addSecretDataItem write. Mismatches are still reported only; they are not overwritten.

The write respects the remote schema (v1 untagged mnemonic vs v2 with PrimarySrp), runs under the controller lock with password sync and token refresh, aborts on identity change, and never throws so unlock is not blocked (submitPassword does not await repair because repair re-acquires the lock). Analytics helper trackIncompleteMetadataBackupEvents now accepts a precomputed plan rather than doing the comparison itself.

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

@lwin-kyaw
lwin-kyaw requested a review from a team as a code owner November 25, 2026 04:31
@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 (12 files)
  • packages/seedless-onboarding-controller/CHANGELOG.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/constants.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

@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 76a3e7d. Configure here.

@lwin-kyaw
lwin-kyaw force-pushed the feat/seedless-repair-missing-primary-srp-backup branch from 76a3e7d to 3556cd6 Compare November 25, 2026 09:42
@lwin-kyaw
lwin-kyaw requested a review from a team as a code owner November 25, 2026 11:59
…with the v1 version

The SDK defaults the item version to v2 and then requires a data type, so a repair without a data type failed. Pass the version explicitly.

Co-authored-by: Cursor <cursoragent@cursor.com>
grvgoel81
grvgoel81 previously approved these changes Nov 27, 2026

@grvgoel81 grvgoel81 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.

Approve, assuming this lands with #11766 next (coalesce / mismatch). Please don’t ship #11748 alone without the concurrent-run guard.

@himanshuchawla009

Copy link
Copy Markdown
Contributor

Should a healthy backup still be re-read on every unlock?

Once a repair has landed — or for the large majority of users whose backup was never broken — run() still does the full detection pass on every single unlock, and the answer is always the same.

Per unlock, for every seedless user:

  • fetchAllSecretDataItems — a network round trip, now also wrapped in #executeWithTokenRefresh, so it can additionally trigger a token refresh (that wrapping is new here; the old #identifyIncompleteMetadataBackup called the SDK directly).
  • exportSeedPhrase(password) → #verifyCredentials → verifyPassword → encryptor.decrypt(password, vault). On mobile the KeyringController encryptor is built with DERIVATION_OPTIONS_MINIMUM_OWASP2023 (600k PBKDF2 iterations) and Encryptor.decrypt derives the key fresh from the payload's keyMetadata with no cache — so this is a second full 600k derivation plus a vault decrypt, moments after submitPassword already paid one, on the JS thread while the wallet home screen is mounting and fetching balances.

To be clear, this is not a regression from this PR: the old telemetry path also called both on every healthy unlock. But the PR changes why we're paying it, which makes it worth asking now. Previously the only output was a telemetry event, and re-checking every unlock was the measurement — it's what counted the standing affected population. Now the output is a repair, and once that has succeeded the answer is stable.

Suggestion: cache only the healthy result. When planPrimarySrpRepair returns undefined, record something like:

primarySrpBackupHealthyCache?: {
  userId: string;
  authConnectionId: string;
  timestamp: number;
};

and skip the whole run while it's fresh. passwordOutdatedCache is the existing precedent for a persisted TTL cache in this controller.

The asymmetry is the point: a missing or mismatch plan is never cached. Affected users keep being detected and repaired on every unlock, and the mismatch cohort — which this PR deliberately doesn't repair — keeps being reported at full rate, so the event denominator doesn't change. The saving lands entirely on the healthy majority, who are paying a network round trip and a 600k-iteration derivation to re-learn something that didn't change.

Things to decide if this seems worth doing:

  • TTL length. Hours rather than days. The remote backup can change from another device, and the local side can change too (a restore, or a different keyring ending up at keyrings[0]), so a cached "healthy" can genuinely go stale. Give it its own constant and controller option — passwordOutdatedCacheTTL is 10s, tuned for within-a-flow dedupe.
  • Key it on userId + authConnectionId, or switching social accounts on one device inherits the previous account's cache and skips a check that should run.
  • Invalidation. clearState at minimum; worth thinking about whether syncLatestGlobalPassword or a migration should clear it too.

If the per-unlock cost is considered acceptable, that's a fine answer — but it'd be good to have it written down as a deliberate choice, since it's now a cost paid indefinitely by every seedless user rather than a time-boxed measurement.

This branch was previously deployed

1 inactive (outdated) deployment
default-branch — 76a3e7dc Deployed Nov 25, 2026 by lwin-kyaw via Determine whether this PR is a release PR #7582
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.

3 participants