fix(signin): run /oauth/authorization before firing webchannel events#20543
Draft
fix(signin): run /oauth/authorization before firing webchannel events#20543
Conversation
2 tasks
Because: * In handleNavigation's verified-OAuth path, sendFxaLogin (the fxaccounts:login webchannel event) was dispatched synchronously BEFORE awaiting getOAuthNavigationTarget (the /v1/oauth/authorization HTTP call). The browser's async handler for that webchannel message can mutate session state, racing with the in-flight HTTP request. * This produced intermittent 401 "Token not found" failures on the HTTP call — most reproducibly when the same browser context goes through two consecutive OAuth flows (e.g. the vpn integration test signing into Sync then VPN). The first /oauth/authorization succeeded; the second one would 401 even though the session remained valid for /account/profile, /session/status, and /oauth/token in between. This commit: * Reorders handleNavigation so getOAuthNavigationTarget runs first, obtaining the OAuth code while the session is still untouched, and THEN fires fxaLogin and fxaOAuthLogin. The Login → OAuthLogin order is preserved (still required for Desktop OAuth per FXA-10388). * Adds a v1-account regression test alongside the existing vpn integration test, using target.createAuthClient(1) so the v1→v2 upgrade path is exercised on the first sign-in even after testAccountTracker defaults to v2 (separate PR). closes FXA-13687 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
204d9de to
76e1354
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because
vpn integration › authorization flow - user already signed into FirefoxPlaywright test was flaking with a 401 on the second/v1/oauth/authorizationcall, blocking CI.errno 110 "Invalid authentication token: Missing authentication"fromtokenNotFoundError()inpackages/fxa-auth-server/lib/routes/auth-schemes/hawk-fxa-token.js. The session was valid for/v1/account/profile,/v1/session/status, and/v1/oauth/tokenimmediately before, but couldn't be found 15 ms later for/v1/oauth/authorization— same Hawk id, no/v1/session/destroyin between.firefox.fxaLogin(andfxaOAuthLogin) dispatchWebChannelMessageToChromesynchronously, but the browser handles those events on its own event loop. The browser'sfxaccounts:loginhandler can mutate session state, racing with an in-flight/v1/oauth/authorizationHTTP request. The vpn integration test reproduced it most reliably because it does two consecutive OAuth flows in the same browser context (Sync, then VPN cached signin).This pull request
handleNavigationinpackages/fxa-settings/src/pages/Signin/utils.tssogetOAuthNavigationTarget(the/v1/oauth/authorizationHTTP call) runs first, obtaining the OAuth code while the session is still untouched.firefox.fxaLoginandfirefox.fxaOAuthLoginfire after the HTTP response returns. TheLogin → OAuthLoginorder is preserved (still required for Desktop OAuth per FXA-10388).packages/functional-tests/tests/misc/vpnIntegration.spec.tsthat builds an auth client withtarget.createAuthClient(1)and runs the same Sync→VPN flow, exercising the v1→v2 upgrade path on first sign-in. This is the test pattern the original flake hit.testAccountTrackeraccounts to v2 key stretching). Either PR can land first.Issue that this pull request solves
Closes: FXA-13687
Checklist
Put an
xin the boxes that applyHow to review (Optional)
packages/fxa-settings/src/pages/Signin/utils.ts— the reorder is small but load-bearing; the new comment block above theawait getOAuthNavigationTarget(...)call documents why the order matters so a future reader doesn't undo it.packages/functional-tests/tests/misc/vpnIntegration.spec.ts— the new regression test mirrors the existing test but explicitly forces a v1 client so it stays as a regression signal even after chore(functional-tests): default test accounts to v2 key stretching #20556's v2 default lands.utils.tsfirst (the actual fix), then the new test.packages/fxa-settings/src/pages/Signin/utils.test.ts) assert thatfxaLoginandfxaOAuthLoginare called with the right args; they don't assert call order, so they remain valid.Other information (Optional)
The flake was originally suspected to be caused by the v1→v2 key-stretching upgrade running during the first sign-in (since
password/change/finishupdatesaccounts.verifierSetAt). PR #20556 was opened on that hypothesis but didn't fix CI on its own — the failure pattern was identical with and without the upgrade flow. Capturing the actual 401 response body from the failing trace narrowed the cause down to a webchannel race rather than anythingverifierSetAt-related.