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

CSRF breaks Auth on dynamic routes #13

Open
timclutton opened this issue Nov 15, 2024 · 0 comments
Open

CSRF breaks Auth on dynamic routes #13

timclutton opened this issue Nov 15, 2024 · 0 comments

Comments

@timclutton
Copy link

There seems to be an incompatibility between leafs/csrf and leafs/auth that causes authentication middleware not to run when using dynamic routing.

A minimal example:

require __DIR__ . '/vendor/autoload.php';

auth()->middleware('auth.required', function () {
    response()->exit(['unauthorised'], 401);
});

app()->get('/secure', [
    'middleware' => 'auth.required',
    function () {
        response()->json(['message' => 'Hello World!']);
    }
]);

app()->get('/{insecure}', [
    'middleware' => 'auth.required',
    function ($insecure) {
        response()->json(['message' => "Uh oh!"]);
    }
]);

app()->csrf();

app()->run();

Using the above, a GET call to /secure will return a 401 error as expected.

But a GET call to /insecure (or since it's dynamic, anything that isn't /secure) will unexpectedly return:

{"message":"Uh oh!"}

Simply commenting out, or removing, the line app()->csrf() and repeating the GET call to /insecure will return a 401 error as expected.

Versions in use:

  • leafs/auth: 3.0.3
  • leafs/csrf: 0.5.3
  • leafs/leaf: 3.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant