Skip to content

Commit f47fce2

Browse files
committed
Add strict checking
1 parent 454fd6e commit f47fce2

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/Authenticator/RequestAuthenticator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Log\LoggerInterface;
1616
use Sylius\Component\Channel\Context\ChannelContextInterface;
1717
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
1819

1920
final class RequestAuthenticator implements RequestAuthenticatorInterface
2021
{
@@ -39,6 +40,14 @@ public function authenticate(Request $request): bool
3940

4041
$content = $request->getContent(false);
4142

43+
if (is_resource($content)) {
44+
$content = stream_get_contents($content);
45+
}
46+
47+
if (false === $content) {
48+
throw new \InvalidArgumentException('Invalid JSON payload', Response::HTTP_BAD_REQUEST);
49+
}
50+
4251
$content = json_encode(
4352
json_decode($content, true),
4453
\JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,

src/Controller/UserComAgreementsController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ public function __invoke(Request $request): Response
3636
if (false === $this->requestAuthenticator->authenticate($request)) {
3737
return new JsonResponse('Unauthorized', Response::HTTP_UNAUTHORIZED);
3838
}
39-
$payload = json_decode($request->getContent(false), true);
39+
40+
$payload = $request->getContent(false);
41+
if (is_resource($payload)) {
42+
$payload = stream_get_contents($payload);
43+
}
44+
45+
if (false === $payload) {
46+
return new JsonResponse('Invalid JSON payload', Response::HTTP_BAD_REQUEST);
47+
}
48+
49+
$payload = json_decode($payload, true);
4050

4151
if (!is_array($payload) || [] === $payload) {
4252
return new JsonResponse('Invalid JSON payload', Response::HTTP_BAD_REQUEST);

0 commit comments

Comments
 (0)