Skip to content

Say why the default HTTP permission check refused a request - #414

Open
softglazee wants to merge 1 commit into
WordPress:trunkfrom
softglazee:fix/398-explain-refused-requests
Open

softglazee wants to merge 1 commit into
WordPress:trunkfrom
softglazee:fix/398-explain-refused-requests

Conversation

@softglazee

@softglazee softglazee commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

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() returned false, so core answered with rest_forbidden for 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_Error with the same status codes as before:

Case Code Status Message
No user signed in authentication_required 401 This request is not signed in as a WordPress user. Check the username and Application Password, or the sign-in plugin you use. See the site-owner guide (link to its 401 section).
Signed in without the capability insufficient_capability 403 The signed-in user does not have the "read" capability that this MCP server requires.
  • The codes are the ones the built-in abilities already use for the same cases.
  • The message does not assume Application Passwords, since OAuth and JWT plugins reach this check too.
  • No WWW-Authenticate header is added, as the issue asks.
  • The denial is still logged through the error handler, unchanged.
  • Custom transport permission callbacks keep their current behaviour (false, and a logged WP_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_denial now expects the 401 WP_Error instead of false.
  • test_default_permission_names_missing_capability: a subscriber with the capability filtered to manage_options gets the 403 error naming manage_options.
  • test_refused_rest_request_explains_the_refusal: an unauthenticated request dispatched through the REST server returns 401 with authentication_required, so core passes the error through.

Testing Instructions

  1. Send a request without credentials:
    curl -sS -i https://example.com/wp-json/mcp/mcp-adapter-default-server \
      -H 'Content-Type: application/json' \
      -H 'Accept: application/json, text/event-stream' \
      -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1"}}}'
    
    On trunk: 401 with rest_forbidden, "Sorry, you are not allowed to do that." With this PR: 401 with authentication_required and the message above.
  2. Add 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 with insufficient_capability, naming manage_options.

Results on PHP 8.3 with WordPress trunk:

  • Full suite: 545 tests, OK (1 skipped, as on trunk).
  • The three tests above fail against the trunk version of HttpTransport.php.
  • PHPStan on the changed file: no errors.

Changelog Entry

Changed - Requests refused by the default HTTP permission check get a specific error: authentication_required (401) when no user is signed in, or insufficient_capability (403) naming the missing capability, instead of the generic rest_forbidden message.

Open WordPress Playground Preview

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.
Copilot AI balanced review requested due to automatic review settings October 23, 2026 19:41
@softglazee
softglazee requested a review from a team as a code owner October 23, 2026 19:41
@github-actions

Copy link
Copy Markdown

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.

Co-authored-by: softglazee <softglaze@git.wordpress.org>
Co-authored-by: apeatling <apeatling@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_Error responses 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

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.14%. Comparing base (f6bb4b7) to head (1838e46).

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              
Flag Coverage Δ
unit 93.14% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

Say why a request was refused (401/403) instead of the generic message

2 participants