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

Skip to content

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

Closed as not planned
@Leboski

Description

@Leboski

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions