Skip to content

Commit 5f11a9f

Browse files
authoredOct 20, 2017
Merge pull request #98 from gflandre/fix-web-extensions-protocols
Fix URLs for web extension protocols for edge and firefox wrongly detected as requests
2 parents a18d1a4 + d8a9bdc commit 5f11a9f

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed
 

‎lib/isUrlRequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function isUrlRequest(url, root) {
55
// 1. it's a Data Url
66
// 2. it's an absolute url or and protocol-relative
77
// 3. it's some kind of url for a template
8-
if(/^data:|^chrome-extension:|^(https?:)?\/\/|^[\{\}\[\]#*;,'§\$%&\(=?`´\^°<>]/.test(url)) return false;
8+
if(/^data:|^chrome-extension:|^moz-extension:|^ms-browser-extension:|^(https?:)?\/\/|^[\{\}\[\]#*;,'§\$%&\(=?`´\^°<>]/.test(url)) return false;
99
// 4. It's also not an request if root isn't set and it's a root-relative url
1010
if((root === undefined || root === false) && /^\//.test(url)) return false;
1111
return true;

‎test/isUrlRequest.test.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"use strict";
2+
3+
const assert = require("assert");
4+
const loaderUtils = require("../");
5+
6+
function ExpectedError(regex) {
7+
this.regex = regex;
8+
}
9+
ExpectedError.prototype.matches = function(err) {
10+
return this.regex.test(err.message);
11+
};
12+
13+
describe("isUrlRequest()", () => {
14+
[
15+
// without root
16+
[["//google.com"], false, "should be negative for scheme-agnostic urls"],
17+
[["http://google.com"], false, "should be negative for http urls"],
18+
[["https://google.com"], false, "should be negative for https urls"],
19+
[["chrome-extension://"], false, "should be negative for https urls"],
20+
[["moz-extension://"], false, "should be negative for https urls"],
21+
[["ms-browser-extension://"], false, "should be negative for https urls"],
22+
[["path/to/thing"], true, "should be positive for implicit relative urls"],
23+
[["./path/to/thing"], true, "should be positive for explicit relative urls"],
24+
[["~path/to/thing"], true, "should be positive for module urls (with ~)"],
25+
[["some/other/stuff/and/then~path/to/thing"], true, "should be positive for module urls with path prefix"],
26+
[["./some/other/stuff/and/then~path/to/thing"], true, "should be positive for module urls with relative path prefix"],
27+
// with root (normal path)
28+
[["path/to/thing", "root/dir"], true, "should be positive with root if implicit relative url"],
29+
[["./path/to/thing", "root/dir"], true, "should be positive with root if explicit relative url"],
30+
[["/path/to/thing", "root/dir"], true, "should be positive with root if root-relative url"],
31+
// with root (boolean)
32+
[["/path/to/thing", true], true, "should be positive for root-relative if root = `true`"],
33+
// with root (boolean) on Windows
34+
[["C:\\path\\to\\thing"], true, "should be positive for Windows absolute paths with drive letter"],
35+
[["\\\\?\\UNC\\ComputerName\\path\\to\\thing"], true, "should be positive for Windows absolute UNC paths"],
36+
// with root (module)
37+
[["/path/to/thing", "~"], true, "should be positive for module url if root = ~"],
38+
// with root (module path)
39+
[["/path/to/thing", "~module"], true, "should be positive for module prefixes when root starts with ~"],
40+
[["/path/to/thing", "~module/"], true, "should be positive for module prefixes (with trailing slash) when root starts with ~"],
41+
// error cases
42+
[["/path/to/thing", 1], new ExpectedError(/unexpected parameters/i), "should throw an error on invalid root"],
43+
44+
// empty url
45+
[[""], true, "should be positive if url is empty"]
46+
].forEach((test) => {
47+
it(test[2], () => {
48+
const expected = test[1];
49+
try {
50+
const request = loaderUtils.isUrlRequest.apply(loaderUtils, test[0]);
51+
assert.equal(request, expected);
52+
} catch(e) {
53+
if(expected instanceof ExpectedError) {
54+
assert.ok(expected.matches(e));
55+
} else {
56+
assert.ok(false, "should not have thrown an error: " + e.message);
57+
}
58+
}
59+
});
60+
});
61+
});

‎yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,11 @@ es6-weak-map@^2.0.1:
596596
es6-iterator "2"
597597
es6-symbol "3"
598598

599-
escape-string-regexp@1.0.2:
599+
escape-string-regexp@1.0.2, escape-string-regexp@^1.0.2:
600600
version "1.0.2"
601601
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz#4dbc2fe674e71949caf3fb2695ce7f2dc1d9a8d1"
602602

603-
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
603+
escape-string-regexp@^1.0.5:
604604
version "1.0.5"
605605
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
606606

0 commit comments

Comments
 (0)
Please sign in to comment.