Skip to content

Commit d2df7f6

Browse files
authored
fix(core): Find the correct IP address regardless their case (#18880)
closes [FE-681](https://linear.app/getsentry/issue/FE-681/senddefaultpii-sets-cloudflare-worker-ip-instead-of-the-one-from-the) It seems that in some cases the headers do have a different casing. E.g. in Cloudflare Workers the `[CF-Connecting-IP](https://developers.cloudflare.com/fundamentals/reference/http-headers/#cf-connecting-ip)` is actually send as `Cf-Connecting-Ip`. <img width="635" height="223" alt="Screenshot 2026-01-19 at 13 15 27" src="https://github.com/user-attachments/assets/e427e10d-35b7-4743-980c-3ff0d5cc1d51" /> According to [RFC7540 8.1.2](https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2) the headers are case-insensitive, but for HTTP2 they must be lowercase. So theoretically this would fix also HTTP2 lower case headers
1 parent 75b183d commit d2df7f6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/core/src/vendor/getIpAddress.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ export const ipHeaderNames = [
5252
* will be returned.
5353
*/
5454
export function getClientIPAddress(headers: { [key: string]: string | string[] | undefined }): string | null {
55+
// Build a map of lowercase header names to their values for case-insensitive lookup
56+
// This is needed because headers from different sources may have different casings
57+
const lowerCaseHeaders: { [key: string]: string | string[] | undefined } = {};
58+
59+
for (const key of Object.keys(headers)) {
60+
lowerCaseHeaders[key.toLowerCase()] = headers[key];
61+
}
62+
5563
// This will end up being Array<string | string[] | undefined | null> because of the various possible values a header
5664
// can take
5765
const headerValues = ipHeaderNames.map((headerName: string) => {
58-
const rawValue = headers[headerName];
66+
const rawValue = lowerCaseHeaders[headerName.toLowerCase()];
5967
const value = Array.isArray(rawValue) ? rawValue.join(';') : rawValue;
6068

6169
if (headerName === 'Forwarded') {

packages/core/test/lib/vendor/getClientIpAddress.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ describe('getClientIPAddress', () => {
2929

3030
expect(ip).toEqual(expectedIP);
3131
});
32+
33+
it('should find headers regardless of case', () => {
34+
const headers = {
35+
'Cf-Connecting-Ip': '1.1.1.1',
36+
};
37+
38+
const ip = getClientIPAddress(headers);
39+
expect(ip).toEqual('1.1.1.1');
40+
});
3241
});

0 commit comments

Comments
 (0)