fix(rest): idempotent, per-controller derived permission_callback#39
Merged
Merged
Conversation
…ller WordPress invokes a route's permission callbacks more than once per request: once at dispatch, then again for EVERY method registered on the matched route while rest_send_allow_header builds the Allow header — all against the same WP_REST_Request instance. checkPermissions() re-ran the controller middleware chain on every invocation. Middleware is not idempotent (ConvertCsvMiddleware converts a CSV param to an array in place), so the second pass threw a TypeError (explode() on an array), which catch(Exception) did not catch — fataling the request after the route callback had already produced a correct response. Clients saw HTTP 200 with an empty body on every middleware- bearing GET endpoint. - Memoize middleware outcomes per (request, controller class) and return the stored answer on repeat invocations without re-running the chain. Per-controller keying matters: the Allow-header pass checks the POST controller against a GET-shaped request, and its validation failure must not clobber the GET controller's passing outcome. - Store auth WP_Errors in the same map so repeated checks return the same rejection without re-running auth middleware. - Catch \Throwable, not Exception: middleware engine errors surface as a 500 WP_Error instead of fataling the request (wrapped for LoggerStrategy::logException(), which takes Exception). Four new regression tests pin: single run across repeated checks, memoized auth rejection, per-controller outcome isolation, and TypeError-to-WP_Error conversion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WordPress invokes permission callbacks repeatedly per request (dispatch + rest_send_allow_header for every method on the route, same WP_REST_Request). 4.3.1's checkPermissions re-ran the middleware chain each time; non-idempotent middleware (ConvertCsvMiddleware) threw a TypeError on the second pass that
catch (Exception)missed, fataling the request after the callback had produced a correct response — every middleware-bearing GET endpoint returned 200 with an empty body. Caught by Siren's WordPress E2E suite (20 failures) on the 4.3.1 bump.67 tests pass, 4 new regressions.