mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-03-21 17:24:28 -05:00
fix: fix redirection for external sites #294
This commit is contained in:
parent
973d50006c
commit
5facdcdd83
|
|
@ -17,7 +17,8 @@ module.exports = {
|
|||
api_base: jsonConfig.api_base,
|
||||
http: {
|
||||
base_url: jsonConfig.http.base_url,
|
||||
port: jsonConfig.http.port
|
||||
port: jsonConfig.http.port,
|
||||
allowed_redirection_suffixes: Array.isArray(jsonConfig.http.allowed_redirection_suffixes) ? jsonConfig.http.allowed_redirection_suffixes : ['pretendo.network']
|
||||
},
|
||||
github: {
|
||||
graphql_token: jsonConfig.github.graphql_token
|
||||
|
|
|
|||
|
|
@ -1,14 +1,31 @@
|
|||
const config = require('../config');
|
||||
|
||||
function isValidRedirect(redirect) {
|
||||
if (!redirect) return false;
|
||||
if (redirect.startsWith('/')) return true;
|
||||
if (redirect.startsWith('http://') || redirect.startsWith('https://')) {
|
||||
try {
|
||||
const url = new URL(redirect);
|
||||
return config.http.valid_redirection_domains.some(domain => url.hostname.endsWith(domain));
|
||||
} catch (ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async function redirectMiddleware(request, response, next) {
|
||||
if (request.path.startsWith('/account/logout')) {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (request.method === 'POST') {
|
||||
request.redirect = request.body.redirect?.startsWith('/') ? request.body.redirect : null;
|
||||
if (request.method === 'POST' && request.body) {
|
||||
request.redirect = isValidRedirect(request.body.redirect) ? request.body.redirect : null;
|
||||
}
|
||||
|
||||
if (request.query.redirect) {
|
||||
response.locals.redirect = request.query.redirect?.startsWith('/') ? request.query.redirect : null;
|
||||
response.locals.redirect = isValidRedirect(request.query.redirect) ? request.query.redirect : null;
|
||||
}
|
||||
|
||||
return next();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user