Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
bastien-phi:authorize_middleware_with_0_as_route_parameter
May 31, 2022
Merged

[9.x] Fix getting '0' from route parameter in Authorize middleware#42582
taylorotwell merged 1 commit into
laravel:9.xfrom
bastien-phi:authorize_middleware_with_0_as_route_parameter

Conversation

@bastien-phi

Copy link
Copy Markdown
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