Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ It also checks the shared `--prpl-*` names against `tokens.json` in
at a checkout of it (`export YOUR_BRANDING_DIR=~/path/to/branding-api`). A name the plugin reads
must be listed there; without the variable the check is skipped.

## Authentication transport rollout

The compatibility release accepts `X-Progress-Planner-Token` on `/get-stats` and
`/tasks`, while retaining the legacy stats URLs (`/get-stats/token/<key>` and
`/get-stats/<key>`) and `token` query parameter. A supplied custom header takes
precedence: an empty or incorrect header is rejected even with a valid URL token.
`Authorization: Bearer` is not used for Progress Planner authentication.

1. Ship this compatibility release first and verify its installation on every
site the server will switch. Old server requests must continue to work, and a
header-only statistics request must succeed without a key in the URL. An
unreachable or older site is not ready for the switch.
2. Deploy the server security and custom-header changes together
([server #68](https://github.com/ProgressPlanner/progress-planner-server/pull/68)
and [server #69](https://github.com/ProgressPlanner/progress-planner-server/pull/69)).
Verify statistics retrieval through the server. If it fails, the server can
return to its previous URL transport while the compatibility plugin is installed.
3. After all server callers use the custom header, ship the strict plugin changes
([#802](https://github.com/ProgressPlanner/progress-planner/pull/802) and
[#803](https://github.com/ProgressPlanner/progress-planner/pull/803)). Remove the
URL fallback and legacy route, and change the compatibility tests to expect
legacy authentication to fail. Reconcile these overlapping REST changes when
updating those branches. After this stage, reverting the server to URL tokens
requires restoring the compatibility plugin first.

This is a transport bridge, not key rotation or the full security release.
Existing credentials are unchanged. Preparing this branch does not deploy it or
establish that installed sites have received it.

## Branches on this repository

We use a couple of branches in this repository to keep things clean:
Expand Down
92 changes: 88 additions & 4 deletions classes/rest/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,65 @@
*/
abstract class Base {

/**
* HTTP header shared with the server's new credential transport.
*/
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;
}

/**
Expand Down Expand Up @@ -59,13 +113,45 @@ protected function get_client_ip() {
}

/**
* Validate the token.
* Authenticate with the new header or a legacy URL token during rollout.
*
* An explicitly supplied header always wins, including an empty or invalid one.
* Remove the URL fallback after the server and installed plugins use the header.
*
* @param \WP_REST_Request $request The REST request.
* @return bool
*/
public function permissions_check( $request ) {
$token = $request->get_header( self::TOKEN_HEADER );
if ( null !== $token ) {
return $this->validate_token( \trim( $token ) );
}

$token = $request->get_param( 'token' );
if ( ! \is_string( $token ) ) {
return false;
}

// The old stats route captures "token/<key>" as one route parameter.
if ( 0 === \strpos( $token, 'token/' ) ) {
$token = \substr( $token, 6 );
}

return $this->validate_token( $token );
}

/**
* Validate the complete credential, without transport-specific prefixes.
*
* @param string $token The token.
* @param mixed $token The token.
*
* @return bool
*/
public function validate_token( $token ) {
if ( ! \is_string( $token ) || '' === $token ) {
return false;
}

// Rate limiting: Check for too many failed attempts.
$ip_address = $this->get_client_ip();
$rate_limit_key = 'prpl_api_rate_limit_' . \md5( $ip_address );
Expand All @@ -76,8 +162,6 @@ public function validate_token( $token ) {
return false;
}

$token = \str_replace( 'token/', '', $token );

// Check test token first (use hash_equals to prevent timing attacks).
$test_token = \get_option( 'progress_planner_test_token', '' );
if ( ! empty( $test_token ) && \hash_equals( $test_token, $token ) ) {
Expand Down
34 changes: 18 additions & 16 deletions classes/rest/class-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Progress_Planner REST-API.
*
* Adds a REST-API endpoint to get stats, in a URL like:
* <site-url>/wp-json/progress-planner/v1/get-stats/token/<site-token>
* <site-url>/wp-json/progress-planner/v1/get-stats (X-Progress-Planner-Token header)
* Legacy URLs with a token remain available during the transport rollout.
*
* The token is generated and saved in the settings.
*
Expand All @@ -19,29 +20,30 @@
*/
class Stats extends Base {

/**
* Route of the endpoint, relative to the namespace.
*/
const ROUTE = '/get-stats';

/**
* Register the REST-API endpoint.
*
* @return void
*/
public function register_rest_endpoint() {
\register_rest_route(
'progress-planner/v1',
'/get-stats/(?P<token>\S+)',
[
foreach ( [ '/get-stats', '/get-stats/(?P<token>\S+)' ] as $route ) {
\register_rest_route(
'progress-planner/v1',
$route,
[
'methods' => 'GET',
'callback' => [ $this, 'get_stats' ],
'permission_callback' => '__return_true',
'args' => [
'token' => [
'required' => true,
'validate_callback' => [ $this, 'validate_token' ],
],
[
'methods' => 'GET',
'callback' => [ $this, 'get_stats' ],
'permission_callback' => [ $this, 'permissions_check' ],
],
],
]
);
]
);
}
}

/**
Expand Down
13 changes: 6 additions & 7 deletions classes/rest/class-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
class Tasks extends Base {

/**
* Route of the endpoint, relative to the namespace.
*/
const ROUTE = '/tasks';

/**
* Register the REST-API endpoint.
*
Expand All @@ -28,13 +33,7 @@ public function register_rest_endpoint() {
[
'methods' => 'GET',
'callback' => [ $this, 'get_tasks' ],
'permission_callback' => '__return_true',
'args' => [
'token' => [
'required' => true,
'validate_callback' => [ $this, 'validate_token' ],
],
],
'permission_callback' => [ $this, 'permissions_check' ],
],
]
);
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/test-class-rest-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function test_validate_token_test_token() {
}

/**
* Test validate_token with token containing "token/" prefix.
* Token validation does not strip a transport prefix from the credential.
*
* @return void
*/
Expand All @@ -270,7 +270,7 @@ public function test_validate_token_with_prefix() {

$result = $this->rest_instance->validate_token( $token );

$this->assertTrue( $result );
$this->assertFalse( $result );
}

/**
Expand Down
Loading
Loading