Skip to content
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

fix: add parameter checking for max_size #6261

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions system/Validation/FileRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\HTTP\RequestInterface;
use Config\Mimes;
use Config\Services;
use InvalidArgumentException;

/**
* File validation rules
Expand Down Expand Up @@ -79,8 +80,11 @@ public function max_size(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);
$paramArray = explode(',', $params);
if (count($paramArray) !== 2) {
throw new InvalidArgumentException('Invalid max_size parameter: "' . $params . '"');
}
$name = array_shift($paramArray);

if (! ($files = $this->request->getFileMultiple($name))) {
$files = [$this->request->getFile($name)];
Expand All @@ -99,7 +103,7 @@ public function max_size(?string $blank, string $params): bool
return false;
}

if ($file->getSize() / 1024 > $params[0]) {
if ($file->getSize() / 1024 > $paramArray[0]) {
return false;
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Validation/FileRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;
use InvalidArgumentException;
use Tests\Support\Validation\TestRules;

/**
Expand Down Expand Up @@ -198,6 +199,15 @@ public function testMaxSizeBad(): void
$this->assertFalse($this->validation->run([]));
}

public function testMaxSizeInvalidParam(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid max_size parameter: "avatar.100"');

$this->validation->setRules(['avatar' => 'max_size[avatar.100]']);
$this->validation->run([]);
}

public function testMaxDims(): void
{
$this->validation->setRules(['avatar' => 'max_dims[avatar,640,480]']);
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Validation/StrictRules/FileRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Validation\Validation;
use Config\Services;
use InvalidArgumentException;
use Tests\Support\Validation\TestRules;

/**
Expand Down Expand Up @@ -199,6 +200,15 @@ public function testMaxSizeBad(): void
$this->assertFalse($this->validation->run([]));
}

public function testMaxSizeInvalidParam(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid max_size parameter: "avatar.100"');

$this->validation->setRules(['avatar' => 'max_size[avatar.100]']);
$this->validation->run([]);
}

public function testMaxDims(): void
{
$this->validation->setRules(['avatar' => 'max_dims[avatar,640,480]']);
Expand Down