-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
288 lines (241 loc) · 9.94 KB
/
server.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const express = require('express');
const ws = require('ws');
const app = express();
const port = process.env.PORT || 3000;
const host = process.env.HOST || '127.0.0.1';
const url = require('url');
const cmd = require('node-cmd');
const crypto = require('crypto');
const fs = require('fs');
const { json } = require('body-parser');
const https = require('https');
function fullUrl (req) {
return url.format({
// note: if server is behind a proxy, and it probably is, you may see 'http' here even if request was 'https'
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl
});
}
// Start the HTTP server. On Glitch the proxy will forward both HTTP and HTTPS
// traffic to this server.
const listener = app.listen(port, host, () => {
console.log(`HTTP Server listening at ${listener.address().address}:${listener.address().port}`);
});
// Start HTTPS server for https://first-party.example and https://third-party.example
// See README.md for setup instructions
if (fs.existsSync('first-party.example+11.pem') && fs.existsSync('first-party.example+11-key.pem')) {
console.log('Running local HTTPS server.');
const httpsOptions = {
key: fs.readFileSync('first-party.example+11-key.pem'),
cert: fs.readFileSync('first-party.example+11.pem')
};
const httpsListener = https.createServer(httpsOptions, app).listen(443, () => {
console.log(`HTTPS server listening on port ${httpsListener.address().port}`);
});
} else {
console.log('HTTPS key and certificate not found. Skipping HTTPS server.');
}
app.use(express.json());
// Parse post request data as JSON for requests with content-type 'application/csp-report'
app.use(json({
type: 'application/csp-report'
}));
function joinPath () {
return '/' + [...arguments].map(s => s.replace(/^[/]/, '').replace(/[/]$/, '')).join('/');
}
function isSearchHostname (req) {
return req.hostname === 'www.search-company.example' || req.hostname === 'www.search-company.site';
}
function isAdHostname (req) {
return (
req.hostname === 'www.ad-company.example' ||
req.hostname === 'www.ad-company.site' ||
req.hostname === 'convert.ad-company.example' ||
req.hostname === 'convert.ad-company.site'
);
}
function isPubHostname (req) {
return req.hostname === 'www.publisher-company.example' || req.hostname === 'www.publisher-company.site';
}
function isPayHostname (req) {
return req.hostname === 'www.payment-company.example' || req.hostname === 'www.payment-company.site';
}
// Handle internal redirects to adClickFlow directories
app.all('*', (req, res, next) => {
const AD_FLOW = '/adClickFlow';
const PRIVACY_PROTECTIONS = '/privacy-protections';
if (
// If we're not a ad click flow domain ignore.
!(isSearchHostname(req) || isAdHostname(req) || isPubHostname(req) || isPayHostname(req)) ||
// If we've already passed to the ad dir then ignore.
req.path.startsWith(AD_FLOW) ||
// If we're a privacy protection dir then ignore.
req.path.startsWith(PRIVACY_PROTECTIONS)
) {
next();
return;
}
if (req.path.includes('/shared/')) {
req.url = joinPath(AD_FLOW, '/', req.path);
app.handle(req, res);
return;
}
if (isSearchHostname(req)) {
req.url = joinPath(AD_FLOW, '/serp/', req.path);
app.handle(req, res);
return;
}
if (isAdHostname(req)) {
req.url = joinPath(AD_FLOW, '/ad/', req.path);
app.handle(req, res);
return;
}
if (isPubHostname(req)) {
req.url = joinPath(AD_FLOW, '/pub/', req.path);
app.handle(req, res);
return;
}
if (isPayHostname(req)) {
req.url = joinPath(AD_FLOW, '/pay/', req.path);
app.handle(req, res);
}
});
async function adClickFlow () {
const adClickFlow = require('./adClickFlow/server/routes');
const adClickFlowRoutes = await adClickFlow.init();
app.use('/adClickFlow', adClickFlowRoutes);
}
adClickFlow();
// serve all static files
app.use(express.static('.', {
setHeaders: (res, path) => {
res.set('Access-Control-Allow-Origin', '*');
res.set('Timing-Allow-Origin', '*');
res.set('Server-Timing', 'loaded');
// send CSP header when fetching request blocking test site
if (path.endsWith('privacy-protections/request-blocking/index.html')) {
res.set('Content-Security-Policy-Report-Only', 'img-src http: https:; report-uri https://bad.third-party.site/block-me/csp');
}
// send Referrer-policy header when fetching referrer trimming test site
if (path.endsWith('privacy-protections/referrer-trimming/index.html')) {
res.set('Referrer-Policy', 'unsafe-url');
}
}
}));
// endpoint for updating the app (https://support.glitch.com/t/tutorial-how-to-auto-update-your-project-with-github/8124)
app.post('/git', (req, res) => {
const hmac = crypto.createHmac('sha1', process.env.SECRET);
const sig = 'sha1=' + hmac.update(JSON.stringify(req.body)).digest('hex');
// don't deploy if push was to a branch other than main
if (req.body.ref !== 'refs/heads/main') {
return res.sendStatus(204);
}
if (req.headers['x-github-event'] === 'push' && crypto.timingSafeEqual(Buffer.from(sig, 'utf8'), Buffer.from(req.headers['x-hub-signature'], 'utf8'))) {
try {
fs.chmodSync('git.sh', '777'); /* Fix no perms after updating */
cmd.runSync('./git.sh');
cmd.runSync('refresh'); // Refresh project
console.log('> 💚 Deploy successful!');
return res.sendStatus(200);
} catch (e) {
console.log('> 💥 Error deploying: ', e);
return res.sendStatus(500);
}
} else {
return res.sendStatus(403);
}
});
// dummy websocket server
const wss = new ws.Server({ server: listener, path: '/block-me/web-socket' });
wss.on('connection', (ws) => {
ws.send('It works 👍');
ws.close();
});
// reflects request headers and request url back
app.get('/reflect-headers', (req, res) => {
res.set('Access-Control-Allow-Origin', req.headers.origin || '*');
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Timing-Allow-Origin', '*');
const result = { url: fullUrl(req), headers: req.headers };
// when navigating to this page firefox will show JSON in it's JSON viewer, sometimes we don't want that
// in those cases add ?text=1 to url and it will load as plain text and not trigger the viewer
if (req.query.text) {
res.write(JSON.stringify(result));
return res.end();
} else {
return res.json(result);
}
});
// sets a cookie with provided value
app.get('/set-cookie', (req, res) => {
res.set('Access-Control-Allow-Origin', req.headers.origin || '*');
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Timing-Allow-Origin', '*');
const expires = new Date((Date.now() + (10 * 24 * 60 * 60 * 1000)));
if (!req.query.value) {
return res.sendStatus(401);
}
let cookieName = 'headerdata';
if (req.query.name) {
cookieName = req.query.name;
}
return res.cookie(cookieName, req.query.value, { expires, httpOnly: true, sameSite: 'none', secure: true }).sendStatus(200);
});
// returns a random number and sets caching for a year
app.get('/cached-random-number', (req, res) => {
res.setHeader('Cache-Control', 'public, max-age=31556926, immutable');
const random = (Math.round(Math.random() * 1000)).toString();
res.end(random);
});
// returns referrer found in the header and in js back to the test page
app.get('/come-back', (req, res) => {
res.end(`<!DOCTYPE html>
<html lang="en">
<head>
<title>Redirecting</title>
</head>
<body>
<script>
const jsReferrer = document.referrer;
document.body.innerHTML += '<p>header: <strong>${req.headers.referer || ''}</strong></p><p>js: <strong>' + jsReferrer + '</strong></p>';
setTimeout(() => {
location.href = 'https://privacy-test-pages.site/privacy-protections/referrer-trimming/?run&header=${req.headers.referer || ''}&js=' + jsReferrer + '&testid=${req.query.testid || ''}';
}, 1000);
</script>
</body>
</html>`);
});
const REDIRECT_ALLOWLIST = ['bad.third-party.site'];
app.get('/redirect', (req, res) => {
const destination = req.query.destination;
if (!REDIRECT_ALLOWLIST.find(allowHost => destination.startsWith('https://' + allowHost + '/'))) {
res.statusCode = 403;
res.end();
return;
}
res.set('Location', destination);
res.statusCode = 307;
res.end();
});
app.use('/content-scope-scripts/', express.static('node_modules/@duckduckgo/content-scope-scripts/integration-test/test-pages/'));
const blockingRoutes = require('./privacy-protections/request-blocking/server/routes');
app.use('/block-me', blockingRoutes);
const partitioningRoutes = require('./privacy-protections/storage-partitioning/server/routes');
app.use('/partitioning', partitioningRoutes);
const cspRoutes = require('./security/csp-report/server/routes.js');
app.use('/security/csp-report', cspRoutes);
const downloadRoutes = require('./features/download/server/routes.js');
app.use('/features/download', downloadRoutes);
const chRoutes = require('./features/client-hints/server/routes.js');
app.use('/features/client-hints', chRoutes);
const clearDataRoutes = require('./features/clear-data/server/routes.js');
app.use('/features/clear-data', clearDataRoutes);
const viewportRoutes = require('./viewport/server/routes.js');
app.use('/viewport', viewportRoutes);
const addressBarSpoofingRoutes = require('./security/address-bar-spoofing/server/routes.js');
app.use('/security/abs/', addressBarSpoofingRoutes);
const phishingDetectionRoutes = require('./security/badware/server/routes.js');
app.use('/security/badware/phishing-redirect', phishingDetectionRoutes);
const networkErrorRoutes = require('./network-error/server/routes.js');
app.use('/network-error', networkErrorRoutes);