Fix for #2675, added HTTPS redirect when x-forwarded-proto:http

Ylian Saint-Hilaire committed May 23, 2021 at 00:02 UTC 98e0801c8c6e17604a40c85ec43303c8f6f16b32
2 files changed +13 -3
redirserver.js
+3 -3
@@ -33,10 +33,10 @@ module.exports.CreateRedirServer = function (parent, db, args, func) {
33 // Perform an HTTP to HTTPS redirection
34 function performRedirection(req, res) {
35 var host = req.headers.host;
36 - if (typeof host == 'string') { host = host.split(":")[0]; }
36 + if (typeof host == 'string') { host = host.split(':')[0]; }
37 if ((host == null) && (obj.certificates != null)) { host = obj.certificates.CommonName; if (obj.certificates.CommonName.indexOf('.') == -1) { host = req.headers.host; } }
38 var httpsPort = ((obj.args.aliasport == null) ? obj.args.port : obj.args.aliasport); // Use HTTPS alias port is specified
39 - res.redirect("https://" + host + ":" + httpsPort + req.url);
39 + res.redirect('https://' + host + ':' + httpsPort + req.url);
40 }
41
42 /*
@@ -59,7 +59,7 @@ module.exports.CreateRedirServer = function (parent, db, args, func) {
59 if (i >= 0) { rootcert = rootcert.substring(i + 29); }
60 i = rootcert.indexOf('-----END CERTIFICATE-----');
61 if (i >= 0) { rootcert = rootcert.substring(i, 0); }
62 - res.send(Buffer.from(rootcert, "base64"));
62 + res.send(Buffer.from(rootcert, 'base64'));
63 } else {
64 res.sendStatus(404);
65 }
webserver.js
+10
@@ -5294,6 +5294,16 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
5294 // Useful for debugging reverse proxy issues
5295 parent.debug('httpheaders', req.method, req.url, req.headers);
5296
5297 + // If this request came over HTTP, redirect to HTTPS
5298 + if (req.headers['x-forwarded-proto'] == 'http') {
5299 + var host = req.headers.host;
5300 + if (typeof host == 'string') { host = host.split(':')[0]; }
5301 + if ((host == null) && (obj.certificates != null)) { host = obj.certificates.CommonName; if (obj.certificates.CommonName.indexOf('.') == -1) { host = req.headers.host; } }
5302 + var httpsPort = ((obj.args.aliasport == null) ? obj.args.port : obj.args.aliasport); // Use HTTPS alias port is specified
5303 + res.redirect('https://' + host + ':' + httpsPort + req.url);
5304 + return;
5305 + }
5306 +
5307 // Perform traffic accounting
5308 if (req.headers.upgrade == 'websocket') {
5309 // We don't count traffic on WebSockets since it's counted by the handling modules.