Skip to content

Commit 3d65e4e

Browse files
committed
fix(layout): useLayoutNavigation possible perf fix
This was caught by CodeQL: CWE-400 CWE-730 > This regular expression that depends on library input may run slow on > strings starting with '?' and with many repetitions of '?'. This really shouldn't happen unless the users can configure the layout tree themselves.
1 parent 75a9b0f commit 3d65e4e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/layout/src/useLayoutNavigation.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ const noop = (): void => {
4949
// do nothing
5050
};
5151

52+
/**
53+
* This used to just be `pathname.replace(/\?.*$/, "")` but that can apparently
54+
* cause performance issues or a DoS attack if the pathname contains multiple
55+
* ?`?` (shouldn't really be possible though)
56+
*
57+
* @remarks \@since 2.9.0
58+
*/
59+
const removeQueryParams = (pathname: string): string => {
60+
const i = pathname.indexOf("?");
61+
if (i === -1) {
62+
return pathname;
63+
}
64+
65+
return pathname.substring(0, i);
66+
};
67+
5268
/**
5369
* This is a pretty reasonable default implementation for having a navigation
5470
* tree within the Layout component. The way it'll work is that the current
@@ -79,7 +95,7 @@ export function useLayoutNavigation<
7995
pathname: string,
8096
linkComponent: ElementType = Link
8197
): LayoutNavigationState<T> {
82-
const itemId = pathname.replace(/\?.*$/, "");
98+
const itemId = removeQueryParams(pathname);
8399
const { expandedIds, onItemExpansion, onMultiItemExpansion } =
84100
useTreeItemExpansion(() => getParentIds(itemId, navItems));
85101

0 commit comments

Comments
 (0)