Skip to content

Commit

Permalink
RequestFactory: throws exception on invalid $_POST/$_COOKIE data
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 30, 2020
1 parent b0f147a commit 9ff6baa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ private function getGetPostCookie(Url $url): array
$list[$key][$k] = $v;
$list[] = &$list[$key][$k];

} else {
} elseif (is_string($v)) {
$list[$key][$k] = (string) preg_replace('#[^' . self::CHARS . ']+#u', '', $v);

} else {
throw new Nette\InvalidStateException(sprintf('Invalid value in $_POST/$_COOKIE in key %s, expected string, %s given.', "'$k'", gettype($v)));
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Http/Request.invalidType.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Test: Nette\Http\Request invalid data.
*/

declare(strict_types=1);

use Nette\Http;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('invalid POST', function () {
$_POST = [
'int' => 1,
];

Assert::exception(function () {
(new Http\RequestFactory)->fromGlobals();
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'int\', expected string, integer given.');
});


test('invalid COOKIE', function () {
$_POST = [];
$_COOKIE = ['x' => [1]];

Assert::exception(function () {
(new Http\RequestFactory)->fromGlobals();
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'0\', expected string, integer given.');
});

0 comments on commit 9ff6baa

Please sign in to comment.