Skip to content

Commit 13070dc

Browse files
committed
prepare support for CSP checking
1 parent f7b5c09 commit 13070dc

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

server/index.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
const express = require('express')
2+
const bodyParser = require('body-parser')
23
const path = require('path')
34
const fs = require('fs');
45
const fsp = require('fs/promises');
56

67
const app = express()
78
const port = 8080
89

10+
const addCSP = false;
11+
12+
app.use(express.json());
13+
914
let frameworkDirectory = path.join(__dirname, "..", "frameworks");
1015
let webDriverResultDirectory = path.join(__dirname, "..", "webdriver-ts-results");
1116

@@ -104,7 +109,15 @@ function addSiteIsolationForIndex(request, response, next) {
104109
}
105110
app.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+
))
108121
app.use('/webdriver-ts-results', express.static(webDriverResultDirectory))
109122
app.use('/css', express.static(path.join(frameworkDirectory, '..', 'css')))
110123
app.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 = /((non-)?keyed\/.*?\/)/
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

122150
app.listen(port, () => {
123-
console.log(`Server running on port ${port}`)
151+
console.log(`Server running on port ${port}`);
124152
})

server/package-lock.json

+77-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14+
"body-parser": "^1.20.1",
1415
"express": "^4.18.1"
1516
},
1617
"devDependencies": {

0 commit comments

Comments
 (0)