Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape prefix value in hasBasename #604

Closed
wants to merge 2 commits into from

Conversation

w33ble
Copy link

@w33ble w33ble commented Jul 3, 2018

Closes #564

  • Adds tests for PathUtils.hasBasename and PathUtils.stripBasename
    • "hasBasename with a search value given as a url with basename returns true" fails
  • Escapes these characters when creating RegExp in hasBasename: | \ { } ( ) [ ] ^ $ + * ? .
    • "hasBasename with a search value given as a url with basename returns true" now passes

This PR fixes PathUtils.hasBasename so that it correctly handles special URL characters. This allows users to specify a basename that contains a search params and other special regex characters and have it both correctly match and correctly strip the basename from the path.

This is helpful when you want to be able to ignore everything in the URL up to a given point. For example, given the URL /basepath/app?search=params&to=ignore#/real/path/to/parse, you may only care about /real/path/to/parse. Providing a basename value of /basepath/app?search=params&to=ignore#/ would not previously work, because the ? was not escaped, so the produced RegExp in hasBasename would not match correctly.

This could be considered a breaking change if you were relying on the internal checking mechanism to use a regex pattern instead of a string. However, given that the docs specifically call for the basename value to be a string, I'd consider that an incorrect use of the basename value in the first place. From the README (emphasis mine):

If all the URLs in your app are relative to some other "base" URL, use the basename option. This option transparently adds the given string to the front of all URLs you use.

Aside: The contributing guide says to add a note to CHANGES.md, but it seems that hasn't been done since v4.6.3, so I skipped that step... looks like most recent PRs haven't done this either.

@a-neumann
Copy link

I have a similar problem with the use of [] and | characters in the path which are interpreted unescaped by the regexp.

@a-neumann
Copy link

@w33ble i created #607 that adds some other problematic characters to your fix. Maybe you could add these to your PR so i could close mine. Thanks in advance

@w33ble
Copy link
Author

w33ble commented Jul 23, 2018

@a-neumann I borrowed from #608, which used escape-string-regexp. I just grabbed the logic from that module and put it into my PR. The following characters are all escaped here: | \ { } ( ) [ ] ^ $ + * ? .

including failing tests when using a search param in a basename
list and logic borrowed from escape-string-regexp
@w33ble
Copy link
Author

w33ble commented Nov 1, 2018

This is failing with the same errors that #634 failed with (that PR was still merged).

Tests all run locally, maybe something is wrong with the travis setup?

Finished in 1.24 secs / 0.852 secs @ 09:56:21 GMT-0700 (MST)

SUMMARY:
✔ 130 tests completed

@w33ble w33ble closed this Nov 16, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jan 15, 2019
@remix-run remix-run deleted a comment from w33ble Mar 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot include URL query in basename
2 participants