Skip to content

Commit 4f48a39

Browse files
authoredApr 1, 2020
fix: Fix querystring encoding (#1386)
fix #1355
1 parent ae85345 commit 4f48a39

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
 

‎index.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -939,10 +939,35 @@ class HtmlWebpackPlugin {
939939
* Encode each path component using `encodeURIComponent` as files can contain characters
940940
* which needs special encoding in URLs like `+ `.
941941
*
942+
* Valid filesystem characters which need to be encoded for urls:
943+
*
944+
* # pound, % percent, & ampersand, { left curly bracket, } right curly bracket,
945+
* \ back slash, < left angle bracket, > right angle bracket, * asterisk, ? question mark,
946+
* blank spaces, $ dollar sign, ! exclamation point, ' single quotes, " double quotes,
947+
* : colon, @ at sign, + plus sign, ` backtick, | pipe, = equal sign
948+
*
949+
* However the query string must not be encoded:
950+
*
951+
* fo:demonstration-path/very fancy+name.js?path=/home?value=abc&value=def#zzz
952+
* ^ ^ ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^
953+
* | | | | | | | || | | | | |
954+
* encoded | | encoded | | || | | | | |
955+
* ignored ignored ignored ignored ignored
956+
*
942957
* @param {string} filePath
943958
*/
944959
urlencodePath (filePath) {
945-
return filePath.split('/').map(encodeURIComponent).join('/');
960+
// People use the filepath in quite unexpected ways.
961+
// Try to extract the first querystring of the url:
962+
//
963+
// some+path/demo.html?value=abc?def
964+
//
965+
const queryStringStart = filePath.indexOf('?');
966+
const urlPath = queryStringStart === -1 ? filePath : filePath.substr(0, queryStringStart);
967+
const queryString = filePath.substr(urlPath.length);
968+
// Encode all parts except '/' which are not part of the querystring:
969+
const encodedUrlPath = urlPath.split('/').map(encodeURIComponent).join('/');
970+
return encodedUrlPath + queryString;
946971
}
947972

948973
/**

‎spec/basic.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ describe('HtmlWebpackPlugin', () => {
116116
}, [/<body>[\s]*<script src="foo\/very%20fancy%2Bname.js"><\/script>[\s]*<\/body>/], null, done);
117117
});
118118

119+
it('properly encodes file names in emitted URIs but keeps the querystring', done => {
120+
testHtmlPlugin({
121+
mode: 'production',
122+
entry: path.join(__dirname, 'fixtures/index.js'),
123+
output: {
124+
path: OUTPUT_DIR,
125+
filename: 'fo:o/very fancy+file-name.js?path=/home?value=abc&value=def#zzz'
126+
},
127+
plugins: [new HtmlWebpackPlugin()]
128+
}, ['<script src="fo%3Ao/very%20fancy%2Bfile-name.js?path=/home?value=abc&value=def#zzz">'], null, done);
129+
});
130+
119131
it('generates a default index.html file with multiple entry points', done => {
120132
testHtmlPlugin({
121133
mode: 'production',

0 commit comments

Comments
 (0)
Please sign in to comment.