From 0af2fe79f2728d88d24618fa0fa47575e5d76a2c Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 28 Nov 2017 12:28:54 +0700 Subject: [PATCH] StandardServer: a bit more validation for parsed json PSR-7 request (related to #202) --- src/Server/Helper.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Server/Helper.php b/src/Server/Helper.php index d855eff..8cc13f0 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -493,16 +493,21 @@ class Helper ); } + // Try parsing ourselves if PSR-7 implementation doesn't parse JSON automatically + if (is_array($bodyParams) && empty($bodyParams)) { + $bodyParams = json_decode($request->getBody(), true); + + if (json_last_error()) { + throw new RequestError("Could not parse JSON: " . json_last_error_msg()); + } + } + if (!is_array($bodyParams)) { throw new RequestError( "GraphQL Server expects JSON object or array, but got " . Utils::printSafeJson($bodyParams) ); } - - if (empty($bodyParams)) { - $bodyParams = json_decode($request->getBody(), true); - } } else { $bodyParams = $request->getParsedBody();