-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathssrf.test.ts
More file actions
177 lines (158 loc) · 5.86 KB
/
Copy pathssrf.test.ts
File metadata and controls
177 lines (158 loc) · 5.86 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
import { describe, expect, it } from 'vitest'
import { isLoopbackIp, isPrivateIp, isPrivateIpHost, unwrapIpv6Brackets } from './ssrf'
describe('isPrivateIp', () => {
describe('IPv4 private/reserved ranges', () => {
it.each([
['192.168.1.1'],
['192.168.0.0'],
['10.0.0.1'],
['10.255.255.255'],
['172.16.0.1'],
['172.31.255.255'],
['127.0.0.1'],
['127.255.255.255'],
['169.254.169.254'],
['0.0.0.0'],
['224.0.0.1'],
])('blocks IPv4 %s', (ip) => {
expect(isPrivateIp(ip)).toBe(true)
})
})
describe('IPv6 reserved ranges', () => {
it.each([['::1'], ['::'], ['fe80::1'], ['fc00::1'], ['fd00::1'], ['ff02::1'], ['2001:db8::1']])(
'blocks IPv6 %s',
(ip) => {
expect(isPrivateIp(ip)).toBe(true)
}
)
})
describe('IPv4-mapped IPv6 (::ffff:0:0/96)', () => {
it.each([
['::ffff:192.168.1.1'],
['::ffff:127.0.0.1'],
['::ffff:169.254.169.254'],
['::ffff:c0a8:101'],
['::ffff:0:0'],
])('blocks mapped private/reserved %s', (ip) => {
expect(isPrivateIp(ip)).toBe(true)
})
it('allows mapped public IPv4 ::ffff:8.8.8.8', () => {
expect(isPrivateIp('::ffff:8.8.8.8')).toBe(false)
})
})
describe('NAT64 (RFC 6052, 64:ff9b::/96)', () => {
it('blocks NAT64-encoded private IPv4', () => {
expect(isPrivateIp('64:ff9b::192.168.1.1')).toBe(true)
})
})
describe('IPv4-compatible IPv6 (::a.b.c.d, RFC 4291 §2.5.5.1, deprecated)', () => {
it.each([
['::c0a8:101', '192.168.1.1 (URL-normalized hex form)'],
['::c0a8:0101', '192.168.1.1 (zero-padded hex form)'],
['::a9fe:a9fe', '169.254.169.254 (cloud metadata)'],
['::7f00:1', '127.0.0.1 (loopback)'],
['::7f00:0001', '127.0.0.1 (zero-padded)'],
['::a00:1', '10.0.0.1 (RFC1918)'],
['::ac10:1', '172.16.0.1 (RFC1918)'],
['::e000:1', '224.0.0.1 (multicast)'],
['::192.168.1.1', 'dotted form ::192.168.1.1'],
['::169.254.169.254', 'dotted form ::169.254.169.254'],
['::127.0.0.1', 'dotted form ::127.0.0.1'],
['::10.0.0.1', 'dotted form ::10.0.0.1'],
])('blocks %s — %s', (ip) => {
expect(isPrivateIp(ip)).toBe(true)
})
it.each([
['::8.8.8.8', 'dotted form embedding public IPv4'],
['::808:808', 'hex form embedding 8.8.8.8'],
['::0808:0808', 'zero-padded hex form embedding 8.8.8.8'],
])('allows IPv4-compatible IPv6 with embedded public IPv4 %s — %s', (ip) => {
expect(isPrivateIp(ip)).toBe(false)
})
it.each([
['::ffff:1', 'embedded 255.255.0.1 (Class E reserved) via parts[6]=0xffff'],
['::ffff:0', 'embedded 255.255.0.0 (Class E reserved)'],
['::ffff:abcd', 'embedded 255.255.171.205 (Class E reserved)'],
['::f000:1', 'embedded 240.0.0.1 (Class E reserved)'],
])('blocks IPv4-compatible IPv6 with Class E embedded IPv4 %s — %s', (ip) => {
expect(isPrivateIp(ip)).toBe(true)
})
})
describe('non-IPv4-compat unicast IPv6 (must not over-block)', () => {
it.each([
['2606:4700:4700::1111'],
['2001:4860:4860::8888'],
['::1:c0a8:101'],
['1::c0a8:101'],
['1:2:3:4:5:6:c0a8:101'],
])('allows %s', (ip) => {
expect(isPrivateIp(ip)).toBe(false)
})
})
describe('IPv4 public addresses', () => {
it.each([['8.8.8.8'], ['1.1.1.1'], ['1.0.0.1']])('allows %s', (ip) => {
expect(isPrivateIp(ip)).toBe(false)
})
})
describe('IPv4 alternate notations', () => {
it.each([['0177.0.0.1'], ['0x7f000001'], ['2130706433']])(
'blocks loopback notation %s',
(ip) => {
expect(isPrivateIp(ip)).toBe(true)
}
)
})
describe('invalid input fails closed', () => {
it.each([['not-an-ip'], [''], ['256.256.256.256'], ['::g'], ['example.com']])(
'rejects %s',
(ip) => {
expect(isPrivateIp(ip)).toBe(true)
}
)
})
describe('URL-parser normalized IPv6 forms', () => {
it('blocks Node-normalized [::192.168.1.1] → ::c0a8:101', () => {
const hostname = new URL('http://[::192.168.1.1]/').hostname
expect(unwrapIpv6Brackets(hostname)).toBe('::c0a8:101')
expect(isPrivateIp(unwrapIpv6Brackets(hostname))).toBe(true)
})
it('blocks Node-normalized [::169.254.169.254] → ::a9fe:a9fe', () => {
const hostname = new URL('http://[::169.254.169.254]/').hostname
expect(unwrapIpv6Brackets(hostname)).toBe('::a9fe:a9fe')
expect(isPrivateIp(unwrapIpv6Brackets(hostname))).toBe(true)
})
})
})
describe('isPrivateIpHost', () => {
it('blocks private/reserved IP literals (IPv4 and IPv6, bracketed or bare)', () => {
expect(isPrivateIpHost('10.0.0.1')).toBe(true)
expect(isPrivateIpHost('169.254.169.254')).toBe(true)
expect(isPrivateIpHost('127.0.0.1')).toBe(true)
expect(isPrivateIpHost('[::1]')).toBe(true)
expect(isPrivateIpHost('[fd00:ec2::254]')).toBe(true)
expect(isPrivateIpHost('[::ffff:127.0.0.1]')).toBe(true)
})
it('allows public IP literals', () => {
expect(isPrivateIpHost('8.8.8.8')).toBe(false)
expect(isPrivateIpHost('[2606:4700:4700::1111]')).toBe(false)
})
it('fails open on DNS names (resolution handled separately)', () => {
expect(isPrivateIpHost('example.com')).toBe(false)
expect(isPrivateIpHost('api.zoominfo.com')).toBe(false)
expect(isPrivateIpHost('localhost')).toBe(false)
})
})
describe('isLoopbackIp', () => {
it('matches the full loopback range and ::1', () => {
expect(isLoopbackIp('127.0.0.1')).toBe(true)
expect(isLoopbackIp('127.0.0.5')).toBe(true)
expect(isLoopbackIp('::1')).toBe(true)
})
it('rejects non-loopback and other private ranges', () => {
expect(isLoopbackIp('10.0.0.1')).toBe(false)
expect(isLoopbackIp('8.8.8.8')).toBe(false)
expect(isLoopbackIp('169.254.169.254')).toBe(false)
expect(isLoopbackIp('not-an-ip')).toBe(false)
expect(isLoopbackIp('localhost')).toBe(false)
})
})