Skip to content

Commit ab1b02a

Browse files
committed
RequestFactory: throws exception on invalid $_POST/$_COOKIE data
1 parent 7a40de9 commit ab1b02a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Http/RequestFactory.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ private function getGetPostCookie(Url $url): array
157157
$list[$key][$k] = $v;
158158
$list[] = &$list[$key][$k];
159159

160-
} else {
160+
} elseif (is_string($v)) {
161161
$list[$key][$k] = (string) preg_replace('#[^' . self::CHARS . ']+#u', '', $v);
162+
163+
} else {
164+
throw new Nette\InvalidStateException(sprintf('Invalid value in $_POST/$_COOKIE in key %s, expected string, %s given.', "'$k'", gettype($v)));
162165
}
163166
}
164167
}

tests/Http/Request.invalidType.phpt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Http\Request invalid data.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Http;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
test('invalid POST', function () {
17+
$_POST = [
18+
'int' => 1,
19+
];
20+
21+
Assert::exception(function () {
22+
(new Http\RequestFactory)->fromGlobals();
23+
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'int\', expected string, integer given.');
24+
});
25+
26+
27+
test('invalid COOKIE', function () {
28+
$_POST = [];
29+
$_COOKIE = ['x' => [1]];
30+
31+
Assert::exception(function () {
32+
(new Http\RequestFactory)->fromGlobals();
33+
}, Nette\InvalidStateException::class, 'Invalid value in $_POST/$_COOKIE in key \'0\', expected string, integer given.');
34+
});

0 commit comments

Comments
 (0)