66f8c3e2e0dd043896582fc171b9a79d88f02df7
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Http / NormalizedParamsTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Http;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Http\NormalizedParams;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class NormalizedParamsTest extends UnitTestCase
26 {
27 /**
28 * @return array[]
29 */
30 public function getHttpHostReturnsSanitizedValueDataProvider(): array
31 {
32 return [
33 'simple HTTP_HOST' => [
34 [
35 'HTTP_HOST' => 'www.domain.com'
36 ],
37 [],
38 'www.domain.com'
39 ],
40 'first HTTP_X_FORWARDED_HOST from configured proxy' => [
41 [
42 'HTTP_HOST' => '',
43 'REMOTE_ADDR' => '123.123.123.123',
44 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com, www.domain2.com,'
45 ],
46 [
47 'SYS' => [
48 'reverseProxyIP' => ' 123.123.123.123',
49 'reverseProxyHeaderMultiValue' => 'first',
50 ]
51 ],
52 'www.domain1.com',
53 ],
54 'last HTTP_X_FORWARDED_HOST from configured proxy' => [
55 [
56 'HTTP_HOST' => '',
57 'REMOTE_ADDR' => '123.123.123.123',
58 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com, www.domain2.com,'
59 ],
60 [
61 'SYS' => [
62 'reverseProxyIP' => '123.123.123.123',
63 'reverseProxyHeaderMultiValue' => 'last',
64 ]
65 ],
66 'www.domain2.com',
67 ],
68 'simple HTTP_HOST if reverseProxyHeaderMultiValue is not configured' => [
69 [
70 'HTTP_HOST' => 'www.domain.com',
71 'REMOTE_ADDR' => '123.123.123.123',
72 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com'
73 ],
74 [
75 'SYS' => [
76 'reverseProxyIP' => '123.123.123.123',
77 ]
78 ],
79 'www.domain.com',
80 ],
81 'simple HTTP_HOST if proxy IP does not match' => [
82 [
83 'HTTP_HOST' => 'www.domain.com',
84 'REMOTE_ADDR' => '123.123.123.123',
85 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com'
86 ],
87 [
88 'SYS' => [
89 'reverseProxyIP' => '234.234.234.234',
90 'reverseProxyHeaderMultiValue' => 'last',
91 ]
92 ],
93 'www.domain.com',
94 ],
95 'simple HTTP_HOST if REMOTE_ADDR misses' => [
96 [
97 'HTTP_HOST' => 'www.domain.com',
98 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com'
99 ],
100 [
101 'SYS' => [
102 'reverseProxyIP' => '234.234.234.234',
103 'reverseProxyHeaderMultiValue' => 'last',
104 ]
105 ],
106 'www.domain.com',
107 ],
108 'simple HTTP_HOST if HTTP_X_FORWARDED_HOST is empty' => [
109 [
110 'HTTP_HOST' => 'www.domain.com',
111 'REMOTE_ADDR' => '123.123.123.123',
112 'HTTP_X_FORWARDED_HOST' => ''
113 ],
114 [
115 'SYS' => [
116 'reverseProxyIP' => '123.123.123.123',
117 'reverseProxyHeaderMultiValue' => 'last',
118 ]
119 ],
120 'www.domain.com',
121 ],
122 ];
123 }
124
125 /**
126 * @test
127 * @dataProvider getHttpHostReturnsSanitizedValueDataProvider
128 * @param array $serverParams
129 * @param array $typo3ConfVars
130 * @param string $expected
131 */
132 public function getHttpHostReturnsSanitizedValue(array $serverParams, array $typo3ConfVars, string $expected)
133 {
134 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
135 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
136 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
137 $this->assertSame($expected, $serverRequestParameters->getHttpHost());
138 }
139
140 /**
141 * @return array[]
142 */
143 public function isHttpsReturnSanitizedValueDataProvider(): array
144 {
145 return [
146 'false if nothing special is set' => [
147 [
148 'HTTP_HOST' => 'www.domain.com',
149 ],
150 [],
151 false
152 ],
153 'true if SSL_SESSION_ID is set' => [
154 [
155 'HTTP_HOST' => 'www.domain.com',
156 'SSL_SESSION_ID' => 'foo',
157 ],
158 [],
159 true
160 ],
161 'false if SSL_SESSION_ID is empty' => [
162 [
163 'HTTP_HOST' => 'www.domain.com',
164 'SSL_SESSION_ID' => '',
165 ],
166 [],
167 false
168 ],
169 'true if HTTPS is "ON"' => [
170 [
171 'HTTP_HOST' => 'www.domain.com',
172 'HTTPS' => 'ON',
173 ],
174 [],
175 true,
176 ],
177 'true if HTTPS is "on"' => [
178 [
179 'HTTP_HOST' => 'www.domain.com',
180 'HTTPS' => 'on',
181 ],
182 [],
183 true,
184 ],
185 'true if HTTPS is "1"' => [
186 [
187 'HTTP_HOST' => 'www.domain.com',
188 'HTTPS' => '1',
189 ],
190 [],
191 true,
192 ],
193 'true if HTTPS is int(1)"' => [
194 [
195 'HTTP_HOST' => 'www.domain.com',
196 'HTTPS' => 1,
197 ],
198 [],
199 true,
200 ],
201 'true if HTTPS is bool(true)' => [
202 [
203 'HTTP_HOST' => 'www.domain.com',
204 'HTTPS' => true,
205 ],
206 [],
207 true,
208 ],
209 // https://secure.php.net/manual/en/reserved.variables.server.php
210 // "Set to a non-empty value if the script was queried through the HTTPS protocol."
211 'true if HTTPS is "somethingrandom"' => [
212 [
213 'HTTP_HOST' => 'www.domain.com',
214 'HTTPS' => 'somethingrandom',
215 ],
216 [],
217 true,
218 ],
219 'false if HTTPS is "0"' => [
220 [
221 'HTTP_HOST' => 'www.domain.com',
222 'HTTPS' => '0',
223 ],
224 [],
225 false,
226 ],
227 'false if HTTPS is int(0)' => [
228 [
229 'HTTP_HOST' => 'www.domain.com',
230 'HTTPS' => 0,
231 ],
232 [],
233 false,
234 ],
235 'false if HTTPS is float(0)' => [
236 [
237 'HTTP_HOST' => 'www.domain.com',
238 'HTTPS' => 0.0,
239 ],
240 [],
241 false,
242 ],
243 'false if HTTPS is not on' => [
244 [
245 'HTTP_HOST' => 'www.domain.com',
246 'HTTPS' => 'off',
247 ],
248 [],
249 false,
250 ],
251 'false if HTTPS is empty' => [
252 [
253 'HTTP_HOST' => 'www.domain.com',
254 'HTTPS' => '',
255 ],
256 [],
257 false,
258 ],
259 'false if HTTPS is null' => [
260 [
261 'HTTP_HOST' => 'www.domain.com',
262 'HTTPS' => null,
263 ],
264 [],
265 false,
266 ],
267 'false if HTTPS is bool(false)' => [
268 [
269 'HTTP_HOST' => 'www.domain.com',
270 'HTTPS' => false,
271 ],
272 [],
273 false,
274 ],
275 // Per PHP documententation 'HTTPS' is:
276 // "Set to a non-empty value if the script
277 // was queried through the HTTPS protocol."
278 // So theoretically an empty array means HTTPS is off.
279 // We do not support that. Therefore this test is disabled.
280 //'false if HTTPS is an empty Array' => [
281 // [
282 // 'HTTP_HOST' => 'www.domain.com',
283 // 'HTTPS' => [],
284 // ],
285 // [],
286 // false,
287 //],
288 'true if ssl proxy IP matches REMOTE_ADDR' => [
289 [
290 'HTTP_HOST' => 'www.domain.com',
291 'REMOTE_ADDR' => '123.123.123.123 ',
292 ],
293 [
294 'SYS' => [
295 'reverseProxySSL' => ' 123.123.123.123',
296 ],
297 ],
298 true
299 ],
300 'false if ssl proxy IP does not match REMOTE_ADDR' => [
301 [
302 'HTTP_HOST' => 'www.domain.com',
303 'REMOTE_ADDR' => '123.123.123.123',
304 ],
305 [
306 'SYS' => [
307 'reverseProxySSL' => '234.234.234.234',
308 ],
309 ],
310 false
311 ],
312 'true if SSL proxy is * and reverse proxy IP matches REMOTE_ADDR' => [
313 [
314 'HTTP_HOST' => 'www.domain.com',
315 'REMOTE_ADDR' => '123.123.123.123',
316 ],
317 [
318 'SYS' => [
319 'reverseProxySSL' => '*',
320 'reverseProxyIP' => '123.123.123.123',
321 ],
322 ],
323 true
324 ],
325 'false if SSL proxy is * and reverse proxy IP does not match REMOTE_ADDR' => [
326 [
327 'HTTP_HOST' => 'www.domain.com',
328 'REMOTE_ADDR' => '123.123.123.123',
329 ],
330 [
331 'SYS' => [
332 'reverseProxySSL' => '*',
333 'reverseProxyIP' => '234.234.234.234',
334 ],
335 ],
336 false
337 ]
338 ];
339 }
340
341 /**
342 * @test
343 * @dataProvider isHttpsReturnSanitizedValueDataProvider
344 * @param array $serverParams
345 * @param array $typo3ConfVars
346 * @param bool $expected
347 */
348 public function isHttpsReturnSanitizedValue(array $serverParams, array $typo3ConfVars, bool $expected)
349 {
350 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
351 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
352 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
353 $this->assertSame($expected, $serverRequestParameters->isHttps());
354 }
355
356 /**
357 * @test
358 */
359 public function getRequestHostReturnsRequestHost()
360 {
361 $serverParams = [
362 'HTTP_HOST' => 'www.domain.com',
363 'HTTPS' => 'on',
364 ];
365 $expected = 'https://www.domain.com';
366 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
367 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
368 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
369 $this->assertSame($expected, $serverRequestParameters->getRequestHost());
370 }
371
372 /**
373 * @return array[]
374 */
375 public function getScriptNameReturnsExpectedValueDataProvider(): array
376 {
377 return [
378 'empty string if nothing is set' => [
379 [
380 'HTTP_HOST' => 'www.domain.com',
381 ],
382 [],
383 ''
384 ],
385 'use ORIG_PATH_INFO' => [
386 [
387 'ORIG_PATH_INFO' => '/orig/path/info.php',
388 'PATH_INFO' => '/path/info.php',
389 'ORIG_SCRIPT_NAME' => '/orig/script/name.php',
390 'SCRIPT_NAME' => '/script/name.php',
391 ],
392 [],
393 '/orig/path/info.php',
394 ],
395 'use PATH_INFO' => [
396 [
397 'PATH_INFO' => '/path/info.php',
398 'ORIG_SCRIPT_NAME' => '/orig/script/name.php',
399 'SCRIPT_NAME' => '/script/name.php',
400 ],
401 [],
402 '/path/info.php',
403 ],
404 'use ORIG_SCRIPT_NAME' => [
405 [
406 'ORIG_SCRIPT_NAME' => '/orig/script/name.php',
407 'SCRIPT_NAME' => '/script/name.php',
408 ],
409 [],
410 '/orig/script/name.php',
411 ],
412 'use SCRIPT_NAME' => [
413 [
414 'SCRIPT_NAME' => '/script/name.php',
415 ],
416 [],
417 '/script/name.php',
418 ],
419 'add proxy ssl prefix' => [
420 [
421 'REMOTE_ADDR' => '123.123.123.123',
422 'HTTPS' => 'on',
423 'PATH_INFO' => '/path/info.php',
424 ],
425 [
426 'SYS' => [
427 'reverseProxyIP' => '123.123.123.123',
428 'reverseProxyPrefixSSL' => '/proxyPrefixSSL',
429 ],
430 ],
431 '/proxyPrefixSSL/path/info.php',
432 ],
433 'add proxy prefix' => [
434 [
435 'REMOTE_ADDR' => '123.123.123.123',
436 'PATH_INFO' => '/path/info.php',
437 ],
438 [
439 'SYS' => [
440 'reverseProxyIP' => '123.123.123.123',
441 'reverseProxyPrefix' => '/proxyPrefix',
442 ],
443 ],
444 '/proxyPrefix/path/info.php',
445 ],
446 ];
447 }
448
449 /**
450 * @test
451 * @dataProvider getScriptNameReturnsExpectedValueDataProvider
452 * @param array $serverParams
453 * @param array $typo3ConfVars
454 * @param string $expected
455 */
456 public function getScriptNameReturnsExpectedValue(array $serverParams, array $typo3ConfVars, string $expected)
457 {
458 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
459 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
460 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
461 $this->assertSame($expected, $serverRequestParameters->getScriptName());
462 }
463
464 /**
465 * @return array[]
466 */
467 public function getRequestUriReturnsExpectedValueDataProvider(): array
468 {
469 return [
470 'slash if nothing is set' => [
471 [
472 'HTTP_HOST' => 'www.domain.com',
473 ],
474 [],
475 '/'
476 ],
477 'use REQUEST_URI' => [
478 [
479 'HTTP_HOST' => 'www.domain.com',
480 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
481 ],
482 [],
483 '/typo3/index.php?route=foo/bar&id=42',
484 ],
485 'use query string and script name if REQUEST_URI is not set' => [
486 [
487 'QUERY_STRING' => 'route=foo/bar&id=42',
488 'SCRIPT_NAME' => '/typo3/index.php',
489 ],
490 [],
491 '/typo3/index.php?route=foo/bar&id=42',
492 ],
493 'prefix with proxy prefix with ssl if using REQUEST_URI' => [
494 [
495 'HTTP_HOST' => 'www.domain.com',
496 'REMOTE_ADDR' => '123.123.123.123',
497 'HTTPS' => 'on',
498 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
499 ],
500 [
501 'SYS' => [
502 'reverseProxyIP' => '123.123.123.123',
503 'reverseProxyPrefixSSL' => '/proxyPrefixSSL',
504 ],
505 ],
506 '/proxyPrefixSSL/typo3/index.php?route=foo/bar&id=42',
507 ],
508 'prefix with proxy prefix if using REQUEST_URI' => [
509 [
510 'HTTP_HOST' => 'www.domain.com',
511 'REMOTE_ADDR' => '123.123.123.123',
512 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
513 ],
514 [
515 'SYS' => [
516 'reverseProxyIP' => '123.123.123.123',
517 'reverseProxyPrefix' => '/proxyPrefix',
518 ],
519 ],
520 '/proxyPrefix/typo3/index.php?route=foo/bar&id=42',
521 ],
522 'prefix with proxy prefix with ssl if using query string and script name' => [
523 [
524 'REMOTE_ADDR' => '123.123.123.123',
525 'HTTPS' => 'on',
526 'QUERY_STRING' => 'route=foo/bar&id=42',
527 'SCRIPT_NAME' => '/typo3/index.php',
528 ],
529 [
530 'SYS' => [
531 'reverseProxyIP' => '123.123.123.123',
532 'reverseProxyPrefixSSL' => '/proxyPrefixSSL',
533 ],
534 ],
535 '/proxyPrefixSSL/typo3/index.php?route=foo/bar&id=42',
536 ],
537 'prefix with proxy prefix if using query string and script name' => [
538 [
539 'REMOTE_ADDR' => '123.123.123.123',
540 'HTTPS' => 'on',
541 'QUERY_STRING' => 'route=foo/bar&id=42',
542 'SCRIPT_NAME' => '/typo3/index.php',
543 ],
544 [
545 'SYS' => [
546 'reverseProxyIP' => '123.123.123.123',
547 'reverseProxyPrefix' => '/proxyPrefix',
548 ],
549 ],
550 '/proxyPrefix/typo3/index.php?route=foo/bar&id=42',
551 ],
552 ];
553 }
554
555 /**
556 * @test
557 * @dataProvider getRequestUriReturnsExpectedValueDataProvider
558 * @param array $serverParams
559 * @param array $typo3ConfVars
560 * @param string $expected
561 */
562 public function getRequestUriReturnsExpectedValue(array $serverParams, array $typo3ConfVars, string $expected)
563 {
564 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
565 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
566 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
567 $this->assertSame($expected, $serverRequestParameters->getRequestUri());
568 }
569
570 /**
571 * @test
572 */
573 public function getRequestUriFetchesFromConfiguredRequestUriVar()
574 {
575 $GLOBALS['foo']['bar'] = '/foo/bar.php';
576 $serverParams = [
577 'HTTP_HOST' => 'www.domain.com',
578 ];
579 $typo3ConfVars = [
580 'SYS' => [
581 'requestURIvar' => 'foo|bar',
582 ],
583 ];
584 $expected = '/foo/bar.php';
585 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
586 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
587 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
588 $this->assertSame($expected, $serverRequestParameters->getRequestUri());
589 }
590
591 /**
592 * @test
593 */
594 public function getRequestUrlReturnsExpectedValue()
595 {
596 $serverParams = [
597 'HTTP_HOST' => 'www.domain.com',
598 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
599 ];
600 $expected = 'http://www.domain.com/typo3/index.php?route=foo/bar&id=42';
601 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
602 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
603 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
604 $this->assertSame($expected, $serverRequestParameters->getRequestUrl());
605 }
606
607 /**
608 * @test
609 */
610 public function getRequestScriptReturnsExpectedValue()
611 {
612 $serverParams = [
613 'HTTP_HOST' => 'www.domain.com',
614 'PATH_INFO' => '/typo3/index.php',
615 ];
616 $expected = 'http://www.domain.com/typo3/index.php';
617 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
618 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
619 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
620 $this->assertSame($expected, $serverRequestParameters->getRequestScript());
621 }
622
623 /**
624 * @test
625 */
626 public function getRequestDirReturnsExpectedValue()
627 {
628 $serverParams = [
629 'HTTP_HOST' => 'www.domain.com',
630 'PATH_INFO' => '/typo3/index.php',
631 ];
632 $expected = 'http://www.domain.com/typo3/';
633 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
634 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
635 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
636 $this->assertSame($expected, $serverRequestParameters->getRequestDir());
637 }
638
639 /**
640 * @return array[]
641 */
642 public function isBehindReverseProxyReturnsExpectedValueDataProvider(): array
643 {
644 return [
645 'false with empty data' => [
646 [
647 'HTTP_HOST' => 'www.domain.com',
648 ],
649 [],
650 false
651 ],
652 'false if REMOTE_ADDR and reverseProxyIP do not match' => [
653 [
654 'HTTP_HOST' => 'www.domain.com',
655 'REMOTE_ADDR' => '100.100.100.100',
656 ],
657 [
658 'SYS' => [
659 'reverseProxyIP' => '200.200.200.200',
660 ],
661 ],
662 false
663 ],
664 'true if REMOTE_ADDR matches configured reverseProxyIP' => [
665 [
666 'HTTP_HOST' => 'www.domain.com',
667 'REMOTE_ADDR' => '100.100.100.100',
668 ],
669 [
670 'SYS' => [
671 'reverseProxyIP' => '100.100.100.100',
672 ],
673 ],
674 true
675 ],
676 'true if trimmed REMOTE_ADDR matches configured trimmed reverseProxyIP' => [
677 [
678 'HTTP_HOST' => 'www.domain.com',
679 'REMOTE_ADDR' => ' 100.100.100.100 ',
680 ],
681 [
682 'SYS' => [
683 'reverseProxyIP' => ' 100.100.100.100 ',
684 ],
685 ],
686 true
687 ]
688 ];
689 }
690
691 /**
692 * @test
693 * @dataProvider isBehindReverseProxyReturnsExpectedValueDataProvider
694 * @param array $serverParams
695 * @param array $typo3ConfVars
696 * @param bool $expected
697 */
698 public function isBehindReverseProxyReturnsExpectedValue(array $serverParams, array $typo3ConfVars, bool $expected)
699 {
700 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
701 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
702 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
703 $this->assertSame($expected, $serverRequestParameters->isBehindReverseProxy());
704 }
705
706 /**
707 * @return array[]
708 */
709 public function getRemoteAddressReturnsExpectedValueDataProvider(): array
710 {
711 return [
712 'simple REMOTE_ADDR' => [
713 [
714 'HTTP_HOST' => 'www.domain.com',
715 'REMOTE_ADDR' => ' 123.123.123.123 ',
716 ],
717 [],
718 '123.123.123.123'
719 ],
720 'reverse proxy with last HTTP_X_FORWARDED_FOR' => [
721 [
722 'HTTP_HOST' => 'www.domain.com',
723 'REMOTE_ADDR' => ' 123.123.123.123 ',
724 'HTTP_X_FORWARDED_FOR' => ' 234.234.234.234, 235.235.235.235,',
725 ],
726 [
727 'SYS' => [
728 'reverseProxyIP' => '123.123.123.123',
729 'reverseProxyHeaderMultiValue' => ' last ',
730 ]
731 ],
732 '235.235.235.235'
733 ],
734 'reverse proxy with first HTTP_X_FORWARDED_FOR' => [
735 [
736 'HTTP_HOST' => 'www.domain.com',
737 'REMOTE_ADDR' => ' 123.123.123.123 ',
738 'HTTP_X_FORWARDED_FOR' => ' 234.234.234.234, 235.235.235.235,',
739 ],
740 [
741 'SYS' => [
742 'reverseProxyIP' => '123.123.123.123 ',
743 'reverseProxyHeaderMultiValue' => ' first ',
744 ]
745 ],
746 '234.234.234.234'
747 ],
748 'reverse proxy with broken reverseProxyHeaderMultiValue returns REMOTE_ADDR' => [
749 [
750 'HTTP_HOST' => 'www.domain.com',
751 'REMOTE_ADDR' => ' 123.123.123.123 ',
752 'HTTP_X_FORWARDED_FOR' => ' 234.234.234.234, 235.235.235.235,',
753 ],
754 [
755 'SYS' => [
756 'reverseProxyIP' => '123.123.123.123 ',
757 'reverseProxyHeaderMultiValue' => ' foo ',
758 ]
759 ],
760 '123.123.123.123'
761 ],
762 ];
763 }
764
765 /**
766 * @test
767 * @dataProvider getRemoteAddressReturnsExpectedValueDataProvider
768 * @param array $serverParams
769 * @param array $typo3ConfVars
770 * @param string $expected
771 */
772 public function getRemoteAddressReturnsExpectedValue(array $serverParams, array $typo3ConfVars, string $expected)
773 {
774 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
775 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
776 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
777 $this->assertSame($expected, $serverRequestParameters->getRemoteAddress());
778 }
779
780 /**
781 * @return array
782 */
783 public static function getRequestHostOnlyReturnsExpectedValueDataProvider(): array
784 {
785 return [
786 'localhost ipv4 without port' => [
787 [
788 'HTTP_HOST' => '127.0.0.1',
789 ],
790 '127.0.0.1'
791 ],
792 'localhost ipv4 with port' => [
793 [
794 'HTTP_HOST' => '127.0.0.1:81',
795 ],
796 '127.0.0.1'
797 ],
798 'localhost ipv6 without port' => [
799 [
800 'HTTP_HOST' => '[::1]',
801 ],
802 '[::1]'
803 ],
804 'localhost ipv6 with port' => [
805 [
806 'HTTP_HOST' => '[::1]:81',
807 ],
808 '[::1]'
809 ],
810 'ipv6 without port' => [
811 [
812 'HTTP_HOST' => '[2001:DB8::1]',
813 ],
814 '[2001:DB8::1]'
815 ],
816 'ipv6 with port' => [
817 [
818 'HTTP_HOST' => '[2001:DB8::1]:81',
819 ],
820 '[2001:DB8::1]'
821 ],
822 'hostname without port' => [
823 [
824 'HTTP_HOST' => 'lolli.did.this',
825 ],
826 'lolli.did.this'
827 ],
828 'hostname with port' => [
829 [
830 'HTTP_HOST' => 'lolli.did.this:42',
831 ],
832 'lolli.did.this'
833 ],
834 ];
835 }
836
837 /**
838 * @test
839 * @dataProvider getRequestHostOnlyReturnsExpectedValueDataProvider
840 * @param array $serverParams
841 * @param string $expected
842 */
843 public function getRequestHostOnlyReturnsExpectedValue(array $serverParams, string $expected)
844 {
845 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
846 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
847 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
848 $this->assertSame($expected, $serverRequestParameters->getRequestHostOnly());
849 }
850
851 /**
852 * @return array
853 */
854 public static function getRequestPortOnlyReturnsExpectedValueDataProvider(): array
855 {
856 return [
857 'localhost ipv4 without port' => [
858 [
859 'HTTP_HOST' => '127.0.0.1',
860 ],
861 0
862 ],
863 'localhost ipv4 with port' => [
864 [
865 'HTTP_HOST' => '127.0.0.1:81',
866 ],
867 81
868 ],
869 'localhost ipv6 without port' => [
870 [
871 'HTTP_HOST' => '[::1]',
872 ],
873 0
874 ],
875 'localhost ipv6 with port' => [
876 [
877 'HTTP_HOST' => '[::1]:81',
878 ],
879 81
880 ],
881 'ipv6 without port' => [
882 [
883 'HTTP_HOST' => '[2001:DB8::1]',
884 ],
885 0
886 ],
887 'ipv6 with port' => [
888 [
889 'HTTP_HOST' => '[2001:DB8::1]:81',
890 ],
891 81
892 ],
893 'hostname without port' => [
894 [
895 'HTTP_HOST' => 'lolli.did.this',
896 ],
897 0
898 ],
899 'hostname with port' => [
900 [
901 'HTTP_HOST' => 'lolli.did.this:42',
902 ],
903 42
904 ],
905 ];
906 }
907
908 /**
909 * @test
910 * @dataProvider getRequestPortOnlyReturnsExpectedValueDataProvider
911 * @param array $serverParams
912 * @param int $expected
913 */
914 public function getRequestPortReturnsExpectedValue(array $serverParams, int $expected)
915 {
916 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
917 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
918 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
919 $this->assertSame($expected, $serverRequestParameters->getRequestPort());
920 }
921
922 /**
923 * @test
924 */
925 public function getScriptFilenameReturnsThirdConstructorArgument()
926 {
927 $serverParams = [
928 'HTTP_HOST' => 'www.domain.com',
929 'SCRIPT_NAME' => '/typo3/index.php',
930 ];
931 $pathSite = '/var/www/';
932 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
933 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
934 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '/var/www/typo3/index.php', $pathSite);
935 $this->assertSame('/var/www/typo3/index.php', $serverRequestParameters->getScriptFilename());
936 }
937
938 /**
939 * @test
940 */
941 public function getDocumentRootReturnsExpectedPath()
942 {
943 $serverParams = [
944 'HTTP_HOST' => 'www.domain.com',
945 'SCRIPT_NAME' => '/typo3/index.php',
946 ];
947 $pathThisScript = '/var/www/myInstance/Web/typo3/index.php';
948 $pathSite = '/var/www/myInstance/Web/';
949 $expected = '/var/www/myInstance/Web';
950 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
951 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
952 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
953 $this->assertSame($expected, $serverRequestParameters->getDocumentRoot());
954 }
955
956 /**
957 * @test
958 */
959 public function getSiteUrlReturnsExpectedUrl()
960 {
961 $serverParams = [
962 'SCRIPT_NAME' => '/typo3/index.php',
963 'HTTP_HOST' => 'www.domain.com',
964 'PATH_INFO' => '/typo3/index.php',
965 ];
966 $pathThisScript = '/var/www/myInstance/Web/typo3/index.php';
967 $pathSite = '/var/www/myInstance/Web';
968 $expected = 'http://www.domain.com/';
969 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
970 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
971 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
972 $this->assertSame($expected, $serverRequestParameters->getSiteUrl());
973 }
974
975 /**
976 * @return array[]
977 */
978 public function getSitePathReturnsExpectedPathDataProvider()
979 {
980 return [
981 'empty config' => [
982 [],
983 '',
984 '',
985 ''
986 ],
987 'not in a sub directory' => [
988 [
989 'SCRIPT_NAME' => '/typo3/index.php',
990 'HTTP_HOST' => 'www.domain.com',
991 ],
992 '/var/www/myInstance/Web/typo3/index.php',
993 '/var/www/myInstance/Web',
994 '/'
995 ],
996 'in a sub directory' => [
997 [
998 'SCRIPT_NAME' => '/some/sub/dir/typo3/index.php',
999 'HTTP_HOST' => 'www.domain.com',
1000 ],
1001 '/var/www/myInstance/Web/typo3/index.php',
1002 '/var/www/myInstance/Web',
1003 '/some/sub/dir/'
1004 ],
1005 ];
1006 }
1007
1008 /**
1009 * @test
1010 * @dataProvider getSitePathReturnsExpectedPathDataProvider
1011 * @param array $serverParams
1012 * @param string $pathThisScript
1013 * @param string $pathSite
1014 * @param string $expected
1015 */
1016 public function getSitePathReturnsExpectedPath(array $serverParams, string $pathThisScript, string $pathSite, string $expected)
1017 {
1018 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1019 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1020 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
1021 $this->assertSame($expected, $serverRequestParameters->getSitePath());
1022 }
1023
1024 /**
1025 * @return array[]
1026 */
1027 public function getSiteScriptReturnsExpectedPathDataProvider()
1028 {
1029 return [
1030 'not in a sub directory' => [
1031 [
1032 'SCRIPT_NAME' => '/typo3/index.php?id=42&foo=bar',
1033 'HTTP_HOST' => 'www.domain.com',
1034 ],
1035 '/var/www/myInstance/Web/typo3/index.php',
1036 '/var/www/myInstance/Web',
1037 'typo3/index.php?id=42&foo=bar'
1038 ],
1039 'in a sub directory' => [
1040 [
1041 'SCRIPT_NAME' => '/some/sub/dir/typo3/index.php?id=42&foo=bar',
1042 'HTTP_HOST' => 'www.domain.com',
1043 ],
1044 '/var/www/myInstance/Web/typo3/index.php',
1045 '/var/www/myInstance/Web',
1046 'typo3/index.php?id=42&foo=bar'
1047 ],
1048 ];
1049 }
1050
1051 /**
1052 * @test
1053 * @dataProvider getSiteScriptReturnsExpectedPathDataProvider
1054 * @param array $serverParams
1055 * @param string $pathThisScript
1056 * @param string $pathSite
1057 * @param string $expected
1058 */
1059 public function getSiteScriptReturnsExpectedPath(array $serverParams, string $pathThisScript, string $pathSite, string $expected)
1060 {
1061 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1062 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1063 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
1064 $this->assertSame($expected, $serverRequestParameters->getSiteScript());
1065 }
1066
1067 /**
1068 * @test
1069 */
1070 public function getPathInfoReturnsExpectedValue()
1071 {
1072 $serverParams = [
1073 'PATH_INFO' => '/typo3/index.php',
1074 ];
1075 $expected = '/typo3/index.php';
1076 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1077 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1078 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1079 $this->assertSame($expected, $serverRequestParameters->getPathInfo());
1080 }
1081
1082 /**
1083 * @test
1084 */
1085 public function getHttpRefererReturnsExpectedValue()
1086 {
1087 $serverParams = [
1088 'HTTP_REFERER' => 'https://www.domain.com/typo3/index.php?id=42',
1089 ];
1090 $expected = 'https://www.domain.com/typo3/index.php?id=42';
1091 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1092 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1093 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1094 $this->assertSame($expected, $serverRequestParameters->getHttpReferer());
1095 }
1096
1097 /**
1098 * @test
1099 */
1100 public function getHttpUserAgentReturnsExpectedValue()
1101 {
1102 $serverParams = [
1103 'HTTP_USER_AGENT' => 'the client browser',
1104 ];
1105 $expected = 'the client browser';
1106 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1107 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1108 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1109 $this->assertSame($expected, $serverRequestParameters->getHttpUserAgent());
1110 }
1111
1112 /**
1113 * @test
1114 */
1115 public function getHttpAcceptEncodingReturnsExpectedValue()
1116 {
1117 $serverParams = [
1118 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
1119 ];
1120 $expected = 'gzip, deflate';
1121 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1122 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1123 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1124 $this->assertSame($expected, $serverRequestParameters->getHttpAcceptEncoding());
1125 }
1126
1127 /**
1128 * @test
1129 */
1130 public function getHttpAcceptLanguageReturnsExpectedValue()
1131 {
1132 $serverParams = [
1133 'HTTP_ACCEPT_LANGUAGE' => 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
1134 ];
1135 $expected = 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7';
1136 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1137 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1138 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1139 $this->assertSame($expected, $serverRequestParameters->getHttpAcceptLanguage());
1140 }
1141
1142 /**
1143 * @test
1144 */
1145 public function getRemoteHostReturnsExpectedValue()
1146 {
1147 $serverParams = [
1148 'REMOTE_HOST' => 'www.clientDomain.com',
1149 ];
1150 $expected = 'www.clientDomain.com';
1151 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1152 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1153 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1154 $this->assertSame($expected, $serverRequestParameters->getRemoteHost());
1155 }
1156
1157 /**
1158 * @test
1159 */
1160 public function getQueryStringReturnsExpectedValue()
1161 {
1162 $serverParams = [
1163 'QUERY_STRING' => 'id=42&foo=bar',
1164 ];
1165 $expected = 'id=42&foo=bar';
1166 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1167 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1168 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1169 $this->assertSame($expected, $serverRequestParameters->getQueryString());
1170 }
1171 }