Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/vendor/getIpAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@ export const ipHeaderNames = [
* will be returned.
*/
export function getClientIPAddress(headers: { [key: string]: string | string[] | undefined }): string | null {
// Build a map of lowercase header names to their values for case-insensitive lookup
// This is needed because headers from different sources may have different casings
const lowerCaseHeaders: { [key: string]: string | string[] | undefined } = {};

for (const key of Object.keys(headers)) {
lowerCaseHeaders[key.toLowerCase()] = headers[key];
}

// This will end up being Array<string | string[] | undefined | null> because of the various possible values a header
// can take
const headerValues = ipHeaderNames.map((headerName: string) => {
const rawValue = headers[headerName];
const rawValue = lowerCaseHeaders[headerName.toLowerCase()];
const value = Array.isArray(rawValue) ? rawValue.join(';') : rawValue;

if (headerName === 'Forwarded') {
Comment on lines +59 to 69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The new case-insensitive IP header reading is not matched by the deletion logic, which remains case-sensitive, potentially leaking IP headers when PII stripping is enabled.
Severity: CRITICAL

Suggested Fix

Modify the header deletion logic in extractNormalizedRequestData() to be case-insensitive. This can be achieved by iterating over the actual request headers, converting each to lowercase, and checking if it exists in a pre-computed set of lowercase IP header names before deletion. This ensures symmetry between reading and removing IP-related headers.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/core/src/vendor/getIpAddress.ts#L52-L69

Potential issue: The pull request updates `getClientIPAddress()` to read IP-related
headers in a case-insensitive manner. However, the corresponding logic in
`extractNormalizedRequestData()` for removing these headers when `sendDefaultPii` is
`false` remains case-sensitive. It iterates through `ipHeaderNames` and performs an
exact-match deletion. This creates a situation where a header like `Cf-Connecting-Ip`
will be correctly identified as containing an IP address but will not be deleted if PII
stripping is enabled. This results in the unintended leakage of IP address information
in events.

Did we get this right? 👍 / 👎 to inform future reviews.

Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/lib/vendor/getClientIpAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ describe('getClientIPAddress', () => {

expect(ip).toEqual(expectedIP);
});

it('should find headers regardless of case', () => {
const headers = {
'Cf-Connecting-Ip': '1.1.1.1',
};

const ip = getClientIPAddress(headers);
expect(ip).toEqual('1.1.1.1');
});
});
Loading