Skip to content

Commit 1be6906

Browse files
Allow nonceEnabled and hashEnabled to take single boolean values
Providing a single boolean value to either of these options will now apply the value to each provided policy directive. Closes #98
1 parent c000a55 commit 1be6906

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
8383
- The `htmlPluginData` is passed into the function as it's first param.
8484
- If `enabled` is set the false, it will disable generating a CSP for all instances of `HtmlWebpackPlugin` in your webpack config.
8585
- `{string}` hashingMethod - accepts 'sha256', 'sha384', 'sha512' - your node version must also accept this hashing method.
86-
- `{object}` hashEnabled - a `<string, boolean>` entry for which policy rules are allowed to include hashes
87-
- `{object}` nonceEnabled - a `<string, boolean>` entry for which policy rules are allowed to include nonces
86+
- `{boolean|object}` hashEnabled - a `<string, boolean>` entry for which policy rules are allowed to include hashes, or a single boolean value to apply to all policy rules
87+
- `{boolean|object}` nonceEnabled - a `<string, boolean>` entry for which policy rules are allowed to include nonces, or a single boolean value to apply to all policy rules
8888
- `{Function}` processFn - allows the developer to overwrite the default method of what happens to the CSP after it has been created
8989
- Parameters are:
9090
- `builtPolicy`: a `string` containing the completed policy;

plugin.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ const defaultProcessFn = (builtPolicy, htmlPluginData, $) => {
4646
: $.html();
4747
};
4848

49+
const convert = (keys, value) =>
50+
typeof value !== 'boolean'
51+
? value
52+
: keys.reduce(
53+
(previousValue, currentValue) => ({
54+
...previousValue,
55+
[currentValue]: value,
56+
}),
57+
{}
58+
);
59+
4960
const defaultPolicy = {
5061
'base-uri': "'self'",
5162
'object-src': "'none'",
@@ -56,14 +67,8 @@ const defaultPolicy = {
5667
const defaultAdditionalOpts = {
5768
enabled: true,
5869
hashingMethod: 'sha256',
59-
hashEnabled: {
60-
'script-src': true,
61-
'style-src': true,
62-
},
63-
nonceEnabled: {
64-
'script-src': true,
65-
'style-src': true,
66-
},
70+
hashEnabled: true,
71+
nonceEnabled: true,
6772
processFn: defaultProcessFn,
6873
};
6974

@@ -112,14 +117,22 @@ class CspHtmlWebpackPlugin {
112117
this.validatePolicy(compilation);
113118

114119
// 2. Lets set which hashes and nonces are enabled for this HtmlWebpackPlugin instance
120+
const policyKeys = Object.keys(this.policy);
121+
115122
this.hashEnabled = Object.freeze({
116-
...this.opts.hashEnabled,
117-
...get(htmlPluginData, 'plugin.options.cspPlugin.hashEnabled', {}),
123+
...convert(policyKeys, this.opts.hashEnabled),
124+
...convert(
125+
policyKeys,
126+
get(htmlPluginData, 'plugin.options.cspPlugin.hashEnabled', {})
127+
),
118128
});
119129

120130
this.nonceEnabled = Object.freeze({
121-
...this.opts.nonceEnabled,
122-
...get(htmlPluginData, 'plugin.options.cspPlugin.nonceEnabled', {}),
131+
...convert(policyKeys, this.opts.nonceEnabled),
132+
...convert(
133+
policyKeys,
134+
get(htmlPluginData, 'plugin.options.cspPlugin.nonceEnabled', {})
135+
),
123136
});
124137

125138
// 3. Get the processFn for this HtmlWebpackPlugin instance.

0 commit comments

Comments
 (0)