Harden Cache-Control TTL sanitization in response controller#4
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new helper method sanitizeDirectiveSeconds to normalize cache directive durations (max-age and stale-while-revalidate) to prevent negative or invalid values from third-party filters. The feedback points out that casting the filter outputs to (int) before sanitization converts invalid types (like null or false) to 0, which bypasses the negative-value check and silently disables caching. The reviewer recommends passing the raw filter values and updating sanitizeDirectiveSeconds to accept mixed types and validate them using is_numeric() to safely fall back to the default TTL.
There was a problem hiding this comment.
Pull request overview
This PR hardens Cache-Control header emission by preventing negative TTL values (potentially returned by third-party WordPress filters) from producing malformed directives in StarResponseController::sendPublicCacheHeaders().
Changes:
- Added
sanitizeDirectiveSeconds()to normalize directive seconds to a non-negative integer. - Applied sanitization to both
starcache_max_ageandstarcache_stale_while_revalidatefilter outputs before building theCache-Controlheader.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Motivation
Cache-Controldirective values which can create malformed headers and unpredictable downstream caching behavior.Description
starcache_max_ageandstarcache_stale_while_revalidateoutputs inStarResponseController::sendPublicCacheHeaders()by introducingsanitizeDirectiveSeconds()and using it to ensure non-negative, predictable TTLs (file modified:StarResponseController.php).Testing
php -lacross plugin PHP files which completed successfully.composer install/composer testbut dependency installation failed due to Packagist network restrictions in the execution environment, so full test-suite execution could not be run.Codex Task