REST API: Allow-list ability schema response keywords#11997
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
What?
This hardens the Ability REST API schema response preparation by switching from an internal-keyword deny-list to a schema-keyword allow-list.
Ability input and output schemas exposed through REST responses now keep only:
rest_get_allowed_schema_keywords();This continues to preserve nested schema structures while stripping WordPress-internal keys such as
sanitize_callback,validate_callback, andarg_options, along with other unsupported schema keywords such asexample,examples,context, andreadonly.Why?
The previous implementation stripped only known internal keywords. An allow-list is stricter and better aligned with the REST API schema surface: unknown or unsupported schema keywords are not exposed to REST clients by default.
The local additional keyword list intentionally preserves a few draft-04 keywords that are useful to stricter client-side validators. This aligns with the approach documented in WordPress/gutenberg#78783, which adds tests for the strict JSON Schema keyword surface supported by the client-side abilities validator.
At the moment, the Core and Gutenberg changes aim to mirror the same allowed schema-property surface: unknown schema keywords should be skipped or stripped rather than passed through. Based on review feedback, we can also move in the opposite direction by relaxing the client-side validator to ignore unknown schema properties instead of treating them as unsupported. In practice, the two possible endpoints are either a strict shared allow-list or a relaxed client that skips unknown properties.
Test Coverage
This PR now mirrors the WordPress-specific keyword cases covered in WordPress/gutenberg#78783 by documenting that unsupported schema keywords are stripped from both ability
input_schemaproperties and top-leveloutput_schemadefinitions:sanitize_callbackvalidate_callbackarg_optionsexampleexamplescontextreadonlyIt also verifies that valid draft-04
requiredarrays are preserved. Booleanrequiredhandling is intentionally left for a follow-up PR.Related
Testing
php -l tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.phpgit diff --check -- tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.phpAttempted after the latest test-only update, but the local Docker runner hung after creating the PHPUnit container:
npm run test:php -- --filter Tests_REST_API_WpRestAbilitiesV1ListControllerPreviously run on this branch before the latest test-only update:
php -l src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.phpnpm run typecheck:php -- src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php --no-progress