|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Filters; |
| 13 | + |
| 14 | +use CodeIgniter\HTTP\CLIRequest; |
| 15 | +use CodeIgniter\HTTP\IncomingRequest; |
| 16 | +use CodeIgniter\HTTP\URI; |
| 17 | +use CodeIgniter\HTTP\UserAgent; |
| 18 | +use CodeIgniter\Test\CIUnitTestCase; |
| 19 | +use CodeIgniter\Test\Mock\MockAppConfig; |
| 20 | +use RuntimeException; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +final class InvalidCharsTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var InvalidChars |
| 29 | + */ |
| 30 | + private $invalidChars; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var IncomingRequest |
| 34 | + */ |
| 35 | + private $request; |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + $_GET = []; |
| 42 | + $_POST = []; |
| 43 | + $_COOKIE = []; |
| 44 | + |
| 45 | + $this->request = $this->createRequest(); |
| 46 | + $this->invalidChars = new InvalidChars(); |
| 47 | + } |
| 48 | + |
| 49 | + private function createRequest(): IncomingRequest |
| 50 | + { |
| 51 | + $config = new MockAppConfig(); |
| 52 | + $uri = new URI(); |
| 53 | + $userAgent = new UserAgent(); |
| 54 | + $request = $this->getMockBuilder(IncomingRequest::class) |
| 55 | + ->setConstructorArgs([$config, $uri, null, $userAgent]) |
| 56 | + ->onlyMethods(['isCLI']) |
| 57 | + ->getMock(); |
| 58 | + $request->method('isCLI')->willReturn(false); |
| 59 | + |
| 60 | + return $request; |
| 61 | + } |
| 62 | + |
| 63 | + public function testBeforeDoNothingWhenCLIRequest() |
| 64 | + { |
| 65 | + $cliRequest = new CLIRequest(new MockAppConfig()); |
| 66 | + |
| 67 | + $ret = $this->invalidChars->before($cliRequest); |
| 68 | + |
| 69 | + $this->assertNull($ret); |
| 70 | + } |
| 71 | + |
| 72 | + public function testBeforeValidString() |
| 73 | + { |
| 74 | + $_POST['val'] = [ |
| 75 | + 'valid string', |
| 76 | + ]; |
| 77 | + $_COOKIE['val'] = 'valid string'; |
| 78 | + |
| 79 | + $ret = $this->invalidChars->before($this->request); |
| 80 | + |
| 81 | + $this->assertNull($ret); |
| 82 | + } |
| 83 | + |
| 84 | + public function testBeforeInvalidUTF8StringCausesException() |
| 85 | + { |
| 86 | + $this->expectException(RuntimeException::class); |
| 87 | + $this->expectExceptionMessage('Invalid UTF-8 characters in post:'); |
| 88 | + |
| 89 | + $sjisString = mb_convert_encoding('SJISの文字列です。', 'SJIS'); |
| 90 | + $_POST['val'] = [ |
| 91 | + 'valid string', |
| 92 | + $sjisString, |
| 93 | + ]; |
| 94 | + |
| 95 | + $this->invalidChars->before($this->request); |
| 96 | + } |
| 97 | + |
| 98 | + public function testBeforeInvalidControllCharCausesException() |
| 99 | + { |
| 100 | + $this->expectException(RuntimeException::class); |
| 101 | + $this->expectExceptionMessage('Invalid Control characters in cookie:'); |
| 102 | + |
| 103 | + $stringWithNullChar = "String contains null char and line break.\0\n"; |
| 104 | + $_COOKIE['val'] = $stringWithNullChar; |
| 105 | + |
| 106 | + $this->invalidChars->before($this->request); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @dataProvider stringWithLineBreakAndTabProvider |
| 111 | + * |
| 112 | + * @param string $input |
| 113 | + */ |
| 114 | + public function testCheckControlStringWithLineBreakAndTabReturnsTheString($input) |
| 115 | + { |
| 116 | + $_GET['val'] = $input; |
| 117 | + |
| 118 | + $ret = $this->invalidChars->before($this->request); |
| 119 | + |
| 120 | + $this->assertNull($ret); |
| 121 | + } |
| 122 | + |
| 123 | + public function stringWithLineBreakAndTabProvider() |
| 124 | + { |
| 125 | + return [ |
| 126 | + ["String contains \n line break."], |
| 127 | + ["String contains \r line break."], |
| 128 | + ["String contains \r\n line break."], |
| 129 | + ["String contains \t tab."], |
| 130 | + ["String contains \t and \r line \n break."], |
| 131 | + ]; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @dataProvider stringWithControlCharsProvider |
| 136 | + * |
| 137 | + * @param string $input |
| 138 | + */ |
| 139 | + public function testCheckControlStringWithControlCharsCausesException($input) |
| 140 | + { |
| 141 | + $this->expectException(RuntimeException::class); |
| 142 | + $this->expectExceptionMessage('Invalid Control characters in get:'); |
| 143 | + |
| 144 | + $_GET['val'] = $input; |
| 145 | + |
| 146 | + $ret = $this->invalidChars->before($this->request); |
| 147 | + |
| 148 | + $this->assertNull($ret); |
| 149 | + } |
| 150 | + |
| 151 | + public function stringWithControlCharsProvider() |
| 152 | + { |
| 153 | + return [ |
| 154 | + ["String contains null char.\0"], |
| 155 | + ["String contains null char and line break.\0\n"], |
| 156 | + ]; |
| 157 | + } |
| 158 | +} |
0 commit comments