11const express = require ( 'express' )
2+ const bodyParser = require ( 'body-parser' )
23const path = require ( 'path' )
34const fs = require ( 'fs' ) ;
45const fsp = require ( 'fs/promises' ) ;
56
67const app = express ( )
78const port = 8080
89
10+ const addCSP = false ;
11+
12+ app . use ( express . json ( ) ) ;
13+
914let frameworkDirectory = path . join ( __dirname , ".." , "frameworks" ) ;
1015let webDriverResultDirectory = path . join ( __dirname , ".." , "webdriver-ts-results" ) ;
1116
@@ -104,7 +109,15 @@ function addSiteIsolationForIndex(request, response, next) {
104109}
105110app . use ( addSiteIsolationForIndex ) ;
106111
107- app . use ( '/frameworks' , express . static ( frameworkDirectory ) )
112+ app . use ( '/frameworks' , express . static ( frameworkDirectory ,
113+ {
114+ setHeaders : function ( res , path ) {
115+ if ( addCSP ) {
116+ res . setHeader ( 'Content-Security-Policy' , "default-src 'self'; report-uri /csp" ) ;
117+ }
118+ }
119+ }
120+ ) )
108121app . use ( '/webdriver-ts-results' , express . static ( webDriverResultDirectory ) )
109122app . use ( '/css' , express . static ( path . join ( frameworkDirectory , '..' , 'css' ) ) )
110123app . get ( '/index.html' , async ( req , res , next ) => {
@@ -117,8 +130,23 @@ app.get('/ls', async (req, res) => {
117130 let t1 = Date . now ( ) ;
118131 console . log ( "/ls duration " , ( t1 - t0 ) ) ;
119132} )
133+ app . use ( '/csp' , bodyParser . json ( { type : 'application/csp-report' } ) )
134+
135+ violations = [ ]
136+
137+ app . post ( '/csp' , async ( req , res ) => {
138+ console . log ( "/CSP " , req . body ) ;
139+ let uri = req . body [ 'csp-report' ] [ "document-uri" ]
140+ let frameworkRegEx = / ( ( n o n - ) ? k e y e d \/ .* ?\/ ) /
141+ violations . push ( uri . match ( frameworkRegEx ) [ 0 ] )
142+ res . sendStatus ( 201 ) ;
143+ } )
144+
145+ app . get ( '/csp' , async ( req , res ) => {
146+ res . send ( violations )
147+ } )
120148
121149
122150app . listen ( port , ( ) => {
123- console . log ( `Server running on port ${ port } ` )
151+ console . log ( `Server running on port ${ port } ` ) ;
124152} )
0 commit comments