fix(auth-js): Fix immutable headers error in x-forwarded request (#614)
* fix: immutable headers error in x-forwarded req * added changesetpull/616/head
parent
52c0e418cd
commit
19f3beae1a
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/auth-js': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix immutable headers error in x-forwarded req
|
|
@ -8,7 +8,6 @@ import { env ,getRuntimeKey} from 'hono/adapter'
|
||||||
import { HTTPException } from 'hono/http-exception'
|
import { HTTPException } from 'hono/http-exception'
|
||||||
import { setEnvDefaults as coreSetEnvDefaults } from '@auth/core'
|
import { setEnvDefaults as coreSetEnvDefaults } from '@auth/core'
|
||||||
|
|
||||||
|
|
||||||
declare module 'hono' {
|
declare module 'hono' {
|
||||||
interface ContextVariableMap {
|
interface ContextVariableMap {
|
||||||
authUser: AuthUser
|
authUser: AuthUser
|
||||||
|
@ -39,18 +38,15 @@ export function setEnvDefaults(env: AuthEnv, config: AuthConfig) {
|
||||||
coreSetEnvDefaults(env, config)
|
coreSetEnvDefaults(env, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cloneRequest(input: URL | string, request: Request){
|
async function cloneRequest(input: URL | string, request: Request, headers?: Headers) {
|
||||||
|
if (getRuntimeKey() === 'bun') {
|
||||||
if ( getRuntimeKey() === "bun") {
|
|
||||||
return new Request(input, {
|
return new Request(input, {
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers:new Headers(request.headers),
|
headers: headers ?? new Headers(request.headers),
|
||||||
body:
|
body:
|
||||||
request.method === "GET" || request.method === "HEAD"
|
request.method === 'GET' || request.method === 'HEAD' ? undefined : await request.blob(),
|
||||||
? undefined
|
|
||||||
: await request.blob(),
|
|
||||||
// @ts-ignore: TS2353
|
// @ts-ignore: TS2353
|
||||||
referrer: "referrer" in request ? (request.referrer as string) : undefined,
|
referrer: 'referrer' in request ? (request.referrer as string) : undefined,
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
referrerPolicy: request.referrerPolicy as any,
|
referrerPolicy: request.referrerPolicy as any,
|
||||||
mode: request.mode,
|
mode: request.mode,
|
||||||
|
@ -60,7 +56,7 @@ async function cloneRequest(input: URL | string, request: Request){
|
||||||
redirect: request.redirect,
|
redirect: request.redirect,
|
||||||
integrity: request.integrity,
|
integrity: request.integrity,
|
||||||
keepalive: request.keepalive,
|
keepalive: request.keepalive,
|
||||||
signal: request.signal
|
signal: request.signal,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return new Request(input, request)
|
return new Request(input, request)
|
||||||
|
@ -75,19 +71,20 @@ export async function reqWithEnvUrl(req: Request, authUrl?: string){
|
||||||
return cloneRequest(reqUrlObj.href, req)
|
return cloneRequest(reqUrlObj.href, req)
|
||||||
} else {
|
} else {
|
||||||
const url = new URL(req.url)
|
const url = new URL(req.url)
|
||||||
const proto = req.headers.get('x-forwarded-proto')
|
const headers = new Headers(req.headers)
|
||||||
const host = req.headers.get('x-forwarded-host') ?? req.headers.get('host')
|
const proto = headers.get('x-forwarded-proto')
|
||||||
|
const host = headers.get('x-forwarded-host') ?? headers.get('host')
|
||||||
if (proto != null) url.protocol = proto.endsWith(':') ? proto : proto + ':'
|
if (proto != null) url.protocol = proto.endsWith(':') ? proto : proto + ':'
|
||||||
if (host != null) {
|
if (host != null) {
|
||||||
url.host = host
|
url.host = host
|
||||||
const portMatch = host.match(/:(\d+)$/)
|
const portMatch = host.match(/:(\d+)$/)
|
||||||
if (portMatch) url.port = portMatch[1]
|
if (portMatch) url.port = portMatch[1]
|
||||||
else url.port = ''
|
else url.port = ''
|
||||||
req.headers.delete("x-forwarded-host")
|
headers.delete('x-forwarded-host')
|
||||||
req.headers.delete("Host")
|
headers.delete('Host')
|
||||||
req.headers.set("Host", host)
|
headers.set('Host', host)
|
||||||
}
|
}
|
||||||
return cloneRequest(url.href, req)
|
return cloneRequest(url.href, req, headers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue