Skip to content

Administration: Use a unique nonce for Find Posts - #12707

Draft
ArkaPrabhaChowdhury wants to merge 1 commit into
WordPress:trunkfrom
ArkaPrabhaChowdhury:trac-43611-find-posts-nonce
Draft

Administration: Use a unique nonce for Find Posts#12707
ArkaPrabhaChowdhury wants to merge 1 commit into
WordPress:trunkfrom
ArkaPrabhaChowdhury:trac-43611-find-posts-nonce

Conversation

@ArkaPrabhaChowdhury

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/43611

The Find Posts modal used the generic _ajax_nonce field name and ID. On post edit screens with the Custom Fields meta box, that produced duplicate #_ajax_nonce elements, so media.js could 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-posts field and updates the AJAX request to read it while continuing to send the nonce under the _ajax_nonce request key expected by the server.

A regression test verifies that find_posts_div() renders the unique nonce ID and no longer renders id="_ajax_nonce".

Validation:

  • phpunit --filter Tests_Admin_IncludesTemplate (43 tests, 125 assertions)
  • grunt jshint:core --file=src/js/_enqueues/admin/media.js
  • phpcbf src/wp-admin/includes/template.php tests/phpunit/tests/admin/includesTemplate.php
  • phpcs src/wp-admin/includes/template.php tests/phpunit/tests/admin/includesTemplate.php

Copilot AI review requested due to automatic review settings July 27, 2026 08:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 with id="_ajax_nonce-find-posts".
  • Update the Find Posts AJAX request in media.js to read the new nonce field while still sending the nonce under the _ajax_nonce request key expected by check_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.

@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.

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