From a3afe63aa0ff3862e99113fc1fc6c127aa93d8b4 Mon Sep 17 00:00:00 2001 From: RemRem Date: Wed, 15 Nov 2023 01:06:25 +0100 Subject: [PATCH] add validation for request json --- app/helpers.php | 31 +++++++++++++++++++++++++++++++ app/routes.php | 6 ++++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 app/helpers.php diff --git a/app/helpers.php b/app/helpers.php new file mode 100644 index 0000000..baadf85 --- /dev/null +++ b/app/helpers.php @@ -0,0 +1,31 @@ +post('/user', function (Request $req, Response $res) { - $req_body = $req->getParsedBody(); - if (!array_key_exists('email', $req_body) || !array_key_exists('hash', $req_body) || !array_key_exists('username', $req_body)) { + if (!Helpers::validJson((string) $req->getBody(), array("email", "hash", "username"))) { return $res->withStatus(400); } + + $req_body = $req->getParsedBody(); $code = (new UserGateway)->createUser($req_body['email'], $req_body['hash'], $req_body['username']); if ($code === -1) return $res->withStatus(409);