Skip to content

Commit e3d201b

Browse files
authored
Merge commit from fork
* fix: validate remaining files after empty upload entries * apply review suggestions
1 parent cf96701 commit e3d201b

4 files changed

Lines changed: 161 additions & 8 deletions

File tree

‎system/Validation/StrictRules/FileRules.php‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function max_size(?string $blank, string $params): bool
100100
}
101101

102102
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
103-
return true;
103+
continue;
104104
}
105105

106106
if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
@@ -135,7 +135,7 @@ public function is_image(?string $blank, string $params): bool
135135
}
136136

137137
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
138-
return true;
138+
continue;
139139
}
140140

141141
// We know that our mimes list always has the first mime
@@ -177,7 +177,7 @@ public function mime_in(?string $blank, string $params): bool
177177
}
178178

179179
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
180-
return true;
180+
continue;
181181
}
182182

183183
if (! in_array($file->getMimeType(), $params, true)) {
@@ -215,7 +215,7 @@ public function ext_in(?string $blank, string $params): bool
215215
}
216216

217217
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
218-
return true;
218+
continue;
219219
}
220220

221221
// Check the real filename extension, not only the guessed extension.
@@ -257,7 +257,11 @@ public function max_dims(?string $blank, string $params): bool
257257
}
258258

259259
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
260-
return true;
260+
continue;
261+
}
262+
263+
if ($file->getError() !== UPLOAD_ERR_OK) {
264+
return false;
261265
}
262266

263267
// Get Parameter sizes
@@ -303,7 +307,11 @@ public function min_dims(?string $blank, string $params): bool
303307
}
304308

305309
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
306-
return true;
310+
continue;
311+
}
312+
313+
if ($file->getError() !== UPLOAD_ERR_OK) {
314+
return false;
307315
}
308316

309317
// Get Parameter sizes

‎tests/system/Validation/StrictRules/FileRulesTest.php‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,79 @@ public function testUploadedArrayReturnsFalse(): void
180180
$this->assertFalse($this->validation->run([]));
181181
}
182182

183+
#[DataProvider('provideOptionalMultipleUploadChecksFilesAfterEmptyEntry')]
184+
public function testOptionalMultipleUploadChecksFilesAfterEmptyEntry(string $rule): void
185+
{
186+
$this->setOptionalMultipleUpload('wrong.txt');
187+
188+
$this->validation->setRules(['files' => $rule]);
189+
190+
$this->assertFalse($this->validation->run([]));
191+
}
192+
193+
/**
194+
* @return iterable<string, array{string}>
195+
*/
196+
public static function provideOptionalMultipleUploadChecksFilesAfterEmptyEntry(): iterable
197+
{
198+
yield 'max_size' => ['max_size[files,0]'];
199+
200+
yield 'is_image' => ['is_image[files]'];
201+
202+
yield 'mime_in' => ['mime_in[files,image/gif]'];
203+
204+
yield 'ext_in' => ['ext_in[files,png]'];
205+
206+
yield 'max_dims' => ['max_dims[files,1,1]'];
207+
208+
yield 'min_dims' => ['min_dims[files,800,600]'];
209+
}
210+
211+
public function testOptionalMultipleUploadAllowsValidFileAfterEmptyEntry(): void
212+
{
213+
$this->setOptionalMultipleUpload('my-avatar.png');
214+
215+
$this->validation->setRules([
216+
'files' => 'max_size[files,100]|is_image[files]|mime_in[files,image/png]'
217+
. '|ext_in[files,png]|max_dims[files,640,480]|min_dims[files,320,240]',
218+
]);
219+
220+
$this->assertTrue($this->validation->run([]));
221+
}
222+
223+
public function testOptionalMultipleUploadAllowsEmptyEntries(): void
224+
{
225+
$this->setOptionalMultipleUpload();
226+
227+
$this->validation->setRules([
228+
'files' => 'max_size[files,100]|is_image[files]|mime_in[files,image/png]'
229+
. '|ext_in[files,png]|max_dims[files,640,480]|min_dims[files,320,240]',
230+
]);
231+
232+
$this->assertTrue($this->validation->run([]));
233+
}
234+
235+
private function setOptionalMultipleUpload(?string $filename = null): void
236+
{
237+
$upload = [
238+
'tmp_name' => [''],
239+
'name' => [''],
240+
'size' => [0],
241+
'type' => [''],
242+
'error' => [UPLOAD_ERR_NO_FILE],
243+
];
244+
245+
if ($filename !== null) {
246+
$upload['tmp_name'][] = TESTPATH . '_support/Validation/uploads/phpUxc0ty';
247+
$upload['name'][] = $filename;
248+
$upload['size'][] = 4614;
249+
$upload['type'][] = 'image/png';
250+
$upload['error'][] = UPLOAD_ERR_OK;
251+
}
252+
253+
service('superglobals')->setFilesArray(['files' => $upload]);
254+
}
255+
183256
public function testMaxSize(): void
184257
{
185258
$this->validation->setRules(['avatar' => 'max_size[avatar,100]']);
@@ -261,6 +334,67 @@ public function testMinDimsBad(): void
261334
$this->assertFalse($this->validation->run([]));
262335
}
263336

337+
#[DataProvider('provideDimensionsFailOnUploadError')]
338+
public function testDimensionsFailOnUploadError(string $rule, int $error): void
339+
{
340+
service('superglobals')->setFilesArray([
341+
'files' => [
342+
'tmp_name' => '',
343+
'name' => 'my-avatar.png',
344+
'size' => 0,
345+
'type' => '',
346+
'error' => $error,
347+
],
348+
]);
349+
350+
$this->validation->setRules(['files' => $rule]);
351+
352+
$this->assertFalse($this->validation->run([]));
353+
}
354+
355+
#[DataProvider('provideDimensionsFailOnUploadError')]
356+
public function testDimensionsFailOnUploadErrorAfterEmptyAndValidEntries(string $rule, int $error): void
357+
{
358+
service('superglobals')->setFilesArray([
359+
'files' => [
360+
'tmp_name' => [
361+
'',
362+
TESTPATH . '_support/Validation/uploads/phpUxc0ty',
363+
TESTPATH . '_support/Validation/uploads/phpUxc0ty',
364+
],
365+
'name' => ['', 'my-avatar.png', 'my-photo.png'],
366+
'size' => [0, 4614, 4614],
367+
'type' => ['', 'image/png', 'image/png'],
368+
'error' => [UPLOAD_ERR_NO_FILE, UPLOAD_ERR_OK, $error],
369+
],
370+
]);
371+
372+
$this->validation->setRules(['files' => $rule]);
373+
374+
$this->assertFalse($this->validation->run([]));
375+
}
376+
377+
/**
378+
* @return iterable<string, array{string, int}>
379+
*/
380+
public static function provideDimensionsFailOnUploadError(): iterable
381+
{
382+
$errors = [
383+
'ini size' => UPLOAD_ERR_INI_SIZE,
384+
'form size' => UPLOAD_ERR_FORM_SIZE,
385+
'partial' => UPLOAD_ERR_PARTIAL,
386+
'no tmp dir' => UPLOAD_ERR_NO_TMP_DIR,
387+
'cant write' => UPLOAD_ERR_CANT_WRITE,
388+
'extension' => UPLOAD_ERR_EXTENSION,
389+
];
390+
391+
foreach (['max_dims[files,640,480]', 'min_dims[files,320,240]'] as $rule) {
392+
foreach ($errors as $name => $error) {
393+
yield $rule . ' ' . $name => [$rule, $error];
394+
}
395+
}
396+
}
397+
264398
public function testIsImage(): void
265399
{
266400
$this->validation->setRules(['avatar' => 'is_image[avatar]']);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ SECURITY
2323
client-provided names in executable public directories may be affected. Use a generated filename or disable script
2424
execution in the upload directory. See the `Security advisory GHSA-4hwf-v4mp-hf6c <https://github.com/codeigniter4/CodeIgniter4/security/advisories/GHSA-4hwf-v4mp-hf6c>`_
2525
for more information.
26+
- **Validation:** File validation rules now check every non-empty file in an
27+
optional multiple upload. Previously, an entry with ``UPLOAD_ERR_NO_FILE``
28+
could cause ``max_size``, ``is_image``, ``mime_in``, ``ext_in``, ``max_dims``,
29+
and ``min_dims`` to accept the upload without checking later files.
30+
``max_dims`` and ``min_dims`` also return ``false`` for upload errors other
31+
than ``UPLOAD_ERR_NO_FILE`` before reading image dimensions.
32+
See the `Security advisory GHSA-cwxp-v62r-xwx2 <https://github.com/codeigniter4/CodeIgniter4/security/advisories/GHSA-cwxp-v62r-xwx2>`_
33+
for more information.
2634
- **View Parser:** Fixed a stored XSS vulnerability where a substituted value containing
2735
Parser syntax (e.g., ``{text|raw}`` or ``{!text!}``) was interpreted by a later
2836
substitution pass, allowing user-supplied data to bypass auto-escaping of other

‎user_guide_src/source/libraries/validation.rst‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,13 +1098,16 @@ max_dims Yes Fails if the maximum width and height of an
10981098
parameter is the field name. The second is
10991099
the width, and the third is the height. Will
11001100
also fail if the file cannot be determined
1101-
to be an image.
1101+
to be an image or the upload failed with an
1102+
error other than ``UPLOAD_ERR_NO_FILE``.
11021103
min_dims Yes Fails if the minimum width and height of an ``min_dims[field_name,300,150]``
11031104
uploaded image not meet values. The first
11041105
parameter is the field name. The second is
11051106
the width, and the third is the height. Will
11061107
also fail if the file cannot be determined
1107-
to be an image. (This rule was added in
1108+
to be an image or the upload failed with an
1109+
error other than ``UPLOAD_ERR_NO_FILE``.
1110+
(This rule was added in
11081111
v4.6.0.)
11091112
mime_in Yes Fails if the file's mime type is not one ``mime_in[field_name,image/png,image/jpeg]``
11101113
listed in the parameters.

0 commit comments

Comments
 (0)