Skip to content

Commit 1146734

Browse files
williamdesDeepSeek
andcommitted
security: Bound the size and the types of an ingested report
/incidents/create takes a body from anybody and sends a notification email for every stored report. The reading of the payload was hardened twice for the types, by 10a7e83 and f002513, without bounding its size: a body of any length was decoded, a report whose errors was not a list stopped on a count() TypeError, a stack that was not a list stopped on a foreach, a mistyped scalar stopped on a TypeError, and a string longer than its column stopped on an SQL error once the row was built. Refuse a body over 1 MiB with a 413 before decoding it, keep the first 40 errors and the first 40 stack frames, cut the strings to the length of their column, read the scalars defensively, and refuse a report whose full report or stacktrace would not fit in a TEXT column. Signed-off-by: William Desportes <williamdes@wdes.fr> Co-Authored-By: DeepSeek <noreply@deepseek.com>
1 parent 7eacb2c commit 1146734

4 files changed

Lines changed: 388 additions & 49 deletions

File tree

‎src/Controller/IncidentsController.php‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,29 @@
3131
use function array_merge;
3232
use function count;
3333
use function in_array;
34+
use function is_array;
3435
use function json_decode;
3536
use function json_encode;
37+
use function strlen;
3638

3739
use const JSON_PRETTY_PRINT;
40+
use const JSON_THROW_ON_ERROR;
3841
use const JSON_UNESCAPED_SLASHES;
3942

4043
/**
4144
* Incidents controller handling incident creation and rendering.
4245
*/
4346
class IncidentsController extends AppController
4447
{
48+
/**
49+
* The largest report body accepted, in bytes.
50+
*
51+
* Reports are submitted without authentication and each new report sends
52+
* a notification email, so the size of a single submission is bounded
53+
* before it is decoded.
54+
*/
55+
private const int MAX_REQUEST_BODY_SIZE = 1048576;
56+
4557
protected NotificationsTable $Notifications;
4658
protected IncidentsTable $Incidents;
4759
protected ReportsTable $Reports;
@@ -74,9 +86,19 @@ public function create(): Response|null
7486
$this->request->allowMethod(['post']);
7587

7688
$requestBody = (string) $this->request->getBody();
89+
90+
// Refuse oversized submissions before decoding them
91+
if (strlen($requestBody) > self::MAX_REQUEST_BODY_SIZE) {
92+
$this->response = $this->response
93+
->withStatus(413)
94+
->withStringBody($this->rejectionBody());
95+
96+
return $this->response;
97+
}
98+
7799
$bugReport = json_decode($requestBody, true);
78100

79-
if ($bugReport === null || $bugReport === false || $requestBody === '') {
101+
if (! is_array($bugReport) || $requestBody === '') {
80102
$response = ['success' => false];
81103

82104
$this->response = $this->response
@@ -115,6 +137,19 @@ public function create(): Response|null
115137
return $this->response;
116138
}
117139

140+
/**
141+
* Builds the body of a refused report.
142+
*
143+
* @return string the JSON encoded failure answer
144+
*/
145+
private function rejectionBody(): string
146+
{
147+
return json_encode(
148+
['success' => false],
149+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
150+
);
151+
}
152+
118153
public function json(string|null $id): Response|null
119154
{
120155
if (empty($id)) {

0 commit comments

Comments
 (0)