@@ -17,15 +17,15 @@ All inline JS and CSS will be hashed and inserted into the policy.
1717
1818Install the plugin with npm:
1919
20- ```
20+ ``` bash
2121npm i --save-dev csp-html-webpack-plugin
2222```
2323
2424## Basic Usage
2525
2626Include the following in your webpack config:
2727
28- ```
28+ ``` js
2929const HtmlWebpackPlugin = require (' html-webpack-plugin' );
3030const CspHtmlWebpackPlugin = require (' csp-html-webpack-plugin' );
3131
@@ -90,6 +90,7 @@ This `CspHtmlWebpackPlugin` accepts 2 params with the following structure:
9090 - ` builtPolicy ` : a ` string ` containing the completed policy;
9191 - ` htmlPluginData ` : the ` HtmlWebpackPlugin ` ` object ` ;
9292 - ` $ ` : the ` cheerio ` object of the html file currently being processed
93+ - ` compilation ` : Internal webpack object to manipulate the build
9394
9495### ` HtmlWebpackPlugin `
9596
@@ -105,6 +106,7 @@ The plugin also adds a new config option onto each `HtmlWebpackPlugin` instance:
105106 - ` builtPolicy ` : a ` string ` containing the completed policy;
106107 - ` htmlPluginData ` : the ` HtmlWebpackPlugin ` ` object ` ;
107108 - ` $ ` : the ` cheerio ` object of the html file currently being processed
109+ - ` compilation ` : Internal webpack object to manipulate the build
108110
109111### Order of Precedence:
110112
@@ -125,7 +127,7 @@ In the case where a config object is defined in multiple places, it will be merg
125127
126128#### Default Policy:
127129
128- ```
130+ ``` js
129131{
130132 ' base-uri' : " 'self'" ,
131133 ' object-src' : " 'none'" ,
@@ -136,7 +138,7 @@ In the case where a config object is defined in multiple places, it will be merg
136138
137139#### Default Additional Options:
138140
139- ```
141+ ``` js
140142{
141143 enabled: true
142144 hashingMethod: ' sha256' ,
@@ -154,7 +156,7 @@ In the case where a config object is defined in multiple places, it will be merg
154156
155157#### Full Default Configuration:
156158
157- ```
159+ ``` js
158160new HtmlWebpackPlugin ({
159161 cspPlugin: {
160162 enabled: true ,
@@ -195,7 +197,49 @@ new CspHtmlWebpackPlugin({
195197 processFn: defaultProcessFn // defined in the plugin itself
196198})
197199```
200+ ## Advanced Usage
201+ ### Generating a file containing the CSP directives
198202
203+ Some specific directives require the CSP to be sent to the client via a response header (e.g. ` report-uri ` and ` report-to ` )
204+ You can set your own ` processFn ` callback to make this happen.
205+
206+ #### nginx
207+
208+ In your webpack config:
209+
210+ ``` js
211+ const RawSource = require (' webpack-sources' ).RawSource ;
212+
213+ function generateNginxHeaderFile (
214+ builtPolicy ,
215+ _htmlPluginData ,
216+ _obj ,
217+ compilation
218+ ) {
219+ const header =
220+ ' add_header Content-Security-Policy "' +
221+ builtPolicy +
222+ ' ; report-uri /csp-report/ ";' ;
223+ compilation .emitAsset (' nginx-csp-header.conf' , new RawSource (header));
224+ }
225+
226+ module .exports = {
227+ {... },
228+ plugins: [
229+ new CspHtmlWebpackPlugin (
230+ {... }, {
231+ processFn: generateNginxHeaderFile
232+ })
233+ ]
234+ };
235+ ```
236+ In your nginx config:
237+ ``` nginx
238+ location / {
239+ ...
240+ include /path/to/webpack/output/nginx-csp-header.conf
241+ }
242+ ```
199243## Contribution
200244
201245Contributions are most welcome! Please see the included contributing file for more information.
0 commit comments