Repository navigation
Expand file tree
/
Copy pathgetClientIp.test.ts
More file actions
181 lines (157 loc) · 5.42 KB
/
Copy pathgetClientIp.test.ts
File metadata and controls
181 lines (157 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { describe, expect, it } from 'vitest';
import { NextRequest } from 'next/server';
import { getClientIp } from './getClientIp';
describe('getClientIp', () => {
it('prefers request.ip on NextRequest if available', () => {
const req = new NextRequest('http://localhost:3000/api/streak');
Object.defineProperty(req, 'ip', { value: '203.0.113.10', writable: true });
const ip = getClientIp(req);
expect(ip).toBe('203.0.113.10');
});
it('ignores spoofed X-Forwarded-For when request.ip is present', () => {
const req = new NextRequest('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '198.51.100.5, 203.0.113.10',
},
});
Object.defineProperty(req, 'ip', { value: '203.0.113.10', writable: true });
const ip = getClientIp(req);
expect(ip).toBe('203.0.113.10');
});
it('ignores direct client-supplied custom priority headers', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'cf-connecting-ip': '198.51.100.99',
},
});
const ip = getClientIp(req);
expect(ip).toBe('127.0.0.1');
});
it('ignores x-forwarded-for entirely when no trusted proxies are configured', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '198.51.100.5, 203.0.113.10',
},
});
const ip = getClientIp(req, {
proxyConfig: { trustedProxies: [], trustPrivateRanges: false },
});
expect(ip).toBe('127.0.0.1');
});
it('extracts correct IP through a trusted proxy chain', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '198.51.100.5, 203.0.113.10, 127.0.0.1',
},
});
// We trust localhost (127.0.0.1) and 203.0.113.10 as proxies, so the true client is 198.51.100.5
const ip = getClientIp(req, {
proxyConfig: {
trustedProxies: ['127.0.0.1', '203.0.113.10'],
trustPrivateRanges: true,
},
directIp: '127.0.0.1',
});
expect(ip).toBe('198.51.100.5');
});
it('stops at the first untrusted proxy in the chain', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '198.51.100.5, 8.8.8.8, 127.0.0.1',
},
});
// 127.0.0.1 is trusted. 8.8.8.8 is not. So the resolved IP must be 8.8.8.8.
const ip = getClientIp(req, {
proxyConfig: {
trustedProxies: ['127.0.0.1'],
trustPrivateRanges: true,
},
directIp: '127.0.0.1',
});
expect(ip).toBe('8.8.8.8');
});
it('supports wildcards to trust all proxies (trust-all behavior)', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '198.51.100.5, 203.0.113.10',
},
});
const ip = getClientIp(req, {
proxyConfig: {
trustedProxies: ['*'],
trustPrivateRanges: false,
},
directIp: '203.0.113.10',
});
expect(ip).toBe('198.51.100.5');
});
it('sanitizes multi-IP lists and traverses untrusted hops from right to left', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '203.0.113.195, 198.51.100.10, 192.168.1.1',
},
});
const options = {
proxyConfig: {
trustedProxies: ['192.168.1.1'],
trustPrivateRanges: false,
},
directIp: '192.168.1.1',
};
expect(getClientIp(req, options)).toBe('198.51.100.10');
});
it('returns the direct peer and ignores spoofed headers when the peer is not trusted', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '127.0.0.1',
'x-real-ip': '127.0.0.1',
'cf-connecting-ip': '127.0.0.1',
},
});
expect(
getClientIp(req, {
proxyConfig: { trustedProxies: ['10.0.0.1'], trustPrivateRanges: false },
directIp: '198.51.100.25',
})
).toBe('198.51.100.25');
});
it('does not allow rotating forwarded headers to rotate the resolved IP', () => {
const options = {
proxyConfig: { trustedProxies: [], trustPrivateRanges: false },
directIp: '198.51.100.25',
};
const first = new Request('http://localhost/api/streak', {
headers: { 'x-real-ip': '1.1.1.1' },
});
const second = new Request('http://localhost/api/streak', {
headers: { 'x-real-ip': '8.8.8.8' },
});
expect(getClientIp(first, options)).toBe('198.51.100.25');
expect(getClientIp(second, options)).toBe('198.51.100.25');
});
it('falls back to 127.0.0.1 when headers are missing or malformed', () => {
const req = new Request('http://localhost:3000/api/streak');
expect(getClientIp(req)).toBe('127.0.0.1');
const reqMalformed = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': ' , ',
},
});
expect(getClientIp(reqMalformed)).toBe('127.0.0.1');
});
it('does not return a trusted proxy as the client IP when it is the only hop in the chain', () => {
const req = new Request('http://localhost:3000/api/streak', {
headers: {
'x-forwarded-for': '203.0.113.5',
},
});
const ip = getClientIp(req, {
proxyConfig: {
trustedProxies: ['203.0.113.5'],
trustPrivateRanges: false,
},
directIp: '203.0.113.5',
});
expect(ip).toBe('127.0.0.1');
});
});