Opened 7 years ago
Last modified 2 weeks ago
#48426 new defect (bug)
Can't update meta via the REST API if an identical value for a field you don't have permission to update is included
| Reported by: | johnstonphilip | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Options, Meta APIs | Version: | 4.7 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | rest-api |
Description
On WP 5.3-RC2-46575, if you update any meta via the REST API via Gutenberg, all meta gets updated. If any of those meta update attempts fail because of user cap requirements, all meta updates fail because of that user cap requirement. Thus, all registered meta require the same user caps.
This happens because all passed-in meta values are iterated through here:
https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php#L135
And then updated here:
https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php#L187-L191
From Gutenberg, those update calls fire regardless of whether the value is being changed. I believe this is because the entire Redux store is passed in and iterated over.
This causes the issue I mentioned in regards to capability checking with protected meta. Because you can require a user capability for protected meta through the auth_callback when running register_meta, any unrelated meta could prevent another unrelated piece of meta from being updated.
For example, if User A has cap_a, and tries to update _custom_meta_a, but _custom_meta_b requires cap_b, that user cannot update _custom_meta_a without getting a WP_Error here:
If we only update meta that has been changed, this should help here.
To replicate the problem follow these steps (props @modernnerd):
- Install WP 5.3-RC1 via the WP beta tester plugin by choosing “bleeding edge nightlies” at Tools → Beta Testing.
- Activate the Twenty Twenty theme (these steps will work with any theme, though).
- Add this PHP code to the Twenty Twenty theme's functions.php file to add custom post meta exposed to the REST API:
function custom_register_post_meta() {
$args = [
'auth_callback' => 'custom_prefix_require_update_plugins',
'type' => 'boolean',
'single' => true,
'show_in_rest' => true,
];
register_meta( 'post', '_custom_post_meta_one', $args );
$args = [
'auth_callback' => 'custom_prefix_require_non_existing_cap',
'type' => 'boolean',
'single' => true,
'show_in_rest' => true,
];
register_meta( 'post', '_custom_post_meta_two', $args );
}
add_action( 'init', 'custom_register_post_meta' );
function custom_prefix_require_update_plugins() {
if ( current_user_can( 'update_plugins' ) ) {
return true;
} else {
return false;
}
}
function custom_prefix_require_non_existing_cap() {
if ( current_user_can( 'this_cap_does_not_exist' ) ) {
return true;
} else {
return false;
}
}
- Edit a post or page. Make a change to the post content and click “Publish” or “Update” to confirm that changes work as expected.
- In the browser console, display the post meta exposed to the REST API by pasting this command and typing enter:
wp.data.select( 'core/editor' ).getEditedPostAttribute( 'meta' );
You should see an object that includes your new custom meta:
{_custom_post_meta_one: false, _custom_post_meta_two: false}
- Update _custom_post_meta_one to 'true' with this command (this persists the data to the Redux store, but not the database):
wp.data.dispatch( 'core/editor' ).editPost( { meta: { _custom_post_meta_one : true } } );
Note that this is just a simplified test case. It emulates a plugin or theme updating meta via the WP API, without having to install a plugin or theme that uses custom post meta and Gutenberg.
- Click Update in the post editor to save your post meta changes to the database.
You'll see “Updating failed. Error message: Sorry, you are not allowed to edit the _custom_post_meta_two custom field.”.
The thing is, _custom_post_meta_two wasn't actually updated or changed by the user, yet their lack of permissions for _custom_post_meta_two is preventing them from updating _custom_post_meta_one.
I looked at doing a comparison check prior to saving for the saved value vs the new value, but because the saved value is always a string when coming from the database, you can't do strict checking, which could be problematic. So I am not sure what the fix needs to be here.
Attachments (1)
Change History (15)
#2
@
7 years ago
- Component Posts, Post Types → Options, Meta APIs
- Milestone Awaiting Review → Future Release
- Summary All registered meta require the same caps on 5.3-RC2-46575 → Can't update meta via the REST API if an identical value for a field you don't have permission to update is included
- Version trunk → 4.7
#4
@
7 years ago
Yep, but I think that you should get back an error response if you are trying to change the value.
#5
@
7 years ago
Yes you should. That is what is happening currently. What I am focused on here is that the wrong is error is being thrown for the wrong field, preventing a legitimate update. A field that wasn't being updated, is being updated. Not only does this cause the permissions error, but I would imagine also wastes update calls that don't actually need to happen.
#6
@
7 years ago
Ah, I just understood how you are saying it. You are saying that if you pass an identical value to what it used to be, it is preventing the update of the other unrelated field. My bad! Just took me a while to process.
#9
@
6 years ago
@TimothyBlynJacobs Thanks for the message! Unfortunately I am still stuck on this part:
I looked at doing a comparison check prior to saving for the saved value vs the new value, but because the saved value is always a string when coming from the database, you can't do strict checking, which could be problematic. So I am not sure what the fix needs to be here.
#10
@
6 years ago
@johnstonphilip Try using the WP_REST_Meta_Fields::is_meta_value_same_as_stored_value method.
This ticket was mentioned in PR #14328 on WordPress/wordpress-develop by @dlocc.
4 weeks ago
#11
- Keywords has-patch has-unit-tests added
When a post is saved in the block editor, it sends back every registered meta field it got from the REST API, including fields the current user isn't allowed to edit. If one of those fields hits the authorization check, the whole save fails with "Sorry, you are not allowed to edit the X custom field." even though the user never touched that field.
56075 fixed this for single fields that already have a stored value. These cases still fail on trunk:
- A field with no stored value, sent back with the default the REST API returned for it. Since 58831 compares against the raw stored value, this also fails when a default is registered.
- A multi field sent back with the same values, because authorization was checked before the values were compared.
- An empty list or
nullfor a field with no stored value. That goes through the delete path, which checked authorization before checking if there was anything to delete.
What changed
All in WP_REST_Meta_Fields, so it covers post, term, comment and user meta:
update_meta_value(): when the user can't edit the field, nothing is stored, and the value matches the default the API returns for it, the update is skipped instead of returning a 403.update_multi_meta_value(): works out what would be added and removed first, returns early when nothing changes, and only checks authorization when something does. The same default check applies.delete_meta_value(): returns early when nothing is stored, before the authorization check.- New
is_meta_value_same_as_default_value()helper that mirrors whatget_value()returns for a field with no stored value.
Users who can edit a field still store the default value, so the 6.7 behavior from #55600 doesn't change. Changing a protected field still returns a 403.
Testing
New tests in tests/phpunit/tests/rest-api/rest-post-meta-fields.php (--group 48426, 21 cases). 8 of them check that changing a protected field is still rejected, so they pass on trunk too.
| trunk | this PR | |
|---|---|---|
--group 48426, single site | 13 failures | 21/21 pass |
--group 48426, multisite | 21/21 pass | |
--group restapi, single site | 3649 tests, no failures | |
--group restapi, multisite | 3684 tests, no failures | |
--group meta, single site / multisite | 507 / 533 tests pass |
The restapi runs have 4 warnings (test_get_items_wdotorg_unavailable, test_create_item_wdotorg_unreachable and the two test_format_validation_is_applied_if_missing_type tests). They show up on trunk too and aren't related to this change.
PHPCS and PHPStan are clean on the changed files.
I also checked it in the block editor with Playwright on a local wordpress-develop site, using the mu-plugin below. On trunk the save fails, with the patch it saves.
Manual testing
- Add this as an mu-plugin:
<?php add_action( 'init', function () { register_post_meta( 'post', '_demo_note', array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function () { return current_user_can( 'edit_posts' ); }, ) ); register_post_meta( 'post', '_demo_locked_flag', array( 'type' => 'boolean', 'single' => true, 'show_in_rest' => true, 'auth_callback' => function () { return current_user_can( 'this_cap_does_not_exist' ); }, ) ); } );
- Go to Posts > Add New Post.
- In the browser console, run
wp.data.dispatch( 'core/editor' ).editPost( { meta: { _demo_note: 'Changed' } } ); - Click "Save draft".
- Before: an "Updating failed." notice, and "Show details" says "Sorry, you are not allowed to edit the _demo_locked_flag custom field."
- After: the draft saves and
_demo_noteis stored.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the ticket, tracing the cause, writing the fix and the PHPUnit tests, the Playwright check, and drafting this description. I reviewed the code and the test results before opening this PR.
@
4 weeks ago
Before and after in the block editor: trunk fails with a 403, PR 14328 saves the draft.
#12
@
4 weeks ago
I took a look at this and it still happens on trunk. [56075] fixed it for single fields that already have a stored value, but a block editor save still fails with a 403 when it sends back a protected field the user didn't change and:
- the field has no stored value, so it's sent back with the default the API returned for it. Since [58831] this also fails when a default is registered.
- it's a multi field with the same values, because authorization was checked before the values were compared.
- it's an empty list or
nullfor a field with nothing stored, which goes through the delete path.
PR 14328 only checks authorization when a value would actually change. Users who can edit a field still store the default, so the behavior from #55600 stays the same, and changing a protected field still returns a 403.
The PR adds 21 PHPUnit test cases in the 48426 group. The 13 that cover the bug fail on trunk, and all 21 pass with the patch on single site and multisite.
Before and after in the block editor:
#13
@
3 weeks ago
Test Report
Description
This report validates that the indicated patch works as expected.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/14328
Environment
- WordPress: 7.2-beta1 (trunk nightly) with the PR's
class-wp-rest-meta-fields.phpapplied - PHP: 8.3
- Server: WordPress Playground CLI 3.1.60
- Database: SQLite
- OS: Windows 11
- Plugins: none
- MU Plugins: test harness (described below)
Steps to Reproduce
- Register protected meta whose
auth_callbackrequires a capability no user has (this_cap_does_not_exist), plus editable meta withauth_callbackreturning true. Covered: post meta (boolean, string with default, string with a stored value, multi, multi with default), term meta, user meta and comment meta. - As a user with the Editor role, GET the object with
context=edit, change only the editable field, and POST all the meta back, the way the block editor does. - Separately, try real changes to the protected fields (change, add, delete, remove one value of a multi field, empty a multi field) and compare the stored meta before and after.
- Run every case on trunk and with the patch. Requests are dispatched with
rest_do_request().
Expected Results
- Echoing back unchanged protected fields does not block the save.
- Real changes to protected fields are still rejected and nothing is written.
Actual Results
Unchanged protected fields echoed back:
| Case | trunk | PR |
| Full echo-back save, editable field changed | 403 | ✅ 200 |
| Boolean, nothing stored | 403 | ✅ 200 |
| String with default, nothing stored | 403 | ✅ 200 |
| String with a stored value | 200 | 200 |
| Multi, nothing stored | 403 | ✅ 200 |
| Multi with default, nothing stored | 403 | ✅ 200 |
| Empty list, multi with nothing stored | 403 | ✅ 200 |
null, string with nothing stored | 403 | ✅ 200 |
| Multi, same values in a different order | 403 | ✅ 200 |
| Term meta echo-back | 403 | ✅ 200 |
| User meta echo-back | 403 | ✅ 200 |
Real changes to protected fields:
| Case | trunk | PR |
| Change boolean, nothing stored | 403 | ✅ 403 |
| Change string with default, nothing stored | 403 | ✅ 403 |
| Change a stored value | 403 | ✅ 403 |
Delete a stored value (null) | 403 | ✅ 403 |
| Add a value to multi, nothing stored | 403 | ✅ 403 |
| Change multi with default, nothing stored | 403 | ✅ 403 |
| Remove one value of a stored multi | 403 | ✅ 403 |
| Empty a stored multi | 403 | ✅ 403 |
| Change protected term / user / comment meta | 403 | ✅ 403 |
For the eight post meta cases above, the stored meta is identical before and after the request, on trunk and with the patch.
A user who can edit a field still stores the default value explicitly (row exists after saving the default), so the #55600 behavior is unchanged. ✅
Additional Notes
- Not covered by this PR (separate bug):
nullfor a protected boolean field with nothing stored returns500 rest_invalid_stored_valueon trunk and with the patch. Same for echoing back comment meta on a clean install, because of the core_wp_note_statusmeta (enumresolved/reopen, no default): it is returned asnullwhen unset, and sending thatnullback fails with the same 500. The cause is inWP_REST_Meta_Fields::update_value(): it validates the empty string returned byget_metadata()for an unset key as if it were an invalid stored value, beforedelete_meta_value()is reached. For string fields, where the empty string passes validation, the patch handles this case. This looks worth a separate ticket. - Pre-existing, unrelated: on trunk, when the echo-back save fails with 403, the editable field is still written to the database. The response is an error, but the save is partial.
@sincetags: the patch uses@since 7.2.0. Trunk is already 7.2-beta1 and the ticket milestone is Future Release, so these may need to become7.3.0depending on where this lands.- The patch applies cleanly together with the patch for #64019, which touches the same file.
- I did not run PHPUnit locally.
Used AI (Claude Opus 5.5) for reading the diff, writing the test harness, running the test matrix and drafting this report. I reviewed the results before posting.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)

@TimothyBlynJacobs Just confirming on this. You changed the title to say "if an identical value for a field". But in my tests the value does not have to be identical to any other value. It can be any value.