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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
<?php
/**
* @group WebRequest
*/
class WebRequestTest extends MediaWikiTestCase {
protected $oldServer;
protected function setUp() {
parent::setUp();
$this->oldServer = $_SERVER;
IP::clearCaches();
}
protected function tearDown() {
$_SERVER = $this->oldServer;
IP::clearCaches();
parent::tearDown();
}
/**
* @dataProvider provideDetectServer
* @covers WebRequest::detectServer
*/
public function testDetectServer( $expected, $input, $description ) {
$_SERVER = $input;
$result = WebRequest::detectServer();
$this->assertEquals( $expected, $result, $description );
}
public static function provideDetectServer() {
return array(
array(
'http://x',
array(
'HTTP_HOST' => 'x'
),
'Host header'
),
array(
'https://x',
array(
'HTTP_HOST' => 'x',
'HTTPS' => 'on',
),
'Host header with secure'
),
array(
'http://x',
array(
'HTTP_HOST' => 'x',
'SERVER_PORT' => 80,
),
'Default SERVER_PORT',
),
array(
'http://x',
array(
'HTTP_HOST' => 'x',
'HTTPS' => 'off',
),
'Secure off'
),
array(
'http://y',
array(
'SERVER_NAME' => 'y',
),
'Server name'
),
array(
'http://x',
array(
'HTTP_HOST' => 'x',
'SERVER_NAME' => 'y',
),
'Host server name precedence'
),
array(
'http://[::1]:81',
array(
'HTTP_HOST' => '[::1]',
'SERVER_NAME' => '::1',
'SERVER_PORT' => '81',
),
'Apache bug 26005'
),
array(
'http://localhost',
array(
'SERVER_NAME' => '[2001'
),
'Kind of like lighttpd per commit message in MW r83847',
),
array(
'http://[2a01:e35:2eb4:1::2]:777',
array(
'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
),
'Possible lighttpd environment per bug 14977 comment 13',
),
);
}
/**
* @dataProvider provideGetIP
* @covers WebRequest::getIP
*/
public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
$_SERVER = $input;
$this->setMwGlobals( array(
'wgSquidServersNoPurge' => $squid,
'wgUsePrivateIPs' => $private,
'wgHooks' => array(
'IsTrustedProxy' => array(
function ( &$ip, &$trusted ) use ( $xffList ) {
$trusted = $trusted || in_array( $ip, $xffList );
return true;
}
)
)
) );
$request = new WebRequest();
$result = $request->getIP();
$this->assertEquals( $expected, $result, $description );
}
public static function provideGetIP() {
return array(
array(
'127.0.0.1',
array(
'REMOTE_ADDR' => '127.0.0.1'
),
array(),
array(),
false,
'Simple IPv4'
),
array(
'::1',
array(
'REMOTE_ADDR' => '::1'
),
array(),
array(),
false,
'Simple IPv6'
),
array(
'12.0.0.1',
array(
'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
),
array( 'ABCD:1:2:3:4:555:6666:7777' ),
array(),
false,
'IPv6 normalisation'
),
array(
'12.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array(),
false,
'With X-Forwaded-For'
),
array(
'12.0.0.1',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array(),
array(),
false,
'With X-Forwaded-For and disallowed server'
),
array(
'12.0.0.2',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array( '12.0.0.1' ),
array(),
false,
'With multiple X-Forwaded-For and only one allowed server'
),
array(
'10.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array(),
false,
'With X-Forwaded-For and private IP (from cache proxy)'
),
array(
'10.0.0.4',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
array(),
true,
'With X-Forwaded-For and private IP (allowed)'
),
array(
'10.0.0.4',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array( '10.0.0.3' ),
true,
'With X-Forwaded-For and private IP (allowed)'
),
array(
'10.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
),
array( '12.0.0.1', '12.0.0.2' ),
array( '10.0.0.3' ),
false,
'With X-Forwaded-For and private IP (disallowed)'
),
array(
'12.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array(),
array( '12.0.0.1', '12.0.0.2' ),
false,
'With X-Forwaded-For'
),
array(
'12.0.0.2',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array(),
array( '12.0.0.1' ),
false,
'With multiple X-Forwaded-For and only one allowed server'
),
array(
'12.0.0.2',
array(
'REMOTE_ADDR' => '12.0.0.2',
'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
),
array(),
array( '12.0.0.2' ),
false,
'With X-Forwaded-For and private IP and hook (disallowed)'
),
array(
'12.0.0.1',
array(
'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
),
array( 'ABCD:1:2:3::/64' ),
array(),
false,
'IPv6 CIDR'
),
array(
'12.0.0.3',
array(
'REMOTE_ADDR' => '12.0.0.1',
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
),
array( '12.0.0.0/24' ),
array(),
false,
'IPv4 CIDR'
),
);
}
/**
* @expectedException MWException
* @covers WebRequest::getIP
*/
public function testGetIpLackOfRemoteAddrThrowAnException() {
// ensure that local install state doesn't interfere with test
$this->setMwGlobals( array(
'wgSquidServersNoPurge' => array(),
'wgSquidServers' => array(),
'wgUsePrivateIPs' => false,
'wgHooks' => array(),
) );
$request = new WebRequest();
# Next call throw an exception about lacking an IP
$request->getIP();
}
public static function provideLanguageData() {
return array(
array( '', array(), 'Empty Accept-Language header' ),
array( 'en', array( 'en' => 1 ), 'One language' ),
array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
array(
'zh-cn,zh-tw',
array( 'zh-cn' => 1, 'zh-tw' => 1 ),
'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
),
array(
'es, en; q=0.5',
array( 'es' => 1, 'en' => '0.5' ),
'Spanish as first language and English and second'
),
array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
array(
'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
array( 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ),
'Preference for Romance languages'
),
array(
'en-gb, en-us; q=1',
array( 'en-gb' => 1, 'en-us' => '1' ),
'Two equally prefered English variants'
),
);
}
/**
* @dataProvider provideLanguageData
* @covers WebRequest::getAcceptLang
*/
public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
$_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
$request = new WebRequest();
$this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
}
}
|