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.
327 lines
11 KiB
PHP
327 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* The export screen.
|
|
*
|
|
* Shows what is waiting, what failed and why, and offers the two buttons that
|
|
* matter: export everything, and try the failures again.
|
|
*
|
|
* It also hosts manual signing. In manual mode the server has no key, so the
|
|
* upload cannot happen without a person: this page builds each queued file
|
|
* server-side, hands the bytes to the browser, and the browser signs and PUTs
|
|
* them straight to the gateway. The file never round-trips through anyone
|
|
* else's server, and the key never touches this one.
|
|
*
|
|
* @package SiriusPress
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class SPE_Admin {
|
|
|
|
const PAGE = 'sirius-press-export';
|
|
|
|
public static function hooks() {
|
|
add_action( 'admin_menu', array( __CLASS__, 'menu' ), 30 );
|
|
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
|
|
add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
|
|
}
|
|
|
|
public static function menu() {
|
|
add_submenu_page(
|
|
'sirius-press',
|
|
__( 'Publishing', 'sirius-press' ),
|
|
__( 'Publishing', 'sirius-press' ),
|
|
'manage_options',
|
|
self::PAGE,
|
|
array( __CLASS__, 'render' )
|
|
);
|
|
}
|
|
|
|
public static function enqueue( $hook ) {
|
|
if ( false === strpos( (string) $hook, self::PAGE ) ) {
|
|
return;
|
|
}
|
|
if ( SP_Settings::MODE_SERVER === SP_Settings::mode() ) {
|
|
return; // Nothing to sign here; cron has the key.
|
|
}
|
|
if ( ! class_exists( 'SPA_Login' ) ) {
|
|
return; // Auth plugin inactive — no wallet library to lean on.
|
|
}
|
|
SPA_Login::enqueue_wallet();
|
|
wp_enqueue_script(
|
|
'sirius-press-export',
|
|
SIRIUS_PRESS_EXPORT_URL . 'assets/export.js',
|
|
array( 'sirius-press-wallet', 'wp-api-fetch' ),
|
|
SIRIUS_PRESS_EXPORT_VERSION,
|
|
true
|
|
);
|
|
wp_localize_script(
|
|
'sirius-press-export',
|
|
'SIRIUS_EXPORT',
|
|
array(
|
|
'restUrl' => esc_url_raw( rest_url( 'sirius-press/v1/export' ) ),
|
|
'nonce' => wp_create_nonce( 'wp_rest' ),
|
|
'gateway' => SP_Settings::gateway(),
|
|
'name' => SP_Settings::name(),
|
|
'prefix' => SP_Settings::prefix(),
|
|
'path' => SP_Settings::derivation_path(),
|
|
)
|
|
);
|
|
add_action( 'admin_footer', array( 'SPA_Login', 'print_config' ) );
|
|
}
|
|
|
|
// -------------------------------------------------- manual-signing routes
|
|
|
|
public static function register_routes() {
|
|
$can_manage = function () {
|
|
return current_user_can( 'manage_options' );
|
|
};
|
|
|
|
register_rest_route(
|
|
'sirius-press/v1',
|
|
'/export/next',
|
|
array(
|
|
'methods' => WP_REST_Server::READABLE,
|
|
'permission_callback' => $can_manage,
|
|
'callback' => array( __CLASS__, 'rest_next' ),
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
'sirius-press/v1',
|
|
'/export/ack',
|
|
array(
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
'permission_callback' => $can_manage,
|
|
'callback' => array( __CLASS__, 'rest_ack' ),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Build the next few queued files and hand them to the browser.
|
|
*
|
|
* Bodies come back base64-encoded because a queued file may be a PNG, and
|
|
* JSON has no way to carry arbitrary bytes otherwise.
|
|
*/
|
|
public static function rest_next( WP_REST_Request $request ) {
|
|
$limit = max( 1, min( 5, (int) $request->get_param( 'limit' ) ?: 3 ) );
|
|
$items = array();
|
|
|
|
foreach ( SPE_Queue::claim( $limit ) as $row ) {
|
|
$body = SPE_Runner::build( $row );
|
|
if ( is_wp_error( $body ) ) {
|
|
SPE_Queue::mark_failed( $row->id, $body->get_error_message() );
|
|
continue;
|
|
}
|
|
$hash = hash( 'sha256', $body );
|
|
if ( '' !== $row->content_hash && hash_equals( $row->content_hash, $hash ) ) {
|
|
SPE_Queue::mark_done( $row->id, $hash );
|
|
continue;
|
|
}
|
|
$items[] = array(
|
|
'id' => (int) $row->id,
|
|
'path' => $row->path,
|
|
'mime' => SP_Gateway::mime_for( $row->path ),
|
|
'sha256' => $hash,
|
|
'body_b64' => base64_encode( $body ),
|
|
);
|
|
}
|
|
|
|
return rest_ensure_response(
|
|
array(
|
|
'items' => $items,
|
|
'left' => SPE_Queue::counts()[ SPE_Queue::STATUS_PENDING ],
|
|
)
|
|
);
|
|
}
|
|
|
|
/** The browser reports how an upload went. */
|
|
public static function rest_ack( WP_REST_Request $request ) {
|
|
$id = (int) $request->get_param( 'id' );
|
|
$hash = (string) $request->get_param( 'sha256' );
|
|
$error = (string) $request->get_param( 'error' );
|
|
|
|
if ( '' !== $error ) {
|
|
SPE_Queue::mark_failed( $id, $error );
|
|
return rest_ensure_response( array( 'ok' => false ) );
|
|
}
|
|
if ( ! preg_match( '/^[a-f0-9]{64}$/', $hash ) ) {
|
|
return new WP_Error( 'sirius_bad_hash', __( 'Bad hash.', 'sirius-press' ), array( 'status' => 400 ) );
|
|
}
|
|
SPE_Queue::mark_done( $id, $hash );
|
|
return rest_ensure_response( array( 'ok' => true ) );
|
|
}
|
|
|
|
// ------------------------------------------------------------------ page
|
|
|
|
public static function render() {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
wp_die( esc_html__( 'You are not allowed to publish this site.', 'sirius-press' ) );
|
|
}
|
|
|
|
$notes = array();
|
|
if ( isset( $_POST['sirius_export_action'] ) && check_admin_referer( 'sirius_export' ) ) {
|
|
$action = sanitize_key( wp_unslash( $_POST['sirius_export_action'] ) );
|
|
if ( 'full' === $action ) {
|
|
$n = SPE_Runner::queue_full_site();
|
|
$notes[] = sprintf(
|
|
/* translators: %d: number of pages queued. */
|
|
_n( '%d page queued for export.', '%d pages queued for export.', $n, 'sirius-press' ),
|
|
$n
|
|
);
|
|
} elseif ( 'retry' === $action ) {
|
|
$n = SPE_Queue::retry_failed();
|
|
$notes[] = sprintf(
|
|
/* translators: %d: number of failed exports re-queued. */
|
|
_n( '%d failed export queued again.', '%d failed exports queued again.', $n, 'sirius-press' ),
|
|
$n
|
|
);
|
|
} elseif ( 'run' === $action ) {
|
|
$r = SPE_Runner::run_batch( 20 );
|
|
$notes[] = sprintf(
|
|
/* translators: 1: uploaded, 2: unchanged, 3: failed, 4: remaining. */
|
|
__( 'Uploaded %1$d, %2$d unchanged, %3$d failed, %4$d still queued.', 'sirius-press' ),
|
|
$r['done'],
|
|
$r['skipped'],
|
|
$r['failed'],
|
|
$r['left']
|
|
);
|
|
} elseif ( 'clear' === $action ) {
|
|
SPE_Queue::clear_done();
|
|
$notes[] = __( 'Completed rows cleared.', 'sirius-press' );
|
|
}
|
|
}
|
|
|
|
$counts = SPE_Queue::counts();
|
|
$manual = SP_Settings::MODE_SERVER !== SP_Settings::mode();
|
|
|
|
echo '<div class="wrap"><h1>' . esc_html__( 'Publishing', 'sirius-press' ) . '</h1>';
|
|
|
|
foreach ( $notes as $n ) {
|
|
printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( $n ) );
|
|
}
|
|
|
|
if ( ! SP_Settings::is_configured() ) {
|
|
printf(
|
|
'<div class="notice notice-error"><p>%s <a href="%s">%s</a></p></div>',
|
|
esc_html__( 'No BCNR name is configured, so there is nowhere to publish to.', 'sirius-press' ),
|
|
esc_url( admin_url( 'admin.php?page=sirius-press' ) ),
|
|
esc_html__( 'Settings', 'sirius-press' )
|
|
);
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Plain permalinks are fatal to a static export and the failure is
|
|
* silent: every post's URL is `/?p=N`, whose path is `/`, so every
|
|
* page in the site maps to index.html and each one overwrites the
|
|
* last. The queue looks healthy the whole time.
|
|
*/
|
|
if ( '' === (string) get_option( 'permalink_structure' ) ) {
|
|
printf(
|
|
'<div class="notice notice-error"><p><strong>%s</strong> %s <a href="%s">%s</a></p></div>',
|
|
esc_html__( 'This site uses plain permalinks.', 'sirius-press' ),
|
|
esc_html__( 'Every page would be exported to the same file and overwrite the one before it, so nothing useful can be published until that is changed.', 'sirius-press' ),
|
|
esc_url( admin_url( 'options-permalink.php' ) ),
|
|
esc_html__( 'Choose a permalink structure', 'sirius-press' )
|
|
);
|
|
}
|
|
|
|
printf(
|
|
'<p>%s <code>%s</code>%s</p>',
|
|
esc_html__( 'Publishing to', 'sirius-press' ),
|
|
esc_html( SP_Settings::name() ),
|
|
$manual
|
|
? ' — ' . esc_html__( 'signing happens in your browser, on this page.', 'sirius-press' )
|
|
: ' — ' . esc_html__( 'this server signs uploads by itself.', 'sirius-press' )
|
|
);
|
|
|
|
printf(
|
|
'<p><strong>%1$d</strong> %2$s <strong>%3$d</strong> %4$s <strong>%5$d</strong> %6$s</p>',
|
|
(int) $counts[ SPE_Queue::STATUS_PENDING ],
|
|
esc_html__( 'queued', 'sirius-press' ),
|
|
(int) $counts[ SPE_Queue::STATUS_DONE ],
|
|
esc_html__( 'published', 'sirius-press' ),
|
|
(int) $counts[ SPE_Queue::STATUS_FAILED ],
|
|
esc_html__( 'failed', 'sirius-press' )
|
|
);
|
|
|
|
echo '<form method="post" style="margin:16px 0">';
|
|
wp_nonce_field( 'sirius_export' );
|
|
echo '<button class="button button-primary" name="sirius_export_action" value="full">'
|
|
. esc_html__( 'Export everything', 'sirius-press' ) . '</button> ';
|
|
if ( ! $manual ) {
|
|
echo '<button class="button" name="sirius_export_action" value="run">'
|
|
. esc_html__( 'Run a batch now', 'sirius-press' ) . '</button> ';
|
|
}
|
|
echo '<button class="button" name="sirius_export_action" value="retry">'
|
|
. esc_html__( 'Try failures again', 'sirius-press' ) . '</button> ';
|
|
echo '<button class="button" name="sirius_export_action" value="clear">'
|
|
. esc_html__( 'Clear finished rows', 'sirius-press' ) . '</button>';
|
|
echo '</form>';
|
|
|
|
if ( $manual ) {
|
|
self::render_manual_panel();
|
|
}
|
|
|
|
self::render_queue_table();
|
|
echo '</div>';
|
|
}
|
|
|
|
private static function render_manual_panel() {
|
|
?>
|
|
<div id="sirius-export-panel" class="sirius-wallet" style="max-width:820px">
|
|
<h2 style="margin-top:0"><?php esc_html_e( 'Sign and upload', 'sirius-press' ); ?></h2>
|
|
<p class="description">
|
|
<?php esc_html_e( 'Your phrase is used in this page to sign each upload and is wiped when the run finishes. Files go straight from this browser to the gateway.', 'sirius-press' ); ?>
|
|
</p>
|
|
<p>
|
|
<label for="sirius_export_phrase"><strong><?php esc_html_e( 'Recovery phrase', 'sirius-press' ); ?></strong></label><br>
|
|
<textarea id="sirius_export_phrase" rows="2" style="width:100%;font-family:monospace"
|
|
autocomplete="off" spellcheck="false"></textarea>
|
|
</p>
|
|
<p>
|
|
<button type="button" class="button button-primary" id="sirius_export_run">
|
|
<?php esc_html_e( 'Publish queued pages', 'sirius-press' ); ?>
|
|
</button>
|
|
<button type="button" class="button" id="sirius_export_stop" hidden>
|
|
<?php esc_html_e( 'Stop', 'sirius-press' ); ?>
|
|
</button>
|
|
</p>
|
|
<p id="sirius_export_status" role="status" aria-live="polite"></p>
|
|
<ul id="sirius_export_log" style="font-family:monospace;font-size:12px;max-height:240px;overflow:auto;margin:0"></ul>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private static function render_queue_table() {
|
|
$rows = SPE_Queue::recent( 100 );
|
|
echo '<h2>' . esc_html__( 'Queue', 'sirius-press' ) . '</h2>';
|
|
if ( ! $rows ) {
|
|
echo '<p>' . esc_html__( 'Nothing queued. Publish a post, or use “Export everything”.', 'sirius-press' ) . '</p>';
|
|
return;
|
|
}
|
|
echo '<table class="widefat striped"><thead><tr>';
|
|
echo '<th>' . esc_html__( 'Path', 'sirius-press' ) . '</th>';
|
|
echo '<th style="width:100px">' . esc_html__( 'Status', 'sirius-press' ) . '</th>';
|
|
echo '<th style="width:60px">' . esc_html__( 'Tries', 'sirius-press' ) . '</th>';
|
|
echo '<th>' . esc_html__( 'Last error', 'sirius-press' ) . '</th>';
|
|
echo '</tr></thead><tbody>';
|
|
foreach ( $rows as $row ) {
|
|
$colour = SPE_Queue::STATUS_FAILED === $row->status
|
|
? '#b32d2e'
|
|
: ( SPE_Queue::STATUS_DONE === $row->status ? '#007017' : '#646970' );
|
|
printf(
|
|
'<tr><td><code>%s</code></td><td style="color:%s;font-weight:600">%s</td><td>%d</td><td><small>%s</small></td></tr>',
|
|
esc_html( $row->path ),
|
|
esc_attr( $colour ),
|
|
esc_html( $row->status ),
|
|
(int) $row->attempts,
|
|
esc_html( (string) $row->last_error )
|
|
);
|
|
}
|
|
echo '</tbody></table>';
|
|
}
|
|
}
|