@@ -46,6 +46,17 @@ const defaultProcessFn = (builtPolicy, htmlPluginData, $) => {
4646 : $ . html ( ) ;
4747} ;
4848
49+ const convert = ( keys , value ) =>
50+ typeof value !== 'boolean'
51+ ? value
52+ : keys . reduce (
53+ ( previousValue , currentValue ) => ( {
54+ ...previousValue ,
55+ [ currentValue ] : value ,
56+ } ) ,
57+ { }
58+ ) ;
59+
4960const defaultPolicy = {
5061 'base-uri' : "'self'" ,
5162 'object-src' : "'none'" ,
@@ -56,14 +67,8 @@ const defaultPolicy = {
5667const defaultAdditionalOpts = {
5768 enabled : true ,
5869 hashingMethod : 'sha256' ,
59- hashEnabled : {
60- 'script-src' : true ,
61- 'style-src' : true ,
62- } ,
63- nonceEnabled : {
64- 'script-src' : true ,
65- 'style-src' : true ,
66- } ,
70+ hashEnabled : true ,
71+ nonceEnabled : true ,
6772 processFn : defaultProcessFn ,
6873} ;
6974
@@ -112,14 +117,22 @@ class CspHtmlWebpackPlugin {
112117 this . validatePolicy ( compilation ) ;
113118
114119 // 2. Lets set which hashes and nonces are enabled for this HtmlWebpackPlugin instance
120+ const policyKeys = Object . keys ( this . policy ) ;
121+
115122 this . hashEnabled = Object . freeze ( {
116- ...this . opts . hashEnabled ,
117- ...get ( htmlPluginData , 'plugin.options.cspPlugin.hashEnabled' , { } ) ,
123+ ...convert ( policyKeys , this . opts . hashEnabled ) ,
124+ ...convert (
125+ policyKeys ,
126+ get ( htmlPluginData , 'plugin.options.cspPlugin.hashEnabled' , { } )
127+ ) ,
118128 } ) ;
119129
120130 this . nonceEnabled = Object . freeze ( {
121- ...this . opts . nonceEnabled ,
122- ...get ( htmlPluginData , 'plugin.options.cspPlugin.nonceEnabled' , { } ) ,
131+ ...convert ( policyKeys , this . opts . nonceEnabled ) ,
132+ ...convert (
133+ policyKeys ,
134+ get ( htmlPluginData , 'plugin.options.cspPlugin.nonceEnabled' , { } )
135+ ) ,
123136 } ) ;
124137
125138 // 3. Get the processFn for this HtmlWebpackPlugin instance.
0 commit comments