Opened 7 months ago
Last modified 5 days ago
#65050 reviewing defect (bug)
REST API: Comments controller returns 403 instead of 404 for invalid post ID in update_item()
| Reported by: | saratheonline | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.3 |
| Component: | REST API | Version: | 7.0 |
| Severity: | normal | Keywords: | has-patch changes-requested has-unit-tests |
| Cc: | Focuses: | coding-standards |
Description
In WP_REST_Comments_Controller, the update_item() method returns HTTP
403 (Forbidden) when an invalid post ID is provided, but the correct status
code is 404 (Not Found).
The same file returns 404 for the identical scenario in
get_item()
(line 418), making this inconsistent within the same controller.
403 implies the client lacks permission. 404 is correct here because
the post does not exist — it is a missing resource, not an authorization
failure.
Affected code (class-wp-rest-comments-controller.php)
if ( empty( $post ) ) {
return new WP_Error(
'rest_comment_invalid_post_id',
__( 'Invalid post ID.' ),
array( 'status' => 403 ) // incorrect
);
}
Proposed Fix
array( 'status' => 404 )
Change History (18)
This ticket was mentioned in PR #11529 on WordPress/wordpress-develop by @saratheonline.
7 months ago
#1
#2
@
7 months ago
- Focuses coding-standards added
Tested patch: https://github.com/WordPress/wordpress-develop/pull/11529
Environment:
- WordPress: 7.1-alpha-62161-src
- PHP: 8.3.30
- Browser: Chrome
- Database: MySQL 8.4.8
- OS: Ubuntu
Steps I followed:
- Confirmed that 'update_item()' in 'WP_REST_Comments_Controller' returns a 403 status when an invalid post ID is provided.
- Verified that the same scenario in
get_item()correctly returns a 404 status. - This creates inconsistency within the same controller.
Agree that 404 is the correct response here, as the post does not exist and this is not a permission issue.
Tested the proposed fix and it resolves the issue as expected without introducing any regressions.
#5
@
7 months ago
- Keywords needs-testing removed
Patch Testing Report
Patch Tested: https://github.com/WordPress/wordpress-develop/pull/11529
Environment
- WordPress: 7.1-alpha-62161-src
- PHP: 8.3.30
- Server: nginx/1.29.5
- Database: mysqli (Server: 8.4.8 / Client: mysqlnd 8.3.30)
- Browser: Chrome 147.0.0.0
- OS: Windows 10/11
- Theme: Twenty Twenty-Five 1.4
- MU Plugins: None activated
- Plugins:
- Test Reports 1.2.1
Steps taken
- Ensure you have the Application password from "Users > Profile".
- In your terminal, run this command:
curl -X POST http://localhost:8889/wp-json/wp/v2/comments/1 \ -u "admin" \ -H "Content-Type: application/json" \ -d '{"post": 9999}' - You'll be prompted to enter a password. Enter the password you got from Application Password.
- Perform steps 2 - 3 before and after applying the patch.
- ✅ Patch is solving the problem.
Expected result
- A 404 status code response is returned when you try to update with a non-existent post ID.
Additional Notes
- Patch fixes the issue, updating keyword (removing "needs-testing"). Add it back if needed.
Screenshots/Screencast with results
- Before:
- After:
#6
@
7 months ago
Tested PR https://github.com/WordPress/wordpress-develop/pull/11529
I created a post and comment locally, then called WP_REST_Comments_Controller::update_item() with that comment ID and post => 999999.
Before the patch, the response was rest_comment_invalid_post_id with status 403.
After the patch, the same error code returns status 404.
This matches the behavior in get_item() and looks correct to me. Patch tests well.
#7
@
6 months ago
Removing trunk version as this is not going to be shipped with WP 7.0 but in the next releases.
This ticket was mentioned in PR #12545 on WordPress/wordpress-develop by @dhrupo.
4 months ago
#8
- Keywords has-unit-tests added
## Summary
WP_REST_Comments_Controller::update_item() returned HTTP 403 (Forbidden) when the request targeted a non-existent post ID (e.g. PUT /wp/v2/comments/<id> with post set to a missing post). The correct status is 404 (Not Found): the post is a missing resource, not an authorization failure.
This also makes update_item() consistent with get_item(), which already returns 404 for the same "post does not exist" scenario within the same controller.
## Changes
class-wp-rest-comments-controller.php: change therest_comment_invalid_post_iderror status from403to404inupdate_item().rest-comments-controller.php(tests): update thetest_update_comment_invalid_post_idassertion to expect404.
The error code (rest_comment_invalid_post_id) is intentionally unchanged to preserve backwards compatibility for clients matching on it. The create_item 403 responses ("Sorry, you are not allowed to create this comment without a post.") are genuine authorization checks and are left untouched.
## Relationship to #11529
This completes the work started in #11529 by @saratheonline, which made the code change but left the existing unit test asserting 403, causing CI to fail. @westonruter requested changes noting the test needed updating. This PR includes both the code fix and the corresponding test update so the suite passes.
## Testing
Reproduced red → green:
- With the unpatched code, updating
test_update_comment_invalid_post_idto expect404fails:Failed asserting that 403 is identical to 404. - After the one-line controller fix, the test passes.
Full controller suite green (verified in the local Docker environment):
WP_Test_REST_Comments_Controller: OK (142 tests, 675 assertions)
Manual reproduction:
curl -X POST http://localhost:8889/wp-json/wp/v2/comments/1 \
-u "admin:<app-password>" \
-H "Content-Type: application/json" \
-d '{"post": 9999}'
# Before: 403 Forbidden
# After: 404 Not Found
Props saratheonline.
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Reproducing the failing test, applying the fix and the test update, and running the test suite. All changes were reviewed by me.
This ticket was mentioned in Slack in #core-test by noruzzaman. View the logs.
4 months ago
@mirmpro commented on PR #12545:
4 months ago
#10
Test Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/12545
Environment
- WordPress: 7.1-alpha
- Subdirectory: No
- PHP: 7.4.33
- Server: PHP.wasm
- Database: WP_SQLite_Driver
- Browser: Chrome 150.0.0.0
- OS: macOS
- Theme: Twenty Twenty-Five
- MU Plugins: None activated
- Plugins: None
Steps taken
- Launched the WordPress testing environment via the Playground PR preview URL for PR #12545.
- Opened the browser developer console while logged in as Administrator.
- Executed a REST API request targeting an existing comment (
/wp/v2/comments/1) and attempted to update its associatedpostto a non-existent post ID (99999):`javascript wp.apiFetch({
path: '/wp/v2/comments/1',
method: 'POST',
data: { post: 99999 }
}).catch( error => console.log('Response Status:', error.data.status, error) );
This ticket was mentioned in Slack in #core by adrianduffell. View the logs.
4 months ago
#12
@
4 months ago
- Milestone 7.1 → Future Release
This was discussed in today's bug scrub. With 7.1 RC 1 due in the coming hours, let's punt this to a future release to give it more time to work through.
This ticket was mentioned in Slack in #core by im3dabasia1. View the logs.
3 weeks ago
#15
@
3 weeks ago
As per today’s 7.2 bug scrub:
the newer PR covers the earlier feedback and passes checks.
@saratheonline, will you be able to refresh the PR and address the feedback?
@westonruter can review once you are ready!
@westonruter commented on PR #11529:
5 days ago
#16
Closing in favor of #12545 which includes tests.
@dhrupo commented on PR #12545:
5 days ago
#18
Thanks for the review. On the other notes:
- Tests: added
test_update_note_invalid_post_idandtest_update_comment_invalid_post_id_without_moderate_comments. A note moved onto a missing post still getsrest_cannot_create_note, becauseedit_postfails for a post that doesn't exist. A user withoutmoderate_commentsstill getsrest_cannot_editbefore the post is looked up. Only a moderator moving a regular comment reaches the new 404. create_item_permissions_check(): I left its missing-post 403 alone, since the ticket is about updates and changing create responses felt like a separate call. Happy to change it here too if you'd prefer one status for this error code everywhere.- Gutenberg's 7.2 compat controller: I'll open a matching PR there once this one is settled.


update_item()was returning HTTP 403 (Forbidden) when a non-existentTrac ticket: https://core.trac.wordpress.org/ticket/65050
Fixes: #65050
## Use of AI Tools