Skip to content

Commit fb11904

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 5278d82 commit fb11904

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
@@ -889,13 +889,15 @@ export default abstract class Server<ServerOptions extends Options = Options> {
889889
)
890890
}
891891

892-
req.headers['x-forwarded-host'] ??= req.headers['host'] ?? this.hostname
893-
req.headers['x-forwarded-port'] ??= this.port?.toString()
894892
const { originalRequest } = req as NodeNextRequest
895-
req.headers['x-forwarded-proto'] ??= (originalRequest.socket as TLSSocket)
896-
?.encrypted
897-
? 'https'
898-
: 'http'
893+
const isHttps = !!(originalRequest?.socket as TLSSocket)?.encrypted
894+
req.headers['x-forwarded-host'] ??= req.headers['host'] ?? this.hostname
895+
req.headers['x-forwarded-port'] ??= this.port
896+
? this.port.toString()
897+
: isHttps
898+
? '443'
899+
: '80'
900+
req.headers['x-forwarded-proto'] ??= isHttps ? 'https' : 'http'
899901
req.headers['x-forwarded-for'] ??= originalRequest.socket?.remoteAddress
900902

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

0 commit comments

Comments
 (0)