Make WordPress Core

Opened 11 months ago

Closed 9 months ago

Last modified 9 months ago

#64370 closed defect (bug) (fixed)

Response header detection in page cache test for Site Health should be more robust

Reported by: westonruter Owned by: westonruter
Priority: normal Milestone: 7.0
Component: Site Health Version: 6.1
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description (last modified by westonruter)

This is a follow-up to #63748. See ​PR comment from @dmsnell:

I wish that the $cache_hit_callback were more robust than it is, since it also matches on values like “this cache is *hit, don’t use it” and “not a hit” but that’s not part of this ticket or work.

would be awesome to have some example strings from each of these new headers as a comment to the right of them.

for example, the ​varnish docs suggest that the _full_ match is hit, meaning we could add => static function ( $v ) { return 'hit' === $v; } /** @see https://www.varnish-software.com/developers/tutorials/logging-cache-hits-misses-varnish/ */

the extra examples are icing on the cake and not necessary here.

according to ​this random survey x-cache-status is expected to only contain hit, though ​for x-cache there is an insignificant but measureable count of requests containing HIT, MISS

Also, ​in Slack:

the only thing that really caught my eye is that we’re needly allocating to do case-insensitive compare instead of calling stripos()

Attachments (1)

64370.patch​ (262.8 KB ) - added by solankisoftware 11 months ago.
FIX :: Response header detection in page cache test for Site Health should be more robust

Download all attachments as: .zip

Change History (16)

#1 @westonruter
11 months ago

  • Description modified (diff)

This ticket was mentioned in ​Slack in #core-performance by westonruter. ​View the logs.


11 months ago

@solankisoftware
11 months ago

FIX :: Response header detection in page cache test for Site Health should be more robust

#3 @solankisoftware
11 months ago

  • Keywords has-patch added; needs-patch removed

Replaces the old string-based cache header detection in Site Health with a more robust method.
Adds case-insensitive matching, supports additional cache headers, and prevents false positives.
Improves accuracy across Varnish, NGINX, Cloudflare, LiteSpeed, and host-level caching systems.

#4 @westonruter
11 months ago

  • Keywords needs-patch added; has-patch removed

@solankisoftware Your patch seems malformed. It is introducing an entire new file. Please consider opening a pull request instead to facilitate review and testing.

This ticket was mentioned in ​PR #10606 on ​WordPress/wordpress-develop by ​@solankisoftware.


11 months ago
#5

  • Keywords has-patch added; needs-patch removed

Trac Ticket: https://core.trac.wordpress.org/ticket/64370

This PR enhances the WordPress Site Health page cache test by introducing
a robust cache header detection mechanism. The previous method could
produce false positives or negatives depending on the caching system used.

Changes include:

  • Added detect_cache_headers() method for strict, case-insensitive header matching
  • Improved support for common cache headers (x-cache, x-cache-status, cf-cache-status, x-litespeed-cache, x-varnish, etc.)
  • Updated get_test_page_cache() to use detect_cache_headers()
  • Prevents false positives when unrelated text contains words like "HIT" or "MISS"

Testing:

  1. Enable any caching layer (Varnish, NGINX fastcgi_cache, Cloudflare, LiteSpeed, host-level caching)
  2. Go to Tools → Site Health → Status
  3. Verify that page cache detection reflects the real server cache status

This PR is backward compatible and does not change existing Site Health UI behavior.

This ticket was mentioned in ​Slack in #core-performance by westonruter. ​View the logs.


10 months ago

​@westonruter commented on ​PR #10606:


10 months ago
#7

Closing due to lack of response and the PR not being touching many unrelated lines.

#8 @mukesh27
10 months ago

  • Keywords needs-patch added; has-patch removed

This ticket was mentioned in ​PR #10855 on ​WordPress/wordpress-develop by ​@westonruter.


9 months ago
#9

  • Keywords has-patch has-unit-tests added; needs-patch removed

### Summary of Changes

This PR improves the robustness of page cache detection in Site Health by refining the response header validation and expanding support for common caching layers.

#### Logic & Header Improvements

  • Stricter Cache Hit Matching: The $cache_hit_callback now uses a word-boundary regular expression (/\bhit\b/i) instead of a simple substring check. This prevents false positives from header values like shit.
  • X-SRCache-Store-Status Correction: Fixed the validation for this header to check for the correct value STORE (the module's indicator for a successful cache storage) instead of HIT.
  • Varnish Support: Added detection for the X-Varnish header, specifically identifying a hit when the header contains two request IDs (e.g., 123 456), as documented in the Varnish FAQ.
  • Standard Headers: Restored Last-Modified, ETag, and Via as valid indicators of a caching layer, while ensuring they are handled as existence-based detections or validated where appropriate.
  • Documentation: Added detailed docblocks and source links for caching headers (e.g., srcache, Varnish) and explicitly listed supported generic proxies (Squid, Go, Fastly, LiteSpeed) in comments.

#### PHPUnit Test Enhancements

  • Comprehensive Test Cases: Added new test scenarios to Tests_Admin_wpSiteHealth to verify:
    • X-Varnish hits vs. misses.
    • X-SRCache-Store-Status (STORE) and X-SRCache-Fetch-Status (HIT).
    • Prevention of false positives from substrings (e.g., x-cache: shit).
    • Robustness of standard headers like ETag, Last-Modified, and Via.
  • Improved Typing: Updated the test suite with strict parameter and return types for data providers and test methods, aligning with modern PHP standards and resolving PHPStan reported issues.

#### Code Quality

  • Applied consistent formatting to class-wp-site-health.php and the test suite using composer format to ensure adherence to WordPress Coding Standards.

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

​@westonruter commented on ​PR #10855:


9 months ago
#10

_Gemini review:_

I have completed the review of the changes committed to the trac-64370 branch.

### Commit Context
The changes aim to improve the robustness of page cache detection in WordPress Site Health. Key improvements include:

  • Using stricter regex (\bhit\b) to prevent false positives from header values like "shit".
  • Adding support for the X-Varnish header by detecting the specific hit format (two request IDs).
  • Correcting x-srcache-store-status detection to look for STORE instead of HIT.
  • Improving documentation and PHPStan typing for the detection methods and tests.

### Code Review

#### src/wp-admin/includes/class-wp-site-health.php

  • Logic Improvement: The move from str_contains to preg_match( '/\bhit\b/i', ... ) is an excellent fix for substring-related false positives.
  • Varnish Header: The x-varnish callback correctly implements the detection of a hit by checking for two space-separated digits, matching Varnish's debugging header behavior.
  • SRCache Correction: Updating x-srcache-store-status to check for STORE is correct as per the module's documentation.
  • Coding Standards:
    • The use of array shape return types (array<string, ?callable>) in docblocks aligns with current project goals for PHPStan level 10 compatibility.
    • The addition of the : array return type hint is compatible with PHP 7.4.
  • Nitpick: The via header is restored but set to null (existence check). While standard, many proxies use Via without caching. However, given the context of other headers, this is a reasonable heuristic for WordPress core.

#### tests/phpunit/tests/admin/wpSiteHealth.php

  • Type Safety: The addition of strict types to properties and method signatures (private WP_Site_Health $instance;, test_get_page_cache( array $responses, ... )) is a great improvement for the test suite's reliability.
  • PHPStan Resolution: The return type for data_get_page_cache has been meticulously defined to match the complex array structure returned, which should resolve the reported PHPStan errors.
  • Test Coverage:
    • New cases for false-positive-hit-in-word, varnish-header, and srcache variants provide thorough coverage for the logic changes.
    • The last-modified test case uses a realistic date string, which is better than the previous "hit" value.
  • Consistency: The change from null to false for good_basic_auth in the data provider, coupled with the signature change to bool, ensures type consistency.

### Summary
The changes are high quality, follow WordPress coding standards, and significantly improve the reliability of Site Health's page cache detection. No regressions or standards violations were identified.

Recommendation: Ready for merge.

#11 @westonruter
9 months ago

  • Owner set to westonruter
  • Status new → accepted

This ticket was mentioned in ​Slack in #core-performance by westonruter. ​View the logs.


9 months ago

#13 @westonruter
9 months ago

  • Type enhancement → defect (bug)

Re-classifying as defect since technically some of the tests are faulty or can be hardened.

#14 @westonruter
9 months ago

  • Resolution → fixed
  • Status accepted → closed

In 61711:

Site Health: Improve page cache detection with added header and refined verification logic.

  • Use a more precise regular expression for identifying "HIT" statuses to avoid false positives (e.g., "no-hit").
  • Add specific verification logic for Varnish's X-Varnish header.
  • Use a null value in the header mapping to indicate a header existence check, when no callback is supplied.
  • Improve documentation with links for information about certain headers.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/10855

Follow-up to [61355], [54043].

Props westonruter, dmsnell.
See #63748.
Fixes #64370.

#15 @westonruter
9 months ago

In r61711, also:

X-SRCache-Store-Status Correction: Fixed the validation for this header to check for the correct value STORE (the module's indicator for a successful cache storage) instead of HIT.

Note: See TracTickets for help on using tickets.