.. * sig = base64url( HMAC-SHA256( token, "equalsites-baseline\n1\n * \n\n\n" ) ) * * The token itself never travels. Expiry, site id and path are all inside the * signed message, so a captured value cannot be extended, pointed at another * page, or used against another site — and it is spendable once. Any value that * fails any of these checks is not an error and not a hint: the page renders * exactly as it would have without the parameter. * * Signature checking runs anywhere; only one-time use needs WordPress (transient * storage), which is why the CLI harness stubs those two functions. * * The reference implementation of the same rules lives in the SaaS repo at * packages/api/src/baseline-bypass.ts; both sides are asserted against the same * vectors so they cannot drift. The HMAC message domain ("equalsites-baseline") * is shared, fixed salt, not a wire field name — it stays as-is on both sides. */ class EqualSites_Baseline_Bypass { const PARAM = 'ableguard_baseline'; const LEGACY_PARAM = 'equalsites_baseline'; const HEADER = 'X-AbleGuard-Baseline'; const SCHEME = '1'; /** * Longest life we will accept, which caps what any single value can ever be * worth and doubles as the allowance for a site clock running behind the * SaaS. Also the lifetime of the spent-value marker, so the marker always * outlives the value it invalidates. */ const MAX_TTL = 900; const SIGNATURE_LENGTH = 43; const NONCE_LENGTH = 16; const MAX_VALUE_LENGTH = 200; /** * @param string $token Site API token (the HMAC key). * @param string $site_id Site id this value must be for. * @param string $path Request path this value must be for. * @param int $exp Expiry, unix seconds. * @param string $nonce Per-value nonce. * @return string base64url signature. */ public static function sign( $token, $site_id, $path, $exp, $nonce ) { // Newline-delimited so no field can bleed into its neighbour; a // percent-encoded path cannot contain a newline. $message = implode( "\n", array( 'equalsites-baseline', self::SCHEME, $site_id, $path, (string) $exp, $nonce ) ); return self::base64url( hash_hmac( 'sha256', $message, $token, true ) ); } /** * Decide whether a value authorises serving this exact path unfixed. * * @param string $value Raw parameter value. * @param string $token Site API token. * @param string $site_id Configured site id. * @param string $path Path of the current request. * @param int $now Current unix time. * @return string|null The signature to spend once honoured, or null to serve the page normally. */ public static function authorize( $value, $token, $site_id, $path, $now ) { // An empty token would still produce a valid HMAC, and then anyone who // knows the site id and path could mint their own bypass. if ( ! is_string( $value ) || ! is_string( $token ) || '' === $token || '' === $site_id || '' === $path ) { return null; } if ( strlen( $value ) > self::MAX_VALUE_LENGTH ) { return null; } $parts = explode( '.', $value ); if ( 4 !== count( $parts ) ) { return null; } list( $scheme, $exp_raw, $nonce, $signature ) = $parts; if ( self::SCHEME !== $scheme ) { return null; } if ( '' === $exp_raw || ! ctype_digit( $exp_raw ) ) { return null; } if ( self::NONCE_LENGTH !== strlen( $nonce ) || ! ctype_xdigit( $nonce ) || strtolower( $nonce ) !== $nonce ) { return null; } if ( self::SIGNATURE_LENGTH !== strlen( $signature ) || ! preg_match( '/^[A-Za-z0-9_-]+$/', $signature ) ) { return null; } $exp = (int) $exp_raw; if ( $exp <= (int) $now || $exp - (int) $now > self::MAX_TTL ) { return null; } $expected = self::sign( $token, $site_id, $path, $exp, $nonce ); if ( ! hash_equals( $expected, $signature ) ) { return null; } // Replay is checked last, so a forged or guessed value never reaches // storage and cannot be told apart by how long the response took. if ( self::is_spent( $signature ) ) { return null; } return $signature; } /** Has this value already produced an unfixed page? */ public static function is_spent( $signature ) { return (bool) get_transient( self::marker( $signature ) ); } /** * Spend the value, so the same URL cannot be replayed inside its lifetime. * * Called once the response body exists rather than at the moment the value is * accepted, so a redirect cannot burn a use without producing a page. Two * genuinely simultaneous requests can therefore both be honoured; closing * that would need an atomic claim on every front-end request, which is not * worth it to stop a second copy of a page an attacker would already have. */ public static function spend( $signature ) { set_transient( self::marker( $signature ), 1, self::MAX_TTL ); } /** * Transient names are length-limited, so key on a hash of the signature — * which also keeps the signature itself out of the options table. */ private static function marker( $signature ) { return 'equalsites_bl_' . substr( hash( 'sha256', $signature ), 0, 32 ); } private static function base64url( $bytes ) { return rtrim( strtr( base64_encode( $bytes ), '+/', '-_' ), '=' ); } }