From 3f2a45ecd01187d72152247c44fffc1752d56c03 Mon Sep 17 00:00:00 2001 From: Sander Kranz Date: Fri, 23 Aug 2019 16:59:56 +0300 Subject: [PATCH 1/3] Add support of multiple values in the "X-Forwarded-Host" header --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4fc55f0..267a7bc 100644 --- a/index.js +++ b/index.js @@ -253,7 +253,12 @@ prerender.buildApiUrl = function(req) { if (this.protocol) { protocol = this.protocol; } - var fullUrl = protocol + "://" + (this.host || req.headers['x-forwarded-host'] || req.headers['host']) + req.url; + + var hostRaw = this.host || req.headers['x-forwarded-host'] || req.headers['host']; + // Take the first hostname in case of multiple values + var host = ('' + hostRaw).replace(/,.*$/, ''); + var fullUrl = protocol + "://" + host + req.url; + return prerenderUrl + forwardSlash + fullUrl; }; From b2b27f7d090076aa9854d40c42f0c37f2d161631 Mon Sep 17 00:00:00 2001 From: Sander Kranz Date: Sat, 24 Aug 2019 05:17:04 +0300 Subject: [PATCH 2/3] Reduce the effect of the previous commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Take the first hostname from the “X-Forwarded-Host” header only --- index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 267a7bc..e2ed51c 100644 --- a/index.js +++ b/index.js @@ -253,12 +253,18 @@ prerender.buildApiUrl = function(req) { if (this.protocol) { protocol = this.protocol; } + + var host = ''; + if (this.host) { + host = this.host; + } else if (req.headers['x-forwarded-host']) { + // Take the first hostname in case of multiple values + host = req.headers['x-forwarded-host'].replace(/,.*$/, ''); + } else if (req.headers['host']) { + host = req.headers['host']; + } - var hostRaw = this.host || req.headers['x-forwarded-host'] || req.headers['host']; - // Take the first hostname in case of multiple values - var host = ('' + hostRaw).replace(/,.*$/, ''); var fullUrl = protocol + "://" + host + req.url; - return prerenderUrl + forwardSlash + fullUrl; }; From f8efc7b1b909cfe7d67e8905f52a5a853b76e291 Mon Sep 17 00:00:00 2001 From: Sander Kranz Date: Sat, 24 Aug 2019 05:24:47 +0300 Subject: [PATCH 3/3] Make host variable undefined by default --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e2ed51c..64bd950 100644 --- a/index.js +++ b/index.js @@ -254,7 +254,7 @@ prerender.buildApiUrl = function(req) { protocol = this.protocol; } - var host = ''; + var host; if (this.host) { host = this.host; } else if (req.headers['x-forwarded-host']) {