Make WordPress Core

Opened 6 days ago

Last modified 3 days ago

#66804 new defect (bug)

REST API: The `show_on_front` and `start_of_week` settings accept values outside the ones they support

Reported by: jorgefilipecosta Owned by:
Priority: normal Milestone: 7.3
Component: REST API Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The settings REST route (POST /wp/v2/settings) answers with success when show_on_front or start_of_week get a value they do not support:

  • show_on_front: "banana" returns 200, and the setting is saved as posts.
  • start_of_week: 99 returns 200, and 99 is saved.

The schema of each setting only has its type, string and integer, so the request passes validation. default_ping_status and default_comment_status already have an enum in their schema, and a value outside it fails with rest_invalid_param.

The proposal is to add an enum with posts and page to show_on_front, and a minimum of 0 and a maximum of 6 to start_of_week. The core/settings-get ability, and the core/settings-update ability proposed in #64616, use the same schema, so they get the same limits.

PR: ​https://github.com/WordPress/wordpress-develop/pull/16207

Change History (3)

This ticket was mentioned in ​PR #16207 on ​WordPress/wordpress-develop by ​@jorgefilipecosta.


6 days ago
#1

Related to ​https://github.com/WordPress/wordpress-develop/pull/10892.

This PR limits two settings to the values they accept, in the schema shared by the settings REST route and the settings abilities:

  • show_on_front gets an enum with posts and page.
  • start_of_week gets a minimum of 0 and a maximum of 6.

Before, a value outside them got a successful response: show_on_front: "banana" was saved as posts, and start_of_week: 99 was saved as sent. Now the request fails with rest_invalid_param and nothing is saved, as it already does for default_ping_status and default_comment_status.

A value outside them that is already stored is returned as null by the settings route and left out by core/settings-get, like any stored value that does not match its schema.

Testing

Verify unit tests are passing:

npm run test:php -- --filter WP_Test_REST_Settings_Controller

Or run this in the editor console as an administrator, and verify it fails with rest_invalid_param:

await wp.apiFetch( { path: '/wp/v2/settings', method: 'POST', data: { show_on_front: 'banana' } } );

AI disclosure: the change and tests were drafted with AI assistance (Claude Code).

#2 @westonruter
5 days ago

  • Milestone Awaiting Review → 7.3

Related: r65525

#3 @abhishekfdd
3 days ago

Test Report

Description: This report validates whether the indicated patch works as expected.
Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/16207 (head 15c660dc08). The PR base is 40 commits behind trunk, so I cherry-picked it onto trunk d450653f30 (clean, identical patch-id). The settings controller tests also pass at the PR head itself.

Environment

  • WordPress: 7.3-alpha-65762-src (trunk d450653f30, includes r65525)
  • PHP: 8.3.36
  • Server: nginx/1.31.8 (wordpress-develop Docker env)
  • Database: MySQL 9.7.4
  • Browser: Chromium 159 (Playwright, headless)
  • OS: macOS 27.2.1
  • Theme: Ipsum
  • MU Plugins: None activated
  • Plugins: None activated

Steps taken

  1. On trunk, ran the PR's new tests and sent invalid values to POST /wp/v2/settings to reproduce.
  2. On the patch, ran the restapi, abilities-api, option and restapi-jsclient groups, QUnit, phpcs and PHPStan.
  3. Sent 23 payloads to /wp/v2/settings (invalid, valid, numeric strings, mixed valid + invalid), checking the response and the stored value.
  4. Checked core/settings-get, and core/settings-update from #64616 by stacking its open PR #10892 locally.
  5. Wrote out-of-range values straight to the database and checked REST, core/settings-get, the calendar, weekly archives, Settings > General/Reading and the editor date picker.

Actual Results

  • ✅ Bug reproduced on trunk: show_on_front: "banana" returns 200 and saves posts; start_of_week: 99 returns 200 and saves 99.
  • ✅ The PR's 4 out-of-schema test cases fail on trunk and pass with the patch.
  • ✅ Patch: settings controller 43/43, restapi group 4040 tests and abilities-api 936 tests with 0 failures, QUnit 719/719, phpcs and PHPStan clean. The fixture change matches regeneration.
  • ✅ Out-of-range or wrong-type values return 400 rest_invalid_param. "6", "0" and 6.0 are still accepted for start_of_week.
  • ✅ A request mixing a valid and an invalid field saves nothing.
  • ✅ core/settings-get exposes the new enum and min/max. With #10892 stacked, core/settings-update rejects the same values and its 1106 abilities tests pass.
  • ✅ A stored out-of-range value reads as null from /wp/v2/settings and is left out of core/settings-get.
  • ✅ With 7 stored, the calendar, weekly archive and editor date picker still start on Sunday (r65525 unchanged).
  • ✅ wp-admin renders and saves for every stored value tested, with no PHP notices or JS errors.

Additional Notes

  • Behavior change for the dev note: with an out-of-range value already stored, sending start_of_week: null now returns 500 rest_invalid_stored_value, where trunk deleted the option. This is the controller's existing handling for any setting with a schema, now applied to these two. Sending a value from 0 to 6 fixes it.
  • Test suggestion: in test_update_item_with_value_outside_schema, the "stored value unchanged" assertion only detects a write for the 7 case. On trunk, "banana", "" and -1 get coerced back to the defaults (posts, 1), so the assertion passes even when a write happens. Setting a non-default value first (e.g. start_of_week 3, show_on_front page) would make it meaningful.
  • Why this matters beyond REST: an out-of-range start_of_week already breaks the UI on trunk. With 42 or 99 stored, the editor date picker shows no days. With -1 it starts on Saturday and drops the last days of the month. With 42 the weekly archive labels November weeks as December. On trunk, any REST client could store 99. This patch closes that path, but update_option() and WP-CLI can still store values above 6, because sanitize_option() only applies absint() (which also turns -1 into 1). A follow-up to normalize start_of_week in sanitize_option() would cover existing bad data.
  • On r65525: the runtime still treats a stored 7 as Sunday while REST now reports it as null. I think that's fine. 0 to 6 is the only range every consumer handles, so I wouldn't widen the schema to accept 7. A line in the dev note should be enough.
  • Not tested: Firefox, Safari, multisite.
  • AI disclosure: testing and this report were done with AI assistance (Claude Code). I reviewed the results.

Supplemental Artifacts

# trunk + PR's new tests
Tests: 8, Assertions: 12, Failures: 4   (the 4 outside-schema cases)

# patch
WP_Test_REST_Settings_Controller   OK (43 tests, 145 assertions)
--group restapi                     Tests: 4040, Failures: 0, Skipped: 2
--group abilities-api               OK (936 tests, 3234 assertions)

# patch, REST
POST {"show_on_front":"banana"}                   400 rest_invalid_param  show_on_front is not one of posts and page.
POST {"start_of_week":99}                         400 rest_invalid_param  start_of_week must be between 0 (inclusive) and 6 (inclusive)
POST {"show_on_front":"page","start_of_week":99}  400, nothing saved

AI disclosure:

I used AI assistance (Claude Code) to run the tests, write the test scripts and draft this report. I reviewed the results and the report before posting.

Note: See TracTickets for help on using tickets.