From 92de302c6075439fe8d376b526b8104f307513c7 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Mon, 16 Nov 2026 11:57:37 +0100 Subject: [PATCH 1/2] Read the site token from X-Progress-Planner-Token instead of Authorization Bearer-auth plugins such as JWT Authentication for WP REST API decode every Authorization: Bearer header and reject the request before our permission check runs, so stats and task sync would fail on those sites. A dedicated header avoids that, and Apache does not strip it, so the getallheaders() fallback is no longer needed. Co-Authored-By: Claude Opus 5.5 --- classes/rest/class-base.php | 32 ++++++-------- classes/rest/class-stats.php | 2 +- tests/e2e/api/tasks.api.ts | 2 +- .../test-rest-api-stats-integration.php | 6 +-- .../test-rest-api-tasks-integration.php | 6 +-- tests/phpunit/test-class-api-get-stats.php | 2 +- tests/phpunit/test-class-rest-base.php | 42 +++++-------------- tests/phpunit/test-class-security-review.php | 8 ++-- 8 files changed, 35 insertions(+), 65 deletions(-) diff --git a/classes/rest/class-base.php b/classes/rest/class-base.php index 102c77259..eaa739f04 100644 --- a/classes/rest/class-base.php +++ b/classes/rest/class-base.php @@ -12,6 +12,11 @@ */ abstract class Base { + /** + * HTTP header that carries the site token. + */ + const TOKEN_HEADER = 'X-Progress-Planner-Token'; + /** * Constructor. */ @@ -40,31 +45,18 @@ protected function get_client_ip() { } /** - * Authenticate a request using a bearer token, never a URL parameter. + * Authenticate a request using the site token header, never a URL parameter. + * + * A dedicated header, not `Authorization: Bearer`: bearer-auth plugins (JWT + * and OAuth) try to decode every bearer token and reject the request before + * this check runs, and some Apache setups strip `Authorization` entirely. * * @param \WP_REST_Request $request The REST request. * @return bool */ public function permissions_check( $request ) { - $authorization = $request->get_header( 'authorization' ); - // Apache may expose Authorization only through its original request headers - // when WordPress's rewrite rules have not populated HTTP_AUTHORIZATION. - if ( null === $authorization ) { - $headers = \array_change_key_case( $this->get_http_request_headers(), CASE_LOWER ); - $authorization = $headers['authorization'] ?? null; - } - return \is_string( $authorization ) - && 1 === \preg_match( '/^Bearer ([^\s]+)$/iD', $authorization, $matches ) - && $this->validate_token( $matches[1] ); - } - - /** - * Read original HTTP headers when the web server exposes them separately. - * - * @return array - */ - protected function get_http_request_headers() { - return \function_exists( 'getallheaders' ) ? \getallheaders() : []; + $token = $request->get_header( self::TOKEN_HEADER ); + return \is_string( $token ) && $this->validate_token( \trim( $token ) ); } /** diff --git a/classes/rest/class-stats.php b/classes/rest/class-stats.php index 7958f286c..80307f18a 100644 --- a/classes/rest/class-stats.php +++ b/classes/rest/class-stats.php @@ -3,7 +3,7 @@ * Progress_Planner REST-API. * * Adds a REST-API endpoint to get stats, in a URL like: - * /wp-json/progress-planner/v1/get-stats (Authorization: Bearer ) + * /wp-json/progress-planner/v1/get-stats (X-Progress-Planner-Token: ) * * The token is generated and saved in the settings. * diff --git a/tests/e2e/api/tasks.api.ts b/tests/e2e/api/tasks.api.ts index ee192a3bc..08d06440e 100644 --- a/tests/e2e/api/tasks.api.ts +++ b/tests/e2e/api/tasks.api.ts @@ -49,7 +49,7 @@ export class TasksApi { { headers: { 'Content-Type': 'application/json', - Authorization: `Bearer ${ testToken }`, + 'X-Progress-Planner-Token': testToken, }, } ); diff --git a/tests/phpunit/integration/test-rest-api-stats-integration.php b/tests/phpunit/integration/test-rest-api-stats-integration.php index c5550dff1..c4431994d 100644 --- a/tests/phpunit/integration/test-rest-api-stats-integration.php +++ b/tests/phpunit/integration/test-rest-api-stats-integration.php @@ -30,7 +30,7 @@ public function test_get_stats_endpoint_with_valid_token() { '/progress-planner/v1/get-stats', 'GET', [], - [ 'Authorization' => 'Bearer ' . $license_key ] + [ 'X-Progress-Planner-Token' => $license_key ] ); $this->assertNotInstanceOf( \WP_Error::class, $response ); @@ -62,7 +62,7 @@ public function test_get_stats_endpoint_with_invalid_token() { '/progress-planner/v1/get-stats', 'GET', [], - [ 'Authorization' => 'Bearer invalid-token' ] + [ 'X-Progress-Planner-Token' => 'invalid-token' ] ); // Should return error due to validation failure. @@ -89,7 +89,7 @@ public function test_get_stats_endpoint_includes_system_data() { '/progress-planner/v1/get-stats', 'GET', [], - [ 'Authorization' => 'Bearer ' . $license_key ] + [ 'X-Progress-Planner-Token' => $license_key ] ); $this->assertInstanceOf( \WP_REST_Response::class, $response ); diff --git a/tests/phpunit/integration/test-rest-api-tasks-integration.php b/tests/phpunit/integration/test-rest-api-tasks-integration.php index 56765340e..8e98857b3 100644 --- a/tests/phpunit/integration/test-rest-api-tasks-integration.php +++ b/tests/phpunit/integration/test-rest-api-tasks-integration.php @@ -33,7 +33,7 @@ public function test_get_tasks_endpoint_with_valid_token() { '/progress-planner/v1/tasks', 'GET', [], - [ 'Authorization' => 'Bearer ' . $license_key ] + [ 'X-Progress-Planner-Token' => $license_key ] ); $this->assertNotInstanceOf( \WP_Error::class, $response ); @@ -63,7 +63,7 @@ public function test_get_tasks_endpoint_with_invalid_token() { '/progress-planner/v1/tasks', 'GET', [], - [ 'Authorization' => 'Bearer invalid-token' ] + [ 'X-Progress-Planner-Token' => 'invalid-token' ] ); // Missing or invalid credentials must not expose tasks. @@ -105,7 +105,7 @@ public function test_get_tasks_endpoint_includes_all_statuses() { '/progress-planner/v1/tasks', 'GET', [], - [ 'Authorization' => 'Bearer ' . $license_key ] + [ 'X-Progress-Planner-Token' => $license_key ] ); $this->assertInstanceOf( \WP_REST_Response::class, $response ); diff --git a/tests/phpunit/test-class-api-get-stats.php b/tests/phpunit/test-class-api-get-stats.php index de8b29eb4..22f83d8bc 100644 --- a/tests/phpunit/test-class-api-get-stats.php +++ b/tests/phpunit/test-class-api-get-stats.php @@ -109,7 +109,7 @@ public function tearDown(): void { */ public function testEndpoint() { $request = new WP_REST_Request( 'GET', '/progress-planner/v1/get-stats' ); - $request->set_header( 'Authorization', 'Bearer ' . $this->token ); + $request->set_header( 'X-Progress-Planner-Token', $this->token ); $response = $this->server->dispatch( $request ); // Check if the response is successful. diff --git a/tests/phpunit/test-class-rest-base.php b/tests/phpunit/test-class-rest-base.php index 9e98fae63..c037bf84d 100644 --- a/tests/phpunit/test-class-rest-base.php +++ b/tests/phpunit/test-class-rest-base.php @@ -33,44 +33,22 @@ public function setUp(): void { } /** - * Apache's original headers authenticate only when REST has no header. + * Only the dedicated token header authenticates; a bearer token is never read, + * so bearer-auth plugins and the site token cannot interfere with each other. * * @return void */ - public function test_original_authorization_header_fallback() { + public function test_only_the_token_header_authenticates() { \update_option( 'progress_planner_license_key', 'fixture-license' ); \delete_transient( 'prpl_api_rate_limit_' . \md5( '0.0.0.0' ) ); unset( $_SERVER['REMOTE_ADDR'] ); - $api = $this->getMockBuilder( Tasks::class ) - ->disableOriginalConstructor() - ->onlyMethods( [ 'get_http_request_headers' ] ) - ->getMock(); - $api->method( 'get_http_request_headers' )->willReturn( [ 'aUtHoRiZaTiOn' => 'Bearer fixture-license' ] ); $request = new \WP_REST_Request( 'GET', '/progress-planner/v1/tasks' ); - $this->assertTrue( $api->permissions_check( $request ) ); - $request->set_header( 'Authorization', 'Bearer wrong-key' ); - $this->assertFalse( $api->permissions_check( $request ) ); - } - - /** - * A raw header fallback still rejects a bad token and URL credentials. - * - * @return void - */ - public function test_original_headers_do_not_bypass_token_validation() { - \update_option( 'progress_planner_license_key', 'fixture-license' ); - $api = $this->getMockBuilder( Tasks::class ) - ->disableOriginalConstructor() - ->onlyMethods( [ 'get_http_request_headers' ] ) - ->getMock(); - $api->method( 'get_http_request_headers' )->willReturnOnConsecutiveCalls( - [ 'Authorization' => 'Bearer wrong-key' ], - [] - ); - $request = new \WP_REST_Request( 'GET', '/progress-planner/v1/tasks' ); - $request->set_param( 'token', 'fixture-license' ); - $this->assertFalse( $api->permissions_check( $request ) ); - $this->assertFalse( $api->permissions_check( $request ) ); + $request->set_header( 'Authorization', 'Bearer fixture-license' ); + $this->assertFalse( $this->rest_instance->permissions_check( $request ) ); + $request->set_header( 'x-progress-planner-token', 'fixture-license' ); + $this->assertTrue( $this->rest_instance->permissions_check( $request ) ); + $request->set_header( 'X-Progress-Planner-Token', 'wrong-key' ); + $this->assertFalse( $this->rest_instance->permissions_check( $request ) ); } /** @@ -291,7 +269,7 @@ public function test_validate_token_test_token() { } /** - * Token validation compares the entire bearer token, including any prefix. + * Token validation compares the entire token, including any prefix. * * @return void */ diff --git a/tests/phpunit/test-class-security-review.php b/tests/phpunit/test-class-security-review.php index 27d8c7c81..d24b60337 100644 --- a/tests/phpunit/test-class-security-review.php +++ b/tests/phpunit/test-class-security-review.php @@ -89,19 +89,19 @@ public function test_spoofed_headers_cannot_bypass_rate_limit() { } /** - * Bearer authentication works without credentials in the URL. + * Header authentication works without credentials in the URL. * * @return void */ - public function test_bearer_authentication() { + public function test_token_header_authentication() { \update_option( 'progress_planner_license_key', 'fixture-license' ); \wp_set_current_user( 0 ); foreach ( [ '/tasks', '/get-stats' ] as $endpoint ) { $request = new \WP_REST_Request( 'GET', '/progress-planner/v1' . $endpoint ); $this->assertSame( 401, \rest_do_request( $request )->get_status() ); - $request->set_header( 'Authorization', 'Bearer fixture-license' ); + $request->set_header( 'X-Progress-Planner-Token', 'fixture-license' ); $this->assertSame( 200, \rest_do_request( $request )->get_status() ); - $request->set_header( 'Authorization', 'Bearer incorrect' ); + $request->set_header( 'X-Progress-Planner-Token', 'incorrect' ); $request->set_param( 'token', 'fixture-license' ); $this->assertSame( 401, \rest_do_request( $request )->get_status() ); } From ffda13addb6abbbc3cab79104022073da2ba7c14 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Tue, 17 Nov 2026 13:36:35 +0100 Subject: [PATCH 2/2] Keep token-authenticated REST responses out of caches With the X-Progress-Planner-Token header every caller requests the same /get-stats and /tasks URL. WordPress sends no-cache headers only to logged-in users, and the server is not logged in, so a page cache could store an authenticated response and serve it to anyone, or hand the server stale data. LiteSpeed Cache caches REST responses for guests by default for seven days. Send Cache-Control: no-store, private on every response from these routes, including rejected ones, and call LiteSpeed Cache's litespeed_control_set_nocache hook because its own cache header overrides the standard one. Same change as on ProgressPlanner/progress-planner#805. Co-Authored-By: Claude Opus 5.5 --- classes/rest/class-base.php | 49 +++++++ classes/rest/class-stats.php | 5 + classes/rest/class-tasks.php | 5 + tests/phpunit/test-rest-prevent-caching.php | 143 ++++++++++++++++++++ 4 files changed, 202 insertions(+) create mode 100644 tests/phpunit/test-rest-prevent-caching.php diff --git a/classes/rest/class-base.php b/classes/rest/class-base.php index eaa739f04..ff0c33b4c 100644 --- a/classes/rest/class-base.php +++ b/classes/rest/class-base.php @@ -17,11 +17,60 @@ abstract class Base { */ const TOKEN_HEADER = 'X-Progress-Planner-Token'; + /** + * REST namespace of the token-authenticated endpoints. + */ + const REST_NAMESPACE = '/progress-planner/v1'; + + /** + * Route of the endpoint, relative to the namespace. Empty means no route. + */ + const ROUTE = ''; + /** * Constructor. */ public function __construct() { \add_action( 'rest_api_init', [ $this, 'register_rest_endpoint' ] ); + \add_filter( 'rest_post_dispatch', [ $this, 'prevent_caching' ], 10, 3 ); + } + + /** + * Keep responses of this endpoint out of every cache. + * + * With header authentication every caller requests the same URL. A cache + * that stored an authenticated response would hand it to anyone requesting + * that URL, and the server would get stale data back. WordPress only sends + * no-cache headers to logged-in users, and the server is not logged in. + * Runs for successful and rejected requests alike. + * + * @param mixed $response The response. + * @param mixed $server The REST server. + * @param mixed $request The request. + * + * @return mixed + */ + public function prevent_caching( $response, $server, $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed + if ( '' === static::ROUTE + || ! $response instanceof \WP_HTTP_Response + || ! $request instanceof \WP_REST_Request + ) { + return $response; + } + + $route = self::REST_NAMESPACE . static::ROUTE; + if ( $route !== $request->get_route() && 0 !== \strpos( $request->get_route(), $route . '/' ) ) { + return $response; + } + + $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, private' ); + $response->header( 'Expires', 'Wed, 11 Jan 1984 05:00:00 GMT' ); + + // LiteSpeed Cache caches REST responses for guests by default and sends + // its own cache header, so the headers above alone do not stop it. + \do_action( 'litespeed_control_set_nocache', 'Progress Planner token-authenticated endpoint' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's public API. + + return $response; } /** diff --git a/classes/rest/class-stats.php b/classes/rest/class-stats.php index 80307f18a..50c493137 100644 --- a/classes/rest/class-stats.php +++ b/classes/rest/class-stats.php @@ -19,6 +19,11 @@ */ class Stats extends Base { + /** + * Route of the endpoint, relative to the namespace. + */ + const ROUTE = '/get-stats'; + /** * Register the REST-API endpoint. * diff --git a/classes/rest/class-tasks.php b/classes/rest/class-tasks.php index 14285937d..c0883178d 100644 --- a/classes/rest/class-tasks.php +++ b/classes/rest/class-tasks.php @@ -15,6 +15,11 @@ */ class Tasks extends Base { + /** + * Route of the endpoint, relative to the namespace. + */ + const ROUTE = '/tasks'; + /** * Register the REST-API endpoint. * diff --git a/tests/phpunit/test-rest-prevent-caching.php b/tests/phpunit/test-rest-prevent-caching.php new file mode 100644 index 000000000..d6708fc7b --- /dev/null +++ b/tests/phpunit/test-rest-prevent-caching.php @@ -0,0 +1,143 @@ +assertNotFalse( \has_filter( 'rest_post_dispatch', [ \progress_planner()->get_rest__stats(), 'prevent_caching' ] ) ); + $this->assertNotFalse( \has_filter( 'rest_post_dispatch', [ \progress_planner()->get_rest__tasks(), 'prevent_caching' ] ) ); + } + + /** + * Accepted and rejected requests both get no-cache headers and the LiteSpeed signal. + * + * @dataProvider endpoint_requests + * + * @param string $class_name The endpoint class. + * @param string $route The route. + * @param string $token The token header value, or '' for none. + * @param int $status The expected status. + * + * @return void + */ + public function test_endpoint_responses_are_not_cacheable( $class_name, $route, $token, $status ) { + global $wp_rest_server; + $previous_server = $wp_rest_server; + \wp_set_current_user( 0 ); + \update_option( 'progress_planner_license_key', 'fixture-key' ); + \delete_option( 'progress_planner_test_token' ); + + // Only the response callback is mocked; routing, authentication and the filter are real. + $callback = Stats::class === $class_name ? 'get_stats' : 'get_tasks'; + $api = $this->getMockBuilder( $class_name ) + ->disableOriginalConstructor() + ->onlyMethods( [ $callback ] ) + ->getMock(); + $api->method( $callback )->willReturn( new \WP_REST_Response( [ 'authenticated' => true ] ) ); + + $request = new \WP_REST_Request( 'GET', '/progress-planner/v1' . $route ); + if ( '' !== $token ) { + $request->set_header( 'X-Progress-Planner-Token', $token ); + } + + $register = function () use ( $api ) { + global $wp_rest_server; + // Keep only this fixture's routes, while still registering on the real hook. + $wp_rest_server = new \WP_REST_Server(); + $api->register_rest_endpoint(); + }; + \add_action( 'rest_api_init', $register, PHP_INT_MAX ); + + try { + \do_action( 'rest_api_init', $wp_rest_server ); + $response = $wp_rest_server->dispatch( $request ); + + $litespeed = \did_action( 'litespeed_control_set_nocache' ); + $response = $api->prevent_caching( $response, $wp_rest_server, $request ); + } finally { + \remove_action( 'rest_api_init', $register, PHP_INT_MAX ); + $wp_rest_server = $previous_server; + } + + $this->assertSame( $status, $response->get_status() ); + $headers = $response->get_headers(); + $this->assertStringContainsString( 'no-store', $headers['Cache-Control'] ?? '' ); + $this->assertStringContainsString( 'private', $headers['Cache-Control'] ?? '' ); + $this->assertSame( $litespeed + 1, \did_action( 'litespeed_control_set_nocache' ) ); + } + + /** + * Accepted and rejected requests to both endpoints. + * + * @return array + */ + public static function endpoint_requests() { + return [ + 'stats accepted' => [ Stats::class, '/get-stats', 'fixture-key', 200 ], + 'stats wrong token' => [ Stats::class, '/get-stats', 'wrong', 401 ], + 'stats no token' => [ Stats::class, '/get-stats', '', 401 ], + 'tasks accepted' => [ Tasks::class, '/tasks', 'fixture-key', 200 ], + 'tasks wrong token' => [ Tasks::class, '/tasks', 'wrong', 401 ], + 'tasks no token' => [ Tasks::class, '/tasks', '', 401 ], + ]; + } + + /** + * Other routes, including look-alikes, keep their own cache headers. + * + * @dataProvider other_routes + * + * @param string $class_name The endpoint class. + * @param string $route The requested route. + * + * @return void + */ + public function test_prevent_caching_leaves_other_routes_alone( $class_name, $route ) { + $api = $this->getMockBuilder( $class_name ) + ->disableOriginalConstructor() + ->onlyMethods( [] ) + ->getMock(); + + $litespeed = \did_action( 'litespeed_control_set_nocache' ); + $response = $api->prevent_caching( + new \WP_REST_Response( [] ), + \rest_get_server(), + new \WP_REST_Request( 'GET', $route ) + ); + + $this->assertArrayNotHasKey( 'Cache-Control', $response->get_headers() ); + $this->assertSame( $litespeed, \did_action( 'litespeed_control_set_nocache' ) ); + } + + /** + * Routes the no-cache filter must not touch. + * + * @return array + */ + public static function other_routes() { + return [ + 'core posts route' => [ Stats::class, '/wp/v2/posts' ], + 'other plugin route' => [ Stats::class, '/progress-planner/v1/recommendations' ], + 'look-alike stats route' => [ Stats::class, '/progress-planner/v1/get-stats-extra' ], + 'look-alike tasks route' => [ Tasks::class, '/progress-planner/v1/tasks-extra' ], + 'other endpoint route' => [ Tasks::class, '/progress-planner/v1/get-stats' ], + ]; + } +}