From 4f48a39e5738a5d431be2bec39c1b1f0de800d57 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Wed, 1 Apr 2020 13:02:43 +0200 Subject: [PATCH] fix: Fix querystring encoding (#1386) fix #1355 --- index.js | 27 ++++++++++++++++++++++++++- spec/basic.spec.js | 12 ++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 764e0fc6..7758eeb6 100644 --- a/index.js +++ b/index.js @@ -939,10 +939,35 @@ class HtmlWebpackPlugin { * Encode each path component using `encodeURIComponent` as files can contain characters * which needs special encoding in URLs like `+ `. * + * Valid filesystem characters which need to be encoded for urls: + * + * # pound, % percent, & ampersand, { left curly bracket, } right curly bracket, + * \ back slash, < left angle bracket, > right angle bracket, * asterisk, ? question mark, + * blank spaces, $ dollar sign, ! exclamation point, ' single quotes, " double quotes, + * : colon, @ at sign, + plus sign, ` backtick, | pipe, = equal sign + * + * However the query string must not be encoded: + * + * fo:demonstration-path/very fancy+name.js?path=/home?value=abc&value=def#zzz + * ^ ^ ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^ + * | | | | | | | || | | | | | + * encoded | | encoded | | || | | | | | + * ignored ignored ignored ignored ignored + * * @param {string} filePath */ urlencodePath (filePath) { - return filePath.split('/').map(encodeURIComponent).join('/'); + // People use the filepath in quite unexpected ways. + // Try to extract the first querystring of the url: + // + // some+path/demo.html?value=abc?def + // + const queryStringStart = filePath.indexOf('?'); + const urlPath = queryStringStart === -1 ? filePath : filePath.substr(0, queryStringStart); + const queryString = filePath.substr(urlPath.length); + // Encode all parts except '/' which are not part of the querystring: + const encodedUrlPath = urlPath.split('/').map(encodeURIComponent).join('/'); + return encodedUrlPath + queryString; } /** diff --git a/spec/basic.spec.js b/spec/basic.spec.js index 5e95725a..769fa0ae 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -116,6 +116,18 @@ describe('HtmlWebpackPlugin', () => { }, [/[\s]*