Gathering detailed insights and metrics for @vinujan59/csp-html-webpack-plugin
Gathering detailed insights and metrics for @vinujan59/csp-html-webpack-plugin
Gathering detailed insights and metrics for @vinujan59/csp-html-webpack-plugin
Gathering detailed insights and metrics for @vinujan59/csp-html-webpack-plugin
A plugin which, when combined with HTMLWebpackPlugin, adds CSP tags to the HTML output.
npm install @vinujan59/csp-html-webpack-plugin
Typescript
Module System
Node Version
NPM Version
Add webpack compilation object to processFn
Updated on Jan 12, 2021
HTML Parsing Fixes
Updated on Dec 23, 2020
Deprecating webpack@2&3, html-webpack-plugin@2&3, Bumping deps
Updated on Dec 17, 2020
Node@8 deprecated, ability to overwrite default process function, bumped deps
Updated on Apr 09, 2020
Bumping NPM Deps
Updated on Oct 29, 2019
Upgrading Deps again
Updated on Jul 30, 2019
JavaScript (96.21%)
HTML (3.79%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
165 Stars
115 Commits
41 Forks
76 Watchers
13 Branches
24 Contributors
Updated on Apr 03, 2025
Latest Version
5.1.10
Package Id
@vinujan59/csp-html-webpack-plugin@5.1.10
Unpacked Size
71.40 kB
Size
15.39 kB
File Count
24
NPM Version
9.5.1
Node Version
18.16.1
Published on
Feb 22, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
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.
All inline JS and CSS will be hashed and inserted into the policy.
Install the plugin with npm:
1npm i --save-dev csp-html-webpack-plugin
Include the following in your webpack config:
1const HtmlWebpackPlugin = require('html-webpack-plugin'); 2const CspHtmlWebpackPlugin = require('csp-html-webpack-plugin'); 3 4module.exports = { 5 // rest of webpack config 6 7 plugins: [ 8 new HtmlWebpackPlugin() 9 new CspHtmlWebpackPlugin({ 10 // config here, see below 11 }) 12 ] 13}
By default, the csp-html-webpack-plugin
has a very lax policy. You should configure it for your needs.
A good starting policy would be the following:
new CspHtmlWebpackPlugin({
'script-src': '',
'style-src': ''
});
Although we're configuring script-src
and style-src
to be blank, the CSP plugin will scan your HTML
generated in html-webpack-plugin
for external/inline script and style tags, and will add the appropriate
hashes and nonces to your CSP policy. This configuration will also add a base-uri
and object-src
entry
that exist in the default policy:
<meta http-equiv="Content-Security-Policy" content="
base-uri 'self';
object-src 'none';
script-src 'sha256-0Tumwf1AbPDHZO4kdvXUd4c5PiHwt55hre+RDxj9O3Q='
'nonce-hOlyTAhW5QI5p+rv9VUPZg==';
style-src 'sha256-zfLUTOi9wwJktpDIoBZQecK4DNIVxW8Tl0cadROvQgo='
">
This configuration should work for most use cases, and will provide a strong layer of extra security.
CspHtmlWebpackPlugin
This CspHtmlWebpackPlugin
accepts 2 params with the following structure:
{object}
Policy (optional) - a flat object which defines your CSP policy. Valid keys and values can be found on the MDN CSP page. Values can either be a string, or an array of strings.{object}
Additional Options (optional) - a flat object with the optional configuration options:
{boolean|Function}
enabled - if false, or the function returns false, the empty CSP tag will be stripped from the html output.
htmlPluginData
is passed into the function as it's first param.enabled
is set the false, it will disable generating a CSP for all instances of HtmlWebpackPlugin
in your webpack config.{string}
hashingMethod - accepts 'sha256', 'sha384', 'sha512' - your node version must also accept this hashing method.{object}
hashEnabled - a <string, boolean>
entry for which policy rules are allowed to include hashes{object}
nonceEnabled - a <string, boolean>
entry for which policy rules are allowed to include nonces{Function}
processFn - allows the developer to overwrite the default method of what happens to the CSP after it has been created
builtPolicy
: a string
containing the completed policy;htmlPluginData
: the HtmlWebpackPlugin
object
;$
: the cheerio
object of the html file currently being processedcompilation
: Internal webpack object to manipulate the buildHtmlWebpackPlugin
The plugin also adds a new config option onto each HtmlWebpackPlugin
instance:
{object}
cspPlugin - an object containing the following properties:
{boolean}
enabled - if false, the CSP tag will be removed from the HTML which this HtmlWebpackPlugin instance is generating.{object}
policy - A custom policy which should be applied only to this instance of the HtmlWebpackPlugin{object}
hashEnabled - a <string, boolean>
entry for which policy rules are allowed to include hashes{object}
nonceEnabled - a <string, boolean>
entry for which policy rules are allowed to include nonces{Function}
processFn - allows the developer to overwrite the default method of what happens to the CSP after it has been created
builtPolicy
: a string
containing the completed policy;htmlPluginData
: the HtmlWebpackPlugin
object
;$
: the cheerio
object of the html file currently being processedcompilation
: Internal webpack object to manipulate the buildYou don't have to include the same policy / hashEnabled
/ nonceEnabled
configuration object in both HtmlWebpackPlugin
and CspHtmlWebpackPlugin
.
CspHtmlWebpackPlugin
will be applied to all instances of HtmlWebpackPlugin
.HtmlWebpackPlugin
instantiation will only be applied to that instance.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.
> HtmlWebpackPlugin cspPlugin.policy
> CspHtmlWebpackPlugin policy
> CspHtmlWebpackPlugin defaultPolicy
1{ 2 'base-uri': "'self'", 3 'object-src': "'none'", 4 'script-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"], 5 'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"] 6};
1{ 2 enabled: true 3 hashingMethod: 'sha256', 4 hashEnabled: { 5 'script-src': true, 6 'style-src': true 7 }, 8 nonceEnabled: { 9 'script-src': true, 10 'style-src': true 11 }, 12 processFn: defaultProcessFn 13}
1new HtmlWebpackPlugin({
2 cspPlugin: {
3 enabled: true,
4 policy: {
5 'base-uri': "'self'",
6 'object-src': "'none'",
7 'script-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
8 'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"]
9 },
10 hashEnabled: {
11 'script-src': true,
12 'style-src': true
13 },
14 nonceEnabled: {
15 'script-src': true,
16 'style-src': true
17 },
18 processFn: defaultProcessFn // defined in the plugin itself
19 }
20});
21
22new CspHtmlWebpackPlugin({
23 'base-uri': "'self'",
24 'object-src': "'none'",
25 'script-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
26 'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"]
27}, {
28 enabled: true,
29 hashingMethod: 'sha256',
30 hashEnabled: {
31 'script-src': true,
32 'style-src': true
33 },
34 nonceEnabled: {
35 'script-src': true,
36 'style-src': true
37 },
38 processFn: defaultProcessFn // defined in the plugin itself
39})
Some specific directives require the CSP to be sent to the client via a response header (e.g. report-uri
and report-to
)
You can set your own processFn
callback to make this happen.
In your webpack config:
1const RawSource = require('webpack-sources').RawSource; 2 3function generateNginxHeaderFile( 4 builtPolicy, 5 _htmlPluginData, 6 _obj, 7 compilation 8) { 9 const header = 10 'add_header Content-Security-Policy "' + 11 builtPolicy + 12 '; report-uri /csp-report/ ";'; 13 compilation.emitAsset('nginx-csp-header.conf', new RawSource(header)); 14} 15 16module.exports = { 17 {...}, 18 plugins: [ 19 new CspHtmlWebpackPlugin( 20 {...}, { 21 processFn: generateNginxHeaderFile 22 }) 23 ] 24};
In your nginx config:
1location / { 2 ... 3 include /path/to/webpack/output/nginx-csp-header.conf 4}
Contributions are most welcome! Please see the included contributing file for more information.
This project is licensed under MIT. Please see the included license file for more information.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/7 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
27 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More