Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.

Commit 98028b1

Browse files
michaelzangerlearjanfrans
authored andcommitted
#13 [HttpKernelExtensions] Prevent duplicate parameters in route and request body
1 parent 7708746 commit 98028b1

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fusonic/http-kernel-extensions",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Symfony HttpKernel Component Extensions.",
55
"type": "library",
66
"authors": [

composer.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controller/RequestDtoResolver.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ public function resolve(Request $request, ArgumentMetadata $argument): Generator
5656

5757
if (in_array($request->getMethod(), self::METHODS_WITH_STRICT_TYPE_CHECKS, true)) {
5858
$options = [];
59-
$content = $this->getRequestContent($request);
60-
$data = array_merge($content, $routeParameters);
59+
$data = $this->mergeRequestData($this->getRequestContent($request), $routeParameters);
6160
} else {
6261
$options = [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
63-
$queries = $this->getRequestQueries($request);
64-
$data = array_merge($queries, $routeParameters);
62+
$data = $this->mergeRequestData($this->getRequestQueries($request), $routeParameters);
6563
}
6664

6765
$dto = $this->denormalize($data, $class, $options);
@@ -135,4 +133,13 @@ private function validate(RequestDto $dto): void
135133
throw new BadRequestHttpException('The request payload is invalid!'.PHP_EOL.$details);
136134
}
137135
}
136+
137+
private function mergeRequestData(array $data, array $routeParameters): array
138+
{
139+
if (count($keys = array_intersect_key($data, $routeParameters)) > 0) {
140+
throw new BadRequestHttpException(sprintf('Parameters (%s) used as route attributes can not be used in the request body or query parameters.', implode(', ', array_keys($keys))));
141+
}
142+
143+
return array_merge($data, $routeParameters);
144+
}
138145
}

tests/Controller/RequestDtoResolverTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,35 @@ public function testInvalidRequestBodyHandling(): void
174174
$generator->current();
175175
}
176176

177+
public function testDuplicateKeyHandling(): void
178+
{
179+
$this->expectException(BadRequestHttpException::class);
180+
$query = [
181+
'int' => 5,
182+
'float' => 9.99,
183+
'string' => 'foobar',
184+
'bool' => true,
185+
];
186+
$attributes = [
187+
'_route_params' => [
188+
'int' => 5,
189+
'float' => 9.99,
190+
'string' => 'foobar',
191+
'bool' => true,
192+
],
193+
];
194+
195+
$request = new Request($query, [], $attributes);
196+
$request->setMethod(Request::METHOD_GET);
197+
$argument = new ArgumentMetadata('dto', TestDto::class, false, false, null);
198+
199+
$resolver = new RequestDtoResolver($this->getDenormalizer(), $this->getValidator());
200+
$generator = $resolver->resolve($request, $argument);
201+
202+
/** @var TestDto $dto */
203+
$dto = $generator->current();
204+
}
205+
177206
public function testQueryParameterHandling(): void
178207
{
179208
$query = [

0 commit comments

Comments
 (0)