Skip to content

REST API: Allow-list ability schema response keywords#11997

Closed
gziolo wants to merge 6 commits into
WordPress:trunkfrom
gziolo:abilities-rest-schema-keyword-allow-list
Closed

REST API: Allow-list ability schema response keywords#11997
gziolo wants to merge 6 commits into
WordPress:trunkfrom
gziolo:abilities-rest-schema-keyword-allow-list

Conversation

@gziolo

@gziolo gziolo commented May 28, 2026

Copy link
Copy Markdown
Member

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:

  • keywords returned by rest_get_allowed_schema_keywords();
  • a small local set of additional draft-04 schema keywords that clients may use for validation or schema traversal.

This continues to preserve nested schema structures while stripping WordPress-internal keys such as sanitize_callback, validate_callback, and arg_options, along with other unsupported schema keywords such as example, examples, context, and readonly.

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_schema properties and top-level output_schema definitions:

  • sanitize_callback
  • validate_callback
  • arg_options
  • example
  • examples
  • context
  • readonly

It also verifies that valid draft-04 required arrays are preserved. Boolean required handling is intentionally left for a follow-up PR.

Related

Testing

  • php -l tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php
  • git diff --check -- tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

Attempted 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_WpRestAbilitiesV1ListController

Previously 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.php
  • npm run typecheck:php -- src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php --no-progress

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@gziolo gziolo marked this pull request as ready for review May 28, 2026 11:39
@github-actions

github-actions Bot commented May 28, 2026

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. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props gziolo, jorgefilipecosta.

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

Comment thread src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php Outdated
Comment thread tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

@jorgefilipecosta jorgefilipecosta left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Left some comments for consideration but the changes look good 👍

gziolo and others added 3 commits June 2, 2026 13:13
`prepare_schema_for_response()` recurses over every node of an ability
schema, and each call rebuilt the allow-list lookup via
`array_fill_keys( array_merge( ... ) )`. Compute it once into a static
and reuse it across the recursive calls.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The controller now allow-lists supported schema keywords, so the tests
verify that unsupported (non-allow-listed) keywords are stripped rather
than only WordPress-internal ones. Rename them accordingly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Rename the test abilities (and their labels/descriptions) registered by
the keyword-stripping tests from `internal` to `unsupported`, matching
the allow-list framing of the test method names.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@gziolo

gziolo commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

A commit was made that references the Trac ticket included in the description of this pull request.

SVN changeset: 62447
GitHub commit: da7588b

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@gziolo gziolo closed this Jun 2, 2026
@gziolo gziolo deleted the abilities-rest-schema-keyword-allow-list branch June 2, 2026 11:43
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.

2 participants