1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
The MIT License (MIT) |
4
|
|
|
|
5
|
|
|
Copyright (c) 2015 Vectorface, Inc. |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in |
15
|
|
|
all copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
THE SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
namespace Vectorface\WhipTests; |
26
|
|
|
|
27
|
|
|
use PHPUnit_Framework_TestCase; |
28
|
|
|
use Vectorface\Whip\Whip; |
29
|
|
|
use Vectorface\Whip\IpRange\IpWhitelist; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Test class for testing Whip. |
33
|
|
|
* @backupGlobals enabled |
34
|
|
|
* @copyright Vectorface, Inc 2015 |
35
|
|
|
* @author Daniel Bruce <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class WhipTest extends PHPUnit_Framework_TestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Tests that we get back 127.0.0.1 when there is no superglobal information |
41
|
|
|
* at all. |
42
|
|
|
*/ |
43
|
|
|
public function testEmptySuperglobal() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$_SERVER = array(); |
46
|
|
|
$lookup = new Whip(); |
47
|
|
|
$this->assertFalse($lookup->getIpAddress()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Tests that we get false when no valid IP address could be found. |
52
|
|
|
*/ |
53
|
|
|
public function testNoAddresFoundDueToBitmask() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$_SERVER = array('REMOTE_ADDR' => '127.0.0.1'); |
56
|
|
|
$lookup = new Whip(Whip::PROXY_HEADERS); |
57
|
|
|
$this->assertFalse($lookup->getIpAddress()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Tests the standard REMOTE_ADDR method. |
62
|
|
|
*/ |
63
|
|
|
public function testRemoteAddrMethod() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$_SERVER = array('REMOTE_ADDR' => '24.24.24.24'); |
66
|
|
|
$lookup = new Whip(Whip::REMOTE_ADDR); |
67
|
|
|
$this->assertEquals('24.24.24.24', $lookup->getValidIpAddress()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Tests that an invalid IPv4 address returns false. |
72
|
|
|
*/ |
73
|
|
|
public function testInvalidIPv4Address() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$_SERVER = array('REMOTE_ADDR' => '127.0.0.256'); |
76
|
|
|
$lookup = new Whip(Whip::REMOTE_ADDR); |
77
|
|
|
$this->assertFalse($lookup->getValidIpAddress()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Tests a valid IPv6 address. |
82
|
|
|
*/ |
83
|
|
|
public function testValidIPv6Address() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$_SERVER = array('REMOTE_ADDR' => '::1'); |
86
|
|
|
$lookup = new Whip(Whip::REMOTE_ADDR); |
87
|
|
|
$this->assertEquals('::1', $lookup->getValidIpAddress()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Tests that we accept whitelisted proxy methods when the IP matches, even |
92
|
|
|
* if the IP listed is a comma separated list. |
93
|
|
|
*/ |
94
|
|
|
public function testValidWhitelistedProxyMethod() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$_SERVER = array( |
97
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
98
|
|
|
'HTTP_X_FORWARDED_FOR' => '192.168.1.1,32.32.32.32' |
99
|
|
|
); |
100
|
|
|
$lookup = new Whip( |
101
|
|
|
Whip::PROXY_HEADERS, |
102
|
|
|
array( |
103
|
|
|
Whip::PROXY_HEADERS => array( |
104
|
|
|
IpWhitelist::IPV4 => array( |
105
|
|
|
'127.0.0.1' |
106
|
|
|
), |
107
|
|
|
IpWhitelist::IPV6 => array( |
108
|
|
|
'::1' |
109
|
|
|
) |
110
|
|
|
) |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
$this->assertEquals('32.32.32.32', $lookup->getIpAddress()); |
114
|
|
|
|
115
|
|
|
$_SERVER['REMOTE_ADDR'] = '::1'; /* Repeat test for IPv6 */ |
116
|
|
|
$this->assertEquals('32.32.32.32', $lookup->getIpAddress()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Tests that we accept proxy method based on a whitelisted IP using the |
121
|
|
|
* dashed range notation. |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function testValidWhitelistedProxyMethodWithDashNotation() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$_SERVER = array( |
126
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
127
|
|
|
'HTTP_X_FORWARDED_FOR' => '32.32.32.32' |
128
|
|
|
); |
129
|
|
|
$lookup = new Whip( |
130
|
|
|
Whip::PROXY_HEADERS, |
131
|
|
|
array( |
132
|
|
|
Whip::PROXY_HEADERS => array( |
133
|
|
|
IpWhitelist::IPV4 => array( |
134
|
|
|
'127.0.0.0-127.0.255.255', |
135
|
|
|
), |
136
|
|
|
IpWhitelist::IPV6 => array( |
137
|
|
|
'::1' |
138
|
|
|
) |
139
|
|
|
) |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
$this->assertEquals('32.32.32.32', $lookup->getIpAddress()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Tests that we accept proxy method based on a whitelisted IP using the |
147
|
|
|
* wildcard asterix notation. |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function testValidWhitelistedProxyMethodWithWildcardNotation() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$_SERVER = array( |
152
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
153
|
|
|
'HTTP_X_FORWARDED_FOR' => '32.32.32.32' |
154
|
|
|
); |
155
|
|
|
$lookup = new Whip( |
156
|
|
|
Whip::PROXY_HEADERS, |
157
|
|
|
array( |
158
|
|
|
Whip::PROXY_HEADERS => array( |
159
|
|
|
IpWhitelist::IPV4 => array( |
160
|
|
|
'127.0.*' |
161
|
|
|
), |
162
|
|
|
IpWhitelist::IPV6 => array( |
163
|
|
|
'::1' |
164
|
|
|
) |
165
|
|
|
) |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
$this->assertEquals('32.32.32.32', $lookup->getIpAddress()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Tests that we accept proxy method based on a whitelisted IP using the |
173
|
|
|
* CIDR address notation. |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function testValidWhitelistedProxyMethodWithCIDRdNotation() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$_SERVER = array( |
178
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
179
|
|
|
'HTTP_X_FORWARDED_FOR' => '32.32.32.32' |
180
|
|
|
); |
181
|
|
|
$lookup = new Whip( |
182
|
|
|
Whip::PROXY_HEADERS, |
183
|
|
|
array( |
184
|
|
|
Whip::PROXY_HEADERS => array( |
185
|
|
|
IpWhitelist::IPV4 => array( |
186
|
|
|
'127.0.0.0/24' |
187
|
|
|
), |
188
|
|
|
IpWhitelist::IPV6 => array( |
189
|
|
|
'::1' |
190
|
|
|
) |
191
|
|
|
) |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
$this->assertEquals('32.32.32.32', $lookup->getIpAddress()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Tests that we get false if there is a valid IP in a proxy header but |
199
|
|
|
* we reject it due to REMOTE_ADDR not being in the whitelist. |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function testValidIpRejectedDueToWhitelist() |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$_SERVER = array( |
204
|
|
|
'REMOTE_ADDR' => '24.24.24.24', |
205
|
|
|
'HTTP_X_FORWARDED_FOR' => '32.32.32.32' |
206
|
|
|
); |
207
|
|
|
$lookup = new Whip( |
208
|
|
|
Whip::PROXY_HEADERS, |
209
|
|
|
array( |
210
|
|
|
Whip::PROXY_HEADERS => array( |
211
|
|
|
IpWhitelist::IPV4 => array( |
212
|
|
|
'127.0.0.1/24' |
213
|
|
|
), |
214
|
|
|
IpWhitelist::IPV6 => array( |
215
|
|
|
'::1' |
216
|
|
|
) |
217
|
|
|
) |
218
|
|
|
) |
219
|
|
|
); |
220
|
|
|
$this->assertFalse($lookup->getIpAddress()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Tests that we reject a proxy listed IPv6 address that does not fall within |
225
|
|
|
* the allowed subnet. |
226
|
|
|
*/ |
227
|
|
|
public function testIPv6AddressRejectedDueToWhitelist() |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
$_SERVER = array( |
230
|
|
|
'REMOTE_ADDR' => '::1', |
231
|
|
|
'HTTP_X_FORWARDED_FOR' => '::1' |
232
|
|
|
); |
233
|
|
|
$lookup = new Whip( |
234
|
|
|
Whip::PROXY_HEADERS, |
235
|
|
|
array( |
236
|
|
|
Whip::PROXY_HEADERS => array( |
237
|
|
|
IpWhitelist::IPV6 => array( |
238
|
|
|
'2400:cb00::/32' |
239
|
|
|
) |
240
|
|
|
) |
241
|
|
|
) |
242
|
|
|
); |
243
|
|
|
$this->assertFalse($lookup->getIpAddress()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Tests that we reject a proxy listed IPv6 address that does not fall within |
248
|
|
|
* the allowed subnet. |
249
|
|
|
*/ |
250
|
|
View Code Duplication |
public function testIPv6AddressFoundInWhitelist() |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
$_SERVER = array( |
253
|
|
|
'REMOTE_ADDR' => '::1', |
254
|
|
|
'HTTP_X_FORWARDED_FOR' => '::1' |
255
|
|
|
); |
256
|
|
|
$lookup = new Whip( |
257
|
|
|
Whip::PROXY_HEADERS, |
258
|
|
|
array( |
259
|
|
|
Whip::PROXY_HEADERS => array( |
260
|
|
|
IpWhitelist::IPV6 => array( |
261
|
|
|
'::1/32' |
262
|
|
|
) |
263
|
|
|
) |
264
|
|
|
) |
265
|
|
|
); |
266
|
|
|
$this->assertEquals('::1', $lookup->getIpAddress()); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Test that an IPv4 address is rejected because the whitelist is empty for |
271
|
|
|
* IPv4. |
272
|
|
|
*/ |
273
|
|
|
public function testIPv4AddressRejectedDueToEmptyWhitelist() |
|
|
|
|
274
|
|
|
{ |
275
|
|
|
$_SERVER = array( |
276
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
277
|
|
|
'HTTP_X_FORWARDED_FOR' => '24.24.24.24' |
278
|
|
|
); |
279
|
|
|
$lookup = new Whip( |
280
|
|
|
Whip::PROXY_HEADERS, |
281
|
|
|
array( |
282
|
|
|
Whip::PROXY_HEADERS => array( |
283
|
|
|
IpWhitelist::IPV6 => array( |
284
|
|
|
'::1/32' |
285
|
|
|
) |
286
|
|
|
) |
287
|
|
|
) |
288
|
|
|
); |
289
|
|
|
$this->assertFalse($lookup->getIpAddress()); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Test that an IPv6 address is rejected because the whitelist is empty for |
294
|
|
|
* IPv6. |
295
|
|
|
*/ |
296
|
|
|
public function testIPv6AddressRejectedDueToEmptyWhitelist() |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
$_SERVER = array( |
299
|
|
|
'REMOTE_ADDR' => '::1', |
300
|
|
|
'HTTP_X_FORWARDED_FOR' => '::1' |
301
|
|
|
); |
302
|
|
|
$lookup = new Whip( |
303
|
|
|
Whip::PROXY_HEADERS, |
304
|
|
|
array( |
305
|
|
|
Whip::PROXY_HEADERS => array( |
306
|
|
|
IpWhitelist::IPV4 => array( |
307
|
|
|
'127.0.0.0/24' |
308
|
|
|
) |
309
|
|
|
) |
310
|
|
|
) |
311
|
|
|
); |
312
|
|
|
$this->assertFalse($lookup->getIpAddress()); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Test a custom header with a whitelisted IP. |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
public function testCustomHeader() |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$_SERVER = array( |
321
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
322
|
|
|
'HTTP_CUSTOM_SECRET_HEADER' => '32.32.32.32' |
323
|
|
|
); |
324
|
|
|
$lookup = new Whip( |
325
|
|
|
Whip::CUSTOM_HEADERS | Whip::REMOTE_ADDR, |
326
|
|
|
array( |
327
|
|
|
Whip::CUSTOM_HEADERS => array( |
328
|
|
|
IpWhitelist::IPV4 => array( |
329
|
|
|
'127.0.0.1', |
330
|
|
|
'::1' |
331
|
|
|
) |
332
|
|
|
) |
333
|
|
|
) |
334
|
|
|
); |
335
|
|
|
$this->assertEquals( |
336
|
|
|
'32.32.32.32', |
337
|
|
|
$lookup->addCustomHeader('HTTP_CUSTOM_SECRET_HEADER')->getIpAddress() |
338
|
|
|
); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Test HTTP_X_REAL_IP header. |
343
|
|
|
*/ |
344
|
|
|
public function testHttpXRealIpHeader() |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
$_SERVER = array( |
347
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
348
|
|
|
'HTTP_X_REAL_IP' => '24.24.24.24' |
349
|
|
|
); |
350
|
|
|
$lookup = new Whip( |
351
|
|
|
Whip::PROXY_HEADERS | Whip::REMOTE_ADDR |
352
|
|
|
); |
353
|
|
|
$this->assertEquals('24.24.24.24', $lookup->getIpAddress()); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Tests that if we specify the source array, it overrides any values found |
358
|
|
|
* in the $_SERVER array. |
359
|
|
|
*/ |
360
|
|
View Code Duplication |
public function testSourceArrayOverridesServerSuperglobal() |
|
|
|
|
361
|
|
|
{ |
362
|
|
|
$_SERVER = array( |
363
|
|
|
'REMOTE_ADDR' => '127.0.0.1' |
364
|
|
|
); |
365
|
|
|
$source = array( |
366
|
|
|
'REMOTE_ADDR' => '24.24.24.24' |
367
|
|
|
); |
368
|
|
|
$lookup = new Whip(Whip::REMOTE_ADDR); |
369
|
|
|
$this->assertNotEquals($source['REMOTE_ADDR'], $lookup->getIpAddress()); |
370
|
|
|
$this->assertEquals($source['REMOTE_ADDR'], $lookup->getIpAddress($source)); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Tests that if we specify the source array through Whip::setSource, the |
375
|
|
|
* class will override any values found in $_SERVER. |
376
|
|
|
*/ |
377
|
|
View Code Duplication |
public function testSetSourceArrayOverridesServerSuperglobal() |
|
|
|
|
378
|
|
|
{ |
379
|
|
|
$_SERVER = array( |
380
|
|
|
'REMOTE_ADDR' => '127.0.0.1' |
381
|
|
|
); |
382
|
|
|
$source = array( |
383
|
|
|
'REMOTE_ADDR' => '24.24.24.24' |
384
|
|
|
); |
385
|
|
|
$lookup = new Whip(Whip::REMOTE_ADDR); |
386
|
|
|
$this->assertNotEquals($source['REMOTE_ADDR'], $lookup->getIpAddress()); |
387
|
|
|
$lookup->setSource($source); |
388
|
|
|
$this->assertEquals($source['REMOTE_ADDR'], $lookup->getIpAddress()); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: