Skip to content

Improved an error message related to controllers #27499

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

Merged
merged 1 commit into from
Jun 27, 2018

Conversation

javiereguiluz
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

This proposal is irrelevant for experienced users but it may be useful for newcomers. After having delivered several introductory Symfony training, I can say that when someone adds return "some string..."; in their controller, the error message is confusing:

before

Maybe we can reword it a bit? (I'm open for suggestions to improve the error message)

after

@ScullWM
Copy link
Contributor

ScullWM commented Jun 5, 2018

Great for newcomers!
Just guessing, expect giving bad habbit to newcomers, why don't Symfony create a Response with this given string ?

@javiereguiluz
Copy link
Member Author

@ScullWM allowing to return strings was discussed in the past (specially because "some frameworks" allow to do that). But it was decided to keep requiring Response objects. At first it might be like a bad decision, but it helps newcomers realize that Symfony apps are always about HTTP Request/Response workflow. No matter if it's a silly "Hello World" app or the most complex app ever created. It's always the same: you are given a Request object and you must return a Response object.

That's why I think we should keep the current behaviour: if people learn about Request/Response soon, they will understand Symfony easier.

@lyrixx
Copy link
Member

lyrixx commented Jun 5, 2018

@javiereguiluz Is your master branch a bit outdated ? Because I made a patch recently that display a "better" trace that highlight the controller. And I can not see it on your screenshot.

Anyway, 👍 with this PR.

@javiereguiluz
Copy link
Member Author

@lyrixx the code change and the screenshot were done in a Symfony 4.1 app and then I copied the code to the master branch 🙈

@linaori
Copy link
Contributor

linaori commented Jun 5, 2018

You can always create your own listener that transforms the string into a response. The SensioFrameworkExtraBundle does this for arrays with the @Template annotation.

@nicolas-grekas nicolas-grekas modified the milestones: 4.2, next Jun 5, 2018
@@ -157,7 +157,7 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
if ($event->hasResponse()) {
$response = $event->getResponse();
} else {
$msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
$msg = sprintf('The controller must return a Symfony\Component\HttpFoundation\Response object but it returned: %s.', is_string($response) ? sprintf('"%s" string', $response) : $this->varToString($response));
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest moving the is_string check inside the varToString method

@@ -269,6 +269,10 @@ private function varToString($var): string
return sprintf('Resource(%s)', get_resource_type($var));
}

if (is_string($var)) {
return sprintf('"%s" string', $var);
Copy link
Member

Choose a reason for hiding this comment

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

Here, $var can be very long, I would strip the string in such cases, maybe using the first 200 or so chars.

Copy link
Member

Choose a reason for hiding this comment

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

Using a generated string in an exception message makes me nervous (it can contain end-user submitted values).

@javiereguiluz
Copy link
Member Author

I've updated this feature to trim long strings.

Before:

before

After:

after


About this other comment from Fabien (#27499 (comment)) I'm not sure how to solve it. Thanks!

@javiereguiluz
Copy link
Member Author

javiereguiluz commented Jun 27, 2018

Thanks to Twig's automatic escaping, I think we're good here. I've tested with some of the examples given by OWASP to prevent XSS (https://www.owasp.org/index.php/Testing_for_Reflected_Cross_site_scripting_(OTG-INPVAL-001)) and it looks like it's working:

xss-error-page

Can anyone spot other "attack vectors" in this code? Thanks

@@ -157,7 +157,7 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
if ($event->hasResponse()) {
$response = $event->getResponse();
} else {
$msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
$msg = sprintf('The controller must return a Symfony\Component\HttpFoundation\Response object but it returned %s.', $this->varToString($response));
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add double quotes around the ...\Request

@fabpot fabpot force-pushed the controller_error branch from ba3e5dd to 7510c3a Compare June 27, 2018 09:26
@fabpot
Copy link
Member

fabpot commented Jun 27, 2018

Thank you @javiereguiluz.

@fabpot fabpot merged commit 7510c3a into symfony:master Jun 27, 2018
fabpot added a commit that referenced this pull request Jun 27, 2018
…ereguiluz)

This PR was squashed before being merged into the 4.2-dev branch (closes #27499).

Discussion
----------

Improved an error message related to controllers

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This proposal is irrelevant for experienced users but it may be useful for newcomers. After having delivered several introductory Symfony training, I can say that when someone adds `return "some string...";` in their controller, the error message is confusing:

![before](https://user-images.githubusercontent.com/73419/40959468-0faf790a-689d-11e8-9ce1-f6e0caf4b113.png)

Maybe we can reword it a bit? (I'm open for suggestions to improve the error message)

![after](https://user-images.githubusercontent.com/73419/40959505-29747070-689d-11e8-834e-92bf18760469.png)

Commits
-------

7510c3a Improved an error message related to controllers
@nicolas-grekas nicolas-grekas removed this from the next milestone Nov 1, 2018
@nicolas-grekas nicolas-grekas added this to the 4.2 milestone Nov 1, 2018
This was referenced Nov 3, 2018
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.

7 participants