Skip to content
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

[Routing] TypeErrors in a controller route should return HTTP 400 (Invalid request) instead of 500 (Server error) #37814

Closed
fmonts opened this issue Aug 12, 2020 · 6 comments

Comments

@fmonts
Copy link

fmonts commented Aug 12, 2020

I think it would make more sense if a HTTP status code 400 is returned when the type of a variable is wrong, for example:

/**
 * @Route("/{_locale}/news/{id}", name="news_show")
 */
public function show(int $id)
{
    ...
}

Which is much easier to write and remember than the annotation requirements={"id"="\d+"}

The TypeError could be caught and shown instead a 400 or 404 error.

Edit: I know an integer is not the same as a numeric string, but casting to integer has been the oldest way to protect from SQL injections since the beginning of time in PHP and I guess it's still widely used...

@fmonts fmonts changed the title TypeErrors in a controller route should return HTTP 400 (Invalid request) instead of 500 (Server error) [Routing] TypeErrors in a controller route should return HTTP 400 (Invalid request) instead of 500 (Server error) Aug 12, 2020
@Tobion
Copy link
Member

Tobion commented Aug 13, 2020

I don't think TypeErrors should be caught here. Instead we could automatically add a regex requirement to a placeholder when the parameter has an int type.

@jkufner
Copy link

jkufner commented Aug 16, 2020

What if a Value Resolver fails to resolve the value?

What if strict types are in use?

What if the argument is boolean, float, or a date?

There are more cases and they all sould behave consistently.

I don't like adding a regexp automatically. That seems as a dirty hotfix. This matter should be solved in a more generic way.

@stof
Copy link
Member

stof commented Aug 17, 2020

What if strict types are in use?

Strict types are not in use in the HttpKernel when calling the controller. We know that for sure.

What if the argument is boolean, float, or a date?

For these cases, you must need an argument resolver to perform the conversion anyway. So a 500 error when it is missing is actually right: the server code is broken.

@nicolas-grekas
Copy link
Member

I'm 👎 here, this is going too deep into type-inference deductions.

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@fmonts
Copy link
Author

fmonts commented Mar 1, 2021

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

Since the big bosses said no, I'm closing it

@fmonts fmonts closed this as completed Mar 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants