Skip to content

Commit 5b16b59

Browse files
fix: x-forwarded-port header is 'undefined' when no port in url (#60484)
### What? See this issue - #61133 following this change #57815 `x-forwarded-port` header value is 'undefined' if the URL has no port ### Why? x-forwarded-port 'undefined' makes other http-proxy throw 405 error for the invalid header value ### How? Give default 80 port if the URL has no port --------- Co-authored-by: Ethan Arrowood <ethan@arrowood.dev>
1 parent fc38ee1 commit 5b16b59

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/next/src/server/base-server.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,13 +913,15 @@ export default abstract class Server<ServerOptions extends Options = Options> {
913913
)
914914
}
915915

916-
req.headers['x-forwarded-host'] ??= req.headers['host'] ?? this.hostname
917-
req.headers['x-forwarded-port'] ??= this.port?.toString()
918916
const { originalRequest } = req as NodeNextRequest
919-
req.headers['x-forwarded-proto'] ??= (originalRequest.socket as TLSSocket)
920-
?.encrypted
921-
? 'https'
922-
: 'http'
917+
const isHttps = !!(originalRequest?.socket as TLSSocket)?.encrypted
918+
req.headers['x-forwarded-host'] ??= req.headers['host'] ?? this.hostname
919+
req.headers['x-forwarded-port'] ??= this.port
920+
? this.port.toString()
921+
: isHttps
922+
? '443'
923+
: '80'
924+
req.headers['x-forwarded-proto'] ??= isHttps ? 'https' : 'http'
923925
req.headers['x-forwarded-for'] ??= originalRequest.socket?.remoteAddress
924926

925927
// This should be done before any normalization of the pathname happens as

0 commit comments

Comments
 (0)