Skip to content

Commit dcd7411

Browse files
POST|PUT: Throw 400 if request body is not valid json
1 parent af618db commit dcd7411

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

application/controllers/ApiV1ContactgroupsController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Icinga\Module\Notifications\Controllers;
66

7+
use Exception;
78
use Icinga\Exception\Http\HttpBadRequestException;
89
use Icinga\Exception\Http\HttpException;
910
use Icinga\Exception\Http\HttpNotFoundException;
@@ -382,9 +383,14 @@ private function removeContactgroup(int $id): void
382383
*/
383384
private function getValidatedData(): array
384385
{
385-
$data = $this->getRequest()->getPost();
386386
$msgPrefix = 'Invalid request body: ';
387387

388+
try {
389+
$data = $this->getRequest()->getPost();
390+
} catch (Exception $e) {
391+
$this->httpBadRequest($msgPrefix . 'given content is not a valid JSON');
392+
}
393+
388394
if (
389395
! isset($data['id'], $data['name'])
390396
|| ! is_string($data['id'])

application/controllers/ApiV1ContactsController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Icinga\Module\Notifications\Controllers;
66

7+
use Exception;
78
use Icinga\Exception\Http\HttpBadRequestException;
89
use Icinga\Exception\Http\HttpException;
910
use Icinga\Exception\Http\HttpNotFoundException;
@@ -506,9 +507,14 @@ private function removeContact(int $id): void
506507
*/
507508
private function getValidatedData(): array
508509
{
509-
$data = $this->getRequest()->getPost();
510510
$msgPrefix = 'Invalid request body: ';
511511

512+
try {
513+
$data = $this->getRequest()->getPost();
514+
} catch (Exception $e) {
515+
$this->httpBadRequest($msgPrefix . 'given content is not a valid JSON');
516+
}
517+
512518
if (
513519
! isset($data['id'], $data['full_name'], $data['default_channel'])
514520
|| ! is_string($data['id'])

0 commit comments

Comments
 (0)