Administration: Use a unique nonce for Find Posts - #12707
Administration: Use a unique nonce for Find Posts#12707ArkaPrabhaChowdhury wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a nonce collision in the admin “Find Posts” modal by giving its nonce field a unique DOM ID, preventing media.js from accidentally reading the Custom Fields meta box nonce on post edit screens and causing 403 failures.
Changes:
- Update
find_posts_div()to render a dedicated nonce field withid="_ajax_nonce-find-posts". - Update the Find Posts AJAX request in
media.jsto read the new nonce field while still sending the nonce under the_ajax_noncerequest key expected bycheck_ajax_referer( 'find-posts' ). - Add a PHPUnit regression test to ensure the modal renders the unique nonce ID and no longer renders
id="_ajax_nonce".
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/phpunit/tests/admin/includesTemplate.php | Adds a regression test asserting the Find Posts modal nonce field uses the unique ID and avoids the generic _ajax_nonce ID. |
| src/wp-admin/includes/template.php | Changes the Find Posts modal nonce field name/ID to _ajax_nonce-find-posts to eliminate duplicate IDs in the DOM. |
| src/js/_enqueues/admin/media.js | Updates the Find Posts AJAX logic to read the nonce from #_ajax_nonce-find-posts while continuing to post it as _ajax_nonce. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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. |
Trac ticket: https://core.trac.wordpress.org/ticket/43611
The Find Posts modal used the generic
_ajax_noncefield name and ID. On post edit screens with the Custom Fields meta box, that produced duplicate#_ajax_nonceelements, somedia.jscould read the Custom Fields nonce and the Find Posts request would fail with a 403 response.This gives the Find Posts nonce a dedicated
_ajax_nonce-find-postsfield and updates the AJAX request to read it while continuing to send the nonce under the_ajax_noncerequest key expected by the server.A regression test verifies that
find_posts_div()renders the unique nonce ID and no longer rendersid="_ajax_nonce".Validation:
phpunit --filter Tests_Admin_IncludesTemplate(43 tests, 125 assertions)grunt jshint:core --file=src/js/_enqueues/admin/media.jsphpcbf src/wp-admin/includes/template.php tests/phpunit/tests/admin/includesTemplate.phpphpcs src/wp-admin/includes/template.php tests/phpunit/tests/admin/includesTemplate.php