diff --git a/handwritten/error-reporting/src/request-extractors/hapi.ts b/handwritten/error-reporting/src/request-extractors/hapi.ts index 7193332021f0..549a0bb949ea 100644 --- a/handwritten/error-reporting/src/request-extractors/hapi.ts +++ b/handwritten/error-reporting/src/request-extractors/hapi.ts @@ -65,10 +65,11 @@ function extractRemoteAddressFromRequest(req: hapi.Request) { /** * Helper to normalize headers that might be arrays into a single string. */ -function getSingleHeader( - val: string | string[] | undefined, -): string | undefined { - return Array.isArray(val) ? val[0] : val; +function getSingleHeader(val: unknown): string | undefined { + if (Array.isArray(val)) { + return typeof val[0] === 'string' ? val[0] : undefined; + } + return typeof val === 'string' ? val : undefined; } /** diff --git a/handwritten/error-reporting/test/unit/request-extractors/hapi.ts b/handwritten/error-reporting/test/unit/request-extractors/hapi.ts index 0fe80f31081a..465011e758b3 100644 --- a/handwritten/error-reporting/test/unit/request-extractors/hapi.ts +++ b/handwritten/error-reporting/test/unit/request-extractors/hapi.ts @@ -138,5 +138,26 @@ describe('hapiRequestInformationExtractor behaviour', () => { EXPECTED, ); }); + it('Should handle array headers correctly', () => { + const REQUEST = { + ...FULL_REQ_DERIVATION_VALUE, + headers: { + 'x-forwarded-for': ['0.0.0.1', '0.0.0.2'], + 'user-agent': ['Mozilla/5.0', 'Chrome/90'], + referrer: ['www.ANOTHER-TEST.com'], + }, + }; + const EXPECTED = { + ...FULL_REQ_EXPECTED_VALUE, + userAgent: 'Mozilla/5.0', + referrer: 'www.ANOTHER-TEST.com', + remoteAddress: '0.0.0.1', + }; + + deepStrictEqual( + hapiRequestInformationExtractor(REQUEST as {} as hapi.Request), + EXPECTED, + ); + }); }); });