Skip to content

Commit ed3c8cd

Browse files
authored
Merge pull request #132 from waysact/issue-131
Fix hash lookup for dynamic chunks
2 parents 1d6bfcd + 3d7090c commit ed3c8cd

File tree

7 files changed

+55
-3
lines changed

7 files changed

+55
-3
lines changed

examples/dynamic-modified/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# With a modified dynamically loaded chunk #hwp
2+
3+
Ensure that when a chunk is modified, it fails to load.

examples/dynamic-modified/corrupt.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('this should never load');

examples/dynamic-modified/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import("./corrupt").then(function error() {
2+
console.log('error');
3+
}).catch(function ok() {
4+
console.log('ok');
5+
});

examples/dynamic-modified/test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var webpackVersionComponents = require('webpack/package.json').version.split(
2+
'.'
3+
);
4+
var webpackVersionMajor = Number(webpackVersionComponents[0]);
5+
6+
var defaultCheck = require("../../test/defaultCheck");
7+
var fs = require('fs');
8+
9+
module.exports.skip = function skip() {
10+
return webpackVersionMajor < 2;
11+
};
12+
13+
module.exports.check = function check(stats, url, browser) {
14+
const otherAsset = Object.keys(stats.compilation.assets).find(key => key !== 'index.js' && key.endsWith(".js"));
15+
fs.writeFileSync('dist/' + otherAsset, 'xxx');
16+
17+
return defaultCheck(stats, url, browser);
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var SriPlugin = require('webpack-subresource-integrity');
2+
var HtmlWebpackPlugin = require('html-webpack-plugin');
3+
4+
module.exports = {
5+
entry: {
6+
index: './index.js'
7+
},
8+
output: {
9+
crossOriginLoading: 'anonymous'
10+
},
11+
plugins: [
12+
new SriPlugin({
13+
hashFuncNames: ['sha256', 'sha384'],
14+
enabled: true
15+
}),
16+
new HtmlWebpackPlugin()
17+
]
18+
};

jmtp.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
var Template = require('webpack/lib/Template');
99
var util = require('./util');
10+
var webpackVersionComponents = require('webpack/package.json').version.split(
11+
'.'
12+
);
13+
var webpackVersionMajor = Number(webpackVersionComponents[0]);
1014

1115
function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) {
1216
this.sriPlugin = sriPlugin;
@@ -47,17 +51,20 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
4751
* Patch jsonp-script code to add the integrity attribute.
4852
*/
4953
WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute =
50-
function addAttribute(mainTemplate, elName, source, chunk) {
54+
function addAttribute(mainTemplate, elName, source) {
5155
const outputOptions = this.compilation.outputOptions || mainTemplate.outputOptions;
5256
if (!outputOptions.crossOriginLoading) {
5357
this.sriPlugin.errorOnce(
5458
this.compilation,
5559
'webpack option output.crossOriginLoading not set, code splitting will not work!'
5660
);
5761
}
62+
5863
return (Template.asString || mainTemplate.asString)([
5964
source,
60-
elName + '.integrity = __webpack_require__.sriHashes[' + (chunk ? `'${chunk.id}'` : 'chunkId') + '];',
65+
elName + '.integrity = __webpack_require__.sriHashes[' +
66+
((webpackVersionMajor >= 5 && elName === 'script') ? 'key.match(/^chunk-([0-9]+)$/)[1]' : 'chunkId') +
67+
'];',
6168
elName + '.crossOrigin = ' + JSON.stringify(outputOptions.crossOriginLoading) + ';',
6269
]);
6370
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-subresource-integrity",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"description": "Webpack plugin for enabling Subresource Integrity",
55
"engines": {
66
"node": ">=4"

0 commit comments

Comments
 (0)