From aca43dabe807f0559bfbe7c14233ad70a37a42f9 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Tue, 25 Dec 2018 15:20:03 +0300 Subject: [PATCH 1/3] fix(isUrlRequest): better handle absolute urls and non standards (#134) --- lib/isUrlRequest.js | 23 +++++++++----- test/isUrlRequest.test.js | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/lib/isUrlRequest.js b/lib/isUrlRequest.js index c59c218..4592cb1 100644 --- a/lib/isUrlRequest.js +++ b/lib/isUrlRequest.js @@ -1,15 +1,22 @@ 'use strict'; +const path = require('path'); + function isUrlRequest(url, root) { // An URL is not an request if - // 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:|^.+-extension:\/|^about:blank$|^(https?:)?\/\/|^[{}[]#*;,'§\$%&\(=?`´\^°<>]/.test( - url - ) - ) { + + // 1. It's an absolute url and it is not `windows` path like `C:\dir\file` + if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !path.win32.isAbsolute(url)) { + return false; + } + + // 2. It's a protocol-relative + if (/^\/\//.test(url)) { + return false; + } + + // 3. It's some kind of url for a template + if (/^[{}[\]#*;,'§$%&(=?`´^°<>]/.test(url)) { return false; } diff --git a/test/isUrlRequest.test.js b/test/isUrlRequest.test.js index d4d1258..08a00d1 100644 --- a/test/isUrlRequest.test.js +++ b/test/isUrlRequest.test.js @@ -14,7 +14,9 @@ describe('isUrlRequest()', () => { // without root [['//google.com'], false, 'should be negative for scheme-agnostic urls'], [['http://google.com'], false, 'should be negative for http urls'], + [['HTTP://google.com'], false, 'should be negative for http urls'], [['https://google.com'], false, 'should be negative for https urls'], + [['HTTPS://google.com'], 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'], @@ -26,6 +28,13 @@ describe('isUrlRequest()', () => { [['custom-extension://'], false, 'should be negative for nonstandard urls'], [['path/to/thing'], true, 'should be positive for implicit relative urls'], + [['./img.png'], true, 'should be positive for implicit relative urls'], + [['../img.png'], true, 'should be positive for implicit relative urls'], + [ + ['./img.png?foo=bar#hash'], + true, + 'should be positive for implicit relative urls', + ], [ ['./path/to/thing'], true, @@ -42,6 +51,18 @@ describe('isUrlRequest()', () => { true, 'should be positive for module urls with relative path prefix', ], + [['C:/thing'], true, 'should be positive for linux path with driver'], + [['C:\\thing'], true, 'should be positive for windows path with driver'], + [ + ['directory/things'], + true, + 'should be positive for relative path (linux)', + ], + [ + ['directory\\things'], + true, + 'should be positive for relative path (windows)', + ], // with root (normal path) [ @@ -110,6 +131,48 @@ describe('isUrlRequest()', () => { // about url [['about:blank'], false, 'should be negative for about:blank'], + + // hash + [['#gradient'], false, 'should be negative for hash url'], + + // url + [['//sindresorhus.com'], false, 'should ignore noscheme url'], + [ + ['//at.alicdn.com/t/font_515771_emcns5054x3whfr.eot'], + false, + 'should ignore noscheme url with path', + ], + [ + ['https://example.com/././foo'], + false, + 'should ignore absolute url with relative', + ], + + // non standard protocols + [ + ['file://sindresorhus.com'], + false, + 'should ignore non standard protocols (file)', + ], + [ + ['mailto:someone@example.com'], + false, + 'should ignore non standard protocols (mailto)', + ], + [ + ['data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'], + false, + 'should ignore non standard protocols (data)', + ], + [ + ['DATA:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'], + false, + 'should ignore non standard protocols (data)', + ], + + // root-relative url + [['/'], false, 'ignore root-relative url'], + [['//'], false, 'ignore root-relative url 1'], ].forEach((test) => { it(test[2], () => { const expected = test[1]; From 73d350a38c89763c9b11a9cac852583e847b3092 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Tue, 25 Dec 2018 15:26:27 +0300 Subject: [PATCH 2/3] revert: PR #79 (#135) You need run `isUrlRequest` before run `urlToRequest`. --- README.md | 15 +++++++++++++++ lib/urlToRequest.js | 3 --- test/urlToRequest.test.js | 3 --- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 83346c6..34443d2 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,21 @@ loaderUtils.stringifyRequest(this, "\\\\network-drive\\test.js"); Converts some resource URL to a webpack module request. +> i Before call `urlToRequest` you need call `isUrlRequest` to ensure it is requestable url + +```javascript +const url = "path/to/module.js"; + +if (loaderUtils.isUrlRequest(url)) { + // Logic for requestable url + const request = loaderUtils.urlToRequest(url); +} else { + // Logic for not requestable url +} +``` + +Simple example: + ```javascript const url = "path/to/module.js"; const request = loaderUtils.urlToRequest(url); // "./path/to/module.js" diff --git a/lib/urlToRequest.js b/lib/urlToRequest.js index 597115f..e2b60d3 100644 --- a/lib/urlToRequest.js +++ b/lib/urlToRequest.js @@ -41,9 +41,6 @@ function urlToRequest(url, root) { '.' ); } - } else if (/^(?:https?:)?\/\//.test(url)) { - // Preserve http and https urls - request = url; } else if (/^\.\.?\//.test(url)) { // A relative url stays request = url; diff --git a/test/urlToRequest.test.js b/test/urlToRequest.test.js index f7160f2..2d9a17e 100644 --- a/test/urlToRequest.test.js +++ b/test/urlToRequest.test.js @@ -13,9 +13,6 @@ ExpectedError.prototype.matches = function(err) { describe('urlToRequest()', () => { [ // without root - [['//google.com'], '//google.com', 'should handle scheme-agnostic urls'], - [['http://google.com'], 'http://google.com', 'should handle http urls'], - [['https://google.com'], 'https://google.com', 'should handle https urls'], [ ['path/to/thing'], './path/to/thing', From 489ef12a900c9f40c32eb0dceeed1eaab55fcc89 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 25 Dec 2018 15:26:57 +0300 Subject: [PATCH 3/3] chore(release): 1.2.1 --- CHANGELOG.md | 15 +++++++++++++++ package.json | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47798d8..7fa1fd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,21 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +## [1.2.1](https://github.com/webpack/loader-utils/compare/v1.2.0...v1.2.1) (2018-12-25) + + +### Bug Fixes + +* **isUrlRequest:** better handle absolute urls and non standards ([#134](https://github.com/webpack/loader-utils/issues/134)) ([aca43da](https://github.com/webpack/loader-utils/commit/aca43da)) + + +### Reverts + +* PR [#79](https://github.com/webpack/loader-utils/issues/79) ([#135](https://github.com/webpack/loader-utils/issues/135)) ([73d350a](https://github.com/webpack/loader-utils/commit/73d350a)) + + + # [1.2.0](https://github.com/webpack/loader-utils/compare/v1.1.0...v1.2.0) (2018-12-24) diff --git a/package.json b/package.json index 4b389b1..98690a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loader-utils", - "version": "1.2.0", + "version": "1.2.1", "author": "Tobias Koppers @sokra", "description": "utils for webpack loaders", "dependencies": {