Repository navigation
Say why the default HTTP permission check refused a request - #414
softglazee wants to merge 1 commit into
Conversation
The default check returned false, so core answered every refusal with the generic rest_forbidden message and owners could not tell a missing sign-in from a missing capability. Return a WP_Error instead: authentication_required (401) when no user is signed in, pointing to the site-owner guide, and insufficient_capability (403) naming the capability. Status codes are unchanged, and custom permission callbacks keep their current behavior. Fixes WordPress#398.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
There was a problem hiding this comment.
🟢 Approval recommended
The focused changes preserve access-control behavior and include regression coverage, with no identified blocking issues.
No open issues 🎉
What changed in this PR
Addresses #398 by making default HTTP permission denials explain whether authentication or a required capability is missing, rather than returning WordPress’s generic refusal.
Changes:
- Returns specific
WP_Errorresponses for unauthenticated requests (401) and missing capabilities (403). - Preserves permission decisions, denial logging, and custom callback behavior.
- Adds coverage for both denial reasons and REST error propagation.
| File | Description |
|---|---|
| tests/phpunit/Integration/HttpTransportPermissionTest.php | Verifies specific denial errors, status codes, and REST propagation. |
| includes/Transport/HttpTransport.php | Adds descriptive default permission errors and updates the return documentation. |
Review effort: Balanced
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk #414 +/- ##
============================================
+ Coverage 93.12% 93.14% +0.01%
- Complexity 1382 1384 +2
============================================
Files 59 59
Lines 4016 4027 +11
============================================
+ Hits 3740 3751 +11
Misses 276 276
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What?
Closes #398
When the default HTTP permission check refuses a request, it now says why, instead of core's generic "Sorry, you are not allowed to do that."
Why?
HttpTransport::check_permission()returnedfalse, so core answered withrest_forbiddenfor every refusal. Missing credentials and a user without the capability got the same message, so owners and agents could not tell what was wrong.How?
The default check now returns a
WP_Errorwith the same status codes as before:authentication_requiredinsufficient_capabilityWWW-Authenticateheader is added, as the issue asks.false, and a loggedWP_Error), since a custom callback can refuse for reasons the default messages would not describe.Tests in
HttpTransportPermissionTest:test_default_permission_requires_authenticated_user_and_logs_denialnow expects the 401WP_Errorinstead offalse.test_default_permission_names_missing_capability: a subscriber with the capability filtered tomanage_optionsgets the 403 error namingmanage_options.test_refused_rest_request_explains_the_refusal: an unauthenticated request dispatched through the REST server returns 401 withauthentication_required, so core passes the error through.Testing Instructions
rest_forbidden, "Sorry, you are not allowed to do that." With this PR: 401 withauthentication_requiredand the message above.add_filter( 'mcp_adapter_default_transport_permission_user_capability', fn() => 'manage_options' );and send the same request with a subscriber's Application Password (-u 'subscriber:APPLICATION_PASSWORD'). With this PR: 403 withinsufficient_capability, namingmanage_options.Results on PHP 8.3 with WordPress trunk:
HttpTransport.php.Changelog Entry