Skip to content

Symfony\Component\HttpKernel\EventListener\ErrorListener & framework.error_controller usage resulting in having the wrong request method #58968

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
Leboski opened this issue Nov 22, 2024 · 2 comments

Comments

@Leboski
Copy link

Leboski commented Nov 22, 2024

Symfony version(s) affected

7.1.*

Description

When we use symfony/symfony and configure the error_controller in the config/packages/framework.yaml file, when we get the request_method, there is an specific case where the request_method it's always GET.

How to reproduce

  1. Create a new symfony project
  2. Configure an error_controller in the config/packages/framework.yaml file
  3. Create a new controller with a POST endpoint that throws a BadRequestHttpException
  4. Make a POST request to the endpoint
  5. Check the request_method in the error_controller
  6. The request_method is set to GET when it should be POST in $request->getMethod()

Possible Solution

Change method Symfony\Component\HttpKernel\EventListener::duplicateRequest lines:

        $attributes = [
            '_controller' => $this->controller,
            'exception' => $exception,
            'logger' => DebugLoggerConfigurator::getDebugLogger($this->logger),
        ];
        $request = $request->duplicate(null, null, $attributes);
        $request->setMethod('GET');

        return $request;

to

        $attributes = [
            '_controller' => $this->controller,
            'exception' => $exception,
            'logger' => DebugLoggerConfigurator::getDebugLogger($this->logger),
        ];
        $originalRequestmethod = $request->getMethod();
        $request = $request->duplicate(null, null, $attributes);
        $request->setMethod($originalRequestmethod);

        return $request;

That is a quick fix, maybe what needs to be done, is make the $request->duplicate method more clear, as it seems it's not really duplicating. Or at least not set a hardcoded GET.

Additional Context

Please check this repository where we reproduce the bug and can be seen in the tests:

https://github.com/Leboski/bug_app_symfony_error_listener

@Leboski
Copy link
Author

Leboski commented Nov 22, 2024

Bug discovered by @rastyazio and @Leboski, let us know if you need more context! Thanks in advance 😄

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

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

3 participants