@@ -9,6 +9,9 @@ const PxClient = rewire('../lib/pxclient');
99const PxEnforcer = require ( '../lib/pxenforcer' ) ;
1010const proxyquire = require ( 'proxyquire' ) ;
1111const { ModuleMode } = require ( '../lib/enums/ModuleMode' ) ;
12+ const Response = require ( './mocks/response' ) ;
13+ const addNonce = require ( '../lib/nonce' ) ;
14+ const { CSP_HEADER , CSPRO_HEADER } = require ( '../lib/utils/constants' ) ;
1215
1316describe ( 'PX Enforcer - pxenforcer.js' , ( ) => {
1417 let params , enforcer , req , stub , pxClient , pxLoggerSpy , logger ;
@@ -804,4 +807,36 @@ describe('PX Enforcer - pxenforcer.js', () => {
804807 done ( ) ;
805808 } ) ;
806809 } ) ;
810+
811+ it ( 'Should add Nonce to CSP header (script-src directive exists)' , ( done ) => {
812+ const nonce = 'ImN0nc3Value' ;
813+ const headerWithoutNonce = 'connect-src \'self\' *.bazaarvoice.com *.google.com *.googleapis.com *.perimeterx.net *.px-cdn.net *.px-client.net; script-src \'self\' \'unsafe-eval\' \'unsafe-inline\' *.bazaarvoice.com *.forter.com *.google-analytics.com report-uri https://csp.px-cloud.net/report?report=1&id=8a3a7c5242c0e7646bd7d86284f408f6&app_id=PXFF0j69T5&p=d767ae06-b964-4b42-96a2-6d4089aab525' ;
814+ const headerWithNonce = 'connect-src \'self\' *.bazaarvoice.com *.google.com *.googleapis.com *.perimeterx.net *.px-cdn.net *.px-client.net; script-src \'nonce-ImN0nc3Value\' \'self\' \'unsafe-eval\' \'unsafe-inline\' *.bazaarvoice.com *.forter.com *.google-analytics.com report-uri https://csp.px-cloud.net/report?report=1&id=8a3a7c5242c0e7646bd7d86284f408f6&app_id=PXFF0j69T5&p=d767ae06-b964-4b42-96a2-6d4089aab525' ;
815+ nonceTestUtil ( headerWithoutNonce , headerWithNonce ) ;
816+ done ( ) ;
817+ } ) ;
818+
819+ it ( 'Should add Nonce to CSP header (script-src directive does not exists)' , ( done ) => {
820+ const headerWithoutNonce = 'connect-src \'self\' https://collector-px8u0i7rwc.px-cdn.net https://collector-px8u0i7rwc.px-cloud.net; report-uri https://csp.px-cloud.net/report?report=1&id=4bd43ac663997dde7c6a84abd14fdd7a&app_id=PX8U0i7rwC&p=70bb7c94-4807-4090-bea4-ffd1f7645126' ;
821+ const headerWithNonce = 'connect-src \'self\' https://collector-px8u0i7rwc.px-cdn.net https://collector-px8u0i7rwc.px-cloud.net; script-src \'nonce-ImN0nc3Value\'; report-uri https://csp.px-cloud.net/report?report=1&id=4bd43ac663997dde7c6a84abd14fdd7a&app_id=PX8U0i7rwC&p=70bb7c94-4807-4090-bea4-ffd1f7645126' ;
822+ nonceTestUtil ( headerWithoutNonce , headerWithNonce ) ;
823+ done ( ) ;
824+ } ) ;
825+
826+ it ( 'Should add Nonce to CSP header, CSP header empty' , ( done ) => {
827+ const headerWithoutNonce = ';' ;
828+ const headerWithNonce = '; script-src \'nonce-ImN0nc3Value\';' ;
829+ nonceTestUtil ( headerWithoutNonce , headerWithNonce ) ;
830+ done ( ) ;
831+ } ) ;
807832} ) ;
833+
834+ const nonceTestUtil = ( headerWithoutNonce , headerWithNonce ) => {
835+ const nonce = 'ImN0nc3Value' ;
836+ const headers = { [ CSP_HEADER ] : headerWithoutNonce , [ CSPRO_HEADER ] : headerWithoutNonce } ;
837+ const response = new Response ( headers ) ;
838+ addNonce ( response , nonce ) ;
839+
840+ ( response . getHeader ( CSP_HEADER ) === headerWithNonce ) . should . equal ( true ) ;
841+ ( response . getHeader ( CSPRO_HEADER ) === headerWithNonce ) . should . equal ( true ) ;
842+ } ;
0 commit comments