Skip to content

Commit 56bace9

Browse files
authored
fix(Filters): check both keys and values in InvalidChars arrays (#10303)
* fix(Filters): check both keys and values in InvalidChars arrays * test(Filters): use DataProvider for InvalidChars control characters test * style: fix PHPStan iterable return type in tests * chore: re-trigger CI * docs: update changelog for InvalidChars array keys fix
1 parent dd409ab commit 56bace9

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎system/Filters/InvalidChars.php‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,18 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
8787
* @param array|string $value
8888
*
8989
* @return array|string
90+
*
91+
* @throws SecurityException
9092
*/
9193
protected function checkEncoding($value)
9294
{
9395
if (is_array($value)) {
94-
array_map($this->checkEncoding(...), $value);
96+
foreach ($value as $key => $item) {
97+
if (is_string($key)) {
98+
$this->checkEncoding($key);
99+
}
100+
$this->checkEncoding($item);
101+
}
95102

96103
return $value;
97104
}
@@ -113,7 +120,12 @@ protected function checkEncoding($value)
113120
protected function checkControl($value)
114121
{
115122
if (is_array($value)) {
116-
array_map($this->checkControl(...), $value);
123+
foreach ($value as $key => $item) {
124+
if (is_string($key)) {
125+
$this->checkControl($key);
126+
}
127+
$this->checkControl($item);
128+
}
117129

118130
return $value;
119131
}

‎tests/system/Filters/InvalidCharsTest.php‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,51 @@ public function testBeforeInvalidUTF8StringCausesException(): void
103103
$this->invalidChars->before($this->request);
104104
}
105105

106-
public function testBeforeInvalidControlCharCausesException(): void
106+
public function testBeforeInvalidUTF8StringInArrayKeyCausesException(): void
107+
{
108+
$this->expectException(SecurityException::class);
109+
$this->expectExceptionMessage('Invalid UTF-8 characters in post:');
110+
111+
$sjisString = mb_convert_encoding('SJISの文字列です。', 'SJIS');
112+
service('superglobals')->setPost('val', [
113+
$sjisString => 'valid string',
114+
]);
115+
116+
$this->invalidChars->before($this->request);
117+
}
118+
119+
#[DataProvider('provideBeforeInvalidControlCharCausesException')]
120+
public function testBeforeInvalidControlCharCausesException(string $invalidString): void
121+
{
122+
$this->expectException(SecurityException::class);
123+
$this->expectExceptionMessage('Invalid Control characters in cookie:');
124+
125+
service('superglobals')->setCookie('val', $invalidString);
126+
127+
$this->invalidChars->before($this->request);
128+
}
129+
130+
/**
131+
* @return iterable<string, list<string>>
132+
*/
133+
public static function provideBeforeInvalidControlCharCausesException(): iterable
134+
{
135+
yield 'null byte' => ["String with null char \0"];
136+
137+
yield 'backspace' => ["String with backspace \x08"];
138+
139+
yield 'escape' => ["String with escape \x1b"];
140+
}
141+
142+
public function testBeforeInvalidControlCharInArrayKeyCausesException(): void
107143
{
108144
$this->expectException(SecurityException::class);
109145
$this->expectExceptionMessage('Invalid Control characters in cookie:');
110146

111147
$stringWithNullChar = "String contains null char and line break.\0\n";
112-
service('superglobals')->setCookie('val', $stringWithNullChar);
148+
service('superglobals')->setCookie('val', [
149+
$stringWithNullChar => 'valid string',
150+
]);
113151

114152
$this->invalidChars->before($this->request);
115153
}

‎user_guide_src/source/changelogs/v4.7.4.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Bugs Fixed
3333
- **API:** Fixed a bug in Transformers where the root request's ``fields`` and ``include`` query parameters leaked into nested transformers created inside ``include*()`` methods, causing incorrect field filtering, unexpected includes, or infinite recursion.
3434
- **Commands:** Fixed a bug where ``spark lang:find`` treated translation keys already provided by the framework or another namespace (such as ``Errors.*`` in ``system/Language``) as new, listing them under ``--show-new`` and writing untranslated placeholders into ``app/Language`` that overrode the existing translations.
3535
- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.
36+
- **Filters:** Fixed a bug in ``InvalidChars`` filter where invalid UTF-8 or control characters in array keys were not checked.
3637
- **HTTP:** Fixed a bug where the User Agent library reported Safari's WebKit version instead of the browser version from the ``Version`` token.
3738
- **Model:** Fixed a bug in ``Model::objectToRawArray()`` where the ``$recursive`` parameter was ignored.
3839
- **Testing:** Fixed a bug where using ``MockInputOutput`` within a test that also uses ``StreamFilterTrait`` tore down the trait's stream filters, so CLI output produced after the ``MockInputOutput`` interaction (such as in ``tearDown()``) was no longer captured and leaked to the console.

0 commit comments

Comments
 (0)