Skip to content

Commit 18c05e1

Browse files
committed
fix(HTTP): read request body when post_max_size is unlimited
post_max_size values of 0 or -1 mean unlimited. The previous code returned the raw value, so the Content-Length check failed and the request body was never read. Treat non-positive values as PHP_INT_MAX.
1 parent df8b96b commit 18c05e1

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,19 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
169169

170170
private function getPostMaxSize(): int
171171
{
172-
$postMaxSize = ini_get('post_max_size');
172+
return $this->parsePostMaxSize((string) ini_get('post_max_size'));
173+
}
174+
175+
/**
176+
* Converts the `post_max_size` ini value to bytes.
177+
*
178+
* A value of 0 or less means unlimited.
179+
*/
180+
private function parsePostMaxSize(string $postMaxSize): int
181+
{
182+
if ((int) $postMaxSize <= 0) {
183+
return PHP_INT_MAX;
184+
}
173185

174186
return match (strtoupper(substr($postMaxSize, -1))) {
175187
'G' => (int) str_replace('G', '', $postMaxSize) * 1024 ** 3,

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,32 @@ public function testSetPath(): void
10811081
$this->assertSame('foobar', $request->getPath());
10821082
}
10831083

1084+
#[DataProvider('provideParsePostMaxSize')]
1085+
public function testParsePostMaxSize(string $value, int $expected): void
1086+
{
1087+
$parsePostMaxSize = $this->getPrivateMethodInvoker($this->request, 'parsePostMaxSize');
1088+
1089+
$this->assertSame($expected, $parsePostMaxSize($value));
1090+
}
1091+
1092+
/**
1093+
* @return iterable<array{string, int}>
1094+
*/
1095+
public static function provideParsePostMaxSize(): iterable
1096+
{
1097+
yield 'zero means unlimited' => ['0', PHP_INT_MAX];
1098+
1099+
yield 'negative means unlimited' => ['-1', PHP_INT_MAX];
1100+
1101+
yield 'gigabytes' => ['1G', 1024 ** 3];
1102+
1103+
yield 'megabytes' => ['8M', 8 * 1024 ** 2];
1104+
1105+
yield 'kilobytes' => ['512K', 512 * 1024];
1106+
1107+
yield 'bytes' => ['12345', 12345];
1108+
}
1109+
10841110
public function testGetIPAddressNormal(): void
10851111
{
10861112
$expected = '123.123.123.123';

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ Bugs Fixed
3838
is used instead. Values that cannot be converted to a string, such as objects
3939
not implementing ``Stringable``, now throw a ``FormatException`` instead of a
4040
fatal ``Error``.
41-
- **Security:** Fixed a bug where a valid JSON request body that is not an object
42-
(for example a JSON array) was corrupted after CSRF verification when the
43-
token was sent via the ``X-CSRF-TOKEN`` header.
41+
- **HTTP:** Fixed a bug where ``IncomingRequest`` did not read the request body
42+
when PHP's ``post_max_size`` was set to ``0`` or ``-1`` (unlimited).
4443
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not
4544
recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted
4645
proxy when the value contained uppercase letters or surrounding
4746
whitespace, or was represented as a single-element array.
47+
- **Security:** Fixed a bug where a valid JSON request body that is not an object
48+
(for example a JSON array) was corrupted after CSRF verification when the
49+
token was sent via the ``X-CSRF-TOKEN`` header.
4850

4951
See the repo's
5052
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)