-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: add filter to check invalid chars in user input #5227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
414ca77
feat: add controller filter to check invalid chars in user input
kenjis 9a67840
test: clear super globals after testing
kenjis 1e3a30c
feat: add SecurityException static consructors
kenjis 991f953
test: fix test code
kenjis 871e499
config: add invalidchars to Filter.php as comment
kenjis 97be037
docs: add InvalidChars in Provided Filters
kenjis fb9f6ec
refactor: add property for control code regex
kenjis abab121
docs: fix by proofreading
kenjis fe3ab5e
docs: fix by proofreading
kenjis e7e492f
test: add param type
kenjis 70424d6
test: add param type
kenjis 72aa844
test: replace return with yield from
kenjis 9d7e038
test: replace return with yield from
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Filters; | ||
|
||
use CodeIgniter\HTTP\RequestInterface; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
use CodeIgniter\Security\Exceptions\SecurityException; | ||
|
||
/** | ||
* InvalidChars filter. | ||
* | ||
* Check if user input data ($_GET, $_POST, $_COOKIE, php://input) do not contain | ||
* invalid characters: | ||
* - invalid UTF-8 characters | ||
* - control characters except line break and tab code | ||
*/ | ||
class InvalidChars implements FilterInterface | ||
{ | ||
/** | ||
* Data source | ||
* | ||
* @var string | ||
*/ | ||
protected $source; | ||
|
||
/** | ||
* Regular expressions for valid control codes | ||
* | ||
* @var string | ||
*/ | ||
protected $controlCodeRegex = '/\A[\r\n\t[:^cntrl:]]*\z/u'; | ||
|
||
/** | ||
* Check invalid characters. | ||
* | ||
* @param array|null $arguments | ||
* | ||
* @return void | ||
*/ | ||
public function before(RequestInterface $request, $arguments = null) | ||
{ | ||
if ($request->isCLI()) { | ||
return; | ||
} | ||
MGatner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$data = [ | ||
'get' => $request->getGet(), | ||
'post' => $request->getPost(), | ||
'cookie' => $request->getCookie(), | ||
'rawInput' => $request->getRawInput(), | ||
]; | ||
|
||
foreach ($data as $source => $values) { | ||
$this->source = $source; | ||
$this->checkEncoding($values); | ||
$this->checkControl($values); | ||
} | ||
} | ||
|
||
/** | ||
* We don't have anything to do here. | ||
* | ||
* @param array|null $arguments | ||
* | ||
* @return void | ||
*/ | ||
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Check the character encoding is valid UTF-8. | ||
* | ||
* @param array|string $value | ||
* | ||
* @return array|string | ||
*/ | ||
protected function checkEncoding($value) | ||
{ | ||
if (is_array($value)) { | ||
array_map([$this, 'checkEncoding'], $value); | ||
|
||
return $value; | ||
} | ||
|
||
if (mb_check_encoding($value, 'UTF-8')) { | ||
return $value; | ||
} | ||
|
||
throw SecurityException::forInvalidUTF8Chars($this->source, $value); | ||
} | ||
|
||
/** | ||
* Check for the presence of control characters except line breaks and tabs. | ||
* | ||
* @param array|string $value | ||
* | ||
* @return array|string | ||
*/ | ||
protected function checkControl($value) | ||
{ | ||
if (is_array($value)) { | ||
array_map([$this, 'checkControl'], $value); | ||
|
||
return $value; | ||
} | ||
|
||
if (preg_match($this->controlCodeRegex, $value) === 1) { | ||
return $value; | ||
} | ||
|
||
throw SecurityException::forInvalidControlChars($this->source, $value); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Filters; | ||
|
||
use CodeIgniter\HTTP\CLIRequest; | ||
use CodeIgniter\HTTP\IncomingRequest; | ||
use CodeIgniter\HTTP\URI; | ||
use CodeIgniter\HTTP\UserAgent; | ||
use CodeIgniter\Security\Exceptions\SecurityException; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\Mock\MockAppConfig; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class InvalidCharsTest extends CIUnitTestCase | ||
{ | ||
/** | ||
* @var InvalidChars | ||
*/ | ||
private $invalidChars; | ||
|
||
/** | ||
* @var IncomingRequest | ||
*/ | ||
private $request; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$_GET = []; | ||
$_POST = []; | ||
$_COOKIE = []; | ||
|
||
$this->request = $this->createRequest(); | ||
$this->invalidChars = new InvalidChars(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
$_GET = []; | ||
$_POST = []; | ||
$_COOKIE = []; | ||
} | ||
|
||
private function createRequest(): IncomingRequest | ||
{ | ||
$config = new MockAppConfig(); | ||
$uri = new URI(); | ||
$userAgent = new UserAgent(); | ||
$request = $this->getMockBuilder(IncomingRequest::class) | ||
->setConstructorArgs([$config, $uri, null, $userAgent]) | ||
->onlyMethods(['isCLI']) | ||
->getMock(); | ||
$request->method('isCLI')->willReturn(false); | ||
|
||
return $request; | ||
} | ||
|
||
/** | ||
* @doesNotPerformAssertions | ||
*/ | ||
public function testBeforeDoNothingWhenCLIRequest() | ||
{ | ||
$cliRequest = new CLIRequest(new MockAppConfig()); | ||
|
||
$this->invalidChars->before($cliRequest); | ||
} | ||
|
||
/** | ||
* @doesNotPerformAssertions | ||
*/ | ||
public function testBeforeValidString() | ||
{ | ||
$_POST['val'] = [ | ||
'valid string', | ||
]; | ||
$_COOKIE['val'] = 'valid string'; | ||
|
||
$this->invalidChars->before($this->request); | ||
} | ||
|
||
public function testBeforeInvalidUTF8StringCausesException() | ||
{ | ||
$this->expectException(SecurityException::class); | ||
$this->expectExceptionMessage('Invalid UTF-8 characters in post:'); | ||
|
||
$sjisString = mb_convert_encoding('SJISの文字列です。', 'SJIS'); | ||
$_POST['val'] = [ | ||
'valid string', | ||
$sjisString, | ||
]; | ||
|
||
$this->invalidChars->before($this->request); | ||
} | ||
|
||
public function testBeforeInvalidControlCharCausesException() | ||
{ | ||
$this->expectException(SecurityException::class); | ||
$this->expectExceptionMessage('Invalid Control characters in cookie:'); | ||
|
||
$stringWithNullChar = "String contains null char and line break.\0\n"; | ||
$_COOKIE['val'] = $stringWithNullChar; | ||
|
||
$this->invalidChars->before($this->request); | ||
} | ||
|
||
/** | ||
* @doesNotPerformAssertions | ||
* | ||
* @dataProvider stringWithLineBreakAndTabProvider | ||
*/ | ||
public function testCheckControlStringWithLineBreakAndTabReturnsTheString(string $input) | ||
{ | ||
$_GET['val'] = $input; | ||
|
||
$this->invalidChars->before($this->request); | ||
} | ||
|
||
public function stringWithLineBreakAndTabProvider() | ||
{ | ||
yield from [ | ||
["String contains \n line break."], | ||
["String contains \r line break."], | ||
["String contains \r\n line break."], | ||
["String contains \t tab."], | ||
["String contains \t and \r line \n break."], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider stringWithControlCharsProvider | ||
*/ | ||
public function testCheckControlStringWithControlCharsCausesException(string $input) | ||
{ | ||
$this->expectException(SecurityException::class); | ||
$this->expectExceptionMessage('Invalid Control characters in get:'); | ||
|
||
$_GET['val'] = $input; | ||
|
||
$this->invalidChars->before($this->request); | ||
} | ||
|
||
public function stringWithControlCharsProvider() | ||
{ | ||
yield from [ | ||
["String contains null char.\0"], | ||
["String contains null char and line break.\0\n"], | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.