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

[9.x] Fix getting '0' from route parameter in Authorize middleware #42582

Merged
merged 1 commit into from
May 31, 2022
Merged

[9.x] Fix getting '0' from route parameter in Authorize middleware #42582

merged 1 commit into from
May 31, 2022

Conversation

bastien-phi
Copy link
Contributor

Given the following :

// api.php
Route::get('sin/{angle}', [SinusController::class, 'compute'])
    ->whereNumber('angle')
    ->can('compute', [Sinus::class, 'angle']); 

// SinusPolicy
public function compute(User $user, int $angle)
{
    return 0<= $angle && $angle <= 360;
}

Calling GET api/sin/90 will correctly pass but calling GET api/sin/0 will fail in the policy because $angle is null instead of expected 0.

Problem comes from the fact that $request->route($model, null) is '0' and '0' ?: 'bar' is 'bar'.

Changing the Elvis ?: to null coalesce ?? solves the issue.

@taylorotwell taylorotwell merged commit 532ea74 into laravel:9.x May 31, 2022
@bastien-phi bastien-phi deleted the authorize_middleware_with_0_as_route_parameter branch May 31, 2022 14:54
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

Successfully merging this pull request may close these issues.

2 participants