@@ -26,14 +26,35 @@ function getPublicOriginFromCookie(request) {
2626 }
2727}
2828
29+ function getAllowedHost ( value ) {
30+ if ( ! value ) return null ;
31+
32+ for ( const candidate of value . split ( "," ) ) {
33+ const trimmed = candidate . trim ( ) ;
34+ if ( ! trimmed ) continue ;
35+
36+ try {
37+ const url = new URL ( `https://${ trimmed } ` ) ;
38+ if ( PUBLIC_AUTH_HOSTS . has ( url . hostname ) ) {
39+ return url . hostname ;
40+ }
41+ } catch {
42+ // Ignore malformed forwarded host values.
43+ }
44+ }
45+
46+ return null ;
47+ }
48+
2949function getPublicOrigin ( request ) {
3050 const headers = request . headers ;
31- const forwardedHost = headers . get ( "x-forwarded-host" ) ?. split ( "," ) [ 0 ] ?. trim ( ) ;
32- const host = forwardedHost || headers . get ( "host" ) ?. split ( "," ) [ 0 ] ?. trim ( ) ;
51+ const forwardedHost = getAllowedHost ( headers . get ( "x-forwarded-host" ) ) ;
52+ const host = forwardedHost || getAllowedHost ( headers . get ( "host" ) ) ;
3353
34- if ( host && PUBLIC_AUTH_HOSTS . has ( host ) ) {
54+ if ( host ) {
3555 const forwardedProto = headers . get ( "x-forwarded-proto" ) ?. split ( "," ) [ 0 ] ?. trim ( ) ;
36- return `${ forwardedProto || "https" } ://${ host } ` ;
56+ const proto = forwardedProto === "http" ? "http" : "https" ;
57+ return `${ proto } ://${ host } ` ;
3758 }
3859
3960 return getPublicOriginFromCookie ( request ) ;
@@ -46,12 +67,14 @@ function normalizeAuthRequest(request) {
4667 const currentUrl = new URL ( request . url ) ;
4768 const publicUrl = new URL ( publicOrigin ) ;
4869 currentUrl . protocol = publicUrl . protocol ;
49- currentUrl . host = publicUrl . host ;
70+ currentUrl . hostname = publicUrl . hostname ;
71+ currentUrl . port = publicUrl . port ;
5072
5173 const headers = new Headers ( request . headers ) ;
5274 headers . set ( "host" , publicUrl . host ) ;
5375 headers . set ( "x-forwarded-host" , publicUrl . host ) ;
5476 headers . set ( "x-forwarded-proto" , publicUrl . protocol . replace ( ":" , "" ) ) ;
77+ headers . set ( "x-forwarded-port" , publicUrl . port || "443" ) ;
5578
5679 return new Request ( currentUrl , {
5780 method : request . method ,
0 commit comments