Skip to content

fix(laravel): fall back to resource class when object is null in ResourceAccessChecker#7948

Merged
soyuka merged 2 commits intoapi-platform:4.3from
soyuka:fix/7945-laravel-policy-null-object
May 5, 2026
Merged

fix(laravel): fall back to resource class when object is null in ResourceAccessChecker#7948
soyuka merged 2 commits intoapi-platform:4.3from
soyuka:fix/7945-laravel-policy-null-object

Conversation

@soyuka
Copy link
Copy Markdown
Member

@soyuka soyuka commented May 4, 2026

Q A
Branch? 4.3
Tickets Closes #7945
License MIT
Doc PR

Summary

ResourceAccessChecker::isGranted() passes the body returned by the decorated provider to Gate::allows() as the second argument. When the operation has deserialize: false (or output: false) and no provider returning an instance, that body is null — and Laravel's Gate cannot resolve a policy class from null, so it silently returns false. The request fails with 403 even when the policy method explicitly returns true.

The existing code already handles the symmetric case for Paginator (collection endpoints, no instance available) by falling back to $resourceClass. This PR applies the same fallback to null.

Behavior

  • Before: Gate::allows('import', null) → silent deny (false negative).
  • After: Gate::allows('import', Item::class) → routes through normal policy resolution; the configured policy method is invoked as the developer intended.

Test plan

  • New unit test Tests/Unit/Security/ResourceAccessCheckerTest.php covers null, Paginator, and concrete-object paths. Verified the null case fails without the fix (Gate is called with NULL) and passes with it.
  • Existing Tests/Policy/* and Tests/AuthTest.php still pass (17 tests, 22 assertions).

soyuka added 2 commits May 4, 2026 14:41
…urceAccessChecker

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Tickets       | Closes api-platform#7945
| License       | MIT
| Doc PR        | ∅

Gate::allows cannot resolve a policy from a null argument, causing 403
on operations with deserialize:false or output:false even when the
policy returns true. Apply the same fallback already used for Paginator.
| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Tickets       | Refs api-platform#7945
| License       | MIT
| Doc PR        | ∅

Add a functional test reproducing the issue scenario: a Post with
deserialize:false, output:false and a policy that returns true. Without
the fix the request returns 403 (Gate cannot resolve a policy from the
null body); with the fix it returns 202 as configured.
@soyuka soyuka merged commit 173dc66 into api-platform:4.3 May 5, 2026
117 of 119 checks passed
@soyuka soyuka deleted the fix/7945-laravel-policy-null-object branch May 5, 2026 05:58
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.

1 participant