Skip to content

Commit 4b3f7fd

Browse files
authored
Make V3 the new master version of this plugin (#31)
* Allow different policies on individual HtmlWebpackPlugin instances (#26) * renaming disableCspPlugin to cspPlugin.enabled to be more inline with the main enabled setting * Adding the option to allow individual policies on a specific html webpack plugin instance * Updating README to reflect the new changes * Adding nonce attrs to external scripts where their host hasnt been defined in the CSP already (#27) * Adding nonce attrs to external scripts where their host hasnt been defined in the CSP already * Making sure that nonces are included when strict-dynamic is set, even if the domain has been whitelisted. Also validating static sources * If plugin is disabled, we should not modify the html at all (#28) * If plugin is disabled, we should not modify the html at all * Updating readme to reflect new recommendation of not including a blank meta tag - it will be added for us * 3.0.0-beta.1 * Fine Grain control for hashes and nonces (#29) * Changing devAllowUnsafe to be more fine-grain by allowing the dev to decide when to allow hashes and nonces * Updating readme to reflect new options * 3.0.0-beta.2
1 parent 6da3843 commit 4b3f7fd

File tree

6 files changed

+835
-115
lines changed

6 files changed

+835
-115
lines changed

README.md

+64-12
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ This plugin will generate meta content for your Content Security Policy tag and
1212

1313
All inline JS and CSS will be hashed, and inserted into the policy.
1414

15-
1615
## Installation
1716

1817
Install the plugin with npm:
18+
1919
```
2020
npm i --save-dev csp-html-webpack-plugin
2121
```
@@ -29,21 +29,36 @@ new HtmlWebpackPlugin()
2929
new CspHtmlWebpackPlugin()
3030
```
3131

32-
Finally, add the following tag to your HTML template where you would like to add the CSP policy:
33-
```
34-
<meta http-equiv="Content-Security-Policy" content="">
35-
```
36-
3732
## Configuration
3833

3934
This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
40-
* `{object}` Policy (optional) - a flat object which defines your CSP policy. Valid keys and values can be found on the [MDN CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) page. Values can either be a string or an array of strings.
41-
* `{object}` Additional Options (optional) - a flat object with the optional configuration options:
42-
* `{boolean}` devAllowUnsafe - if you as the developer want to allow `unsafe-inline`/`unsafe-eval` and _not_ include hashes for inline scripts. If any hashes are included in the policy, modern browsers ignore the `unsafe-inline` rule.
43-
* `{boolean|Function}` enabled - if false, or the function returns false, the empty CSP tag will be stripped from the html output. The `htmlPluginData` is passed into the function as it's first param.
44-
* `{string}` hashingMethod - accepts 'sha256', 'sha384', 'sha512' - your node version must also accept this hashing method.
4535

46-
_Note: CSP runs on all files created by HTMLWebpackPlugin. You can disable it for a particular instance by setting `disableCspPlugin` to `true` in the HTMLWebpackPlugin options
36+
- `{object}` Policy (optional) - a flat object which defines your CSP policy. Valid keys and values can be found on the [MDN CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) page. Values can either be a string or an array of strings.
37+
- `{object}` Additional Options (optional) - a flat object with the optional configuration options:
38+
- `{boolean|Function}` enabled - if false, or the function returns false, the empty CSP tag will be stripped from the html output.
39+
- The `htmlPluginData` is passed into the function as it's first param.
40+
- If `enabled` is set the false, it will disable generating a CSP for all instances of `HtmlWebpackPlugin` in your webpack config.
41+
- `{string}` hashingMethod - accepts 'sha256', 'sha384', 'sha512' - your node version must also accept this hashing method.
42+
- `{object}` hashEnabled - a `<string, boolean>` entry for which policy rules are allowed to include hashes
43+
- `{object}` nonceEnabled - a `<string, boolean>` entry for which policy rules are allowed to include nonces
44+
45+
The plugin also adds a new config option onto each `HtmlWebpackPlugin` instance:
46+
47+
- `{object}` cspPlugin - an object containing the following properties:
48+
- `{boolean}` enabled - if false, the CSP tag will be removed from the HTML which this HtmlWebpackPlugin instance is generating.
49+
- `{object}` policy - A custom policy which should be applied only to this instance of the HtmlWebpackPlugin
50+
- `{object}` hashEnabled - a `<string, boolean>` entry for which policy rules are allowed to include hashes
51+
- `{object}` nonceEnabled - a `<string, boolean>` entry for which policy rules are allowed to include nonces
52+
53+
Note that policies and `hashEnabled` / `nonceEnabled` are merged in the following order:
54+
55+
```
56+
> HtmlWebpackPlugin cspPlugin.policy
57+
> CspHtmlWebpackPlugin policy
58+
> CspHtmlWebpackPlugin defaultPolicy
59+
```
60+
61+
If 2 policies have the same key/policy rule, the former policy will override the latter policy. Entries in a specific rule will not be merged; they will be replaced.
4762

4863
#### Default Policy:
4964

@@ -63,11 +78,40 @@ _Note: CSP runs on all files created by HTMLWebpackPlugin. You can disable it fo
6378
devAllowUnsafe: false,
6479
enabled: true
6580
hashingMethod: 'sha256',
81+
hashEnabled: {
82+
'script-src': true,
83+
'style-src': true
84+
},
85+
nonceEnabled: {
86+
'script-src': true,
87+
'style-src': true
88+
}
6689
}
6790
```
6891

6992
#### Full Configuration with all options:
93+
7094
```
95+
new HtmlWebpackPlugin({
96+
cspPlugin: {
97+
enabled: true,
98+
policy: {
99+
'base-uri': "'self'",
100+
'object-src': "'none'",
101+
'script-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
102+
'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"]
103+
},
104+
hashEnabled: {
105+
'script-src': true,
106+
'style-src': true
107+
},
108+
nonceEnabled: {
109+
'script-src': true,
110+
'style-src': true
111+
}
112+
}
113+
});
114+
71115
new CspHtmlWebpackPlugin({
72116
'base-uri': "'self'",
73117
'object-src': "'none'",
@@ -77,6 +121,14 @@ new CspHtmlWebpackPlugin({
77121
devAllowUnsafe: false,
78122
enabled: true
79123
hashingMethod: 'sha256',
124+
hashEnabled: {
125+
'script-src': true,
126+
'style-src': true
127+
},
128+
nonceEnabled: {
129+
'script-src': true,
130+
'style-src': true
131+
}
80132
})
81133
```
82134

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "csp-html-webpack-plugin",
3-
"version": "2.5.0",
3+
"version": "3.0.0-beta.2",
44
"description": "A plugin which, when combined with HTMLWebpackPlugin, adds CSP tags to the HTML output",
55
"main": "plugin.js",
66
"scripts": {
77
"eslint": "eslint .",
88
"eslint:fix": "eslint . --fix",
99
"jest": "jest --config=./jest.config.js plugin.jest.js",
10-
"jest:watch": "jest --watch --config=./jest.config.js plugin.jest.js",
10+
"jest:watch": "jest --watch --verbose=false --config=./jest.config.js plugin.jest.js",
1111
"jest:coverage:generate": "jest --coverage --config=./jest.config.js plugin.jest.js",
1212
"jest:coverage:clean": "rm -rf ./coverage",
1313
"jest:coverage:upload": "npx codecov --token=252086ef-c14d-4f29-ab36-720265249fa2",

0 commit comments

Comments
 (0)