Skip to content

Commit aca43da

Browse files
fix(isUrlRequest): better handle absolute urls and non standards (#134)
1 parent ba4f0d0 commit aca43da

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

lib/isUrlRequest.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
'use strict';
22

3+
const path = require('path');
4+
35
function isUrlRequest(url, root) {
46
// An URL is not an request if
5-
// 1. it's a Data Url
6-
// 2. it's an absolute url or and protocol-relative
7-
// 3. it's some kind of url for a template
8-
if (
9-
/^data:|^.+-extension:\/|^about:blank$|^(https?:)?\/\/|^[{}[]#*;,'§\$%&\(=?`´\^°<>]/.test(
10-
url
11-
)
12-
) {
7+
8+
// 1. It's an absolute url and it is not `windows` path like `C:\dir\file`
9+
if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !path.win32.isAbsolute(url)) {
10+
return false;
11+
}
12+
13+
// 2. It's a protocol-relative
14+
if (/^\/\//.test(url)) {
15+
return false;
16+
}
17+
18+
// 3. It's some kind of url for a template
19+
if (/^[{}[\]#*;,'§$%&(=?`´^°<>]/.test(url)) {
1320
return false;
1421
}
1522

test/isUrlRequest.test.js

+63
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ describe('isUrlRequest()', () => {
1414
// without root
1515
[['//google.com'], false, 'should be negative for scheme-agnostic urls'],
1616
[['http://google.com'], false, 'should be negative for http urls'],
17+
[['HTTP://google.com'], false, 'should be negative for http urls'],
1718
[['https://google.com'], false, 'should be negative for https urls'],
19+
[['HTTPS://google.com'], false, 'should be negative for https urls'],
1820

1921
[['chrome-extension://'], false, 'should be negative for nonstandard urls'],
2022
[['moz-extension://'], false, 'should be negative for nonstandard urls'],
@@ -26,6 +28,13 @@ describe('isUrlRequest()', () => {
2628
[['custom-extension://'], false, 'should be negative for nonstandard urls'],
2729

2830
[['path/to/thing'], true, 'should be positive for implicit relative urls'],
31+
[['./img.png'], true, 'should be positive for implicit relative urls'],
32+
[['../img.png'], true, 'should be positive for implicit relative urls'],
33+
[
34+
['./img.png?foo=bar#hash'],
35+
true,
36+
'should be positive for implicit relative urls',
37+
],
2938
[
3039
['./path/to/thing'],
3140
true,
@@ -42,6 +51,18 @@ describe('isUrlRequest()', () => {
4251
true,
4352
'should be positive for module urls with relative path prefix',
4453
],
54+
[['C:/thing'], true, 'should be positive for linux path with driver'],
55+
[['C:\\thing'], true, 'should be positive for windows path with driver'],
56+
[
57+
['directory/things'],
58+
true,
59+
'should be positive for relative path (linux)',
60+
],
61+
[
62+
['directory\\things'],
63+
true,
64+
'should be positive for relative path (windows)',
65+
],
4566

4667
// with root (normal path)
4768
[
@@ -110,6 +131,48 @@ describe('isUrlRequest()', () => {
110131

111132
// about url
112133
[['about:blank'], false, 'should be negative for about:blank'],
134+
135+
// hash
136+
[['#gradient'], false, 'should be negative for hash url'],
137+
138+
// url
139+
[['//sindresorhus.com'], false, 'should ignore noscheme url'],
140+
[
141+
['//at.alicdn.com/t/font_515771_emcns5054x3whfr.eot'],
142+
false,
143+
'should ignore noscheme url with path',
144+
],
145+
[
146+
['https://example.com/././foo'],
147+
false,
148+
'should ignore absolute url with relative',
149+
],
150+
151+
// non standard protocols
152+
[
153+
['file://sindresorhus.com'],
154+
false,
155+
'should ignore non standard protocols (file)',
156+
],
157+
[
158+
['mailto:someone@example.com'],
159+
false,
160+
'should ignore non standard protocols (mailto)',
161+
],
162+
[
163+
['data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'],
164+
false,
165+
'should ignore non standard protocols (data)',
166+
],
167+
[
168+
['DATA:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'],
169+
false,
170+
'should ignore non standard protocols (data)',
171+
],
172+
173+
// root-relative url
174+
[['/'], false, 'ignore root-relative url'],
175+
[['//'], false, 'ignore root-relative url 1'],
113176
].forEach((test) => {
114177
it(test[2], () => {
115178
const expected = test[1];

0 commit comments

Comments
 (0)