Framework: VerifyCsrfToken doesn't play well with Responsables

Created on 3 Sep 2019  Â·  3Comments  Â·  Source: laravel/framework

  • Laravel Version: 5.8.34
  • PHP Version: 7.3
  • Database Driver & Version: MySQL 5.7.X

Description:

I'm seeing users getting this error:

Undefined property: Inertia\Response::$headers {"exception":"[object] (ErrorException(code: 0): Undefined property: Inertia\Response::$headers at …/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:180)

The Inertia\Response class implements the Responsable interface.

When VerifyCsrfToken tries to add cookies to a Responsable object, it triggers an error. The problem is that if the Responsable hasn't been transformed to a response yet, the $headers property might not exist.

https://github.com/laravel/framework/blob/7b39e0825ce3c837e1cae6be6e821ce22cad3efd/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php#L180

We (temporarily) solved this by overwriting the method in our own VerifyCsrfToken middleware.

use Inertia\Response;

//

protected function addCookieToResponse($request, $response)
{
    if ($response instanceof Response) {
        $response = $response->toResponse($request);
    }

    return parent::addCookieToResponse($request, $response);
}

Shouldn't Laravel unwrap the Responsable before adding the cookie?

Steps To Reproduce:

I don't have any exact reproduction steps... It depends on the code path, it doesn't always fail, but I've yet to find the root cause.

bug

Most helpful comment

Can do! I'll make some time early next week.

All 3 comments

Hmm, I think you're right and that this is indeed a bug. Can you send in a PR?

Send it to the 6.x branch.

Can do! I'll make some time early next week.

Was this page helpful?
0 / 5 - 0 ratings