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

fix: ignore all nonstandard extension protocols #123

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/isUrlRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ function isUrlRequest(url, root) {
// 1. it's a Data Url
// 2. it's an absolute url or and protocol-relative
// 3. it's some kind of url for a template
if(/^data:|^chrome-extension:|^moz-extension:|^ms-browser-extension:|^(https?:)?\/\/|^[\{\}\[\]#*;,'§\$%&\(=?`´\^°<>]/.test(url)) return false;
if(/^data:|^.+-extension:\/|^(https?:)?\/\/|^[\{\}\[\]#*;,'§\$%&\(=?`´\^°<>]/.test(url)) {
return false;
}

// 4. It's also not an request if root isn't set and it's a root-relative url
if((root === undefined || root === false) && /^\//.test(url)) return false;
if((root === undefined || root === false) && /^\//.test(url)) {
return false;
}

return true;
}

Expand Down
15 changes: 12 additions & 3 deletions test/isUrlRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,37 @@ describe("isUrlRequest()", () => {
[["//google.com"], false, "should be negative for scheme-agnostic urls"],
[["http://google.com"], false, "should be negative for http urls"],
[["https://google.com"], false, "should be negative for https urls"],
[["chrome-extension://"], false, "should be negative for https urls"],
[["moz-extension://"], false, "should be negative for https urls"],
[["ms-browser-extension://"], false, "should be negative for https urls"],

[["chrome-extension://"], false, "should be negative for nonstandard urls"],
[["moz-extension://"], false, "should be negative for nonstandard urls"],
[["ms-browser-extension://"], false, "should be negative for nonstandard urls"],
[["custom-extension://"], false, "should be negative for nonstandard urls"],

[["path/to/thing"], true, "should be positive for implicit relative urls"],
[["./path/to/thing"], true, "should be positive for explicit relative urls"],
[["~path/to/thing"], true, "should be positive for module urls (with ~)"],
[["some/other/stuff/and/then~path/to/thing"], true, "should be positive for module urls with path prefix"],
[["./some/other/stuff/and/then~path/to/thing"], true, "should be positive for module urls with relative path prefix"],

// with root (normal path)
[["path/to/thing", "root/dir"], true, "should be positive with root if implicit relative url"],
[["./path/to/thing", "root/dir"], true, "should be positive with root if explicit relative url"],
[["/path/to/thing", "root/dir"], true, "should be positive with root if root-relative url"],

// with root (boolean)
[["/path/to/thing", true], true, "should be positive for root-relative if root = `true`"],

// with root (boolean) on Windows
[["C:\\path\\to\\thing"], true, "should be positive for Windows absolute paths with drive letter"],
[["\\\\?\\UNC\\ComputerName\\path\\to\\thing"], true, "should be positive for Windows absolute UNC paths"],

// with root (module)
[["/path/to/thing", "~"], true, "should be positive for module url if root = ~"],

// with root (module path)
[["/path/to/thing", "~module"], true, "should be positive for module prefixes when root starts with ~"],
[["/path/to/thing", "~module/"], true, "should be positive for module prefixes (with trailing slash) when root starts with ~"],

// error cases
[["/path/to/thing", 1], new ExpectedError(/unexpected parameters/i), "should throw an error on invalid root"],

Expand Down