From 01d65b4d4ae4b0f092f00b8229d407927571e376 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Wed, 25 Nov 2026 20:10:44 +0100 Subject: [PATCH 1/2] Prevent personalized Markdown from entering shared caches --- README.md | 26 ++++- readme.txt | 5 +- src/Output/CachePolicy.php | 59 ++++++++++ src/Output/ContentRenderer.php | 24 ++-- src/Router/RewriteHandler.php | 12 +- tests/bootstrap.php | 21 ++++ tests/router-request.php | 148 ++++++++++++++++++++++++ tests/run.php | 2 + tests/security.php | 205 +++++++++++++++++++++++++++++++++ 9 files changed, 477 insertions(+), 25 deletions(-) create mode 100644 src/Output/CachePolicy.php create mode 100644 tests/router-request.php create mode 100644 tests/security.php diff --git a/README.md b/README.md index 2f8b827..54f3edc 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ add_filter( 'markdown_alternate_supported_post_types', function( $types ) { ### Adjust cache duration -Markdown output is cached using transients for 24 hours by default. You can change that with `markdown_alternate_cache_expiration`: +Eligible anonymous Markdown output is cached using transients for 24 hours by default. You can change that with `markdown_alternate_cache_expiration`: ```php add_filter( 'markdown_alternate_cache_expiration', function( $seconds ) { @@ -301,8 +301,17 @@ default value. Return `false` from `markdown_alternate_cache_enabled` to bypass both transient reads and writes. The filter receives the `WP_Post` as its second argument. HTTP responses also send private, no-store headers and mark the page as uncacheable. -The WooCommerce integration disables this cache for products; posts and pages -still default to cached output. +Logged-in visitors, password-protected posts, and visitors carrying WordPress +post-password, WooCommerce session/cart, or Easy Digital Downloads cart cookies +always bypass shared caching. The filter cannot turn caching back on for them. +The WooCommerce integration also disables caching for all products. Other +anonymous requests still use the shared cache; use this filter for custom +sessions or other visitor-dependent output. + +Markdown response handlers run at `template_redirect` priority 15, after the +default-priority access checks and canonical redirects. Explicit `.md` URLs +still skip canonical redirects; `?format=markdown` and Accept-negotiated requests +can be normalized first. Access checks on this hook must run before priority 15. ### Invalidate integration data @@ -319,15 +328,20 @@ add_filter( 'markdown_alternate_cache_version', function( $version, $post ) { Append your token to the existing value so multiple integrations can participate. The token is stored with the cached document; it does not change the transient key. -Old cache entries without a version token are regenerated once. Cache duration -continues to use `markdown_alternate_cache_expiration`. +Old cache entries without the current cache schema are regenerated once, even +when an integration's version token still matches, so previously cached personal +output is not reused. Cache duration continues to use +`markdown_alternate_cache_expiration`. ### Run renderer regression tests Run `php tests/run.php` after installing dependencies. These standalone tests use WordPress API doubles and the real Markdown converter, and compare default output with fixtures captured from `main` at commit `7afba03`. CI runs them on PHP 7.4–8.3; -they do not replace a full WordPress integration test. +they do not replace a full WordPress integration test. The suite also checks +visitor isolation, old-cache invalidation, private response headers, access-check +ordering, and canonical redirects. Router cases run in child PHP processes so +the real handlers can terminate their responses normally. ### Run WooCommerce integration tests diff --git a/readme.txt b/readme.txt index 7f9210a..22064dd 100644 --- a/readme.txt +++ b/readme.txt @@ -86,7 +86,7 @@ Example: = Is the output cached? = -Yes. Markdown output is cached using WordPress transients for 24 hours by default. The cache is automatically bypassed if the post has changed. Developers can adjust the cache duration with the `markdown_alternate_cache_expiration` filter. +Yes. Eligible anonymous Markdown output is cached using WordPress transients for 24 hours by default. Logged-in visitors, password-protected posts, and visitors with known session or cart cookies bypass shared caching and receive private, no-store responses. Old cache entries are regenerated after the cache-isolation fix. The cache is also regenerated if the post has changed. Developers can adjust the duration with `markdown_alternate_cache_expiration` and disable caching for custom visitor-dependent output with `markdown_alternate_cache_enabled`. = What does the Markdown output include? = @@ -108,6 +108,9 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret * Changed frontmatter links to terms to absolute URLs, like the links in the body * Added product URL, modified date, GTIN, sale end date and ratings to WooCommerce product frontmatter * Removed the post author from WooCommerce product frontmatter +* Prevented visitor-specific Markdown from entering shared caches and invalidated previously generated entries +* Moved Markdown response handlers after default-priority access checks and canonical redirects +* Added cache-isolation, response-header, and routing-order regression tests = 1.2.0 = * Added WooCommerce product metadata, formatted prices, short descriptions, visible attributes, and variations diff --git a/src/Output/CachePolicy.php b/src/Output/CachePolicy.php new file mode 100644 index 0000000..bc7e16b --- /dev/null +++ b/src/Output/CachePolicy.php @@ -0,0 +1,59 @@ +post_password ) { + return false; + } + + // Guest sessions can personalize ordinary posts and pages too. + $session_cookies = array( + 'wp-postpass_', + 'wp_woocommerce_session_', + 'woocommerce_items_in_cart', + 'woocommerce_cart_hash', + 'edd_items_in_cart', + ); + foreach ( array_keys( $_COOKIE ) as $name ) { + foreach ( $session_cookies as $prefix ) { + if ( 0 === strpos( (string) $name, $prefix ) ) { + return false; + } + } + } + + /** + * Filters whether otherwise eligible output can use shared caching. + * + * Disable for custom sessions or other visitor-dependent content. + * The HTTP router also uses this to send private, no-store headers. + * Logged-in visitors, password-protected posts and known session + * cookies always bypass caching, regardless of this filter. + * + * @since 1.2.0 + * + * @param bool $enabled Whether output may be cached. Default true. + * @param WP_Post $post The post being rendered. + */ + return (bool) apply_filters( 'markdown_alternate_cache_enabled', true, $post ); + } +} diff --git a/src/Output/ContentRenderer.php b/src/Output/ContentRenderer.php index f54cf53..550c657 100644 --- a/src/Output/ContentRenderer.php +++ b/src/Output/ContentRenderer.php @@ -21,6 +21,13 @@ */ class ContentRenderer { + /** + * Cache schema; older entries may contain another visitor's output. + * + * @var int + */ + private const CACHE_SCHEMA = 1; + /** * The HTML to Markdown converter. * @@ -45,18 +52,7 @@ public function __construct() { */ public function render( WP_Post $post ): string { $transient_key = 'md_alt_cache_' . $post->ID; - /** - * Filters whether rendered output can use shared caching. - * - * Disable for content whose output depends on the current customer. - * The HTTP router also uses this to send private, no-store headers. - * - * @since 1.2.0 - * - * @param bool $enabled Whether output may be cached. Default true. - * @param WP_Post $post The post being rendered. - */ - $cache_enabled = (bool) apply_filters( 'markdown_alternate_cache_enabled', true, $post ); + $cache_enabled = CachePolicy::is_enabled( $post ); $cached_data = $cache_enabled ? get_transient( $transient_key ) : false; /** @@ -74,7 +70,8 @@ public function render( WP_Post $post ): string { $cache_version = (string) apply_filters( 'markdown_alternate_cache_version', '', $post ); if ( is_array( $cached_data ) - && isset( $cached_data['markdown'], $cached_data['modified'], $cached_data['version'] ) + && isset( $cached_data['markdown'], $cached_data['modified'], $cached_data['version'], $cached_data['schema'] ) + && self::CACHE_SCHEMA === $cached_data['schema'] && $post->post_modified === $cached_data['modified'] && $cache_version === $cached_data['version'] ) { @@ -149,6 +146,7 @@ public function render( WP_Post $post ): string { set_transient( $transient_key, array( + 'schema' => self::CACHE_SCHEMA, 'markdown' => $output, 'modified' => $post->post_modified, 'version' => $cache_version, diff --git a/src/Router/RewriteHandler.php b/src/Router/RewriteHandler.php index 9c38bf5..cf15403 100644 --- a/src/Router/RewriteHandler.php +++ b/src/Router/RewriteHandler.php @@ -11,6 +11,7 @@ use WP_Term; use MarkdownAlternate\Output\ContentRenderer; use MarkdownAlternate\Output\TermRenderer; +use MarkdownAlternate\Output\CachePolicy; use MarkdownAlternate\PostTypeSupport; use MarkdownAlternate\TaxonomySupport; @@ -52,10 +53,11 @@ public function register(): void { add_action('parse_request', [$this, 'parse_markdown_url']); // Prevent canonical redirect for .md URLs add_filter('redirect_canonical', [$this, 'prevent_markdown_redirect'], 10, 2); - // Order: format parameter -> Accept negotiation -> markdown request (URL wins first) - add_action('template_redirect', [$this, 'handle_format_parameter'], 1); - add_action('template_redirect', [$this, 'handle_accept_negotiation'], 1); - add_action('template_redirect', [$this, 'handle_markdown_request'], 1); + // Run after default-priority access checks and canonical redirects. + // Keep format -> Accept -> .md ordering; explicit .md URLs win. + add_action( 'template_redirect', array( $this, 'handle_format_parameter' ), 15 ); + add_action( 'template_redirect', array( $this, 'handle_accept_negotiation' ), 15 ); + add_action( 'template_redirect', array( $this, 'handle_markdown_request' ), 15 ); } /** @@ -342,7 +344,7 @@ private function serve_markdown_response(WP_Post $post): void { } // Keep customer-dependent output out of HTTP and WordPress page caches too. - if ( ! apply_filters( 'markdown_alternate_cache_enabled', true, $post ) ) { + if ( ! CachePolicy::is_enabled( $post ) ) { nocache_headers(); header( 'Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0' ); if ( ! defined( 'DONOTCACHEPAGE' ) ) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fcb2218..f4fd9da 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,6 +16,7 @@ class WP_Post { public $post_title = 'Post Title'; public $post_content = '

Hello world.

'; public $post_type = 'post'; + public $post_password = ''; } class WP_Term { @@ -32,7 +33,9 @@ class WP_Error {} define( 'DAY_IN_SECONDS', 86400 ); function md_alt_test_reset() { + $_COOKIE = array(); $GLOBALS['md_alt_test_state'] = array( + 'logged_in' => false, 'filters' => array(), 'cache' => array(), 'expiration' => array(), @@ -47,6 +50,24 @@ function md_alt_test_reset() { ); } +function is_user_logged_in() { + return $GLOBALS['md_alt_test_state']['logged_in']; +} + +function add_action( $tag, $callback, $priority = 10, $accepted_args = 1 ) { + add_filter( $tag, $callback, $priority, $accepted_args ); +} + +function do_action( $tag, ...$args ) { + $priorities = $GLOBALS['md_alt_test_state']['filters'][ $tag ] ?? array(); + ksort( $priorities ); + foreach ( $priorities as $callbacks ) { + foreach ( $callbacks as list( $callback, $accepted_args ) ) { + $callback( ...array_slice( $args, 0, $accepted_args ) ); + } + } +} + function get_post( $post ) { return $post instanceof WP_Post ? $post : ( $GLOBALS['md_alt_test_state']['posts'][ $post ] ?? null ); } diff --git a/tests/router-request.php b/tests/router-request.php new file mode 100644 index 0000000..a0a6705 --- /dev/null +++ b/tests/router-request.php @@ -0,0 +1,148 @@ +post_password = 'dummy-password'; + } elseif ( 'filter-disabled' === $md_alt_mode ) { + add_filter( + 'markdown_alternate_cache_enabled', + static function () { + return false; + } + ); + } + + ob_start(); + register_shutdown_function( + static function () { + echo json_encode( + array( + 'body' => ob_get_clean(), + 'status' => $GLOBALS['md_alt_response_status'], + 'headers' => $GLOBALS['md_alt_response_headers'], + 'cache' => $GLOBALS['md_alt_test_state']['cache'], + 'renders' => $GLOBALS['md_alt_test_state']['renders'], + 'page_cache_disabled' => defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE, + ) + ); + } + ); + ( new \MarkdownAlternate\Router\RewriteHandler() )->register(); + if ( 'deny10' === $md_alt_mode || 'deny14' === $md_alt_mode ) { + add_action( + 'template_redirect', + static function () { + status_header( 403 ); + echo 'Access denied'; + exit; + }, + 'deny10' === $md_alt_mode ? 10 : 14 + ); + } elseif ( 'canonical' === $md_alt_mode ) { + add_action( + 'template_redirect', + static function () { + $target = 'https://example.com/post/' . ( 'format' === $GLOBALS['md_alt_route'] ? '?format=markdown' : '' ); + $target = apply_filters( 'redirect_canonical', $target, 'https://example.com/post' ); + if ( $target ) { + wp_safe_redirect( $target, 301 ); + exit; + } + }, + 10 + ); + } + do_action( 'template_redirect' ); + echo 'HTML template'; +} diff --git a/tests/run.php b/tests/run.php index cb41289..99ce7f2 100644 --- a/tests/run.php +++ b/tests/run.php @@ -449,6 +449,8 @@ static function () { md_alt_assert_same( $url, $integration->rewrite_post_link( $url, $post ), 'guard reset after exception' ); }; +require __DIR__ . '/security.php'; + $md_alt_failures = 0; foreach ( $md_alt_tests as $md_alt_name => $md_alt_test ) { try { diff --git a/tests/security.php b/tests/security.php new file mode 100644 index 0000000..ea4ec6b --- /dev/null +++ b/tests/security.php @@ -0,0 +1,205 @@ +Log out' + : 'Log in'; + } + ); + return $post; +} + +$md_alt_tests['A logged-in cache miss cannot leak a logout nonce to an anonymous visitor'] = static function () { + $post = md_alt_personalized_fixture(); + $renderer = new ContentRenderer(); + $GLOBALS['md_alt_test_state']['logged_in'] = true; + md_alt_assert_contains( 'private-test-nonce', $renderer->render( $post ) ); + md_alt_assert_same( false, get_transient( 'md_alt_cache_17' ) ); + $GLOBALS['md_alt_test_state']['logged_in'] = false; + $anonymous = $renderer->render( $post ); + md_alt_assert_contains( 'Log in', $anonymous ); + md_alt_assert_same( false, strpos( $anonymous, 'private-test-nonce' ) ); + md_alt_assert_same( $anonymous, $renderer->render( $post ) ); + md_alt_assert_same( 2, $GLOBALS['md_alt_test_state']['renders'] ); +}; + +$md_alt_tests['Logged-in requests neither read nor replace the anonymous cache, including after expiration'] = static function () { + $post = md_alt_personalized_fixture(); + $renderer = new ContentRenderer(); + $renderer->render( $post ); + $anonymous_cache = get_transient( 'md_alt_cache_17' ); + $GLOBALS['md_alt_test_state']['logged_in'] = true; + md_alt_assert_contains( 'private-test-nonce', $renderer->render( $post ) ); + md_alt_assert_same( $anonymous_cache, get_transient( 'md_alt_cache_17' ) ); + // WordPress returns false from get_transient() once an entry expires. + delete_transient( 'md_alt_cache_17' ); + md_alt_assert_contains( 'private-test-nonce', $renderer->render( $post ) ); + md_alt_assert_same( false, get_transient( 'md_alt_cache_17' ) ); + $GLOBALS['md_alt_test_state']['logged_in'] = false; + md_alt_assert_same( false, strpos( $renderer->render( $post ), 'private-test-nonce' ) ); +}; + +$md_alt_tests['Pre-fix caches with a matching integration version are discarded'] = static function () { + $post = md_alt_personalized_fixture(); + add_filter( + 'markdown_alternate_cache_version', + static function () { + return 'integration-v1'; + } + ); + set_transient( + 'md_alt_cache_17', + array( + 'markdown' => 'private-test-nonce', + 'modified' => $post->post_modified, + 'version' => 'integration-v1', + ), + DAY_IN_SECONDS + ); + $renderer = new ContentRenderer(); + $output = $renderer->render( $post ); + md_alt_assert_same( false, strpos( $output, 'private-test-nonce' ) ); + md_alt_assert_contains( 'Log in', $output ); + md_alt_assert_same( $output, $renderer->render( $post ) ); + md_alt_assert_same( 1, $GLOBALS['md_alt_test_state']['renders'] ); +}; + +$md_alt_tests['Known guest sessions bypass shared reads and writes'] = static function () { + foreach ( array( 'wp-postpass_test', 'wp_woocommerce_session_test', 'woocommerce_items_in_cart', 'woocommerce_cart_hash', 'edd_items_in_cart' ) as $cookie ) { + $post = md_alt_test_fixture( 'ordinary' ); + $renderer = new ContentRenderer(); + $renderer->render( $post ); + $cache = get_transient( 'md_alt_cache_17' ); + $_COOKIE[ $cookie ] = 'dummy-session'; + $post->post_content = '

Private guest content

'; + md_alt_assert_contains( 'Private guest content', $renderer->render( $post ) ); + md_alt_assert_same( $cache, get_transient( 'md_alt_cache_17' ), $cookie ); + delete_transient( 'md_alt_cache_17' ); + $renderer->render( $post ); + md_alt_assert_same( false, get_transient( 'md_alt_cache_17' ), $cookie ); + } +}; + +$md_alt_tests['Cache filters cannot opt authenticated or password-protected output back into sharing'] = static function () { + $post = md_alt_test_fixture( 'ordinary' ); + add_filter( + 'markdown_alternate_cache_enabled', + static function () { + return true; + } + ); + $GLOBALS['md_alt_test_state']['logged_in'] = true; + md_alt_assert_same( false, CachePolicy::is_enabled( $post ) ); + $GLOBALS['md_alt_test_state']['logged_in'] = false; + $post->post_password = 'dummy-password'; + md_alt_assert_same( false, CachePolicy::is_enabled( $post ) ); + ( new ContentRenderer() )->render( $post ); + md_alt_assert_same( false, get_transient( 'md_alt_cache_17' ) ); +}; + +$md_alt_tests['Unrelated cookies retain caching and integrations can disable custom sessions'] = static function () { + $post = md_alt_test_fixture( 'ordinary' ); + $_COOKIE['wordpress_test_cookie'] = 'test'; + md_alt_assert_same( true, CachePolicy::is_enabled( $post ) ); + add_filter( + 'markdown_alternate_cache_enabled', + static function () { + return false; + } + ); + md_alt_assert_same( false, CachePolicy::is_enabled( $post ) ); +}; + +/** + * Run a terminating router request in its own process. + * + * @param string $route Request route. + * @param string $mode Visitor or redirect scenario. + * @return array Captured response. + */ +function md_alt_router_request( $route, $mode ) { + $command = escapeshellarg( PHP_BINARY ) . ' ' . escapeshellarg( __DIR__ . '/router-request.php' ) . ' ' . escapeshellarg( $route ) . ' ' . escapeshellarg( $mode ); + $descriptors = array( + 1 => array( 'pipe', 'w' ), + 2 => array( 'pipe', 'w' ), + ); + $process = proc_open( $command, $descriptors, $pipes ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_proc_open -- Isolate exit() in a CLI test child. + $output = stream_get_contents( $pipes[1] ); + $error = stream_get_contents( $pipes[2] ); + fclose( $pipes[1] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose + fclose( $pipes[2] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose + md_alt_assert_same( 0, proc_close( $process ), $error ); + $response = json_decode( $output, true ); + md_alt_assert_same( true, is_array( $response ), $output ); + return $response; +} + +foreach ( array( 'md', 'format', 'accept' ) as $md_alt_route ) { + $md_alt_tests[ 'Access checks run before ' . $md_alt_route . ' responses' ] = static function () use ( $md_alt_route ) { + foreach ( array( 'deny10', 'deny14' ) as $mode ) { + $response = md_alt_router_request( $md_alt_route, $mode ); + md_alt_assert_same( 403, $response['status'] ); + md_alt_assert_same( 'Access denied', $response['body'] ); + md_alt_assert_same( 0, $response['renders'] ); + md_alt_assert_same( array(), $response['cache'] ); + } + }; +} + +foreach ( array( 'md', 'format' ) as $md_alt_route ) { + $md_alt_tests[ 'Private HTTP and transient cache policy agree for ' . $md_alt_route ] = static function () use ( $md_alt_route ) { + foreach ( array( 'logged-in', 'session', 'password', 'filter-disabled' ) as $mode ) { + $response = md_alt_router_request( $md_alt_route, $mode ); + md_alt_assert_same( 200, $response['status'], $mode ); + md_alt_assert_contains( 'private, no-store', $response['headers']['Cache-Control'] ); + md_alt_assert_same( true, $response['page_cache_disabled'] ); + md_alt_assert_same( array(), $response['cache'] ); + } + $response = md_alt_router_request( $md_alt_route, 'anonymous' ); + md_alt_assert_same( 200, $response['status'] ); + md_alt_assert_same( 'text/markdown; charset=UTF-8', $response['headers']['Content-Type'] ); + md_alt_assert_same( true, isset( $response['cache']['md_alt_cache_17'] ) ); + }; +} + +$md_alt_tests['Canonical redirects precede format and Accept handling, but do not replace .md URLs'] = static function () { + foreach ( array( 'format', 'accept' ) as $route ) { + $response = md_alt_router_request( $route, 'canonical' ); + md_alt_assert_same( 301, $response['status'] ); + md_alt_assert_contains( '/post/', $response['headers']['Location'] ); + md_alt_assert_same( 0, $response['renders'] ); + } + $response = md_alt_router_request( 'md', 'canonical' ); + md_alt_assert_same( 200, $response['status'] ); + md_alt_assert_contains( '# Post Title', $response['body'] ); +}; + +$md_alt_tests['Accept negotiation and explicit format precedence remain unchanged'] = static function () { + $response = md_alt_router_request( 'accept', 'anonymous' ); + md_alt_assert_same( 303, $response['status'] ); + md_alt_assert_same( 'https://example.com/post.md', $response['headers']['Location'] ); + md_alt_assert_same( 0, $response['renders'] ); + $response = md_alt_router_request( 'format', 'also-accept' ); + md_alt_assert_same( 200, $response['status'] ); + md_alt_assert_contains( '# Post Title', $response['body'] ); + $response = md_alt_router_request( 'html', 'anonymous' ); + md_alt_assert_same( 'HTML template', $response['body'] ); + md_alt_assert_same( 0, $response['renders'] ); +}; From a25975a843315084a317da995538307570dec832 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Wed, 25 Nov 2026 20:26:23 +0100 Subject: [PATCH 2/2] Apply visitor cache policy to taxonomy archives --- src/Output/CachePolicy.php | 43 +++++++++++++++++++++++------------ src/Router/RewriteHandler.php | 2 +- tests/router-request.php | 9 ++++++++ tests/term-integration.php | 19 ++++++++++++++++ 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/Output/CachePolicy.php b/src/Output/CachePolicy.php index bc7e16b..d679d19 100644 --- a/src/Output/CachePolicy.php +++ b/src/Output/CachePolicy.php @@ -21,7 +21,33 @@ class CachePolicy { * @return bool Whether shared caching is safe for this request. */ public static function is_enabled( WP_Post $post ): bool { - if ( is_user_logged_in() || '' !== $post->post_password ) { + if ( ! self::is_public_request() || '' !== $post->post_password ) { + return false; + } + + /** + * Filters whether otherwise eligible output can use shared caching. + * + * Disable for custom sessions or other visitor-dependent content. + * The HTTP router also uses this to send private, no-store headers. + * Logged-in visitors, password-protected posts and known session + * cookies always bypass caching, regardless of this filter. + * + * @since 1.2.0 + * + * @param bool $enabled Whether output may be cached. Default true. + * @param WP_Post $post The post being rendered. + */ + return (bool) apply_filters( 'markdown_alternate_cache_enabled', true, $post ); + } + + /** + * Whether a post or archive request has no known visitor-specific state. + * + * @return bool Whether the request is eligible for shared caching. + */ + public static function is_public_request(): bool { + if ( is_user_logged_in() ) { return false; } @@ -41,19 +67,6 @@ public static function is_enabled( WP_Post $post ): bool { } } - /** - * Filters whether otherwise eligible output can use shared caching. - * - * Disable for custom sessions or other visitor-dependent content. - * The HTTP router also uses this to send private, no-store headers. - * Logged-in visitors, password-protected posts and known session - * cookies always bypass caching, regardless of this filter. - * - * @since 1.2.0 - * - * @param bool $enabled Whether output may be cached. Default true. - * @param WP_Post $post The post being rendered. - */ - return (bool) apply_filters( 'markdown_alternate_cache_enabled', true, $post ); + return true; } } diff --git a/src/Router/RewriteHandler.php b/src/Router/RewriteHandler.php index cf15403..f2291ba 100644 --- a/src/Router/RewriteHandler.php +++ b/src/Router/RewriteHandler.php @@ -265,7 +265,7 @@ private function serve_term_response( WP_Term $term ): void { * @param bool $enabled Whether output may be cached. Default true. * @param WP_Term $term The term being served. */ - if ( ! apply_filters( 'markdown_alternate_term_cache_enabled', true, $term ) ) { + if ( ! CachePolicy::is_public_request() || ! apply_filters( 'markdown_alternate_term_cache_enabled', true, $term ) ) { nocache_headers(); header( 'Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0' ); if ( ! defined( 'DONOTCACHEPAGE' ) ) { diff --git a/tests/router-request.php b/tests/router-request.php index a0a6705..9c48f75 100644 --- a/tests/router-request.php +++ b/tests/router-request.php @@ -42,6 +42,15 @@ function get_queried_object() { function is_singular() { return true; } + function is_category() { + return false; + } + function is_tag() { + return false; + } + function is_tax() { + return false; + } function get_post_status( $post ) { return 'publish'; } diff --git a/tests/term-integration.php b/tests/term-integration.php index 03087c6..cf41b6b 100644 --- a/tests/term-integration.php +++ b/tests/term-integration.php @@ -90,6 +90,25 @@ function md_alt_term_assert( $condition, $message ) { ) ); md_alt_term_assert( $md_alt_term_post_id > 0, 'Archive regression fixture created' ); + if ( '1' === getenv( 'MD_ALT_TEST_HTTP' ) ) { + $md_alt_admin_ids = get_users( + array( + 'role' => 'administrator', + 'number' => 1, + 'fields' => 'ID', + ) + ); + md_alt_term_assert( ! empty( $md_alt_admin_ids ), 'HTTP cache tests have an administrator fixture' ); + $md_alt_auth_cookie = wp_generate_auth_cookie( (int) $md_alt_admin_ids[0], time() + 600, 'logged_in' ); + foreach ( array( array( LOGGED_IN_COOKIE => $md_alt_auth_cookie ), array( 'wp_woocommerce_session_fixture' => 'test-session' ) ) as $md_alt_cookies ) { + $md_alt_response = wp_remote_get( + UrlConverter::convert_to_markdown_url( get_term_link( $md_alt_term_child ) ), + array( 'cookies' => $md_alt_cookies ) + ); + md_alt_term_assert( 200 === wp_remote_retrieve_response_code( $md_alt_response ), 'Visitor-specific archive request returns Markdown' ); + md_alt_term_assert( false !== strpos( wp_remote_retrieve_header( $md_alt_response, 'cache-control' ), 'private, no-store' ), 'Visitor-specific archives forbid shared HTTP caching' ); + } + } // Exercise the public request parser with root and subdirectory home URLs. foreach ( array( false, true ) as $md_alt_subdirectory ) {