Skip to content

Script Loader: Guard compression_test() with manage_options check - #12748

Open
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/65750-compression-test-unauthorized-ajax
Open

Script Loader: Guard compression_test() with manage_options check#12748
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/65750-compression-test-unauthorized-ajax

Conversation

@irozum

@irozum irozum commented Jul 29, 2026

Copy link
Copy Markdown

compression_test() is called from wp-admin/admin-footer.php for every user who can access wp-admin, whenever the can_compress_scripts option is unset — not only administrators. It outputs a <script> block that fires AJAX requests to the wp-compression-test action, but that AJAX handler (wp_ajax_wp_compression_test()) requires the manage_options capability, so non-admin users (e.g. Editors) get -1 responses and can never actually resolve the test. This also contradicts the function's own docblock, which states it "Has no effect when the current user is not an administrator."

This PR adds a current_user_can( 'manage_options' ) check at the top of compression_test() so it returns early — and outputs nothing — for users who can't act on the result, matching the documented behavior. Administrators are unaffected.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copilot AI review requested due to automatic review settings July 29, 2026 00:21
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum, szepeviktor.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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 updates the Script Loader compression test so it only outputs its inline <script> block for users who can actually complete the related AJAX flow (i.e., users with the manage_options capability), aligning runtime behavior with the existing docblock and the capability check already enforced by the wp_ajax_wp_compression_test() handler.

Changes:

  • Add an early current_user_can( 'manage_options' ) guard in compression_test() to prevent output for non-privileged wp-admin users.
  • Add PHPUnit coverage to verify compression_test() outputs nothing for a user without manage_options, and still outputs the test script for a user with it.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/wp-admin/includes/template.php Adds a manage_options capability guard to prevent emitting the compression-test inline script for non-admin-capable users.
tests/phpunit/tests/admin/includesTemplate.php Adds tests asserting compression_test() output is empty for an Editor and contains the test script for an Administrator.

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

@szepeviktor

Copy link
Copy Markdown

@irozum Maybe you should add your modification at the root:

// get_site_option() won't exist when auto upgrading from <= 2.7.
if ( function_exists( 'get_site_option' )
&& false === get_site_option( 'can_compress_scripts' )
) {
compression_test();
}

Comment on lines +562 to +564
ob_start();
compression_test();
$output = ob_get_clean();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
ob_start();
compression_test();
$output = ob_get_clean();
$output = get_echo( 'compression_test' );

Comment on lines +581 to +583
ob_start();
compression_test();
$output = ob_get_clean();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
ob_start();
compression_test();
$output = ob_get_clean();
$output = get_echo( 'compression_test' );

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.

4 participants