-
Notifications
You must be signed in to change notification settings - Fork 59
Make regex and glob pattern matching stricter #130
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,11 @@ export const matchesSavedMap = (url, matchDomainOnly, {host}) => { | |
| } | ||
|
|
||
| if (host[0] === PREFIX_REGEX) { | ||
| const regex = host.substr(1); | ||
| let regex = host.substr(1); | ||
| if (matchDomainOnly) { | ||
| // This might generate double ^^ characters, but that works anyway | ||
| regex = "^" + regex + "$"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a linting error. Please run |
||
| } | ||
|
Comment on lines
-86
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the comments earlier, this is an unnecessary change based on the assumption that regexes should match from the beginning of the string. |
||
| try { | ||
| return new RegExp(regex).test(toMatch); | ||
| } catch (e) { | ||
|
|
@@ -93,10 +97,14 @@ export const matchesSavedMap = (url, matchDomainOnly, {host}) => { | |
| // turning glob into regex isn't the worst thing: | ||
| // 1. * becomes .* | ||
| // 2. ? becomes .? | ||
| return new RegExp(host.substr(1) | ||
| .replace(/\*/g, '.*') | ||
| .replace(/\?/g, '.?')) | ||
| .test(toMatch); | ||
| // Because the string is regex escaped, you must match \* to instead of * | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's either a word too many here or one missing. |
||
| let regex = escapeRegExp(host.substr(1)) | ||
| .replace(/\\\*/g, '.*') | ||
| .replace(/\\\?/g, '.?') | ||
| if (matchDomainOnly) { | ||
| regex = "^" + regex + "$"; | ||
| } | ||
| return new RegExp(regex).test(toMatch); | ||
| } else { | ||
| const key = urlKeyFromUrl(urlO); | ||
| const _url = ((key.indexOf('/') === -1) ? key.concat('/') : key).toLowerCase(); | ||
|
|
@@ -135,3 +143,13 @@ export function formatString(string, context) { | |
| return replacement; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Escape all regex metacharacters in a string | ||
| * | ||
| * @param string {String} | ||
| */ | ||
| function escapeRegExp(string) { | ||
| // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping | ||
| return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section's purpose is to make sure that
matchDomain=truereally only uses the domain for matching and not the URL path contain the domain. WhenmatchDomain=falsethe url should match because the URL path contains the domain.It looks like you repurposed the section to use
evilUrlwhich makes sense when you put the domain in the URL's path. However, it becomes a lot less clear in the other cases.I'd recommend explicitly adding an
itfor your test and reworking this part to add the domain ofexpectedUrlto the URL path.