From 06943fd7fef863c2267a6f02705762a949de25bd Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 14 Jan 2025 15:40:18 +0200 Subject: [PATCH] Boost: Fix cache bypass patterns not applied from start of URL (#40999) --- projects/plugins/boost/.phan/baseline.php | 8 +++----- .../plugins/boost/app/lib/minify/functions-service.php | 9 ++++++++- .../page-cache/data-sync/Page_Cache_Entry.php | 9 ++++++++- .../optimizations/page-cache/pre-wordpress/Request.php | 4 +++- .../boost/changelog/fix-boost-cache-bypass-patterns | 4 ++++ 5 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 projects/plugins/boost/changelog/fix-boost-cache-bypass-patterns diff --git a/projects/plugins/boost/.phan/baseline.php b/projects/plugins/boost/.phan/baseline.php index 507c6915ac89a..c009f71b48236 100644 --- a/projects/plugins/boost/.phan/baseline.php +++ b/projects/plugins/boost/.phan/baseline.php @@ -14,8 +14,8 @@ // PhanTypeArraySuspicious : 9 occurrences // PhanTypeMismatchArgument : 8 occurrences // PhanParamTooMany : 6 occurrences - // PhanPossiblyUndeclaredVariable : 6 occurrences - // PhanTypeMismatchReturnProbablyReal : 6 occurrences + // PhanPossiblyUndeclaredVariable : 5 occurrences + // PhanTypeMismatchReturnProbablyReal : 5 occurrences // PhanUndeclaredConstant : 5 occurrences // PhanTypeMismatchArgumentNullableInternal : 4 occurrences // PhanUndeclaredFunction : 4 occurrences @@ -30,7 +30,6 @@ // PhanTypeMismatchReturnNullable : 2 occurrences // PhanCoalescingNeverNull : 1 occurrence // PhanImpossibleTypeComparisonInGlobalScope : 1 occurrence - // PhanPluginNeverReturnFunction : 1 occurrence // PhanRedefineFunction : 1 occurrence // PhanRedundantCondition : 1 occurrence // PhanTypeInvalidUnaryOperandIncOrDec : 1 occurrence @@ -55,13 +54,12 @@ 'app/lib/minify/Config.php' => ['PhanTypeMismatchReturnNullable'], 'app/lib/minify/Dependency_Path_Mapping.php' => ['PhanUndeclaredConstant'], 'app/lib/minify/functions-helpers.php' => ['PhanImpossibleTypeComparison', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeMismatchDefault', 'PhanUndeclaredClassMethod', 'PhanUndeclaredClassReference', 'PhanUndeclaredConstant'], - 'app/lib/minify/functions-service.php' => ['PhanImpossibleTypeComparison', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginNeverReturnFunction', 'PhanPluginUseReturnValueInternalKnown', 'PhanPossiblyUndeclaredVariable', 'PhanTypeComparisonToArray', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentNullableInternal'], + 'app/lib/minify/functions-service.php' => ['PhanImpossibleTypeComparison', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginUseReturnValueInternalKnown', 'PhanPossiblyUndeclaredVariable', 'PhanTypeComparisonToArray', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentNullableInternal'], 'app/modules/Modules_Setup.php' => ['PhanTypeMismatchPropertyDefault'], 'app/modules/image-guide/Image_Guide_Proxy.php' => ['PhanPluginDuplicateConditionalNullCoalescing'], 'app/modules/image-size-analysis/data-sync/Image_Size_Analysis_Action_Fix.php' => ['PhanPossiblyUndeclaredVariable', 'PhanRedundantCondition'], 'app/modules/optimizations/critical-css/CSS_Proxy.php' => ['PhanPluginDuplicateConditionalNullCoalescing'], 'app/modules/optimizations/page-cache/Page_Cache_Setup.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchReturnProbablyReal', 'PhanTypeMissingReturn'], - 'app/modules/optimizations/page-cache/data-sync/Page_Cache_Entry.php' => ['PhanTypeMismatchReturnProbablyReal'], 'app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php' => ['PhanTypeMismatchReturnProbablyReal'], 'app/modules/optimizations/page-cache/pre-wordpress/Logger.php' => ['PhanCoalescingNeverNull', 'PhanPluginDuplicateConditionalNullCoalescing'], 'app/modules/optimizations/page-cache/pre-wordpress/Request.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchPropertyDefault'], diff --git a/projects/plugins/boost/app/lib/minify/functions-service.php b/projects/plugins/boost/app/lib/minify/functions-service.php index 08fbd8997447d..68c11a2e695ac 100644 --- a/projects/plugins/boost/app/lib/minify/functions-service.php +++ b/projects/plugins/boost/app/lib/minify/functions-service.php @@ -323,9 +323,16 @@ function jetpack_boost_page_optimize_get_file_paths( $args ) { return $args; } +/** + * Exit with a given HTTP status code. + * + * @param int $status HTTP status code. + * + * @return never + */ function jetpack_boost_page_optimize_status_exit( $status ) { http_response_code( $status ); - exit; + exit( 0 ); // This is a workaround, until a bug in phan is fixed - https://github.com/phan/phan/issues/4888 } function jetpack_boost_page_optimize_get_mime_type( $file ) { diff --git a/projects/plugins/boost/app/modules/optimizations/page-cache/data-sync/Page_Cache_Entry.php b/projects/plugins/boost/app/modules/optimizations/page-cache/data-sync/Page_Cache_Entry.php index 4e25efb74c161..fb7538916e3f6 100644 --- a/projects/plugins/boost/app/modules/optimizations/page-cache/data-sync/Page_Cache_Entry.php +++ b/projects/plugins/boost/app/modules/optimizations/page-cache/data-sync/Page_Cache_Entry.php @@ -31,7 +31,7 @@ public function set( $value ) { * * @param mixed $value The value to sanitize. * - * @return string The sanitized value, as a list. + * @return array The sanitized value. */ private function sanitize_value( $value ) { if ( is_array( $value ) ) { @@ -56,12 +56,19 @@ private function sanitize_value( $value ) { // Remove double shashes. $path = str_replace( '//', '/', $path ); + // Remove symbols, as they are included in the regex check. + $path = ltrim( $path, '^' ); + $path = rtrim( $path, '$' ); + $path = preg_replace( '/\/\?$/', '', $path ); + // Make sure there's a leading slash. $path = '/' . ltrim( $path, '/' ); // Fix up any wildcards. $path = $this->sanitize_wildcards( $path ); } + + $value = array_values( array_unique( array_filter( $value ) ) ); } else { $value = array(); } diff --git a/projects/plugins/boost/app/modules/optimizations/page-cache/pre-wordpress/Request.php b/projects/plugins/boost/app/modules/optimizations/page-cache/pre-wordpress/Request.php index 540c481946ca7..d94eefb8eeac2 100644 --- a/projects/plugins/boost/app/modules/optimizations/page-cache/pre-wordpress/Request.php +++ b/projects/plugins/boost/app/modules/optimizations/page-cache/pre-wordpress/Request.php @@ -95,6 +95,8 @@ public function is_url_excluded( $request_uri = '' ) { /** * Filters the bypass patterns for the page cache. + * If you need to sanitize them, do it before passing them to this filter, + * as there's no sanitization done after this filter. * * @since 3.2.0 * @@ -104,7 +106,7 @@ public function is_url_excluded( $request_uri = '' ) { $bypass_patterns[] = 'wp-.*\.php'; foreach ( $bypass_patterns as $expr ) { - if ( ! empty( $expr ) && preg_match( "~$expr~", $request_uri ) ) { + if ( ! empty( $expr ) && preg_match( "~^$expr/?$~", $request_uri ) ) { return true; } } diff --git a/projects/plugins/boost/changelog/fix-boost-cache-bypass-patterns b/projects/plugins/boost/changelog/fix-boost-cache-bypass-patterns new file mode 100644 index 0000000000000..a7371df8724df --- /dev/null +++ b/projects/plugins/boost/changelog/fix-boost-cache-bypass-patterns @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Page Cache: Fixed bypass patterns getting applied to the whole URL instead of the start.