Skip to content

Commit cfc5c72

Browse files
committed
fix(HTTP): read request body when post_max_size is unlimited
PHP treats post_max_size values of 0 or -1 as unlimited, but IncomingRequest compared Content-Length against the raw value, so the request body was never read. Use ini_parse_quantity() and treat non-positive values as PHP_INT_MAX.
1 parent 6ddf104 commit cfc5c72

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
140140
// php://input is not available with enctype="multipart/form-data".
141141
// See https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input
142142
&& ! str_contains($this->getHeaderLine('Content-Type'), 'multipart/form-data')
143-
&& (int) $this->getHeaderLine('Content-Length') <= $this->getPostMaxSize()
143+
&& (int) $this->getHeaderLine('Content-Length') <= $this->getPostMaxSize((string) ini_get('post_max_size'))
144144
) {
145145
// Get our body from php://input
146146
$body = file_get_contents('php://input');
@@ -167,16 +167,16 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
167167
$this->detectLocale($config);
168168
}
169169

170-
private function getPostMaxSize(): int
170+
/**
171+
* Converts the `post_max_size` ini value to bytes.
172+
*
173+
* A value of 0 or less means unlimited.
174+
*/
175+
private function getPostMaxSize(string $postMaxSize): int
171176
{
172-
$postMaxSize = ini_get('post_max_size');
177+
$bytes = ini_parse_quantity($postMaxSize);
173178

174-
return match (strtoupper(substr($postMaxSize, -1))) {
175-
'G' => (int) str_replace('G', '', $postMaxSize) * 1024 ** 3,
176-
'M' => (int) str_replace('M', '', $postMaxSize) * 1024 ** 2,
177-
'K' => (int) str_replace('K', '', $postMaxSize) * 1024,
178-
default => (int) $postMaxSize,
179-
};
179+
return $bytes <= 0 ? PHP_INT_MAX : $bytes;
180180
}
181181

182182
/**

‎tests/system/HTTP/IncomingRequestTest.php‎

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

1084+
#[DataProvider('provideGetPostMaxSize')]
1085+
public function testGetPostMaxSize(string $value, int $expected): void
1086+
{
1087+
$getPostMaxSize = $this->getPrivateMethodInvoker($this->request, 'getPostMaxSize');
1088+
1089+
$this->assertSame($expected, $getPostMaxSize($value));
1090+
}
1091+
1092+
/**
1093+
* @return iterable<array{string, int}>
1094+
*/
1095+
public static function provideGetPostMaxSize(): iterable
1096+
{
1097+
yield 'zero means unlimited' => ['0', PHP_INT_MAX];
1098+
1099+
yield 'zero with suffix means unlimited' => ['0K', PHP_INT_MAX];
1100+
1101+
yield 'negative means unlimited' => ['-1', PHP_INT_MAX];
1102+
1103+
yield 'gigabytes' => ['1G', 1024 ** 3];
1104+
1105+
yield 'lowercased gigabytes' => ['1g', 1024 ** 3];
1106+
1107+
yield 'megabytes' => ['8M', 8 * 1024 ** 2];
1108+
1109+
yield 'lowercased megabytes' => ['8m', 8 * 1024 ** 2];
1110+
1111+
yield 'kilobytes' => ['512K', 512 * 1024];
1112+
1113+
yield 'lowercased kilobytes' => ['512k', 512 * 1024];
1114+
1115+
yield 'hexadecimal' => ['0x10M', 16 * 1024 ** 2];
1116+
1117+
yield 'octal' => ['010M', 8 * 1024 ** 2];
1118+
1119+
yield 'binary' => ['0b10M', 2 * 1024 ** 2];
1120+
1121+
yield 'bytes' => ['12345', 12345];
1122+
}
1123+
10841124
public function testGetIPAddressNormal(): void
10851125
{
10861126
$expected = '123.123.123.123';

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ Bugs Fixed
3535
- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
3636
- **Format:** Fixed ``XMLFormatter`` so that an array key that becomes empty after sanitization no longer triggers a ``ValueError``; the fallback tag name ``item`` is used instead. Values that cannot be converted to a string, such as objects not implementing ``Stringable``, now throw a ``FormatException`` instead of a fatal ``Error``.
3737
- **Helpers:** Fixed a regression in v4.7.5 where ``set_value()`` threw a ``TypeError`` when the default value was not a string or an array, such as an integer, a float, ``null``, or a ``Stringable`` object. Such values are now converted to a string.
38-
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
38+
- **HTTP:** Fixed a bug where ``IncomingRequest`` did not read the request body when PHP's ``post_max_size`` was set to ``0`` or ``-1`` (unlimited).
3939
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted proxy when the value contained uppercase letters or surrounding whitespace, or was represented as a single-element array.
40+
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
4041
- **Security:** Fixed a bug where a valid JSON request body that is not an object (for example a JSON array) was corrupted after CSRF verification when the token was sent via the ``X-CSRF-TOKEN`` header.
4142

4243
See the repo's

0 commit comments

Comments
 (0)