Skip to content

Commit 697f95b

Browse files
authored
fix(remix): Correctly parse X-Forwarded-For Http header (#7329)
Handle white spaces more robustly when splitting forwarded-for http header values
1 parent 0e63e3a commit 697f95b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/remix/src/utils/getIpAddress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function getClientIPAddress(headers: Headers): string | null {
5252
return parseForwardedHeader(value);
5353
}
5454

55-
return value?.split(', ');
55+
return value?.split(',').map((v: string) => v.trim());
5656
});
5757

5858
// Flatten the array and filter out any falsy entries
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getClientIPAddress } from '../../src/utils/getIpAddress';
2+
3+
class Headers {
4+
private _headers: Record<string, string> = {};
5+
6+
get(key: string): string | null {
7+
return this._headers[key] ?? null;
8+
}
9+
10+
set(key: string, value: string): void {
11+
this._headers[key] = value;
12+
}
13+
}
14+
15+
describe('getClientIPAddress', () => {
16+
it.each([
17+
[
18+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5,2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35',
19+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
20+
],
21+
[
22+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35',
23+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
24+
],
25+
[
26+
'2a01:cb19:8350:ed00:d0dd:INVALID_IP_ADDR:8be5,141.101.69.35,2a01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
27+
'141.101.69.35',
28+
],
29+
[
30+
'2b01:cb19:8350:ed00:d0dd:fa5b:nope:8be5, 2b01:cb19:NOPE:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35 ',
31+
'141.101.69.35',
32+
],
33+
['2b01:cb19:8350:ed00:d0 dd:fa5b:de31:8be5, 141.101.69.35', '141.101.69.35'],
34+
])('should parse the IP from the X-Forwarded-For header %s', (headerValue, expectedIP) => {
35+
const headers = new Headers();
36+
headers.set('X-Forwarded-For', headerValue);
37+
38+
const ip = getClientIPAddress(headers as any);
39+
40+
expect(ip).toEqual(expectedIP);
41+
});
42+
});

0 commit comments

Comments
 (0)