-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
144 lines (118 loc) · 4.31 KB
/
app.js
File metadata and controls
144 lines (118 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const hostname = '127.0.0.1';
const port = 3443;
const puppeteer = require('puppeteer')
const crypto = require("crypto");
const express = require('express')
const bodyParser = require('body-parser');
const views = require('./views/viewRoutes');
const fs = require('fs');
const https = require('https');
const path = require('path')
const { saveReport, processReport } = require('./helpers')
var app = express();
app.use(express.static(__dirname + '/views'));
app.use('/csp-reports', bodyParser.json({ type: 'application/csp-report' }));
app.use('/run-reports', express.urlencoded({ extended: true }))
app.use('/trustedTypes-report', bodyParser.json({ type: 'application/csp-report' }));
app.use('/coep-reports', bodyParser.json({ type: 'text/plain' }));
app.use('/csp/*', function (req, res, next) {
res.setHeader('Content-Security-Policy', "object-src 'none';script-src 'nonce-r4nd0m' 'strict-dynamic' 'report-sample' https: http:;base-uri 'none'; report-uri /csp-reports;");
next()
})
app.use('/trustedTypes/*', function (req, res, next) {
res.setHeader('Content-Security-Policy', "object-src 'none'; require-trusted-types-for 'script';base-uri 'none'; report-uri /trustedTypes-report");
next()
})
app.use('/coep/*', function (req, res, next) {
res.setHeader('Cross-Origin-Embedder-Policy',"require-corp");
next()
})
// https.createServer({
// cert: fs.readFileSync(path.join(__dirname, 'ssl', 'server.crt')),
// key: fs.readFileSync(path.join(__dirname, 'ssl', 'server.key'))
// }, app)
// .listen(port, function () {
// console.log(`Example app listening on port ${port}! Go to https://localhost:${port}/`)
// })
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
app.get('/', function (req, res) {
res.setHeader("Report-To", "{ group: 'coep-endpoint', max_age: 86400, endpoints: [{ url: 'https://doomedcoepreports.com:8080/coep-reports'}]}")
res.sendFile(__dirname + "/views" + "/index.html")
})
//!Leaving this in just incase we want it back later
// app.post('/run-reports', async (req, res) => {
// var id = crypto.randomBytes(20).toString('hex');
// try {
// const browser = await puppeteer.launch();
// const openPage = await browser.newPage();
// const pages = Object.values(req.body).map(e => `http://${hostname}:${port}/${e}?id=${id}`)
// // open report endpoints
// for (const page of pages) {
// await openPage.goto(page, { waitUntil: 'networkidle0' }) // wait for puppeteer to complete fetch
// }
// await browser.close()
// } catch (e) {
// console.log(e)
// }
// res.redirect('/see-reports?id=' + id);
// })
app.get(`/see-reports`, function (req, res) {
res.sendFile(__dirname + '/views/seeReports.html');
})
app.get(`/run-reports`, function (req, res) {
res.sendFile(__dirname + '/views/runReports.html');
})
app.get('/getTableContent', function (req, res) {
const queue_file = __dirname + '/reports/table_queue.json'
var directory = __dirname + '/reports'
let rawdata = fs.readFileSync(queue_file, 'utf8')
console.log(rawdata)
var arr = JSON.parse(rawdata)
var reports = {}
for (i = arr.length - 1; i >= 0; i--) {
var id = arr[i].id;
reports[id] = {
'numberOfReports': fs.readdirSync(directory + '/' + id, 'utf8').length,
'date': arr[i].date,
'browser': arr[i].browser
}
}
console.log(reports)
res.send(reports)
})
app.get(`/get-reports`, function (req, res) {
let directory_name = __dirname + '/reports/' + req.query.id
//make sure directory exists
if (!fs.existsSync(directory_name)) {
res.sendStatus(400)
}
processedReports = []
files = fs.readdirSync(directory_name)
files.forEach(file => {
let rawdata = fs.readFileSync(directory_name + '/' + file)
let reportJSON = JSON.parse(rawdata);
processedReports.push(reportJSON)
})
res.send(processedReports)
})
app.post('/csp-reports', function (req, res) {
console.log('CSP violation!')
console.log(req.body)
saveReport(req, 'csp_')
res.sendStatus(204)
})
app.post('/coep-reports', function (req, res) {
console.log('COEP violation!')
console.log(req.body)
saveReport(req, 'coep_')
res.sendStatus(204)
})
app.post('/trustedTypes-report', function (req, res) {
console.log('Trusted types violation!')
console.log(req.body)
saveReport(req, 'tt_')
res.sendStatus(204)
})
app.use('/', views);