Skip to content

escape rootURL and baseURL before regexp in getURL#21492

Merged
NullVoxPopuli merged 2 commits into
emberjs:mainfrom
ubeddulla:location-geturl-escape-regexp
Jul 10, 2026
Merged

escape rootURL and baseURL before regexp in getURL#21492
NullVoxPopuli merged 2 commits into
emberjs:mainfrom
ubeddulla:location-geturl-escape-regexp

Conversation

@ubeddulla

Copy link
Copy Markdown
Contributor

getURL in HistoryLocation and NoneLocation strips the configured rootURL and baseURL off location.pathname by dropping them straight into new RegExp(^${rootURL}(?=/|$)), so any regex metacharacter in those values is matched as a pattern against the browser-controlled pathname instead of a literal. a prefix like /a.c/ then strips /axc/secret down to /secret and hands the router the wrong route, an unbalanced ( throws a SyntaxError on every navigation, and a nested-quantifier prefix backtracks exponentially on a short path. this routes both values through a small escapeRegExp helper in location-utils so the prefixes only ever match literally, with tests covering the metacharacter case in both location classes.

@NullVoxPopuli NullVoxPopuli left a comment

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.

thanks for submitting this PR! just a couple things

Escapes any regular-expression metacharacters in `str` so it can be safely
interpolated into a `RegExp` and matched as a literal.
*/
export function escapeRegExp(str: string): string {

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.

Image

https://caniuse.com/mdn-javascript_builtins_regexp_escape

we can't use this yet, but should document to delete this function once our minimum browser support all includes RegExp.escape

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call. Added a TODO on the helper pointing at the caniuse link so we drop it for RegExp.escape once our minimum browsers cover it.

Comment thread packages/@ember/routing/tests/location/none_location_test.js

@NullVoxPopuli NullVoxPopuli left a comment

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.

Confirmed locally

@NullVoxPopuli

Copy link
Copy Markdown
Contributor

duplicate of #21436

@NullVoxPopuli NullVoxPopuli merged commit 7a9c460 into emberjs:main Jul 10, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants