Skip to content

Commit bdf459d

Browse files
authored
fix: handle zero key and non-array intermediate values in ArrayHelper::dotKeyExists() (#10757)
1 parent 6ad9858 commit bdf459d

3 files changed

Lines changed: 69 additions & 12 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Release Date: Unreleased
2727
- **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas.
2828
- **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters.
2929
- **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`.
30+
- **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements.
3031
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
3132
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
3233
- **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format.

‎system/Helpers/Array/ArrayHelper.php‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,30 +143,50 @@ public static function dotKeyExists(string $index, array $array): bool
143143
return false;
144144
}
145145

146-
$currentArray = $array;
146+
return self::checkDotKeyExists($indexes, $array);
147+
}
147148

149+
/**
150+
* Recursively check if the key exists in the array with wildcards.
151+
*
152+
* @used-by dotKeyExists()
153+
*
154+
* @param list<string> $indexes
155+
* @param array<array-key, mixed> $currentArray
156+
*/
157+
private static function checkDotKeyExists(array $indexes, array $currentArray): bool
158+
{
148159
// Grab the current index
149-
while ($currentIndex = array_shift($indexes)) {
160+
while ($indexes !== []) {
161+
$currentIndex = array_shift($indexes);
162+
150163
if ($currentIndex === '*') {
151-
$currentIndex = array_shift($indexes);
164+
if (! is_array($currentArray) || $currentArray === []) {
165+
return false;
166+
}
152167

153-
foreach ($currentArray as $item) {
154-
if (! array_key_exists($currentIndex, $item)) {
155-
return false;
168+
if (count($indexes) === 1) {
169+
$nextIndex = $indexes[0];
170+
171+
foreach ($currentArray as $item) {
172+
if (! is_array($item) || ! array_key_exists($nextIndex, $item)) {
173+
return false;
174+
}
156175
}
157-
}
158176

159-
// If indexes is empty, all elements are checked.
160-
if ($indexes === []) {
161177
return true;
162178
}
163179

164-
$currentArray = self::dotSearch('*.' . $currentIndex, $currentArray);
180+
foreach ($currentArray as $item) {
181+
if (! is_array($item) || ! self::checkDotKeyExists($indexes, $item)) {
182+
return false;
183+
}
184+
}
165185

166-
continue;
186+
return true;
167187
}
168188

169-
if (! array_key_exists($currentIndex, $currentArray)) {
189+
if (! is_array($currentArray) || ! array_key_exists($currentIndex, $currentArray)) {
170190
return false;
171191
}
172192

‎tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ public function testDotKeyExists(): void
4343
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends', $this->array));
4444
$this->assertFalse(ArrayHelper::dotKeyExists('not.friends', $this->array));
4545
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends.0.name', $this->array));
46+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.nonexistent', $this->array));
4647
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.name', $this->array));
48+
$this->assertFalse(ArrayHelper::dotKeyExists('0', []));
49+
$this->assertTrue(ArrayHelper::dotKeyExists('0', ['0' => 'value']));
50+
$this->assertTrue(ArrayHelper::dotKeyExists('0', ['0' => null]));
51+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.0.name.sub', $this->array));
52+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.sub', $this->array));
4753
}
4854

4955
public function testDotKeyExistsWithEndingWildCard(): void
@@ -72,6 +78,36 @@ public function testDotKeyExistsWithWildCard(): void
7278
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.age', $this->array));
7379
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.1.age', $this->array));
7480
$this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.name', $this->array));
81+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.0.nonexistent', $this->array));
7582
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.1.name', $this->array));
83+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.age.sub', $this->array));
84+
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.*.sub', $this->array));
85+
$this->assertFalse(ArrayHelper::dotKeyExists('*.name', [['name' => 'Fred'], 'not-an-array']));
86+
$this->assertFalse(ArrayHelper::dotKeyExists('*.name', []));
87+
}
88+
89+
public function testDotKeyExistsWithZeroIndexAfterWildCard(): void
90+
{
91+
$array = [
92+
'users' => [
93+
['contacts' => [['email' => 'alice@example.com']]],
94+
['contacts' => [['email' => 'bob@example.com']]],
95+
],
96+
];
97+
98+
$this->assertTrue(ArrayHelper::dotKeyExists('users.*.contacts.0.email', $array));
99+
$this->assertTrue(ArrayHelper::dotKeyExists('users.*.contacts.*.email', $array));
100+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.0.nonexistent', $array));
101+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.1.email', $array));
102+
103+
$arrayWithMissingKey = [
104+
'users' => [
105+
['contacts' => [['email' => 'alice@example.com']]],
106+
['contacts' => [['name' => 'Bob']]],
107+
],
108+
];
109+
110+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.0.email', $arrayWithMissingKey));
111+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.*.email', $arrayWithMissingKey));
76112
}
77113
}

0 commit comments

Comments
 (0)