You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+64-19Lines changed: 64 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,10 @@
8
8
9
9
## About
10
10
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/).
12
13
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.
14
15
15
16
## Installation
16
17
@@ -22,18 +23,61 @@ npm i --save-dev csp-html-webpack-plugin
22
23
23
24
## Basic Usage
24
25
25
-
In the plugins section of your webpack config file, include the following:
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`
33
77
34
78
This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
35
79
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.
37
81
-`{object}` Additional Options (optional) - a flat object with the optional configuration options:
38
82
-`{boolean|Function}` enabled - if false, or the function returns false, the empty CSP tag will be stripped from the html output.
39
83
- 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:
47
91
-`htmlPluginData`: the `HtmlWebpackPlugin``object`;
48
92
-`$`: the `cheerio` object of the html file currently being processed
49
93
94
+
### `HtmlWebpackPlugin`
95
+
50
96
The plugin also adds a new config option onto each `HtmlWebpackPlugin` instance:
51
97
52
98
-`{object}` cspPlugin - an object containing the following properties:
@@ -60,20 +106,22 @@ The plugin also adds a new config option onto each `HtmlWebpackPlugin` instance:
60
106
-`htmlPluginData`: the `HtmlWebpackPlugin``object`;
61
107
-`$`: the `cheerio` object of the html file currently being processed
62
108
63
-
####Order of Precedence:
109
+
### Order of Precedence:
64
110
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.
66
117
67
118
```
68
119
> HtmlWebpackPlugin cspPlugin.policy
69
120
> CspHtmlWebpackPlugin policy
70
121
> CspHtmlWebpackPlugin defaultPolicy
71
122
```
72
123
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
77
125
78
126
#### Default Policy:
79
127
@@ -104,10 +152,7 @@ in the same webpack config.
104
152
}
105
153
```
106
154
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:
111
156
112
157
```
113
158
new HtmlWebpackPlugin({
@@ -127,7 +172,7 @@ new HtmlWebpackPlugin({
127
172
'script-src': true,
128
173
'style-src': true
129
174
},
130
-
processFn: defaultProcessFn
175
+
processFn: defaultProcessFn // defined in the plugin itself
131
176
}
132
177
});
133
178
@@ -147,7 +192,7 @@ new CspHtmlWebpackPlugin({
147
192
'script-src': true,
148
193
'style-src': true
149
194
},
150
-
processFn: defaultProcessFn
195
+
processFn: defaultProcessFn // defined in the plugin itself
0 commit comments