sirius-press/plugins/sirius-press-auth/includes/class-spa-rest.php

261 lines
7.7 KiB
PHP
Raw Normal View History

feat(sirius-press): a WordPress where the account is a key, not a mailbox WordPress makes two assumptions this project cannot accept: that identity comes from an email address, and that a site lives at one server. Both are things somebody else can take away — a mailbox is rented from a provider who can close it or be compelled to open it, and a server is one seizure from being gone. Sirius Press replaces the first and hedges the second. Signing in means signing a challenge with the key that controls a CashAddress. The address is recovered from the signature, so nothing is typed but the signature itself, and the result is an ordinary WordPress session cookie — roles, capabilities, nonces and the REST API never learn the login was different. Three ways to produce one: a wallet the browser already exposes, a phrase used once in the page and wiped, or a signature pasted in from any BIP-137 wallet, which needs no JavaScript and lets the key stay on a machine that never touches the web. There is no password reset, and the recovery page says so plainly rather than offering a form that cannot work. A reset mechanism is by construction a way to take an account from its owner, and it is always easier to attack than the cryptography it bypasses. Publishing a post also exports it as static HTML to the name's storage on Sia, signed by the key that owns the name, so the site keeps answering when the server does not. Email as a feature is untouched. wp_mail() still works, SMTP still sends, and contact forms still deliver to addresses real people typed. Only mail to the site's own unroutable placeholder addresses is diverted to an in-app inbox. The objection was to email as identity, not to email. Core is pinned and patched rather than vendored. WordPress 7.1.1 is 149 MB and 5,008 files; the fork's entire core diff is 75 lines in wp-admin/install.php. Carrying the former to express the latter would bury the patch where nobody reviews it and make every clone of the monorepo pay for it. Upstream releases still merge through tools/update-wordpress.sh, which reapplies the series and says exactly which hunk needs a human. The cryptography is implemented twice — PHP on the server, JavaScript in the page — because the server must verify and the browser must sign. Both are pinned against libauth, the library the Sirius portal wallet and the BNS gateway already use, so a disagreement of one byte fails the test suite rather than presenting as a rejected login at three in the morning. 132 checks, no framework, about a second.
2026-09-21 01:39:38 +02:00
<?php
/**
* The wallet-auth REST surface.
*
* Two audiences. Themes and front-end code that want a sign-in form somewhere
* other than `wp-login.php`, and other plugins that want to demand a fresh
* signature before doing something irreversible deleting a site, moving
* money, transferring a name. The second is the reason `confirm` exists: a
* capability check proves what a session is allowed to do, but it cannot
* prove that the person holding the key is still at the keyboard. A fresh
* signature can.
*
* Everything here is unauthenticated by design except `confirm`, and every
* endpoint that does curve maths is rate limited, because signature recovery
* is the most expensive thing a stranger can make this server do.
*
* @package SiriusPress
*/
defined( 'ABSPATH' ) || exit;
final class SPA_REST {
const NS = 'sirius-press/v1';
public static function hooks() {
add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
}
public static function register_routes() {
register_rest_route(
self::NS,
'/challenge',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => array( __CLASS__, 'challenge' ),
'args' => array(
'purpose' => array(
'type' => 'string',
'default' => SPA_Challenge::PURPOSE_LOGIN,
),
),
)
);
register_rest_route(
self::NS,
'/login',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => '__return_true',
'callback' => array( __CLASS__, 'login' ),
'args' => self::signature_args(),
)
);
register_rest_route(
self::NS,
'/register',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => '__return_true',
'callback' => array( __CLASS__, 'register' ),
'args' => self::signature_args(),
)
);
register_rest_route(
self::NS,
'/confirm',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => function () {
return is_user_logged_in();
},
'callback' => array( __CLASS__, 'confirm' ),
'args' => self::signature_args(),
)
);
}
private static function signature_args() {
return array(
'nonce' => array(
'type' => 'string',
'required' => true,
),
'signature' => array(
'type' => 'string',
'required' => true,
),
);
}
/** Hand out something to sign. */
public static function challenge( WP_REST_Request $request ) {
$limited = SPA_Challenge::check_rate_limit( 'challenge', 60, 300 );
if ( is_wp_error( $limited ) ) {
return $limited;
}
$purpose = (string) $request->get_param( 'purpose' );
if ( ! SPA_Challenge::is_known_purpose( $purpose ) ) {
return new WP_Error( 'sirius_bad_purpose', __( 'Unknown purpose.', 'sirius-press' ), array( 'status' => 400 ) );
}
if ( SPA_Challenge::PURPOSE_REGISTER === $purpose && ! SP_Settings::open_registration() ) {
return new WP_Error( 'sirius_closed', __( 'This site is not accepting new accounts.', 'sirius-press' ), array( 'status' => 403 ) );
}
$nonce = SPA_Challenge::issue();
return rest_ensure_response(
array(
'nonce' => $nonce,
'message' => SPA_Challenge::message( $nonce, $purpose ),
'purpose' => $purpose,
'expires_in' => SPA_Challenge::TTL,
'prefix' => SP_Settings::prefix(),
)
);
}
public static function login( WP_REST_Request $request ) {
$limited = SPA_Challenge::check_rate_limit( 'login' );
if ( is_wp_error( $limited ) ) {
return $limited;
}
$address = SPA_Challenge::verify(
(string) $request->get_param( 'nonce' ),
(string) $request->get_param( 'signature' ),
SPA_Challenge::PURPOSE_LOGIN
);
if ( is_wp_error( $address ) ) {
return self::with_status( $address, 401 );
}
$user = SP_Identity::user_by_address( $address );
if ( ! $user ) {
return new WP_Error(
'sirius_no_account',
__( 'No account on this site uses that wallet.', 'sirius-press' ),
array(
'status' => 404,
'address' => $address,
)
);
}
$remember = (bool) $request->get_param( 'remember' );
wp_set_current_user( $user->ID );
wp_set_auth_cookie( $user->ID, $remember );
do_action( 'wp_login', $user->user_login, $user );
do_action( 'sirius_press_wallet_authenticated', $user, $address );
return rest_ensure_response(
array(
'ok' => true,
'user_id' => (int) $user->ID,
'address' => $address,
'redirect' => user_can( $user, 'read' ) ? admin_url() : home_url( '/' ),
)
);
}
public static function register( WP_REST_Request $request ) {
if ( ! SP_Settings::open_registration() ) {
return new WP_Error( 'sirius_closed', __( 'This site is not accepting new accounts.', 'sirius-press' ), array( 'status' => 403 ) );
}
$limited = SPA_Challenge::check_rate_limit( 'register', 10, 900 );
if ( is_wp_error( $limited ) ) {
return $limited;
}
fix(sirius-press): recovering a key is not the same as verifying a signature Running the fork against a live WordPress found a real hole in registration, and it is the kind that only shows up when you actually try it. ECDSA public-key recovery always succeeds. Given any well-formed signature and any digest it returns a key — just not the signer's, unless the digest is the one that was signed. The auth flow leaned on that as if a wrong message would fail. It does not; it quietly yields a stranger's address. At sign-in this was harmless, because the wrong address matches no account and the attempt fails. Registration and wallet-linking were another matter: both took the recovered address and bound it to an account, so a signature over slightly different text — a challenge copied without its blank line, a wallet that rewrote the text, a login signature replayed at the registration form — created an account keyed to an address nobody could sign for. The person would see "success" and discover the truth the next time they tried to get in. Wallet-linking was worse still: it would move an existing account onto a dead address and lock its owner out of their own site. Both paths now require the address the signer claims and compare it to the recovered one, which is what verification actually means. Sign-in accepts the claim when the page sends it and uses it to turn "no account uses that wallet" into the more useful "that signature is not over the text we asked for". Also from running it: URL rewriting mangled every link on a site whose URL carries a port. The protocol-relative pass matched inside absolute URLs and gave each one a second scheme, and matching the host without its port left the port stranded as `//host:8760:8760/`. Local and staging installs would have exported a site of broken links. Plain permalinks silently collapse an entire site onto one exported file, because every post's URL is `/?p=N` and its path is `/`. The queue looks healthy the whole time. The Publishing screen now says so. Translations loaded on `plugins_loaded`, which WordPress 6.7 warns about on every request — the kind of noise that trains people to stop reading logs. And one deletion: an `is_email()` filter written on the assumption that WordPress rejects `.invalid` addresses. It does not — `is_email()` validates syntax, not whether a domain could exist — so the filter never fired. A filter that appears to relax a rule but does not is worse than no filter, because someone later reasons from it. The documentation made the same claim and has been corrected. Verification added rather than asserted: tests/live.mjs drives a real instance over HTTP (40 checks), and tests/mock-gateway.mjs answers uploads with the signature check transcribed from the gateway's own source, so the publishing path can be exercised without a registered name.
2026-09-21 02:35:32 +02:00
// Required, for the reason set out in class-spa-challenge.php: recovery
// alone would happily mint an account for an address the caller cannot
// sign with.
$claimed = (string) $request->get_param( 'address' );
if ( '' === $claimed ) {
return new WP_Error(
'sirius_missing_address',
__( 'Send the address you signed with, so the signature can be checked against it.', 'sirius-press' ),
array( 'status' => 400 )
);
}
feat(sirius-press): a WordPress where the account is a key, not a mailbox WordPress makes two assumptions this project cannot accept: that identity comes from an email address, and that a site lives at one server. Both are things somebody else can take away — a mailbox is rented from a provider who can close it or be compelled to open it, and a server is one seizure from being gone. Sirius Press replaces the first and hedges the second. Signing in means signing a challenge with the key that controls a CashAddress. The address is recovered from the signature, so nothing is typed but the signature itself, and the result is an ordinary WordPress session cookie — roles, capabilities, nonces and the REST API never learn the login was different. Three ways to produce one: a wallet the browser already exposes, a phrase used once in the page and wiped, or a signature pasted in from any BIP-137 wallet, which needs no JavaScript and lets the key stay on a machine that never touches the web. There is no password reset, and the recovery page says so plainly rather than offering a form that cannot work. A reset mechanism is by construction a way to take an account from its owner, and it is always easier to attack than the cryptography it bypasses. Publishing a post also exports it as static HTML to the name's storage on Sia, signed by the key that owns the name, so the site keeps answering when the server does not. Email as a feature is untouched. wp_mail() still works, SMTP still sends, and contact forms still deliver to addresses real people typed. Only mail to the site's own unroutable placeholder addresses is diverted to an in-app inbox. The objection was to email as identity, not to email. Core is pinned and patched rather than vendored. WordPress 7.1.1 is 149 MB and 5,008 files; the fork's entire core diff is 75 lines in wp-admin/install.php. Carrying the former to express the latter would bury the patch where nobody reviews it and make every clone of the monorepo pay for it. Upstream releases still merge through tools/update-wordpress.sh, which reapplies the series and says exactly which hunk needs a human. The cryptography is implemented twice — PHP on the server, JavaScript in the page — because the server must verify and the browser must sign. Both are pinned against libauth, the library the Sirius portal wallet and the BNS gateway already use, so a disagreement of one byte fails the test suite rather than presenting as a rejected login at three in the morning. 132 checks, no framework, about a second.
2026-09-21 01:39:38 +02:00
$address = SPA_Challenge::verify(
(string) $request->get_param( 'nonce' ),
(string) $request->get_param( 'signature' ),
fix(sirius-press): recovering a key is not the same as verifying a signature Running the fork against a live WordPress found a real hole in registration, and it is the kind that only shows up when you actually try it. ECDSA public-key recovery always succeeds. Given any well-formed signature and any digest it returns a key — just not the signer's, unless the digest is the one that was signed. The auth flow leaned on that as if a wrong message would fail. It does not; it quietly yields a stranger's address. At sign-in this was harmless, because the wrong address matches no account and the attempt fails. Registration and wallet-linking were another matter: both took the recovered address and bound it to an account, so a signature over slightly different text — a challenge copied without its blank line, a wallet that rewrote the text, a login signature replayed at the registration form — created an account keyed to an address nobody could sign for. The person would see "success" and discover the truth the next time they tried to get in. Wallet-linking was worse still: it would move an existing account onto a dead address and lock its owner out of their own site. Both paths now require the address the signer claims and compare it to the recovered one, which is what verification actually means. Sign-in accepts the claim when the page sends it and uses it to turn "no account uses that wallet" into the more useful "that signature is not over the text we asked for". Also from running it: URL rewriting mangled every link on a site whose URL carries a port. The protocol-relative pass matched inside absolute URLs and gave each one a second scheme, and matching the host without its port left the port stranded as `//host:8760:8760/`. Local and staging installs would have exported a site of broken links. Plain permalinks silently collapse an entire site onto one exported file, because every post's URL is `/?p=N` and its path is `/`. The queue looks healthy the whole time. The Publishing screen now says so. Translations loaded on `plugins_loaded`, which WordPress 6.7 warns about on every request — the kind of noise that trains people to stop reading logs. And one deletion: an `is_email()` filter written on the assumption that WordPress rejects `.invalid` addresses. It does not — `is_email()` validates syntax, not whether a domain could exist — so the filter never fired. A filter that appears to relax a rule but does not is worse than no filter, because someone later reasons from it. The documentation made the same claim and has been corrected. Verification added rather than asserted: tests/live.mjs drives a real instance over HTTP (40 checks), and tests/mock-gateway.mjs answers uploads with the signature check transcribed from the gateway's own source, so the publishing path can be exercised without a registered name.
2026-09-21 02:35:32 +02:00
SPA_Challenge::PURPOSE_REGISTER,
$claimed
feat(sirius-press): a WordPress where the account is a key, not a mailbox WordPress makes two assumptions this project cannot accept: that identity comes from an email address, and that a site lives at one server. Both are things somebody else can take away — a mailbox is rented from a provider who can close it or be compelled to open it, and a server is one seizure from being gone. Sirius Press replaces the first and hedges the second. Signing in means signing a challenge with the key that controls a CashAddress. The address is recovered from the signature, so nothing is typed but the signature itself, and the result is an ordinary WordPress session cookie — roles, capabilities, nonces and the REST API never learn the login was different. Three ways to produce one: a wallet the browser already exposes, a phrase used once in the page and wiped, or a signature pasted in from any BIP-137 wallet, which needs no JavaScript and lets the key stay on a machine that never touches the web. There is no password reset, and the recovery page says so plainly rather than offering a form that cannot work. A reset mechanism is by construction a way to take an account from its owner, and it is always easier to attack than the cryptography it bypasses. Publishing a post also exports it as static HTML to the name's storage on Sia, signed by the key that owns the name, so the site keeps answering when the server does not. Email as a feature is untouched. wp_mail() still works, SMTP still sends, and contact forms still deliver to addresses real people typed. Only mail to the site's own unroutable placeholder addresses is diverted to an in-app inbox. The objection was to email as identity, not to email. Core is pinned and patched rather than vendored. WordPress 7.1.1 is 149 MB and 5,008 files; the fork's entire core diff is 75 lines in wp-admin/install.php. Carrying the former to express the latter would bury the patch where nobody reviews it and make every clone of the monorepo pay for it. Upstream releases still merge through tools/update-wordpress.sh, which reapplies the series and says exactly which hunk needs a human. The cryptography is implemented twice — PHP on the server, JavaScript in the page — because the server must verify and the browser must sign. Both are pinned against libauth, the library the Sirius portal wallet and the BNS gateway already use, so a disagreement of one byte fails the test suite rather than presenting as a rejected login at three in the morning. 132 checks, no framework, about a second.
2026-09-21 01:39:38 +02:00
);
if ( is_wp_error( $address ) ) {
return self::with_status( $address, 400 );
}
$user_id = SP_Identity::create_user( $address, (string) $request->get_param( 'user_login' ) );
if ( is_wp_error( $user_id ) ) {
return self::with_status( $user_id, 409 );
}
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, false );
return rest_ensure_response(
array(
'ok' => true,
'user_id' => (int) $user_id,
'address' => $address,
)
);
}
/**
* Re-prove the current user's key.
*
* Returns 403 rather than 200-with-false when the signature belongs to
* somebody else's wallet, so a caller that forgets to check the body still
* fails closed.
*/
public static function confirm( WP_REST_Request $request ) {
$limited = SPA_Challenge::check_rate_limit( 'confirm', 30, 300 );
if ( is_wp_error( $limited ) ) {
return $limited;
}
$address = SPA_Challenge::verify(
(string) $request->get_param( 'nonce' ),
(string) $request->get_param( 'signature' ),
SPA_Challenge::PURPOSE_CONFIRM
);
if ( is_wp_error( $address ) ) {
return self::with_status( $address, 400 );
}
$mine = SP_Identity::address_of( get_current_user_id() );
if ( '' === $mine || ! hash_equals( $mine, $address ) ) {
return new WP_Error(
'sirius_wrong_wallet',
__( 'That signature is from a different wallet than the one signed in.', 'sirius-press' ),
array( 'status' => 403 )
);
}
/**
* Fires when a signed-in user re-proves their key.
*
* @param int $user_id
* @param string $address
*/
do_action( 'sirius_press_wallet_confirmed', get_current_user_id(), $address );
return rest_ensure_response(
array(
'ok' => true,
'address' => $address,
)
);
}
/** Attach an HTTP status to a WP_Error that was created without one. */
private static function with_status( WP_Error $error, $status ) {
$data = $error->get_error_data();
if ( ! is_array( $data ) ) {
$data = array();
}
if ( empty( $data['status'] ) ) {
$data['status'] = $status;
}
$error->add_data( $data, $error->get_error_code() );
return $error;
}
}