Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const cheerio = require('cheerio');
const crypto = require('crypto');
const uniq = require('lodash/uniq');
const compact = require('lodash/compact');
const flatten = require('lodash/flatten');
const isFunction = require('lodash/isFunction');

const defaultPolicy = {
Expand Down Expand Up @@ -114,8 +115,13 @@ class CspHtmlWebpackPlugin {
.map((i, element) => this.hash($(element).html()))
.get();

policyObj['script-src'] = policyObj['script-src'].concat(inlineSrc);
policyObj['style-src'] = policyObj['style-src'].concat(inlineStyle);
// Wrapped in flatten([]) to handle both when policy is a string and an array
Copy link
Contributor Author

@wanecek wanecek Mar 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two alternative ways to solve the issue without relying on lodash/flatten are:

Just straight up, but a bit difficult to read imo

policyObj['script-src'] = Array.isArray(policyObj['script-src']) ? policyObj['script-src'].concat(inlineSrc) : [policyObj['script-src'], inlineSrc]; policyObj['style-src'] = Array.isArray(policyObj['style-src']) ? policyObj['style-src'].concat(inlineStyle) : [policyObj['style-src'], inlineStyle];

or

creating a helper function that I can't come up with a good name for 😂

// Somewhere in plugin.js, e.g. line 19. function castToArray(strOrArray) { return Array.isArray(strOrArray) ? strOrArray : [strOrArray]; } // and at line 119 policyObj['script-src'] = castToArray(policyObj['script-src']).concat(inlineSrc); policyObj['style-src'] = castToArray(policyObj['style-src']).concat(inlineStyle);
policyObj['script-src'] = flatten([policyObj['script-src']]).concat(
inlineSrc
);
policyObj['style-src'] = flatten([policyObj['style-src']]).concat(
inlineStyle
);

$('meta[http-equiv="Content-Security-Policy"]').attr(
'content',
Expand Down
35 changes: 35 additions & 0 deletions spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,39 @@ describe('CspHtmlWebpackPlugin', () => {
);
}).toThrow(new Error(`'invalid' is not a valid hashing method`));
});

it('handles string values for policies where the hash is appended', done => {
const webpackConfig = {
entry: path.join(__dirname, 'fixtures/index.js'),
output: { path: OUTPUT_DIR, filename: 'index.bundle.js' },
plugins: [
new HtmlWebpackPlugin({
filename: path.join(OUTPUT_DIR, 'index.html'),
template: path.join(__dirname, 'fixtures', 'with-js.html'),
inject: 'body',
}),
new CspHtmlWebpackPlugin({
'script-src': "'self'",
'style-src': "'self'",
}),
],
};

testCspHtmlWebpackPlugin(
webpackConfig,
'index.html',
(cspPolicy, _, doneFn) => {
const expected =
"base-uri 'self';" +
" object-src 'none';" +
" script-src 'self' 'sha256-9nPWXYBnlIeJ9HmieIATDv9Ab5plt35XZiT48TfEkJI=';" +
" style-src 'self'";

expect(cspPolicy).toEqual(expected);

doneFn();
},
done
);
});
});