Skip to content

escape regex special characters in location prefix stripping#21436

Closed
rootvector2 wants to merge 1 commit into
emberjs:mainfrom
rootvector2:escape-location-prefix-regex
Closed

escape regex special characters in location prefix stripping#21436
rootvector2 wants to merge 1 commit into
emberjs:mainfrom
rootvector2:escape-location-prefix-regex

Conversation

@rootvector2

Copy link
Copy Markdown
Contributor

HistoryLocation.getURL builds its prefix-stripping RegExp by interpolating rootURL and the baseURL read from the page's directly, so a rootURL like /app.v2/ over-strips /appXv2/posts and a base href containing [ makes getURL throw. Run both through escapeRegExp so the prefix matches literally; NoneLocation.getURL had the same unescaped rootURL.

@rootvector2

Copy link
Copy Markdown
Contributor Author

any update?

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.

can you add a test for when the rootURL contains a slash in the middle?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR prevents HistoryLocation.getURL() and NoneLocation.getURL() from treating rootURL / baseURL as regular-expression patterns when stripping URL prefixes, avoiding both incorrect over-stripping and RegExp construction errors when those values contain regex-special characters.

Changes:

  • Add a shared escapeRegExp() helper in packages/@ember/routing/lib/location-utils.ts.
  • Escape baseURL and rootURL before building prefix-stripping RegExps in HistoryLocation.getURL().
  • Escape rootURL before building the prefix-stripping RegExp in NoneLocation.getURL(), and add a regression test for HistoryLocation rootURL special characters.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/@ember/routing/history-location.ts Escapes baseURL/rootURL prior to RegExp construction during prefix stripping.
packages/@ember/routing/none-location.ts Escapes rootURL prior to RegExp construction during prefix stripping.
packages/@ember/routing/lib/location-utils.ts Introduces escapeRegExp() utility for literal prefix matching.
packages/@ember/routing/tests/location/history_location_test.js Adds regression coverage for HistoryLocation.getURL() with regex-special chars in rootURL (and should be extended to cover baseURL).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +235 to 253
['@test HistoryLocation.getURL() treats regex special characters in rootURL literally'](
assert
) {
HistoryTestLocation.reopen({
init() {
this._super(...arguments);
set(this, 'location', mockBrowserLocation('/appXv2/posts'));
set(this, 'rootURL', '/app.v2/');
},
});

createLocation();

assert.equal(location.getURL(), '/appXv2/posts');
}

['@test HistoryLocation.getURL() returns the current url, does not remove baseURL if its not at start of url'](
assert
) {

// remove rootURL from url
return path.replace(new RegExp(`^${rootURL}(?=/|$)`), '');
return path.replace(new RegExp(`^${escapeRegExp(rootURL)}(?=/|$)`), '');
@NullVoxPopuli

Copy link
Copy Markdown
Contributor

same change as #21492 (and merging this one, because it has one extra test)

thank you!

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.

3 participants