-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpKernel] Prevent TypeError for out-of-range route parameters #61458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mudassaralichouhan
wants to merge
4
commits into
symfony:7.4
Choose a base branch
from
mudassaralichouhan:feature/request-attribute-scalar-resolver
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+324
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d35ff3a
Prevent type error on invalid or out-of-range route params by adding …
mudassaralichouhan 7b01081
CS: remove :void from test methods per fixer rules
mudassaralichouhan 736e9dd
Fix coding style issues with PHP-CS-Fixer
mudassaralichouhan 07701bc
Refactor: use ParameterBag::filter() for scalar type validation in Re…
mudassaralichouhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...le/FrameworkBundle/Tests/Functional/RequestAttributeScalarValueResolverFunctionalTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; | ||
|
||
class RequestAttributeScalarValueResolverFunctionalTest extends AbstractWebTestCase | ||
{ | ||
public function testValidIntReturns200() | ||
{ | ||
$client = $this->createClient(['test_case' => 'RequestAttributeScalarValueResolver']); | ||
|
||
$client->request('GET', '/123'); | ||
$response = $client->getResponse(); | ||
|
||
$this->assertSame(200, $response->getStatusCode()); | ||
$this->assertSame('123', $response->getContent()); | ||
} | ||
|
||
public function testInvalidStringReturns404() | ||
{ | ||
$client = $this->createClient(['test_case' => 'RequestAttributeScalarValueResolver']); | ||
|
||
$client->request('GET', '/abc'); | ||
$response = $client->getResponse(); | ||
|
||
$this->assertSame(404, $response->getStatusCode()); | ||
} | ||
|
||
public function testOutOfRangeIntReturns404() | ||
{ | ||
$client = $this->createClient(['test_case' => 'RequestAttributeScalarValueResolver']); | ||
|
||
$client->request('GET', '/9223372036854775808'); | ||
$response = $client->getResponse(); | ||
|
||
$this->assertSame(404, $response->getStatusCode()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...le/FrameworkBundle/Tests/Functional/RequestAttributeScalarValueResolverTestController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
class RequestAttributeScalarValueResolverTestController | ||
{ | ||
#[Route(path: '/{id}', name: 'test_scalar_id')] | ||
public function __invoke(int $id): Response | ||
{ | ||
return new Response((string) $id); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ndle/FrameworkBundle/Tests/Functional/app/RequestAttributeScalarValueResolver/bundles.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
|
||
return [ | ||
new FrameworkBundle(), | ||
]; |
4 changes: 4 additions & 0 deletions
4
...undle/FrameworkBundle/Tests/Functional/app/RequestAttributeScalarValueResolver/config.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
...ndle/FrameworkBundle/Tests/Functional/app/RequestAttributeScalarValueResolver/routing.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
controllers: | ||
resource: Symfony\Bundle\FrameworkBundle\Tests\Functional\RequestAttributeScalarValueResolverTestController | ||
type: attribute | ||
|
||
|
113 changes: 113 additions & 0 deletions
113
.../Component/HttpKernel/Controller/ArgumentResolver/RequestAttributeScalarValueResolver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* Safely resolves typed scalar and BackedEnum arguments from request attributes (path parameters). | ||
* | ||
* If a value cannot be converted to the expected type (e.g. invalid int or out-of-range), | ||
* a 404 Not Found is thrown instead of letting execution reach the controller and erroring. | ||
* | ||
* Types handled: string, int, float, bool, \BackedEnum | ||
*/ | ||
final class RequestAttributeScalarValueResolver implements ValueResolverInterface | ||
{ | ||
public function resolve(Request $request, ArgumentMetadata $argument): array | ||
{ | ||
if ($argument->isVariadic()) { | ||
return []; | ||
} | ||
|
||
$name = $argument->getName(); | ||
if (!$request->attributes->has($name)) { | ||
return []; | ||
} | ||
|
||
$type = $argument->getType(); | ||
if (null === $type) { | ||
// Untyped; let the default attribute resolver handle it. | ||
return []; | ||
} | ||
|
||
// Skip union or intersection types; fall back to default behavior | ||
if (str_contains($type, '|') || str_contains($type, '&')) { | ||
return []; | ||
} | ||
|
||
$value = $request->attributes->get($name); | ||
|
||
// Handle Backed Enum typed arguments | ||
if (is_subclass_of($type, \BackedEnum::class, true)) { | ||
if (!\is_string($value) && !\is_int($value)) { | ||
throw new NotFoundHttpException(\sprintf('The value for the "%s" route parameter is invalid.', $name)); | ||
} | ||
try { | ||
return [$type::from($value)]; | ||
} catch (\ValueError) { | ||
throw new NotFoundHttpException(\sprintf('The value for the "%s" route parameter is invalid.', $name)); | ||
} | ||
} | ||
|
||
if ($value instanceof \Stringable) { | ||
$value = (string) $value; | ||
} | ||
|
||
switch ($type) { | ||
case 'string': | ||
if (\is_scalar($value) || (null === $value && $argument->isNullable())) { | ||
return [(string) $value]; | ||
} | ||
throw new NotFoundHttpException(\sprintf('The value for the "%s" route parameter is invalid.', $name)); | ||
case 'int': | ||
if (\is_int($value)) { | ||
return [$value]; | ||
} | ||
$filtered = filter_var($value, \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE); | ||
if (null === $filtered) { | ||
// Includes out-of-range values | ||
throw new NotFoundHttpException(\sprintf('The value for the "%s" route parameter is invalid.', $name)); | ||
} | ||
|
||
return [$filtered]; | ||
|
||
case 'float': | ||
if (\is_float($value)) { | ||
return [$value]; | ||
} | ||
$filtered = filter_var($value, \FILTER_VALIDATE_FLOAT, \FILTER_NULL_ON_FAILURE); | ||
if (null === $filtered) { | ||
throw new NotFoundHttpException(\sprintf('The value for the "%s" route parameter is invalid.', $name)); | ||
} | ||
|
||
return [$filtered]; | ||
|
||
case 'bool': | ||
if (\is_bool($value)) { | ||
return [$value]; | ||
} | ||
$filtered = filter_var($value, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE); | ||
if (null === $filtered) { | ||
throw new NotFoundHttpException(\sprintf('The value for the "%s" route parameter is invalid.', $name)); | ||
} | ||
|
||
return [$filtered]; | ||
} | ||
|
||
// Unknown type; let other resolvers handle it (e.g., Request, Session, custom types) | ||
return []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
.../HttpKernel/Tests/Controller/ArgumentResolver/RequestAttributeScalarValueResolverTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class RequestAttributeScalarValueResolverTest extends TestCase | ||
{ | ||
public function testValidIntWithinRangeWorks() | ||
{ | ||
$resolver = new RequestAttributeValueResolver(); | ||
$request = new Request(); | ||
$request->attributes->set('id', '123'); | ||
$metadata = new ArgumentMetadata('id', 'int', false, false, null); | ||
|
||
$result = iterator_to_array($resolver->resolve($request, $metadata)); | ||
|
||
$this->assertSame([123], $result); | ||
} | ||
|
||
public function testInvalidStringBecomes404() | ||
{ | ||
$resolver = new RequestAttributeValueResolver(); | ||
$request = new Request(); | ||
$request->attributes->set('id', 'abc'); | ||
$metadata = new ArgumentMetadata('id', 'int', false, false, null); | ||
|
||
$this->expectException(NotFoundHttpException::class); | ||
iterator_to_array($resolver->resolve($request, $metadata)); | ||
} | ||
|
||
public function testOutOfRangeIntBecomes404() | ||
{ | ||
$resolver = new RequestAttributeValueResolver(); | ||
$request = new Request(); | ||
// one more than PHP_INT_MAX on 64-bit (string input) | ||
$request->attributes->set('id', '9223372036854775808'); | ||
$metadata = new ArgumentMetadata('id', 'int', false, false, null); | ||
|
||
$this->expectException(NotFoundHttpException::class); | ||
iterator_to_array($resolver->resolve($request, $metadata)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.