diff --git a/proxy/utils/headers.test.ts b/proxy/utils/headers.test.ts index bc97cc51..1e187a8c 100644 --- a/proxy/utils/headers.test.ts +++ b/proxy/utils/headers.test.ts @@ -18,7 +18,7 @@ const mockReq = { headers: { 'content-type': 'application/json', 'content-length': '24354', - host: 'fpjs.sh', + host: 'example.org', 'transfer-encoding': 'br', via: 'azure.com', cookie: '_iidt=7A03Gwg; _vid_t=gEFRuIQlzYmv692/UL4GLA==', @@ -28,6 +28,7 @@ const mockReq = { 'x-azure-requestchain': 'hops=1', 'x-azure-socketip': '46.204.4.119', 'x-forwarded-for': '127.0.0.1', + 'x-forwarded-host': 'fpjs.sh', }, user: null, params: {}, @@ -163,7 +164,15 @@ describe('prepareHeadersForIngressAPI', () => { expect(result['fpjs-proxy-client-ip']).toBe(mockReq.headers['x-forwarded-for']) expect(result['fpjs-proxy-secret']).toBe('secret') - expect(result['fpjs-proxy-forwarded-host']).toBe(new URL(mockReq.url).hostname) + expect(result['fpjs-proxy-forwarded-host']).toBe('fpjs.sh') + }) + + it('should set correct host', () => { + const result = prepareHeadersForIngressAPI(mockReq, 'secret') + + expect(result['fpjs-proxy-client-ip']).toBe(mockReq.headers['x-forwarded-for']) + expect(result['fpjs-proxy-secret']).toBe('secret') + expect(result['fpjs-proxy-forwarded-host']).toBe('fpjs.sh') }) it('should not set secret if it is undefined', () => { diff --git a/proxy/utils/headers.ts b/proxy/utils/headers.ts index 05b3e5a6..6907c028 100644 --- a/proxy/utils/headers.ts +++ b/proxy/utils/headers.ts @@ -92,6 +92,10 @@ function resolveClientIp(request: HttpRequest, logger?: Logger) { return clientIp } +export function getHost(request: Pick) { + return request.headers['x-forwarded-host'] || request.headers.host || new URL(request.url).hostname +} + export function prepareHeadersForIngressAPI(request: HttpRequest, preSharedSecret?: string, logger?: Logger) { const headers = filterRequestHeaders(request.headers) @@ -99,7 +103,7 @@ export function prepareHeadersForIngressAPI(request: HttpRequest, preSharedSecre if (preSharedSecret) { headers['fpjs-proxy-secret'] = preSharedSecret - headers['fpjs-proxy-forwarded-host'] = new URL(request.url).hostname + headers['fpjs-proxy-forwarded-host'] = getHost(request) } return headers