Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Validate typed request attribute values before calling controllers#61458

Merged
nicolas-grekas merged 1 commit into
symfony:8.1from
mudassaralichouhan:feature/request-attribute-scalar-resolver
Feb 6, 2026
Merged

[HttpKernel] Validate typed request attribute values before calling controllers#61458
nicolas-grekas merged 1 commit into
symfony:8.1from
mudassaralichouhan:feature/request-attribute-scalar-resolver

Conversation

@mudassaralichouhan
Copy link
Copy Markdown
Contributor

@mudassaralichouhan mudassaralichouhan commented Aug 19, 2025

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #61451
License MIT
Doc PR N/A

This PR fixes issue #61451 by introducing a new RequestAttributeScalarValueResolver that safely casts request attributes (route parameters) to scalar types (int, float, bool, string, or \BackedEnum).

If a value cannot be safely cast (e.g. 9223372036854775808 for an int), a NotFoundHttpException (404) is thrown before reaching the controller, avoiding a TypeError.

Changes:

  • Added: RequestAttributeScalarValueResolver (registered with high priority)
  • Tests: Unit and functional tests for valid, invalid, and out-of-range int values
  • Config: Functional test app with a /{id} route expecting int $id
  • Docs: CHANGELOG.md entry under 7.4

This makes route handling safer and more predictable for typed controllers.

@carsonbot carsonbot added this to the 7.4 milestone Aug 19, 2025
@carsonbot
Copy link
Copy Markdown

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title Prevent TypeError for out-of-range route parameters (Fixes #61451) Prevent TypeError for out-of-range route parameters (Fixes #61451) Aug 19, 2025
Comment thread src/Symfony/Component/HttpKernel/Controller/ArgumentResolver.php Outdated
Copy link
Copy Markdown
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For enums, this looks already covered by BackedEnumValueResolver
For other scalar types, I'd suggest implementing this in RequestAttributeValueResolver

@carsonbot carsonbot changed the title Prevent TypeError for out-of-range route parameters (Fixes #61451) [HttpKernel] Prevent TypeError for out-of-range route parameters (Fixes #61451) Aug 19, 2025
@nicolas-grekas nicolas-grekas changed the title [HttpKernel] Prevent TypeError for out-of-range route parameters (Fixes #61451) [HttpKernel] Prevent TypeError for out-of-range route parameters Aug 19, 2025
@stof
Copy link
Copy Markdown
Member

stof commented Aug 19, 2025

Btw, the implementation in RequestAttributeValueResolver should probably use the right getter of the ParameterBag instead of reimplementing the conversion logic in the value resolver after using get.

@nicolas-grekas nicolas-grekas modified the milestones: 7.4, 8.1 Nov 16, 2025
@nicolas-grekas nicolas-grekas force-pushed the feature/request-attribute-scalar-resolver branch from 07701bc to bf5134e Compare February 4, 2026 19:37
@nicolas-grekas nicolas-grekas changed the title [HttpKernel] Prevent TypeError for out-of-range route parameters [HttpKernel] Validate typed request attribute values before calling controllers Feb 4, 2026
Copy link
Copy Markdown
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked the implementation, please have a second look. Please also re-read the description to see if it needs changes.

@symfony/mergers review welcome

…ontrollers

When a route parameter is bound to a typed controller argument (int, float, bool, string, or \BackedEnum), invalid or out-of-range values now result in an HTTP error instead of triggering a TypeError.
@nicolas-grekas nicolas-grekas force-pushed the feature/request-attribute-scalar-resolver branch from bf5134e to bb0f715 Compare February 5, 2026 15:15
Copy link
Copy Markdown
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HypeMC, that's exactly the review I needed πŸ™
PR updated

@nicolas-grekas
Copy link
Copy Markdown
Member

Thank you @mudassaralichouhan.

@nicolas-grekas nicolas-grekas merged commit 47ed844 into symfony:8.1 Feb 6, 2026
10 of 11 checks passed
@fabpot fabpot mentioned this pull request May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants