-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.36 KB
/
index.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
const express = require('express')
const app = express()
app.set('view engine', 'ejs')
const paramName = process.env.PARAM || 'redirect';
const patterns = process.env.ALLOWED_URL_PATTERNS
? process.env.ALLOWED_URL_PATTERNS.split(',').map(s => new RegExp(s.trim()))
: [new RegExp('.*')];
console.log("allowed patterns: %s", patterns);
function isAllowedURL(url, patterns) {
for (let i = 0; i < patterns.length; i++) {
if (url.match(patterns[i])) {
return true;
}
}
return false;
}
app.get('/', (req, res) => {
var redirectUrl = req.query[paramName];
if (redirectUrl) {
var url = new URL(redirectUrl);
// add all query parameters to the redirect url except the redirect parameter
var params = new URLSearchParams(req.query);
params.delete(paramName);
var strParams = params.toString();
redirectUrl = url.origin + url.pathname + (strParams.length > 0 ? '?' + strParams : '');
if (!isAllowedURL(redirectUrl, patterns)) {
console.log('URL not allowed: %s', redirectUrl)
redirectUrl = "";
}
}
res.render('index', { redirect: redirectUrl });
})
// /*eslint no-unused-vars: "next"*/
app.use((err, req, res, next) => {
console.error('Internal redirect app error: %s', err)
res.status(400).send('Internal redirect app error')
})
app.listen(3000, () => {
console.log('Internal redirect app listening on port 3000!')
})