Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/hooks/useAIFeaturesPromoModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ function useAIFeaturesPromoModal(session: OnyxEntry<Session>) {
if (lastRoute === NAVIGATORS.SHARE_MODAL_NAVIGATOR || lastRoute === SCREENS.NOT_FOUND) {
return;
}
if (lastRoute === NAVIGATORS.AI_FEATURES_PROMO_MODAL_NAVIGATOR) {
// The promo modal is already the active route (e.g. deep-linked directly). Re-navigating would
// append the suffix onto a base that already ends in it, producing a duplicated
// `…/ai-features-promo/ai-features-promo` path. Treat it as already shown and bail out.
hasRedirectedToAIFeaturesPromoModal = true;
return;
}
Comment on lines +101 to +107

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.

I wonder if this shouldn't be checked at a higher level, within the methods responsible for creating dynamic paths. I doubt there is a scenario where a dynamic path should be added again to the same path. Handling this at a higher level could help us avoid similar errors elsewhere in the future.

However, I think we can address this in a subsequent task if this PR needs to be merged urgently :)

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.

@MelvinBot can you create an issue for this and assign @collectioneur and @mountiny also include note about https://github.com/Expensify/App/pull/97849/changes#r3747400514

Log.info('[useAIFeaturesPromoModal] Navigating to AI features promo modal');
hasRedirectedToAIFeaturesPromoModal = true;
Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.AI_FEATURES_PROMO.path, Navigation.getActiveRoute() || ROUTES.HOME));
Expand Down
10 changes: 8 additions & 2 deletions src/libs/Navigation/helpers/getPathFromState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ function getPathFromStateWithDynamicRoute(state: State): string {
const tabPath = focusedTab && isScreen(focusedTab.name) ? normalizedConfigs[focusedTab.name]?.path : undefined;
if (tabPath) {
const [suffixPathOnly, suffixQueryOnly] = splitPathAndQuery(actualSuffix);
actualSuffix = `${suffixPathOnly}/${tabPath}${suffixQueryOnly ? `?${suffixQueryOnly}` : ''}`;
const combinedSuffixPath = suffixPathOnly === '/' ? `/${tabPath}` : `${suffixPathOnly}/${tabPath}`;
actualSuffix = `${combinedSuffixPath}${suffixQueryOnly ? `?${suffixQueryOnly}` : ''}`;
}
}
}
Expand All @@ -163,7 +164,12 @@ function getPathFromStateWithDynamicRoute(state: State): string {
}
const queryString = mergedParams.toString();

return `${basePathWithoutQuery}/${suffixPath}${queryString ? `?${queryString}` : ''}`;
// Mirror createDynamicRoute's slash handling: a root base (`/`) must not be concatenated with a leading slash,
// otherwise the result is a `//`-prefixed path that the browser parses as protocol-relative (host = first segment),
// making `history.pushState` throw a SecurityError.
const combinedPath = basePathWithoutQuery === '/' ? `/${suffixPath}` : `${basePathWithoutQuery}/${suffixPath}`;

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.

NAB: Maybe we can extract a helper to return an adjusted path. I see that this logic is repeated elsewhere in the code


return `${combinedPath}${queryString ? `?${queryString}` : ''}`;
}

function getPathFromState(state: State): string {
Expand Down
11 changes: 11 additions & 0 deletions tests/navigation/getPathFromStateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ describe('getPathFromState', () => {
expect(result).toBe('/settings/wallet/test-dynamic');
});

it('root base path does not produce a doubled leading slash', () => {
// A base screen that resolves to root ('/') must not be concatenated with a leading slash,
// otherwise the result is a '//'-prefixed protocol-relative URL that makes history.pushState
// throw a SecurityError. Regression test for the AI Features Promo crash (issue #97470).
mockRNGetPathFromState.mockReturnValue('/');

const state = buildState([{name: 'StandardScreen'}, {name: 'TestDynamicScreen'}]);

expect(getPathFromState(state as PartialState<NavigationState>)).toBe('/test-dynamic');
});

it('should use RN getPathFromState for standard screens', () => {
const expectedPath = '/standard/path';
mockRNGetPathFromState.mockReturnValue(expectedPath);
Expand Down
Loading