Skip to content

[Due for payment 2026-09-09] [Sentry: APP-HT5] Web onboarding private-domain step loops history.replaceState past Safari's rate limit #97473

Closed

Description

@mountiny

Sentry

https://expensify.sentry.io/issues/APP-HT5

Impact (snapshot at filing)

  • Users (total since first seen): 22
  • Events: 38
  • Users (last 7d, in 9.4.44–9.4.46): 2
  • First seen: 2026-07-16
  • Last seen: ongoing (events today)
  • Platform: web — Mobile Safari (iOS 18.7) observed; Safari rate limit is what surfaces it
  • App version(s): 9.4.45-14 and others in range
  • Affected route(s): /onboarding/private-domain
  • View name: Onboarding_Private_Domain
  • Mechanism: auto.browser.global_handlers.onunhandledrejection
  • DOMException.code: 18 (SecurityError)

Error

SecurityError: Attempt to use history.replaceState() more than 100 times per 10 seconds

Stack trace (top frames, first-party only)

../node_modules/@react-navigation/native/lib/module/useLinking.js:373:24 (onStateChange)
        history.replace({
../node_modules/@react-navigation/native/lib/module/createMemoryHistory.js:128:34 (<object>.replace)
      window.history.replaceState({
[native code] (replaceState)

Suspected cause

A navigation-state loop on the onboarding private-domain step. Something re-dispatches navigation on
every state change — a redirect effect whose dependency changes each render, or a
navigate/setParams call in an effect that itself re-triggers the effect — so react-navigation's
useLinking onStateChange fires history.replace repeatedly. Safari caps replaceState at 100
calls per 10 seconds and throws once the loop exceeds it.

The rate limit is the symptom, not the bug: the underlying problem is an unbounded navigation loop
that will also burn CPU and block the onboarding flow on browsers without a cap.

Worth auditing the redirect logic on the private-domain onboarding screen for an effect that
navigates without a guard on the current route/state.

Reproduction

Unknown — see Sentry events linked above. Reaching /onboarding/private-domain (sign up with a
private email domain and proceed through onboarding) on Mobile Safari is the observed context.

Related

  • Same useLinking/createMemoryHistory layer as the pushState SecurityError filed alongside this
    one in the same scan (APP-HTY / APP-HNR / APP-CFF), but a different call site and a different root
    cause — that one is a malformed path, this one is a loop.
  • Found by a 7-day production crash scan of the latest 3 released versions (9.4.44, 9.4.45, 9.4.46)
Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @mallenexpensify

Activity

  1. self-assigned this
    on Jul 30, 2026
  2. added
    External
    Daily
    Bug
    on Jul 30, 2026
  3. melvin-bot commented on Jul 30, 2026

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @gijoe0295 (External)

  4. melvin-bot commented on Jul 30, 2026

    @melvin-bot
  5. MelvinBot commented on Jul 30, 2026

    @MelvinBot
    Contributor

    Proposal

    Updated 2026-08-21: Revised based on huult's runtime debugging (logs + test branch), which confirmed the loop is driven by a redundant no-op resetRoot on the shared router. The root cause below is now stated route-agnostically, and the recommended fix is the general resetRoot idempotency guard (previously described as the durable follow-up).

    TL;DR: The always-mounted onboarding router calls resetRoot({stale: true}) even when the resulting route stack is identical to the current one. stale: true unconditionally re-runs react-navigation's useLinking.onStateChange → history.replaceState, so each redundant call burns one replaceState without changing the visible route. When an onboarding Onyx dependency keeps flipping (or a stale ONBOARDING_LAST_VISITED_PATH read makes the computed route name disagree with the visible one), this repeats fast enough to exceed Safari's 100-calls/10s cap → SecurityError. /onboarding/private-domain is the most common trigger, not the only one.

    What is the root cause of that problem?

    startOnboardingFlow has two structural weaknesses on its resetRoot path, neither of which is private-domain-specific:

    1. Its idempotency guard compares route name only — OnboardingFlow.ts:97 bails only if focusedRoute?.name === currentRoute?.name. The navigate side already guards on the full path + params (linkTo/index.ts:173 via areNamesAndParamsEqual/arePathAndBackToEqual); the resetRoot side has no equivalent.
    2. The merge collapses the computed target back onto the current stack — OnboardingFlow.ts:102-106 only appends routes whose top-level name isn't already present. Since ONBOARDING_MODAL_NAVIGATOR is already in the root state, the computed route is filtered out and the merged list equals the current one — so resetRoot re-asserts the current stack instead of navigating, yet stale: true still fires history.replaceState.

    So whenever the computed target's route name differs from the focused route (the name-only guard falls through) but the merge collapses to the same stack, resetRoot fires a no-op replaceState. The common way the names diverge: getOnboardingInitialPath returns the stale initialPath (from ONBOARDING_LAST_VISITED_PATH), which is read via useOnyx (useOnboardingFlow.ts:56) but written async on every onboarding navigation (NavigationRoot.tsx:94). The always-mounted effect re-runs on Onyx dependency changes (useOnboardingFlow.ts:137) before that write lands, so the computed name disagrees with the visible route and the no-op resetRoot fires again — over and over.

    Why Safari-only, and the runtime evidence
    • Chromium coalesces rapid replaceState; Safari enforces 100/10s — matching the Sentry platform data (Mobile Safari, DOMException.code: 18) and the stack (useLinking.onStateChange → createMemoryHistory.replace → replaceState).
    • huult's test branch instrumented the path and captured the no-op directly: with currentRouteName: Onboarding_Private_Domain and a computed onboardingPath: /onboarding/personal-details, mismatchDetected: true (name-only guard falls through), a routesUnchanged: true guard evaluates (the merge would be a no-op), yet replaceState url=/onboarding/private-domain still fires because the current code has no such guard. Once the async LAST_VISITED_PATH write lands, mismatchDetected: false and it settles.
    • Private-domain is the most common trigger because BaseOnboardingPrivateDomain's own redirect effect keeps flipping the exact Onyx keys (loading, joinablePolicies) the router depends on — but the same no-op-resetRoot mechanism can surface on any onboarding route (matching the other Sentry events on /onboarding/purpose, /accounting, /personal-details, /personaltrackcase).

    What changes do you think we should make in order to solve the problem?

    Primary (route-agnostic, the real fix) — add a full idempotency guard on the resetRoot side of startOnboardingFlow. Before calling resetRoot, compute the merged route list and bail if it is identical to the current root state (this is huult's FIX #2):

    • Build mergedRoutes (same expression already used at line 106), then return early when it matches the current rootState.routes — mirroring the areNamesAndParamsEqual/arePathAndBackToEqual bail that linkTo already applies on the navigate side. Since inter-screen onboarding navigation goes through navigate/forceReplace (not resetRoot) once the modal navigator is mounted, skipping the redundant resetRoot changes no observable behavior — it only drops the wasted replaceState. This neutralizes every onboarding route at once.
    • Recommended refinement: compare the full computed path (name + params + backTo) against the active path rather than top-level route names only, so the guard also covers a future case where a nested onboarding path/params legitimately differ under an already-present top-level navigator.

    Defensive (already in flight, keep) — PR #97709's private-domain changes (forward validated private-domain users off the stale path + a one-shot redirect guard on the screen) remain correct and worth landing; they cut the most common trigger. The resetRoot guard above is the durable fix — either fold it into #97709 or land it separately.

    What alternative solutions did you explore? (Optional)

    • Fixing it route-by-route (the original private-domain-only framing) — rejected as the sole fix: it leaves the shared no-op resetRoot in place, so the loop can resurface on other onboarding routes (as the additional Sentry events show).
    • Guarding only the screen redirect effect — insufficient; the router still re-asserts the current stack via resetRoot, so the replaceState spam persists.
    Confidence

    High — the name-only guard, the collapsing merge, the async LAST_VISITED_PATH write, and the always-mounted re-run are all confirmed in code, and huult's instrumented logs demonstrate the no-op replaceState at runtime and its resolution once state settles. Consistent with the Sentry stack and Safari-only platform data. (The test branch widens the async-write window artificially to make the race reproducible, so it proves the mechanism rather than the exact production timing.)


    Next Steps for Contributor+ team:
    To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/97473#issuecomment-5131669600) to create a draft PR (the route-agnostic resetRoot guard; can extend #97709 or be a new PR).
    To refine: @MelvinBot <your feedback>
    To reject: Explain why you are rejecting Melvin's proposal.

  6. gijoe0295 commented on Aug 3, 2026

    @gijoe0295
    Contributor

    @MelvinBot implement this

  7. MelvinBot commented on Aug 3, 2026

    @MelvinBot
    Contributor

    🤖 Draft PR created: #97709

    I implemented the approved proposal — both the primary router fix and the defensive screen guard:

    • Primary — getOnboardingInitialPath now resolves forward when the last-visited path is /onboarding/private-domain and the account is validated (Workspaces if there are accessible policies, else Purpose/Employees), instead of returning the stale private-domain path. The existing guard covered only public-domain validated users; it now also covers validated private-domain users, so the router no longer sends them back onto the screen. Unvalidated users still stay put to confirm their code.
    • Defensive — BaseOnboardingPrivateDomain now uses a useRef "hasRedirected" guard so the forced-replace redirect fires at most once per mount, bounding replaceState calls even if a re-mount slips through.

    Testing: Added unit tests to tests/unit/OnboardingFlowTest.ts covering the new router branches (validated private-domain → join-workspaces / purpose / employees, and unvalidated private-domain staying on the screen). All 14 tests in that file pass, and typecheck, lint-changed, oxfmt, and the React Compiler compliance check are clean.

    ⚠️ I couldn't run a live browser smoke test of the onboarding flow — reaching /onboarding/private-domain in a validated-with-policies state requires SKIP_ONBOARDING=false, which is CI-owned and not toggleable in this environment. The router branch is fully covered by the added unit tests; the human co-author should still perform the manual/QA steps outlined in the PR before marking it ready for review.

    Please mark it as "Ready for review" when it's ready.

  8. melvin-bot commented on Aug 6, 2026

    @melvin-bot

    @gijoe0295 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

  9. 59 remaining items

  10. added
    Awaiting Payment
    on Sep 8, 2026
  11. melvin-bot commented on Sep 8, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  12. melvin-bot commented on Sep 8, 2026

    @melvin-bot

    Payment Summary

    Resolving PRs:

    BugZero Checklist (@mallenexpensify)

    • I have confirmed assignees, roles, and Upwork contracts look correct
    • I have paid out Upwork contracts / manual NewDot requests
    • [BugZero Assignee] I have created a GH issue for creating/updating the regression test once above steps have been agreed upon
  13. mountiny commented on Sep 10, 2026

    @mountiny
    ContributorAuthor

    this is resolved so we can pay out @mallenexpensify

  14. melvin-bot commented on Sep 11, 2026

    @melvin-bot

    @mallenexpensify Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

  15. mallenexpensify commented on Sep 14, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @huult due $250 via NewDot

  16. mallenexpensify commented on Sep 14, 2026

    @mallenexpensify
    Contributor

    Claude posting on behalf of Matt:

    $125 to @huult (Contributor+), 50%. No reviewer checklist was posted on the PR, only "LGTM".

    Contributor Nothing due, Melvin wrote the PR
    Contributor+ @huult due $125 via NewDot, 50%
    Job price $250, standard, no price in title
    Reduction 50%, no reviewer checklist on the PR
    Increase None
    Blockers None linked
    Precedent #93343 "$125 cuz tests weren't completed", #98922 "No tests and small PR so $125". Against that, #96239, where the cut was reversed because the tests were done in Slack
    Needs a ruling No, but see the Slack precedent below
    How the amount is reached
    PR #99314, author MelvinBot
    Size 2 files, +74 / -9
    C+ response on this issue @huult posted ### No Checklist Needed with a reason: "This is an edge case that's difficult to reproduce under normal conditions"
    Reviewer checklist on the PR None. Only a short "LGTM" approval
    Platform evidence Author checklist 5 of 5 boxes checked, 0 of 5 with media, all placeholders empty
    • Job price $250. No price in the title.
    • Contributor: nothing due. Melvin wrote the PR.
    • Contributor+: 50%. The rule pays the C+ half when the review checklist carries no test steps. Here no checklist was posted at all. The test is mechanical: was a reviewer checklist posted, and did it contain test steps.
    • No regression cut. No blocker linked.
    • No increase.

    Absence of a checklist on the PR is not proof the tests were not done. On #96239 Matt proposed 50% for "no tests on PR" and then paid full: "tests were done in #contributor-plus in Slack." If @huult tested this and the record sits off the PR, say where and this goes to $250.

    One fact worth seeing either way. Melvin's PR body says "The human co-author must fill out the manual test steps" and that it "was unable to execute the test suite / typecheck / lint locally". All five platform sections are empty. So on the record as it stands, nobody has demonstrated this was tested on any platform.

    To pay the rules amount:

    ### Payment Summary
    Contributor+: @huult due $125 via NewDot
    

    If the testing happened off the PR:

    ### Payment Summary
    Contributor+: @huult due $250 via NewDot
    

    @huult, flagging this since it proposes less than the standard price. If you tested it and the evidence is elsewhere, link it.


    Claude posting on behalf of Matt. PAYRUBRICTEST v2

  17. huult commented on Sep 15, 2026

    @huult
    Contributor

    No reviewer checklist was posted on the PR, only "LGTM".

    @mallenexpensify It looks like your AI assistant didn't collect everything, and we have a reviewer checklist here: #99314 (comment)

  18. JmillsExpensify commented on Oct 14, 2026

    @JmillsExpensify
    Contributor

    $250 approved for @huult

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

Metadata

Metadata

Labels

Awaiting PaymentBugDailyExternal

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions