| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Request\ProxyLookup; |
| 5 |
use MediaWiki\Request\WebRequest; |
| 6 |
use MediaWiki\User\UserIdentity; |
| 7 |
use MediaWiki\User\UserIdentityValue; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class WebRequestTest extends MediaWikiIntegrationTestCase { |
| 15 |
private const INTERNAL_SERVER = 'http://wiki.site'; |
| 16 |
|
| 17 |
|
| 18 |
private $oldServer; |
| 19 |
|
| 20 |
|
| 21 |
private $oldCookies; |
| 22 |
|
| 23 |
protected function setUp(): void { |
| 24 |
parent::setUp(); |
| 25 |
|
| 26 |
$this->oldServer = $_SERVER; |
| 27 |
$this->oldCookies = $_COOKIE; |
| 28 |
} |
| 29 |
|
| 30 |
protected function tearDown(): void { |
| 31 |
$_SERVER = $this->oldServer; |
| 32 |
$_COOKIE = $this->oldCookies; |
| 33 |
|
| 34 |
parent::tearDown(); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testGetCookie( $expected, $cookies, $prefix, $cookieName, $defaultValue ) { |
| 47 |
$_COOKIE = $cookies; |
| 48 |
$request = new WebRequest(); |
| 49 |
|
| 50 |
$actual = $defaultValue !== null ? |
| 51 |
$request->getCookie( $cookieName, $prefix, $defaultValue ) : |
| 52 |
$request->getCookie( $cookieName, $prefix ); |
| 53 |
|
| 54 |
$this->assertSame( $expected, $actual ); |
| 55 |
} |
| 56 |
|
| 57 |
public static function provideGetCookie() { |
| 58 |
yield 'no cookies with no default override' => [ null, [], '', 'test', null ]; |
| 59 |
yield 'no cookies with explicit default override' => [ false, [], '', 'test', false ]; |
| 60 |
|
| 61 |
yield 'missing cookie with no default override' => [ null, [ 'other' => 'bar' ], '', 'test', null ]; |
| 62 |
yield 'missing cookie with explicit default override' => [ false, [ 'other' => 'bar' ], '', 'test', false ]; |
| 63 |
|
| 64 |
yield 'cookie not matching prefix with no default override' => [ |
| 65 |
null, [ 'test' => 'bar' ], 'prefix', 'test', null |
| 66 |
]; |
| 67 |
yield 'cookie not matching prefix with explicit default override' => [ |
| 68 |
false, [ 'test' => 'bar' ], 'prefix', 'test', false |
| 69 |
]; |
| 70 |
|
| 71 |
|
| 72 |
yield 'cookie with array value with no default override' => [ null, [ 'test' => [ 'bar' ] ], '', 'test', null ]; |
| 73 |
yield 'cookie with array value explicit default override' => [ false, [ 'test' => [ 'bar' ] ], '', 'test', false ]; |
| 74 |
|
| 75 |
yield 'valid cookie' => [ 'value', [ 'test' => 'value' ], '', 'test', null ]; |
| 76 |
yield 'valid cookie with prefix' => [ 'value', [ 'prefixtest' => 'value' ], 'prefix', 'test', null ]; |
| 77 |
yield 'mangled cookie name' => [ 'value', [ 'test_mangled' => 'value' ], '', 'test.mangled', null ]; |
| 78 |
yield 'mangled cookie name with prefix' => [ |
| 79 |
'value', [ 'prefix_partstest_mangled' => 'value' ], 'prefix.parts', 'test.mangled', null |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function testDetectServer( $expected, $input, $description ) { |
| 87 |
$this->setServerVars( $input ); |
| 88 |
$result = WebRequest::detectServer( true ); |
| 89 |
$this->assertEquals( $expected, $result, $description ); |
| 90 |
} |
| 91 |
|
| 92 |
public static function provideDetectServer() { |
| 93 |
return [ |
| 94 |
[ |
| 95 |
'http://x', |
| 96 |
[ |
| 97 |
'HTTP_HOST' => 'x' |
| 98 |
], |
| 99 |
'Host header' |
| 100 |
], |
| 101 |
[ |
| 102 |
'https://x', |
| 103 |
[ |
| 104 |
'HTTP_HOST' => 'x', |
| 105 |
'HTTPS' => 'on', |
| 106 |
], |
| 107 |
'Host header with secure' |
| 108 |
], |
| 109 |
[ |
| 110 |
'http://x', |
| 111 |
[ |
| 112 |
'HTTP_HOST' => 'x:80', |
| 113 |
], |
| 114 |
'Host header with port' |
| 115 |
], |
| 116 |
[ |
| 117 |
'http://x', |
| 118 |
[ |
| 119 |
'HTTP_HOST' => 'x', |
| 120 |
'SERVER_PORT' => 80, |
| 121 |
], |
| 122 |
'Default SERVER_PORT as int', |
| 123 |
], |
| 124 |
[ |
| 125 |
'http://x', |
| 126 |
[ |
| 127 |
'HTTP_HOST' => 'x', |
| 128 |
'SERVER_PORT' => '80', |
| 129 |
], |
| 130 |
'Default SERVER_PORT as string', |
| 131 |
], |
| 132 |
[ |
| 133 |
'http://x', |
| 134 |
[ |
| 135 |
'HTTP_HOST' => 'x', |
| 136 |
'HTTPS' => 'off', |
| 137 |
], |
| 138 |
'Secure off' |
| 139 |
], |
| 140 |
[ |
| 141 |
'https://x', |
| 142 |
[ |
| 143 |
'HTTP_HOST' => 'x', |
| 144 |
'HTTP_X_FORWARDED_PROTO' => 'https', |
| 145 |
], |
| 146 |
'Forwarded HTTPS' |
| 147 |
], |
| 148 |
[ |
| 149 |
'https://x', |
| 150 |
[ |
| 151 |
'HTTP_HOST' => 'x', |
| 152 |
'HTTPS' => 'off', |
| 153 |
'SERVER_PORT' => '81', |
| 154 |
'HTTP_X_FORWARDED_PROTO' => 'https', |
| 155 |
], |
| 156 |
'Forwarded HTTPS' |
| 157 |
], |
| 158 |
[ |
| 159 |
'http://y', |
| 160 |
[ |
| 161 |
'SERVER_NAME' => 'y', |
| 162 |
], |
| 163 |
'Server name' |
| 164 |
], |
| 165 |
[ |
| 166 |
'http://x', |
| 167 |
[ |
| 168 |
'HTTP_HOST' => 'x', |
| 169 |
'SERVER_NAME' => 'y', |
| 170 |
], |
| 171 |
'Host server name precedence' |
| 172 |
], |
| 173 |
[ |
| 174 |
'http://[::1]:81', |
| 175 |
[ |
| 176 |
'HTTP_HOST' => '[::1]', |
| 177 |
'SERVER_NAME' => '::1', |
| 178 |
'SERVER_PORT' => '81', |
| 179 |
], |
| 180 |
'Apache bug 26005' |
| 181 |
], |
| 182 |
[ |
| 183 |
'http://localhost', |
| 184 |
[ |
| 185 |
'SERVER_NAME' => '[2001' |
| 186 |
], |
| 187 |
'Kind of like lighttpd per commit message in MW r83847', |
| 188 |
], |
| 189 |
[ |
| 190 |
'http://[2a01:e35:2eb4:1::2]:777', |
| 191 |
[ |
| 192 |
'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777' |
| 193 |
], |
| 194 |
'Possible lighttpd environment per bug 14977 comment 13', |
| 195 |
], |
| 196 |
]; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
protected function mockWebRequest( array $data = [], array $config = [] ) { |
| 206 |
|
| 207 |
|
| 208 |
$reflection = new ReflectionClass( WebRequest::class ); |
| 209 |
$req = $reflection->newInstanceWithoutConstructor(); |
| 210 |
|
| 211 |
$prop = $reflection->getProperty( 'data' ); |
| 212 |
$prop->setValue( $req, $data ); |
| 213 |
|
| 214 |
if ( isset( $config['requestTime'] ) ) { |
| 215 |
$prop = $reflection->getProperty( 'requestTime' ); |
| 216 |
$prop->setValue( $req, $config['requestTime'] ); |
| 217 |
} |
| 218 |
|
| 219 |
return $req; |
| 220 |
} |
| 221 |
|
| 222 |
public function testGetElapsedTime() { |
| 223 |
$now = microtime( true ) - 10.0; |
| 224 |
$req = $this->mockWebRequest( [], [ 'requestTime' => $now ] ); |
| 225 |
$this->assertGreaterThanOrEqual( 10.0, $req->getElapsedTime() ); |
| 226 |
|
| 227 |
$this->assertEqualsWithDelta( 10.0, $req->getElapsedTime(), 60.0 ); |
| 228 |
} |
| 229 |
|
| 230 |
public function testGetValNormal() { |
| 231 |
|
| 232 |
$input = "a \x00 null"; |
| 233 |
$normal = "a \xef\xbf\xbd null"; |
| 234 |
$req = $this->mockWebRequest( [ 'x' => $input, 'y' => [ $input, $input ] ] ); |
| 235 |
$this->assertSame( $normal, $req->getVal( 'x' ) ); |
| 236 |
$this->assertNotSame( $input, $req->getVal( 'x' ) ); |
| 237 |
$this->assertSame( [ $normal, $normal ], $req->getArray( 'y' ) ); |
| 238 |
} |
| 239 |
|
| 240 |
public function testGetVal() { |
| 241 |
$req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a' ], 'crlf' => "A\r\nb" ] ); |
| 242 |
$this->assertSame( 'Value', $req->getVal( 'x' ), 'Simple value' ); |
| 243 |
$this->assertNull( $req->getVal( 'z' ), 'Not found' ); |
| 244 |
$this->assertNull( $req->getVal( 'y' ), 'Array is ignored' ); |
| 245 |
$this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' ); |
| 246 |
} |
| 247 |
|
| 248 |
public function testGetRawVal() { |
| 249 |
$req = $this->mockWebRequest( [ |
| 250 |
'x' => 'Value', |
| 251 |
'y' => [ 'a' ], |
| 252 |
'crlf' => "A\r\nb" |
| 253 |
] ); |
| 254 |
$this->assertSame( 'Value', $req->getRawVal( 'x' ) ); |
| 255 |
$this->assertNull( $req->getRawVal( 'z' ), 'Not found' ); |
| 256 |
$this->assertNull( $req->getRawVal( 'y' ), 'Array is ignored' ); |
| 257 |
$this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' ); |
| 258 |
} |
| 259 |
|
| 260 |
public function testGetArray() { |
| 261 |
$req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a', 'b' ] ] ); |
| 262 |
$this->assertSame( [ 'Value' ], $req->getArray( 'x' ), 'Value becomes array' ); |
| 263 |
$this->assertNull( $req->getArray( 'z' ), 'Not found' ); |
| 264 |
$this->assertSame( [ 'a', 'b' ], $req->getArray( 'y' ) ); |
| 265 |
} |
| 266 |
|
| 267 |
public function testGetIntArray() { |
| 268 |
$req = $this->mockWebRequest( [ 'x' => [ 'Value' ], 'y' => [ '0', '4.2', '-2' ] ] ); |
| 269 |
$this->assertSame( [ 0 ], $req->getIntArray( 'x' ), 'Text becomes 0' ); |
| 270 |
$this->assertNull( $req->getIntArray( 'z' ), 'Not found' ); |
| 271 |
$this->assertSame( [ 0, 4, -2 ], $req->getIntArray( 'y' ) ); |
| 272 |
} |
| 273 |
|
| 274 |
public function testGetInt() { |
| 275 |
$req = $this->mockWebRequest( [ |
| 276 |
'x' => 'Value', |
| 277 |
'y' => [ 'a' ], |
| 278 |
'zero' => '0', |
| 279 |
'answer' => '4.2', |
| 280 |
'neg' => '-2', |
| 281 |
] ); |
| 282 |
$this->assertSame( 0, $req->getInt( 'x' ), 'Text' ); |
| 283 |
$this->assertSame( 0, $req->getInt( 'y' ), 'Array' ); |
| 284 |
$this->assertSame( 0, $req->getInt( 'z' ), 'Not found' ); |
| 285 |
$this->assertSame( 0, $req->getInt( 'zero' ) ); |
| 286 |
$this->assertSame( 4, $req->getInt( 'answer' ) ); |
| 287 |
$this->assertSame( -2, $req->getInt( 'neg' ) ); |
| 288 |
} |
| 289 |
|
| 290 |
public function testGetIntOrNull() { |
| 291 |
$req = $this->mockWebRequest( [ |
| 292 |
'x' => 'Value', |
| 293 |
'y' => [ 'a' ], |
| 294 |
'zero' => '0', |
| 295 |
'answer' => '4.2', |
| 296 |
'neg' => '-2', |
| 297 |
] ); |
| 298 |
$this->assertNull( $req->getIntOrNull( 'x' ), 'Text' ); |
| 299 |
$this->assertNull( $req->getIntOrNull( 'y' ), 'Array' ); |
| 300 |
$this->assertNull( $req->getIntOrNull( 'z' ), 'Not found' ); |
| 301 |
$this->assertSame( 0, $req->getIntOrNull( 'zero' ) ); |
| 302 |
$this->assertSame( 4, $req->getIntOrNull( 'answer' ) ); |
| 303 |
$this->assertSame( -2, $req->getIntOrNull( 'neg' ) ); |
| 304 |
} |
| 305 |
|
| 306 |
public function testGetFloat() { |
| 307 |
$req = $this->mockWebRequest( [ |
| 308 |
'x' => 'Value', |
| 309 |
'y' => [ 'a' ], |
| 310 |
'zero' => '0', |
| 311 |
'answer' => '4.2', |
| 312 |
'neg' => '-2', |
| 313 |
] ); |
| 314 |
$this->assertSame( 0.0, $req->getFloat( 'x' ), 'Text' ); |
| 315 |
$this->assertSame( 0.0, $req->getFloat( 'y' ), 'Array' ); |
| 316 |
$this->assertSame( 0.0, $req->getFloat( 'z' ), 'Not found' ); |
| 317 |
$this->assertSame( 0.0, $req->getFloat( 'zero' ) ); |
| 318 |
$this->assertSame( 4.2, $req->getFloat( 'answer' ) ); |
| 319 |
$this->assertSame( -2.0, $req->getFloat( 'neg' ) ); |
| 320 |
} |
| 321 |
|
| 322 |
public function testGetBool() { |
| 323 |
$req = $this->mockWebRequest( [ |
| 324 |
'x' => 'Value', |
| 325 |
'y' => [ 'a' ], |
| 326 |
'zero' => '0', |
| 327 |
'f' => 'false', |
| 328 |
't' => 'true', |
| 329 |
] ); |
| 330 |
$this->assertTrue( $req->getBool( 'x' ), 'Text' ); |
| 331 |
$this->assertFalse( $req->getBool( 'y' ), 'Array' ); |
| 332 |
$this->assertFalse( $req->getBool( 'z' ), 'Not found' ); |
| 333 |
$this->assertFalse( $req->getBool( 'zero' ) ); |
| 334 |
$this->assertTrue( $req->getBool( 'f' ) ); |
| 335 |
$this->assertTrue( $req->getBool( 't' ) ); |
| 336 |
} |
| 337 |
|
| 338 |
public static function provideFuzzyBool() { |
| 339 |
return [ |
| 340 |
[ 'Text', true ], |
| 341 |
[ '', false, '(empty string)' ], |
| 342 |
[ '0', false ], |
| 343 |
[ '1', true ], |
| 344 |
[ 'false', false ], |
| 345 |
[ 'true', true ], |
| 346 |
[ 'False', false ], |
| 347 |
[ 'True', true ], |
| 348 |
[ 'FALSE', false ], |
| 349 |
[ 'TRUE', true ], |
| 350 |
]; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public function testGetFuzzyBool( $value, $expected, $message = null ) { |
| 357 |
$req = $this->mockWebRequest( [ 'x' => $value ] ); |
| 358 |
$this->assertSame( $expected, $req->getFuzzyBool( 'x' ), $message ?: "Value: '$value'" ); |
| 359 |
} |
| 360 |
|
| 361 |
public function testGetFuzzyBoolDefault() { |
| 362 |
$req = $this->mockWebRequest(); |
| 363 |
$this->assertFalse( $req->getFuzzyBool( 'z' ), 'Not found' ); |
| 364 |
} |
| 365 |
|
| 366 |
public function testGetFuzzyBoolDefaultTrue() { |
| 367 |
$req = $this->mockWebRequest(); |
| 368 |
$this->assertTrue( $req->getFuzzyBool( 'z', true ), 'Not found, default true' ); |
| 369 |
} |
| 370 |
|
| 371 |
public function testGetCheck() { |
| 372 |
$req = $this->mockWebRequest( [ 'x' => 'Value', 'zero' => '0' ] ); |
| 373 |
$this->assertFalse( $req->getCheck( 'z' ), 'Not found' ); |
| 374 |
$this->assertTrue( $req->getCheck( 'x' ), 'Text' ); |
| 375 |
$this->assertTrue( $req->getCheck( 'zero' ) ); |
| 376 |
} |
| 377 |
|
| 378 |
public function testGetText() { |
| 379 |
|
| 380 |
$req = $this->mockWebRequest( [ 'crlf' => "Va\r\nlue" ] ); |
| 381 |
$this->assertSame( "Va\nlue", $req->getText( 'crlf' ), 'CR stripped' ); |
| 382 |
} |
| 383 |
|
| 384 |
public function testGetValues() { |
| 385 |
$values = [ 'x' => 'Value', 'y' => '' ]; |
| 386 |
$req = $this->mockWebRequest( $values ); |
| 387 |
$this->assertSame( $values, $req->getValues() ); |
| 388 |
$this->assertSame( [ 'x' => 'Value' ], $req->getValues( 'x' ), 'Specific keys' ); |
| 389 |
} |
| 390 |
|
| 391 |
public function testGetValueNames() { |
| 392 |
$req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => '' ] ); |
| 393 |
$this->assertSame( [ 'x', 'y' ], $req->getValueNames() ); |
| 394 |
$this->assertSame( [ 'x' ], $req->getValueNames( [ 'y' ] ), 'Exclude keys' ); |
| 395 |
} |
| 396 |
|
| 397 |
public function testGetFullRequestURL() { |
| 398 |
$this->overrideConfigValue( MainConfigNames::Server, '//wiki.test' ); |
| 399 |
$req = $this->getMockBuilder( WebRequest::class ) |
| 400 |
->onlyMethods( [ 'getRequestURL', 'getProtocol' ] ) |
| 401 |
->getMock(); |
| 402 |
$req->method( 'getRequestURL' )->willReturn( '/path' ); |
| 403 |
$req->method( 'getProtocol' )->willReturn( 'https' ); |
| 404 |
|
| 405 |
$this->assertSame( |
| 406 |
'https://wiki.test/path', |
| 407 |
$req->getFullRequestURL() |
| 408 |
); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function testGetIP( $expected, $input, $cdn, $xffList, $private, $description ) { |
| 415 |
$this->setServerVars( $input ); |
| 416 |
$this->overrideConfigValue( MainConfigNames::UsePrivateIPs, $private ); |
| 417 |
|
| 418 |
$hookContainer = $this->createHookContainer( [ |
| 419 |
'IsTrustedProxy' => static function ( &$ip, &$trusted ) use ( $xffList ) { |
| 420 |
$trusted = $trusted || in_array( $ip, $xffList ); |
| 421 |
return true; |
| 422 |
} |
| 423 |
] ); |
| 424 |
$this->setService( 'ProxyLookup', new ProxyLookup( [], $cdn, $hookContainer ) ); |
| 425 |
|
| 426 |
$request = new WebRequest(); |
| 427 |
$result = $request->getIP(); |
| 428 |
$this->assertEquals( $expected, $result, $description ); |
| 429 |
} |
| 430 |
|
| 431 |
public static function provideGetIP() { |
| 432 |
return [ |
| 433 |
[ |
| 434 |
'127.0.0.1', |
| 435 |
[ |
| 436 |
'REMOTE_ADDR' => '127.0.0.1' |
| 437 |
], |
| 438 |
[], |
| 439 |
[], |
| 440 |
false, |
| 441 |
'Simple IPv4' |
| 442 |
], |
| 443 |
[ |
| 444 |
'::1', |
| 445 |
[ |
| 446 |
'REMOTE_ADDR' => '::1' |
| 447 |
], |
| 448 |
[], |
| 449 |
[], |
| 450 |
false, |
| 451 |
'Simple IPv6' |
| 452 |
], |
| 453 |
[ |
| 454 |
'12.0.0.1', |
| 455 |
[ |
| 456 |
'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777', |
| 457 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777', |
| 458 |
], |
| 459 |
[ 'ABCD:1:2:3:4:555:6666:7777' ], |
| 460 |
[], |
| 461 |
false, |
| 462 |
'IPv6 normalisation' |
| 463 |
], |
| 464 |
[ |
| 465 |
'12.0.0.3', |
| 466 |
[ |
| 467 |
'REMOTE_ADDR' => '12.0.0.1', |
| 468 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2' |
| 469 |
], |
| 470 |
[ '12.0.0.1', '12.0.0.2' ], |
| 471 |
[], |
| 472 |
false, |
| 473 |
'With X-Forwaded-For' |
| 474 |
], |
| 475 |
[ |
| 476 |
'12.0.0.1', |
| 477 |
[ |
| 478 |
'REMOTE_ADDR' => '12.0.0.1', |
| 479 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2' |
| 480 |
], |
| 481 |
[], |
| 482 |
[], |
| 483 |
false, |
| 484 |
'With X-Forwaded-For and disallowed server' |
| 485 |
], |
| 486 |
[ |
| 487 |
'12.0.0.2', |
| 488 |
[ |
| 489 |
'REMOTE_ADDR' => '12.0.0.1', |
| 490 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2' |
| 491 |
], |
| 492 |
[ '12.0.0.1' ], |
| 493 |
[], |
| 494 |
false, |
| 495 |
'With multiple X-Forwaded-For and only one allowed server' |
| 496 |
], |
| 497 |
[ |
| 498 |
'10.0.0.3', |
| 499 |
[ |
| 500 |
'REMOTE_ADDR' => '12.0.0.2', |
| 501 |
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2' |
| 502 |
], |
| 503 |
[ '12.0.0.1', '12.0.0.2' ], |
| 504 |
[], |
| 505 |
false, |
| 506 |
'With X-Forwaded-For and private IP (from cache proxy)' |
| 507 |
], |
| 508 |
[ |
| 509 |
'10.0.0.4', |
| 510 |
[ |
| 511 |
'REMOTE_ADDR' => '12.0.0.2', |
| 512 |
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2' |
| 513 |
], |
| 514 |
[ '12.0.0.1', '12.0.0.2', '10.0.0.3' ], |
| 515 |
[], |
| 516 |
true, |
| 517 |
'With X-Forwaded-For and private IP (allowed)' |
| 518 |
], |
| 519 |
[ |
| 520 |
'10.0.0.4', |
| 521 |
[ |
| 522 |
'REMOTE_ADDR' => '12.0.0.2', |
| 523 |
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2' |
| 524 |
], |
| 525 |
[ '12.0.0.1', '12.0.0.2' ], |
| 526 |
[ '10.0.0.3' ], |
| 527 |
true, |
| 528 |
'With X-Forwaded-For and private IP (allowed)' |
| 529 |
], |
| 530 |
[ |
| 531 |
'10.0.0.3', |
| 532 |
[ |
| 533 |
'REMOTE_ADDR' => '12.0.0.2', |
| 534 |
'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2' |
| 535 |
], |
| 536 |
[ '12.0.0.1', '12.0.0.2' ], |
| 537 |
[ '10.0.0.3' ], |
| 538 |
false, |
| 539 |
'With X-Forwaded-For and private IP (disallowed)' |
| 540 |
], |
| 541 |
[ |
| 542 |
'12.0.0.3', |
| 543 |
[ |
| 544 |
'REMOTE_ADDR' => '12.0.0.1', |
| 545 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2' |
| 546 |
], |
| 547 |
[], |
| 548 |
[ '12.0.0.1', '12.0.0.2' ], |
| 549 |
false, |
| 550 |
'With X-Forwaded-For' |
| 551 |
], |
| 552 |
[ |
| 553 |
'12.0.0.2', |
| 554 |
[ |
| 555 |
'REMOTE_ADDR' => '12.0.0.1', |
| 556 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2' |
| 557 |
], |
| 558 |
[], |
| 559 |
[ '12.0.0.1' ], |
| 560 |
false, |
| 561 |
'With multiple X-Forwaded-For and only one allowed server' |
| 562 |
], |
| 563 |
[ |
| 564 |
'12.0.0.2', |
| 565 |
[ |
| 566 |
'REMOTE_ADDR' => '12.0.0.2', |
| 567 |
'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2' |
| 568 |
], |
| 569 |
[], |
| 570 |
[ '12.0.0.2' ], |
| 571 |
false, |
| 572 |
'With X-Forwaded-For and private IP and hook (disallowed)' |
| 573 |
], |
| 574 |
[ |
| 575 |
'12.0.0.1', |
| 576 |
[ |
| 577 |
'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777', |
| 578 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777', |
| 579 |
], |
| 580 |
[ 'ABCD:1:2:3::/64' ], |
| 581 |
[], |
| 582 |
false, |
| 583 |
'IPv6 CIDR' |
| 584 |
], |
| 585 |
[ |
| 586 |
'12.0.0.3', |
| 587 |
[ |
| 588 |
'REMOTE_ADDR' => '12.0.0.1', |
| 589 |
'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2' |
| 590 |
], |
| 591 |
[ '12.0.0.0/24' ], |
| 592 |
[], |
| 593 |
false, |
| 594 |
'IPv4 CIDR' |
| 595 |
], |
| 596 |
]; |
| 597 |
} |
| 598 |
|
| 599 |
public function testGetIpLackOfRemoteAddrThrowAnException() { |
| 600 |
|
| 601 |
$this->overrideConfigValues( [ |
| 602 |
MainConfigNames::CdnServers => [], |
| 603 |
MainConfigNames::CdnServersNoPurge => [], |
| 604 |
MainConfigNames::UsePrivateIPs => false, |
| 605 |
] ); |
| 606 |
|
| 607 |
$hookContainer = $this->createHookContainer(); |
| 608 |
$this->setService( 'ProxyLookup', new ProxyLookup( [], [], $hookContainer ) ); |
| 609 |
|
| 610 |
$request = new WebRequest(); |
| 611 |
|
| 612 |
$this->expectException( MWException::class ); |
| 613 |
$request->getIP(); |
| 614 |
} |
| 615 |
|
| 616 |
public static function provideLanguageData() { |
| 617 |
return [ |
| 618 |
[ '', [], 'Empty Accept-Language header' ], |
| 619 |
[ 'en', [ 'en' => 1.0 ], 'One language' ], |
| 620 |
[ 'en;q=', [ 'en' => 1.0 ], 'Empty q= defaults to 1' ], |
| 621 |
[ 'en;q=0, de;q=0. pt;q=0.0 it;q=0.0000', [], 'Zeros to be skipped' ], |
| 622 |
[ 'EN;Q=1.0009', [ 'en' => 1.000 ], 'Limited to max. 3 decimal places' ], |
| 623 |
[ 'en, ar', [ 'en' => 1.0, 'ar' => 1.0 ], 'Two languages listed in appearance order.' ], |
| 624 |
[ |
| 625 |
'zh-cn,zh-tw', |
| 626 |
[ 'zh-cn' => 1.0, 'zh-tw' => 1.0 ], |
| 627 |
'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' |
| 628 |
], |
| 629 |
[ |
| 630 |
'es, en; q=0.5', |
| 631 |
[ 'es' => 1.0, 'en' => 0.5 ], |
| 632 |
'Spanish as first language and English and second' |
| 633 |
], |
| 634 |
[ 'en; q=0.5, es', [ 'es' => 1.0, 'en' => 0.5 ], 'Less prefered language first' ], |
| 635 |
[ 'fr, en; q=0.5, es', [ 'fr' => 1.0, 'es' => 1.0, 'en' => 0.5 ], 'Three languages' ], |
| 636 |
[ 'en; q=0.5, es', [ 'es' => 1.0, 'en' => 0.5 ], 'Two languages' ], |
| 637 |
[ 'en, zh;q=0', [ 'en' => 1.0 ], "It's Chinese to me" ], |
| 638 |
[ |
| 639 |
'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0', |
| 640 |
[ 'es' => 1.0, 'pt' => 0.7, 'it' => 0.6, 'de' => 0.1 ], |
| 641 |
'Preference for Romance languages' |
| 642 |
], |
| 643 |
[ |
| 644 |
'en-gb, en-us; q=1', |
| 645 |
[ 'en-gb' => 1.0, 'en-us' => 1.0 ], |
| 646 |
'Two equally prefered English variants' |
| 647 |
], |
| 648 |
[ '_', [], 'Invalid input' ], |
| 649 |
]; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public function testAcceptLang( $acceptLanguageHeader, array $expectedLanguages, $description ) { |
| 656 |
$this->setServerVars( [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ] ); |
| 657 |
$request = new WebRequest(); |
| 658 |
$this->assertSame( $expectedLanguages, $request->getAcceptLang(), $description ); |
| 659 |
} |
| 660 |
|
| 661 |
public function testGetHeaderCanYieldSpecialCgiHeaders() { |
| 662 |
$contentType = 'application/json; charset=utf-8'; |
| 663 |
$contentLength = '4711'; |
| 664 |
$contentMd5 = 'rL0Y20zC+Fzt72VPzMSk2A=='; |
| 665 |
$this->setServerVars( [ |
| 666 |
'HTTP_CONTENT_TYPE' => $contentType, |
| 667 |
'HTTP_CONTENT_LENGTH' => $contentLength, |
| 668 |
'HTTP_CONTENT_MD5' => $contentMd5, |
| 669 |
] ); |
| 670 |
$request = new WebRequest(); |
| 671 |
$this->assertSame( $request->getHeader( 'Content-Type' ), $contentType ); |
| 672 |
$this->assertSame( $request->getHeader( 'Content-Length' ), $contentLength ); |
| 673 |
$this->assertSame( $request->getHeader( 'Content-Md5' ), $contentMd5 ); |
| 674 |
} |
| 675 |
|
| 676 |
public function testGetHeaderKeyIsCaseInsensitive() { |
| 677 |
$cacheControl = 'private, must-revalidate, max-age=0'; |
| 678 |
$this->setServerVars( [ 'HTTP_CACHE_CONTROL' => $cacheControl ] ); |
| 679 |
$request = new WebRequest(); |
| 680 |
$this->assertSame( $request->getHeader( 'Cache-Control' ), $cacheControl ); |
| 681 |
$this->assertSame( $request->getHeader( 'cache-control' ), $cacheControl ); |
| 682 |
} |
| 683 |
|
| 684 |
protected function setServerVars( $vars ) { |
| 685 |
|
| 686 |
if ( !isset( $vars['REQUEST_TIME_FLOAT'] ) ) { |
| 687 |
$vars['REQUEST_TIME_FLOAT'] = $_SERVER['REQUEST_TIME_FLOAT']; |
| 688 |
} |
| 689 |
if ( !isset( $vars['REQUEST_TIME'] ) ) { |
| 690 |
$vars['REQUEST_TIME'] = $_SERVER['REQUEST_TIME']; |
| 691 |
} |
| 692 |
$_SERVER = $vars; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
public function testMatchURLForCDN( $url, $cdnUrls, $matchOrder, $expected ) { |
| 699 |
$this->setServerVars( [ 'REQUEST_URI' => $url ] ); |
| 700 |
$this->overrideConfigValues( [ |
| 701 |
MainConfigNames::InternalServer => self::INTERNAL_SERVER, |
| 702 |
MainConfigNames::CdnMatchParameterOrder => $matchOrder, |
| 703 |
] ); |
| 704 |
$request = new WebRequest(); |
| 705 |
$this->assertEquals( $expected, $request->matchURLForCDN( $cdnUrls ) ); |
| 706 |
} |
| 707 |
|
| 708 |
public static function provideMatchURLForCDN() { |
| 709 |
$cdnUrls = [ |
| 710 |
self::INTERNAL_SERVER . '/Title', |
| 711 |
self::INTERNAL_SERVER . '/w/index.php?title=Title&action=history', |
| 712 |
]; |
| 713 |
return [ |
| 714 |
[ self::INTERNAL_SERVER . '/Title', $cdnUrls, false, true ], |
| 715 |
[ self::INTERNAL_SERVER . '/Title', $cdnUrls, true, true ], |
| 716 |
[ self::INTERNAL_SERVER . '/Foo', $cdnUrls, false, false ], |
| 717 |
[ self::INTERNAL_SERVER . '/Foo', $cdnUrls, true, false ], |
| 718 |
[ self::INTERNAL_SERVER . '/Thing', $cdnUrls, false, false ], |
| 719 |
[ self::INTERNAL_SERVER . '/Thing', $cdnUrls, true, false ], |
| 720 |
[ self::INTERNAL_SERVER . '/w/index.php?action=history&title=Foo', $cdnUrls, false, false ], |
| 721 |
[ self::INTERNAL_SERVER . '/w/index.php?action=history&title=Foo', $cdnUrls, true, false ], |
| 722 |
[ self::INTERNAL_SERVER . '/w/index.php?title=Thing&action=history', $cdnUrls, false, false ], |
| 723 |
[ self::INTERNAL_SERVER . '/w/index.php?action=history&title=Thing', $cdnUrls, true, false ], |
| 724 |
[ self::INTERNAL_SERVER . '/w/index.php?action=history&title=Title', $cdnUrls, false, true ], |
| 725 |
[ self::INTERNAL_SERVER . '/w/index.php?action=history&title=Title', $cdnUrls, true, false ], |
| 726 |
]; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
public function testRequestPathSuffix( string $basePath, string $requestUrl, $expected ) { |
| 737 |
$suffix = WebRequest::getRequestPathSuffix( $basePath, $requestUrl ); |
| 738 |
$this->assertSame( $expected, $suffix ); |
| 739 |
} |
| 740 |
|
| 741 |
public static function provideRequestPathSuffix() { |
| 742 |
yield [ |
| 743 |
'/w/index.php', |
| 744 |
'/w/index.php/Hello', |
| 745 |
'Hello' |
| 746 |
]; |
| 747 |
yield [ |
| 748 |
'/w/index.php', |
| 749 |
'/w/index.php/Hello?x=y', |
| 750 |
'Hello' |
| 751 |
]; |
| 752 |
yield [ |
| 753 |
'/wiki/', |
| 754 |
'/w/index.php/Hello?x=y', |
| 755 |
false |
| 756 |
]; |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
public function testGetSecurityLogContext( ?UserIdentity $user ) { |
| 763 |
$request = new WebRequest(); |
| 764 |
$this->setServerVars( [ 'REMOTE_ADDR' => '127.0.0.1' ] ); |
| 765 |
$this->setTemporaryHook( |
| 766 |
'GetSecurityLogContext', |
| 767 |
function ( array $info, array &$context ) use ( $request, $user ) { |
| 768 |
$this->assertSame( $request, $info['request'] ); |
| 769 |
$this->assertSame( $user, $info['user'] ); |
| 770 |
$context['foo'] = 'bar'; |
| 771 |
} |
| 772 |
); |
| 773 |
|
| 774 |
$context = $request->getSecurityLogContext( $user ); |
| 775 |
$this->assertSame( '127.0.0.1', $context['clientIp'] ); |
| 776 |
if ( $user ) { |
| 777 |
$this->assertSame( $user->getName(), $context['user'] ); |
| 778 |
} else { |
| 779 |
$this->assertArrayNotHasKey( 'user', $context ); |
| 780 |
} |
| 781 |
$this->assertSame( 'bar', $context['foo'] ); |
| 782 |
|
| 783 |
$this->setTemporaryHook( 'GetSecurityLogContext', fn () => $this->fail( 'should be cached' ) ); |
| 784 |
$request->getSecurityLogContext( $user ); |
| 785 |
|
| 786 |
$this->setTemporaryHook( |
| 787 |
'GetSecurityLogContext', |
| 788 |
static function ( array $info, array &$context ) use ( $request, $user ) { |
| 789 |
$context['foo'] = 'bar2'; |
| 790 |
} |
| 791 |
); |
| 792 |
$context2 = $request->getSecurityLogContext( new UserIdentityValue( 0, 'DifferentTestUser' ) ); |
| 793 |
|
| 794 |
$this->assertSame( 'DifferentTestUser', $context2['user'] ); |
| 795 |
} |
| 796 |
|
| 797 |
public static function provideGetSecurityLogContext() { |
| 798 |
return [ |
| 799 |
[ null ], |
| 800 |
[ new UserIdentityValue( 0, 'TestUser' ) ], |
| 801 |
]; |
| 802 |
} |
| 803 |
} |