From 8447313a7658c5adf0a076f7a6cb4a15315adbb7 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 11 Oct 2024 15:15:27 +0100 Subject: [PATCH 1/4] fix: revert regression of port computation --- .changeset/tidy-dancers-act.md | 5 +++++ packages/astro/src/core/app/node.ts | 11 +++++------ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 .changeset/tidy-dancers-act.md diff --git a/.changeset/tidy-dancers-act.md b/.changeset/tidy-dancers-act.md new file mode 100644 index 000000000000..abe7895da123 --- /dev/null +++ b/.changeset/tidy-dancers-act.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix a regression where a port was incorrectly added to the `Astro.url` diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index 87ef2a0defad..aa3159d407a2 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -85,11 +85,11 @@ export class NodeApp extends App { // @example "443,8080,80" => "443" const forwardedPort = getFirstForwardedValue(req.headers['x-forwarded-port']); const port = - forwardedPort ?? req.socket?.remotePort?.toString() ?? (isEncrypted ? '443' : '80'); + forwardedPort ?? (isEncrypted ? '443' : '80'); const portInHostname = typeof hostname === 'string' && /:\d+$/.test(hostname); - const hostnamePort = portInHostname ? hostname : `${hostname}:${port}`; - + const hostnamePort = portInHostname ? hostname : `${hostname}${port ? `:${port}` : ''}`; + const url = `${protocol}://${hostnamePort}${req.url}`; const options: RequestInit = { method: req.method || 'GET', @@ -105,9 +105,8 @@ export class NodeApp extends App { // Get the IP of end client behind the proxy. // @example "1.1.1.1,8.8.8.8" => "1.1.1.1" const forwardedClientIp = getFirstForwardedValue(req.headers['x-forwarded-for']); - const clientIp = forwardedClientIp || req.socket?.remoteAddress; - if (clientIp) { - Reflect.set(request, clientAddressSymbol, clientIp); + if (forwardedClientIp) { + Reflect.set(request, clientAddressSymbol, forwardedClientIp); } return request; From 0ae3a42f511322d06e88c8709cf2cc31ebb507f5 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 11 Oct 2024 16:03:13 +0100 Subject: [PATCH 2/4] revert tests failing --- packages/astro/src/core/app/node.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index aa3159d407a2..18999e141cf8 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -105,8 +105,9 @@ export class NodeApp extends App { // Get the IP of end client behind the proxy. // @example "1.1.1.1,8.8.8.8" => "1.1.1.1" const forwardedClientIp = getFirstForwardedValue(req.headers['x-forwarded-for']); - if (forwardedClientIp) { - Reflect.set(request, clientAddressSymbol, forwardedClientIp); + const clientIp = forwardedClientIp || req.socket?.remoteAddress; + if (clientIp) { + Reflect.set(request, clientAddressSymbol, clientIp); } return request; From b2d147e6325e7e333f4c70291a0b3f75ed46b91b Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 11 Oct 2024 16:04:33 +0100 Subject: [PATCH 3/4] address feedback --- packages/astro/src/core/app/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index 18999e141cf8..c172ed5b5bb3 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -88,7 +88,7 @@ export class NodeApp extends App { forwardedPort ?? (isEncrypted ? '443' : '80'); const portInHostname = typeof hostname === 'string' && /:\d+$/.test(hostname); - const hostnamePort = portInHostname ? hostname : `${hostname}${port ? `:${port}` : ''}`; + const hostnamePort = portInHostname ? hostname : `${hostname}:${port}`; const url = `${protocol}://${hostnamePort}${req.url}`; const options: RequestInit = { From bd0b105910031947ff57e4458ccff3f2818c89b6 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 14 Oct 2024 09:22:08 +0100 Subject: [PATCH 4/4] revert logic to how it was before --- packages/astro/src/core/app/node.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index c172ed5b5bb3..3ad965cd08c6 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -83,12 +83,11 @@ export class NodeApp extends App { const hostname = forwardedHostname ?? req.headers.host ?? req.headers[':authority']; // @example "443,8080,80" => "443" - const forwardedPort = getFirstForwardedValue(req.headers['x-forwarded-port']); - const port = - forwardedPort ?? (isEncrypted ? '443' : '80'); + const port = getFirstForwardedValue(req.headers['x-forwarded-port']); const portInHostname = typeof hostname === 'string' && /:\d+$/.test(hostname); - const hostnamePort = portInHostname ? hostname : `${hostname}:${port}`; + const hostnamePort = portInHostname ? hostname : `${hostname}${port ? `:${port}` : ''}`; + const url = `${protocol}://${hostnamePort}${req.url}`; const options: RequestInit = {