Skip to content

Commit 96491f9

Browse files
authored
Merge pull request #78 from slackhq/an-readme-updates
Updates to the readme to clarify best practices and how to configure the plugin
2 parents 540e339 + 6f20e8b commit 96491f9

File tree

1 file changed

+64
-19
lines changed

1 file changed

+64
-19
lines changed

README.md

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
## About
1010

11-
This plugin will generate meta content for your Content Security Policy tag and input the correct data into your HTML template, generated by [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin/).
11+
This plugin will generate meta content for your [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
12+
tag and input the correct data into your HTML template, generated by [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin/).
1213

13-
All inline JS and CSS will be hashed, and inserted into the policy.
14+
All inline JS and CSS will be hashed and inserted into the policy.
1415

1516
## Installation
1617

@@ -22,18 +23,61 @@ npm i --save-dev csp-html-webpack-plugin
2223

2324
## Basic Usage
2425

25-
In the plugins section of your webpack config file, include the following:
26+
Include the following in your webpack config:
2627

2728
```
28-
new HtmlWebpackPlugin()
29-
new CspHtmlWebpackPlugin()
29+
const HtmlWebpackPlugin = require('html-webpack-plugin');
30+
const CspHtmlWebpackPlugin = require('csp-html-webpack-plugin');
31+
32+
module.exports = {
33+
// rest of webpack config
34+
35+
plugins: [
36+
new HtmlWebpackPlugin()
37+
new CspHtmlWebpackPlugin({
38+
// config here, see below
39+
})
40+
]
41+
}
3042
```
3143

32-
## Configuration
44+
## Recommended Configuration
45+
46+
By default, the `csp-html-webpack-plugin` has a very lax policy. You should configure it for your needs.
47+
48+
A good starting policy would be the following:
49+
50+
```
51+
new CspHtmlWebpackPlugin({
52+
'script-src': '',
53+
'style-src': ''
54+
});
55+
```
56+
57+
Although we're configuring `script-src` and `style-src` to be blank, the CSP plugin will scan your HTML
58+
generated in `html-webpack-plugin` for external/inline script and style tags, and will add the appropriate
59+
hashes and nonces to your CSP policy. This configuration will also add a `base-uri` and `object-src` entry
60+
that exist in the default policy:
61+
62+
```
63+
<meta http-equiv="Content-Security-Policy" content="
64+
base-uri 'self';
65+
object-src 'none';
66+
script-src 'sha256-0Tumwf1AbPDHZO4kdvXUd4c5PiHwt55hre+RDxj9O3Q='
67+
'nonce-hOlyTAhW5QI5p+rv9VUPZg==';
68+
style-src 'sha256-zfLUTOi9wwJktpDIoBZQecK4DNIVxW8Tl0cadROvQgo='
69+
">
70+
```
71+
72+
This configuration should work for most use cases, and will provide a strong layer of extra security.
73+
74+
## All Configuration Options
75+
76+
### `CspHtmlWebpackPlugin`
3377

3478
This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
3579

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.
80+
- `{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.
3781
- `{object}` Additional Options (optional) - a flat object with the optional configuration options:
3882
- `{boolean|Function}` enabled - if false, or the function returns false, the empty CSP tag will be stripped from the html output.
3983
- The `htmlPluginData` is passed into the function as it's first param.
@@ -47,6 +91,8 @@ This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
4791
- `htmlPluginData`: the `HtmlWebpackPlugin` `object`;
4892
- `$`: the `cheerio` object of the html file currently being processed
4993

94+
### `HtmlWebpackPlugin`
95+
5096
The plugin also adds a new config option onto each `HtmlWebpackPlugin` instance:
5197

5298
- `{object}` cspPlugin - an object containing the following properties:
@@ -60,20 +106,22 @@ The plugin also adds a new config option onto each `HtmlWebpackPlugin` instance:
60106
- `htmlPluginData`: the `HtmlWebpackPlugin` `object`;
61107
- `$`: the `cheerio` object of the html file currently being processed
62108

63-
#### Order of Precedence:
109+
### Order of Precedence:
64110

65-
Note that policies and `hashEnabled` / `nonceEnabled` are merged in the following order:
111+
You don't have to include the same policy / `hashEnabled` / `nonceEnabled` configuration object in both `HtmlWebpackPlugin` and `CspHtmlWebpackPlugin`.
112+
113+
- Config included in `CspHtmlWebpackPlugin` will be applied to all instances of `HtmlWebpackPlugin`.
114+
- Config included in a single `HtmlWebpackPlugin` instantiation will only be applied to that instance.
115+
116+
In the case where a config object is defined in multiple places, it will be merged in the order defined below, with former keys overriding latter. This means entries for a specific rule will not be merged; they will be replaced.
66117

67118
```
68119
> HtmlWebpackPlugin cspPlugin.policy
69120
> CspHtmlWebpackPlugin policy
70121
> CspHtmlWebpackPlugin defaultPolicy
71122
```
72123

73-
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.
74-
75-
This is useful if you need different policy rules / processing functions for different `HtmlWebpackPlugin` instances
76-
in the same webpack config.
124+
## Appendix
77125

78126
#### Default Policy:
79127

@@ -104,10 +152,7 @@ in the same webpack config.
104152
}
105153
```
106154

107-
#### Full Configuration with all options:
108-
109-
Note that you don't have to include the same section in both `HtmlWebpackPlugin` and `CspHtmlWebpackPlugin`.
110-
See the [Order of Precedence](#order-of-precedence) section above.
155+
#### Full Default Configuration:
111156

112157
```
113158
new HtmlWebpackPlugin({
@@ -127,7 +172,7 @@ new HtmlWebpackPlugin({
127172
'script-src': true,
128173
'style-src': true
129174
},
130-
processFn: defaultProcessFn
175+
processFn: defaultProcessFn // defined in the plugin itself
131176
}
132177
});
133178
@@ -147,7 +192,7 @@ new CspHtmlWebpackPlugin({
147192
'script-src': true,
148193
'style-src': true
149194
},
150-
processFn: defaultProcessFn
195+
processFn: defaultProcessFn // defined in the plugin itself
151196
})
152197
```
153198

0 commit comments

Comments
 (0)