Skip to content

[PM-20012] fix: Incorrect endpoint saved - #2796

Open
LRNcardozoWDF wants to merge 6 commits into
mainfrom
cmcg/pm-20012-fix-incorrect-endpoint-saved
Open

[PM-20012] fix: Incorrect endpoint saved#2796
LRNcardozoWDF wants to merge 6 commits into
mainfrom
cmcg/pm-20012-fix-incorrect-endpoint-saved

Conversation

@LRNcardozoWDF

@LRNcardozoWDF LRNcardozoWDF commented Jun 16, 2026

Copy link
Copy Markdown
Member

🎟️ Tracking

PM-20012

📔 Objective

We are setting the pre-auth environment URLs when using the autofill. That data is shared with the main app, so if we use the autofill in middle of performing login we will have the wrong environment URLs for the account.

@LRNcardozoWDF
LRNcardozoWDF requested review from a team and matt-livefront as code owners June 16, 2026 14:59
@github-actions github-actions Bot added app:password-manager Bitwarden Password Manager app context t:bug Change Type - Bug labels Jun 16, 2026
@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

🤖 Bitwarden Claude Code Review

Overall Assessment: REQUEST CHANGES

Reviewed the revised fix for PM-20012. The earlier approach (removing setPreAuthURLs from loadURLsForActiveAccount) was dropped in favor of snapshotting the pre-auth environment URLs per email at the Landing screen and preferring that snapshot when the account is created in getIdentityTokenResponse, which addresses the reviewer concern about active-account switching still needing to update the pre-auth URLs. The approach reuses the existing accountCreationEnvironmentURLs mechanism already used by the registration flow, and the in-memory EnvironmentService URLs are per-process so the identity token request itself still targets the correct server. One correctness gap remains: the snapshot has no invalidation, so a leftover entry can now override the live pre-auth URLs during account creation.

Code Review Details
  • ⚠️ : Stale accountCreationEnvironmentURLs entry can take precedence over the current pre-auth URLs, saving an account with another server's URLs
    • BitwardenShared/Core/Auth/Services/AuthService.swift:987-995

urls = .defaultUS
}

await setPreAuthURLs(urls: managedSettingsURLs ?? urls)

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.

🤔 Are there any downsides to removing this? I think this currently is used so that the pre-auth URLs are updated when you switch the active account (the environment URLs when adding a new account are always set to the last active account). Assuming that's still a requirement, we should account for that before removing this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You may be right, I didn't test that flow. I'll test that, meanwhile this PR will be in draft. Thank you for bringing this to my attention.

@LRNcardozoWDF
LRNcardozoWDF marked this pull request as draft June 17, 2026 13:12
# Conflicts:
#	BitwardenShared/Core/Platform/Services/EnvironmentServiceTests.swift
@LRNcardozoWDF
LRNcardozoWDF marked this pull request as ready for review August 4, 2026 14:59
Comment on lines +987 to +995
// Create the account. Prefer the environment URLs snapshotted for this email when the
// login flow started, since the global pre-auth URLs may have since been overwritten
// by an unrelated active-account sync (e.g. triggered by the AutoFill extension).
let urls: EnvironmentURLData? = if let accountCreationURLs = await stateService
.getAccountCreationEnvironmentURLs(email: email) {
accountCreationURLs
} else {
await stateService.getPreAuthEnvironmentURLs()
}

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.

⚠️ IMPORTANT: A stale per-email snapshot now wins over the live pre-auth URLs, so an account can be saved pointing at the wrong server.

Details and fix

accountCreationEnvironmentURLs is only written in two places (LandingProcessor.validateEmailAndContinue and StartRegistrationProcessor on the email-verification path) and is never cleared, but it is now consulted for every login, so a leftover entry takes precedence over the current pre-auth URLs.

Traceable path:

  1. Landing on the US region, enter email@x.com, tap Continue → snapshot = US.
  2. Back out, switch the region to self-hosted server S, tap Create account, register the same email. When startRegistration returns a token directly (email verification disabled — common on self-hosted), StartRegistrationProcessor navigates to .completeRegistration without updating the snapshot.
  3. CompleteRegistrationProcessor.setRegion() early-returns because state.fromEmail == false, so the pre-auth URLs are correctly S and the account is created on S. It then calls loginWithMasterPassword(username:isNewAccount: true), which reaches this code.
  4. The snapshot still holds US → the account is persisted with US URLs while it actually lives on S, which is the failure mode this PR is trying to prevent.

CompleteRegistrationProcessor:283 is a second route to .login(username:) that bypasses Landing, so it has the same exposure.

Suggested hardening: write the snapshot on every entry into the login/account-creation flow (including the direct-token path in StartRegistrationProcessor and the fallback navigation in CompleteRegistrationProcessor), and clear the entry once it has been consumed here so it can never be reused by a later flow.

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.72%. Comparing base (f0c6864) to head (1216e8d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2796      +/-   ##
==========================================
+ Coverage   79.43%   81.72%   +2.29%     
==========================================
  Files        1172     1049     -123     
  Lines       74915    67628    -7287     
==========================================
- Hits        59506    55271    -4235     
+ Misses      15409    12357    -3052     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

app:password-manager Bitwarden Password Manager app context t:bug Change Type - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants