1
1
const express = require ( 'express' )
2
+ const bodyParser = require ( 'body-parser' )
2
3
const path = require ( 'path' )
3
4
const fs = require ( 'fs' ) ;
4
5
const fsp = require ( 'fs/promises' ) ;
5
6
6
7
const app = express ( )
7
8
const port = 8080
8
9
10
+ const addCSP = false ;
11
+
12
+ app . use ( express . json ( ) ) ;
13
+
9
14
let frameworkDirectory = path . join ( __dirname , ".." , "frameworks" ) ;
10
15
let webDriverResultDirectory = path . join ( __dirname , ".." , "webdriver-ts-results" ) ;
11
16
@@ -104,7 +109,15 @@ function addSiteIsolationForIndex(request, response, next) {
104
109
}
105
110
app . use ( addSiteIsolationForIndex ) ;
106
111
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
+ ) )
108
121
app . use ( '/webdriver-ts-results' , express . static ( webDriverResultDirectory ) )
109
122
app . use ( '/css' , express . static ( path . join ( frameworkDirectory , '..' , 'css' ) ) )
110
123
app . get ( '/index.html' , async ( req , res , next ) => {
@@ -117,8 +130,23 @@ app.get('/ls', async (req, res) => {
117
130
let t1 = Date . now ( ) ;
118
131
console . log ( "/ls duration " , ( t1 - t0 ) ) ;
119
132
} )
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
+ } )
120
148
121
149
122
150
app . listen ( port , ( ) => {
123
- console . log ( `Server running on port ${ port } ` )
151
+ console . log ( `Server running on port ${ port } ` ) ;
124
152
} )
0 commit comments