hash ]. An option (not usermeta) so no rows are * ever keyed to a user ID that does not exist. The wardpair_ prefix keeps * it out of option sync and lets Sentinel_Uninstall clean it up. */ const PENDING_HASH_OPTION = 'wardpair_pending_pw_hashes'; // Log direction constants. const DIR_PUSH = 'push'; const DIR_PULL = 'pull'; /** * Payload types a PRIMARY will accept over the sync endpoint. * * The link is overwhelmingly one-directional (primary pushes to * secondary), but these two travel back the other way and must not be * dropped: remote_disconnect is how a disconnect initiated on the * secondary tears down the primary's pairing state, and clone_complete_ack * is how the primary learns its secondary finished restoring. */ const PRIMARY_ACCEPTED_TYPES = array( 'remote_disconnect', 'clone_complete_ack', 'epoch_announce', 'handback_prepare', ); /** * Message types that coordinate the two sites rather than copy data. * They are never blocked by the fence and never held for a missing clone. */ const CONTROL_TYPES = array( 'primary_heartbeat', 'epoch_announce', 'handback_prepare', 'tail_batch', 'rows_batch', ); /** > 0 while trusted data is being applied; stops the capture hooks from echoing it back. */ private static int $applying = 0; /** Why the most recent send() failed (read by the journal and switch-back). */ public static string $last_error = ''; /** * Data a payload handler wants returned to the primary in the SAME HTTP * response. * * The inter-site channel was built as fire-and-forget: the secondary * replies {ok: true|false} and nothing else. That is fine for pushes * ("here is a post, store it") but not for questions ("which of these * files are you missing?"). Rather than add a second endpoint — and a * second signed request, a second WAF surface, a second set of retry * semantics — a handler calls set_response() and handle_incoming_payload() * merges it into the JSON it was already sending back. * * Reset per request; a stale value from a previous handler must never * leak into an unrelated reply. */ private static array $response_extra = array(); /** * Called by a payload handler on the SECONDARY to attach data to the reply. * Keys collide-safe: 'ok' is reserved and cannot be overwritten. */ public static function set_response( array $data ): void { unset( $data['ok'] ); self::$response_extra = array_merge( self::$response_extra, $data ); } // Log status constants. const STATUS_OK = 'ok'; const STATUS_FAILED = 'failed'; const STATUS_SKIP = 'skipped'; /** * In-memory deduplication guard. * Tracks option names already queued THIS request so rapid multi-save * plugins don't flood AS with 20 identical jobs. * * @var array */ private static array $queued_options = array(); public static function get_instance(): self { if ( null === self::$instance ) { self::$instance = new self(); self::$instance->register_hooks(); } return self::$instance; } private function __construct() {} // ========================================================================= // HOOK REGISTRATION // ========================================================================= private function register_hooks(): void { $role = get_option( 'wardpair_role', '' ); // Live content syncing: the event hooks on the authority. A paid feature. // Both roles pull large files: a standby pulls from the primary, and during a failover // the original primary pulls from the secondary. Registering this for the secondary only // left every pull queued on the original server with no handler, so it silently did nothing. if ( in_array( $role, array( 'primary', 'secondary' ), true ) ) { add_action( 'wardpair_pull_files', array( $this, 'handle_pull_files' ), 10, 3 ); } } // ========================================================================= // PERIODIC SYNC SCHEDULER // ========================================================================= // ========================================================================= // SYNC LOGGER // ========================================================================= public static function log( string $direction, string $type, string $status, string $detail, string $error = '', ?string $partner = null ): void { global $wpdb; list( $row, $fmt ) = Sentinel_Connections::stamp( array( 'direction' => $direction, 'type' => $type, 'status' => $status, 'detail' => substr( $detail, 0, 255 ), 'error' => substr( $error, 0, 255 ), 'created_at' => current_time( 'mysql' ), ), array( '%s', '%s', '%s', '%s', '%s', '%s' ), $partner ); $wpdb->insert( $wpdb->prefix . 'wardpair_sync_log', $row, $fmt ); // Keep the log trimmed: at most 200 rows for each connection, so a busy // connection never pushes an earlier site's history out. if ( isset( $row['partner'] ) ) { $wpdb->query( $wpdb->prepare( " DELETE FROM {$wpdb->prefix}wardpair_sync_log WHERE partner = %s AND id NOT IN ( SELECT id FROM ( SELECT id FROM {$wpdb->prefix}wardpair_sync_log WHERE partner = %s ORDER BY id DESC LIMIT 200 ) AS t ) ", $row['partner'], $row['partner'] ) ); } else { $wpdb->query( $wpdb->prepare( " DELETE FROM %i WHERE id NOT IN ( SELECT id FROM ( SELECT id FROM %i ORDER BY id DESC LIMIT 200 ) AS t ) ", $wpdb->prefix . 'wardpair_sync_log', $wpdb->prefix . 'wardpair_sync_log' ) ); } if ( 'clone' === $type || 'verify' === $type ) { Sentinel_Core::progress_from_log( $type, $status, $detail ); } } /** * @param string|null $partner Connection key to show; null returns every connection's rows. */ public static function get_recent_log( int $limit = 50, ?string $partner = null ): array { global $wpdb; if ( null !== $partner && Sentinel_Connections::ready() ) { return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wardpair_sync_log WHERE partner = %s ORDER BY id DESC LIMIT %d", $partner, $limit ), ARRAY_A ) ?: array(); } return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wardpair_sync_log ORDER BY id DESC LIMIT %d", $limit ), ARRAY_A ) ?: array(); } // ========================================================================= // OPTION EXCLUSION FILTER // ========================================================================= // ========================================================================= // PRIMARY: HOOK CALLBACKS // ========================================================================= // ========================================================================= // PRIMARY: ACTION SCHEDULER HANDLERS // ========================================================================= // ========================================================================= // PRIMARY: PERIODIC OPTIONS DIFF // ========================================================================= // ========================================================================= // PRIMARY: PERIODIC UPLOAD DIFF // ========================================================================= // ========================================================================= // SYNC HEALTH SCORE // ========================================================================= // ========================================================================= // PRIMARY: BOOTSTRAP // ========================================================================= // ========================================================================= // PAYLOAD BUILDERS // ========================================================================= // ========================================================================= // PRIMARY: PUSH TO SECONDARY // ========================================================================= /** * Send one payload to the partner, subject to the data rules: * - a fenced (demoted) site sends nothing but control traffic, * - a never-cloned secondary receives nothing but control traffic, * - every message is stamped with this site's epoch. * Despite the name this now serves whichever site is the sync source. */ private function push_to_secondary( array $payload, bool $write_log = true ): bool { return $this->push_guarded( $payload, $write_log )['ok']; } /** push_to_secondary() with the partner's reply body, for messages that ask a question. */ private function push_guarded( array $payload, bool $write_log = true ): array { $none = array( 'ok' => false, 'body' => array(), 'code' => 0 ); $payload_type = $payload['type'] ?? 'unknown'; $is_control = in_array( $payload_type, self::CONTROL_TYPES, true ); // A secondary that has never completed a clone restore has no // wp-content of its own to receive options, posts, or theme_mods // into. Every granular push funnels through here, so this is the one // place that has to hold the line. Only the primary-to-secondary // direction is affected; the clone transfer uses a separate transport. if ( ! $is_control && 'primary' === get_option( 'wardpair_role' ) && ! get_option( 'wardpair_bootstrap_complete', false ) ) { if ( $write_log && ! get_transient( 'wardpair_awaiting_clone_notice' ) ) { set_transient( 'wardpair_awaiting_clone_notice', 1, HOUR_IN_SECONDS ); self::log( self::DIR_PUSH, $payload_type, self::STATUS_SKIP, 'Push held — secondary has never completed a clone restore. Run a Full Clone; nothing else will be sent until then.' ); Sentinel_Core::log_event( 'push_held_no_clone', 'Content changes are not being sent to the secondary because it has never completed a clone restore. Run a Full Clone.' ); } return $none; } $started = time(); $r = $this->send( $payload, $write_log ); return $r; } /** * Raw send: encrypt, sign, post, decode, learn the partner's epoch. * * @return array{ ok: bool, body: array, code: int } */ private function send( array $payload, bool $write_log = true ): array { $out = array( 'ok' => false, 'body' => array(), 'code' => 0 ); $partner_url = get_option( 'wardpair_partner_url', '' ); $slug = get_option( 'wardpair_endpoint_slug', '' ); $shared_secret = get_option( 'wardpair_shared_secret', '' ); if ( empty( $partner_url ) || empty( $slug ) || empty( $shared_secret ) ) { if ( $write_log ) { self::log( self::DIR_PUSH, $payload['type'] ?? 'unknown', self::STATUS_FAILED, 'Push skipped — site not fully paired.' ); } return $out; } $payload = Sentinel_Epoch::stamp( $payload ); $payload['sender_role'] = Sentinel_Epoch::role(); $plain = wp_json_encode( $payload ); $encrypted = Sentinel_Crypto::encrypt( $plain, $shared_secret ); $ts = time(); $token = Sentinel_Crypto::sign( $ts, $encrypted, $shared_secret ); $domain_ajax = trailingslashit( $partner_url ) . 'wp-admin/admin-ajax.php'; $post_body = array( 'action' => 'swp_' . $slug . '_sync', 'swp_data' => $encrypted, 'swp_ts' => $ts, 'swp_token' => $token, ); // While the secondary is serving the primary's domain, that domain // resolves back to the secondary itself. Pin the origin route so a // message meant for the primary can never loop back to its sender. $pin = ( 'secondary' === Sentinel_Epoch::role() && 'secondary' === Sentinel_Epoch::authority() ); if ( $pin ) { add_filter( 'wardpair_transport_route', array( __CLASS__, 'force_origin_route' ) ); } // Candidate origin IPs. While pinned (failover), prefer the IP derived // from the saved Cloudflare record: the IP stored at pairing can be an // internal address that nothing can connect to. $ips = array(); if ( $pin ) { } $stored_ip = (string) get_option( 'wardpair_partner_ip', '' ); if ( '' !== $stored_ip && ! in_array( $stored_ip, $ips, true ) ) { $ips[] = $stored_ip; } if ( empty( $ips ) ) { $ips[] = ''; } $tried = array(); foreach ( $ips as $try_ip ) { $tried[] = $try_ip; $result = Sentinel_Transport::post( $domain_ajax, $try_ip, $post_body, array(), 30 ); if ( ! is_wp_error( $result['response'] ) || ! $pin ) { break; } } if ( $pin ) { remove_filter( 'wardpair_transport_route', array( __CLASS__, 'force_origin_route' ) ); } $response = $result['response']; $type = $payload['type'] ?? 'unknown'; $detail = $this->describe_payload( $payload ); if ( is_wp_error( $response ) ) { Sentinel_Pairing::note_standby_contact( false ); // No answer at all: the standby watch counts it. self::$last_error = substr( $response->get_error_message(), 0, 160 ) . ( $pin ? ' [origin IPs tried: ' . implode( ', ', array_filter( $tried ) ) . ']' : '' ); if ( $write_log ) { self::log( self::DIR_PUSH, $type, self::STATUS_FAILED, "Push failed: {$detail}", $response->get_error_message() . ( $pin ? ' (origin IPs tried: ' . implode( ', ', array_filter( $tried ) ) . ')' : '' ) ); } return $out; } $out['code'] = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); $out['body'] = is_array( $body ) ? $body : array(); $out['ok'] = ! empty( $out['body']['ok'] ); Sentinel_Pairing::note_partner_reply( ! $out['ok'] && Sentinel_Pairing::reply_says_unpaired( $out['code'], (string) wp_remote_retrieve_body( $response ) ) ); // Did something that is WordPress answer? A JSON reply, even a refusal, says the standby is alive; // a host's HTML page (suspended, 5xx) does not. Feeds the "standby not answering" watch on the primary. Sentinel_Pairing::note_standby_contact( $out['ok'] || is_array( $body ) || Sentinel_Pairing::reply_says_unpaired( $out['code'], (string) wp_remote_retrieve_body( $response ) ) ); // Whatever the partner says about the epoch, the higher one wins. Sentinel_Epoch::observe_reply( $out['body'], 'reply' ); // The partner reports its own safety trips in its replies. self::$last_error = $out['ok'] ? '' : substr( 'HTTP ' . $out['code'] . ( ! empty( $out['body']['stale'] ) ? ', this site is no longer authoritative' : ', partner answered ok=false' ) . ( ! empty( $out['body']['reason'] ) ? ': ' . (string) $out['body']['reason'] : '' ), 0, 200 ); if ( $write_log ) { if ( $out['ok'] ) { self::log( self::DIR_PUSH, $type, self::STATUS_OK, "Pushed: {$detail}" ); } else { self::log( self::DIR_PUSH, $type, self::STATUS_FAILED, "Rejected: {$detail}", 'HTTP ' . $out['code'] . ( ! empty( $out['body']['stale'] ) ? ' — this site is no longer authoritative.' : ' — ok=false.' ) . ( '' !== Sentinel_Transport::$last_route ? ' Route: ' . Sentinel_Transport::$last_route . '.' : '' ) // First part of what the partner actually answered (a PHP/host error page says why). . ( '' !== ( $swp_reply = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( (string) wp_remote_retrieve_body( $response ) ) ) ) ) ? ' Reply: ' . substr( $swp_reply, 0, 200 ) : '' ) ); } } if ( $out['ok'] ) { Sentinel_Core::clear_waf_block(); } elseif ( Sentinel_Clone_Primary::looks_like_host_level_block( $out['code'], $body, wp_remote_retrieve_body( $response ) ) ) { Sentinel_Core::flag_waf_block( Sentinel_Pairing::get_server_ip(), $out['code'], substr( wp_strip_all_tags( wp_remote_retrieve_body( $response ) ), 0, 200 ) ); } return $out; } public static function force_origin_route(): string { return 'origin'; } // ─── Public entry points used by Journal / Reconcile / Epoch ────────────── /** * Push a data payload through the normal guards and return the partner's * reply body. Used by the delta lanes, which ask questions ("which of * these buckets differ?") and act on the answer. * * @return array{ ok: bool, body: array, code: int } */ public static function push_with_reply( array $payload, bool $write_log = false ): array { return ( new self() )->push_guarded( $payload, $write_log ); } /** * "Are you there?" to the standby. Sends one small signed message that changes nothing on the other * site; the result is recorded by send() for the standby watch. Not logged, so a quiet site does not * fill its sync log with these. */ public static function probe_partner(): bool { $r = ( new self() )->send( array( 'type' => 'standby_ping', 'at' => time() ), false ); return ! empty( $r['ok'] ); } /** Push a data payload through the normal guards. */ public static function push_payload( array $payload, bool $write_log = true, bool $internal = false ): bool { return ( new self() )->push_to_secondary( $payload, $write_log ); } /** * Send a coordination message (epoch, tail, hand-back, row batches). * * @return array{ ok: bool, body: array, code: int } */ public static function push_control( array $payload, bool $write_log = true ): array { return ( new self() )->send( $payload, $write_log ); } private function describe_payload( array $payload ): string { switch ( $payload['type'] ?? 'unknown' ) { case 'post': if ( isset( $payload['post'] ) ) { return "Post #{$payload['post']['ID']} {$payload['action']}: \"{$payload['post']['post_title']}\""; } return "Post #{$payload['post_id']} {$payload['action']}"; case 'user': return isset( $payload['user'] ) ? "User {$payload['action']}: {$payload['user']['user_login']}" : "User #{$payload['user_id']} {$payload['action']}"; case 'option': return "Option: {$payload['option_name']}"; case 'options_batch': return 'Options batch: ' . count( $payload['options'] ?? array() ) . ' item(s)'; case 'attachment_file': return "File (inline): {$payload['relative_path']}"; case 'attachment_file_pull': return "File (pull): {$payload['relative_path']}"; case 'upload_batch': return 'Upload batch: ' . count( $payload['files'] ?? array() ) . ' file(s)'; case 'upload_pull_batch': return 'Upload pull: ' . count( $payload['files'] ?? array() ) . ' large file(s)'; case 'term': return 'delete' === ( $payload['action'] ?? '' ) ? "Term #{$payload['term']['term_id']} deleted ({$payload['term']['taxonomy']})" : "Term: {$payload['term']['name']} ({$payload['term']['taxonomy']})"; case 'password': return "Password update for user #{$payload['user_id']}"; case 'delta_probe': return 'Delta probe: ' . ( $payload['table'] ?? '?' ); case 'delta_rows': return 'Delta rows: ' . ( $payload['table'] ?? '?' ); case 'schema_apply': return 'Structure copy: ' . ( $payload['table'] ?? '?' ); case 'files_batch': return 'Files: ' . count( $payload['files'] ?? array() ) . ' file(s) (' . ( $payload['root'] ?? '' ) . ')'; case 'files_pull': return 'File pull request: ' . count( $payload['files'] ?? array() ) . ' file(s)'; case 'files_probe': return 'File check: ' . count( $payload['items'] ?? array() ) . ' file(s)'; case 'code_commit': return 'Code commit: ' . ( $payload['unit'] ?? '?' ) . ' (' . count( $payload['files'] ?? array() ) . ' file(s))'; case 'rows_batch': return 'Row batch: ' . count( $payload['items'] ?? array() ) . ' item(s)'; case 'tail_batch': return 'Tail batch: ' . count( $payload['items'] ?? array() ) . ' item(s)'; case 'epoch_announce': return 'Epoch ' . (int) ( $payload['epoch'] ?? 0 ) . ' announced'; case 'handback_prepare': return 'Hand-back to epoch ' . (int) ( $payload['handback_epoch'] ?? $payload['epoch'] ?? 0 ); default: return $payload['type'] ?? 'unknown'; } } // ========================================================================= // SECONDARY: RECEIVE INCOMING PAYLOAD // ========================================================================= // phpcs:disable WordPress.Security.NonceVerification.Missing -- Server-to-server request authenticated by an HMAC signature, a timestamp and the shared secret. There is no logged-in user here, so a nonce does not apply. public static function handle_incoming_payload(): void { $slug = get_option( 'wardpair_endpoint_slug', '' ); $shared_secret = get_option( 'wardpair_shared_secret', '' ); // ── Fast-path: pair_ack from secondary (hits the PRIMARY) ───────────── if ( ! empty( $_POST['swp_pair_ack'] ) ) { $ts = absint( wp_unslash( $_POST['swp_ts'] ?? 0 ) ); $token = sanitize_text_field( wp_unslash( $_POST['swp_token'] ?? '' ) ); $secret = get_option( 'wardpair_shared_secret', '' ); if ( $secret && $ts && $token && Sentinel_Crypto::verify( $token, $ts, 'pair_ack', $secret ) ) { update_option( 'wardpair_pair_ack_received', time() ); wp_send_json( array( 'ok' => true ), 200 ); } else { wp_send_json( array( 'ok' => false, 'error' => 'ack_verify_failed' ), 200 ); } return; } // ── Encrypted payload path ─────────────────────────────────────────── // // Most traffic on this endpoint is primary → secondary, but not all of // it: remote_disconnect and clone_complete_ack travel the other way. // The role check therefore cannot happen here, before the payload is // decrypted — doing so silently dropped every secondary → primary // message while still returning HTTP 200, so the sender logged a // success for a message the receiver never saw. That is why // disconnecting from the secondary left the primary still showing as // paired, and why the primary's bootstrap banner never cleared on its // own. The role is enforced below instead, once the type is known. if ( ! get_option( 'wardpair_is_paired', false ) ) { wp_send_json( array(), 200 ); return; } // swp_data is base64 (up to ~15 MB on legacy chunks): a single strict // character-class pass is far cheaper than sanitize_text_field() and // cannot alter valid base64. $encrypted = isset( $_POST['swp_data'] ) && is_string( $_POST['swp_data'] ) ? preg_replace( '/[^A-Za-z0-9+\/=]/', '', wp_unslash( $_POST['swp_data'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reduced to the base64 alphabet here; the payload is then authenticated and decrypted. $ts = absint( wp_unslash( $_POST['swp_ts'] ?? 0 ) ); $token = sanitize_text_field( wp_unslash( $_POST['swp_token'] ?? '' ) ); if ( empty( $encrypted ) || empty( $ts ) || empty( $token ) ) { wp_send_json( array( 'ok' => false ), 200 ); return; } $verification = Sentinel_Crypto::verify_with_reason( $token, $ts, $encrypted, $shared_secret ); if ( ! $verification['ok'] ) { $rate_key = 'wardpair_sync_rate_' . md5( Sentinel_Utils::client_ip() ); $count = (int) get_transient( $rate_key ); if ( $count >= 20 ) { wp_send_json( array(), 429 ); return; } set_transient( $rate_key, $count + 1, MINUTE_IN_SECONDS ); if ( 'clock_skew' === $verification['reason'] ) { $skew = $verification['skew_seconds']; self::log( self::DIR_PULL, 'unknown', self::STATUS_FAILED, sprintf( 'Request rejected — server clocks are %d second(s) apart (limit: 300s). ' . 'Check that the system time on both primary and secondary is correct (NTP sync).', abs( $skew ) ) ); } else { self::log( self::DIR_PULL, 'unknown', self::STATUS_FAILED, 'HMAC verification failed.' ); } wp_send_json( array(), 200 ); return; } $plain = Sentinel_Crypto::decrypt( $encrypted, $shared_secret ); if ( false === $plain ) { self::log( self::DIR_PULL, 'unknown', self::STATUS_FAILED, 'Decryption failed.' ); wp_send_json( array( 'ok' => false ), 200 ); return; } $payload = json_decode( $plain, true ); if ( ! is_array( $payload ) || empty( $payload['type'] ) ) { self::log( self::DIR_PULL, 'unknown', self::STATUS_FAILED, 'Invalid payload structure.' ); wp_send_json( array( 'ok' => false ), 200 ); return; } // A message that came from a site with OUR role is a loop (while the // secondary serves the primary's domain, a request meant for the // primary can resolve back to the secondary). Never act on it. $own_role = Sentinel_Epoch::role(); if ( ! empty( $payload['sender_role'] ) && $payload['sender_role'] === $own_role ) { wp_send_json( array_merge( array( 'ok' => false, 'loop' => true ), Sentinel_Epoch::reply_fields() ), 200 ); return; } // Learn the sender's epoch first: the higher epoch always wins. This // is what fences a primary that was unreachable when the secondary // took over, the moment anything arrives from the secondary. $sender_epoch = (int) ( $payload['epoch'] ?? 0 ); $sender_authority = (string) ( $payload['authority'] ?? '' ); if ( $sender_epoch > 0 && '' !== $sender_authority ) { Sentinel_Epoch::observe( $sender_epoch, $sender_authority, 'incoming ' . $payload['type'] ); } $type = (string) $payload['type']; // Coordination messages are exempt from the stale-sender rule; row // batches are real data and are NOT. $is_control = in_array( $type, array( 'primary_heartbeat', 'epoch_announce', 'handback_prepare', 'standby_ping' ), true ) || in_array( $type, self::PRIMARY_ACCEPTED_TYPES, true ); $local = Sentinel_Epoch::current(); // Stale sender: it lost authority and has not heard yet. Its data must // not overwrite the active site. Tail batches are the one deliberate // exception, and only the authority takes them. if ( 'tail_batch' !== $type && ! $is_control && ( $local > 0 || ! empty( $payload['sender_role'] ) ) ) { $sender_is_authority = ( ! empty( $payload['sender_role'] ) && $payload['sender_role'] === Sentinel_Epoch::authority() ); if ( $sender_epoch < $local || ! $sender_is_authority ) { self::log( self::DIR_PULL, $type, self::STATUS_SKIP, "Rejected: sender is not the authority (sender epoch {$sender_epoch}, local {$local})." ); wp_send_json( array_merge( array( 'ok' => false, 'stale' => true ), Sentinel_Epoch::reply_fields() ), 200 ); return; } } if ( 'tail_batch' === $type && ! Sentinel_Epoch::is_authority() ) { wp_send_json( array_merge( array( 'ok' => false, 'stale' => true ), Sentinel_Epoch::reply_fields() ), 200 ); return; } // Role enforcement, now that the payload type is known. A secondary // accepts anything; a primary accepts the few control types legitimately // sent back up the link, plus real data while it is the fenced // follower of an authoritative secondary. if ( 'secondary' !== $own_role && ! in_array( $type, self::PRIMARY_ACCEPTED_TYPES, true ) && ! ( Sentinel_Epoch::is_fenced() && 'tail_batch' !== $type ) && ! ( 'tail_batch' === $type ) ) { self::log( self::DIR_PULL, $type, self::STATUS_SKIP, 'Payload rejected — this site is not the secondary in this pair.' ); wp_send_json( array(), 200 ); return; } // Each signed request counts once (see Sentinel_Replay). Rejections above did nothing, // so only a request that is about to change something needs a claim. list( $replay_state, $replay_cached ) = Sentinel_Replay::claim( $token ); if ( 'done' === $replay_state ) { self::log( self::DIR_PULL, $type, self::STATUS_SKIP, 'Repeat of a request already processed; answered again without applying it twice.' ); wp_send_json( is_array( $replay_cached ) ? $replay_cached : array_merge( array( 'ok' => true, 'duplicate' => true ), Sentinel_Epoch::reply_fields() ), 200 ); return; } if ( 'busy' === $replay_state ) { wp_send_json( array_merge( array( 'ok' => false, 'busy' => true ), Sentinel_Epoch::reply_fields() ), 200 ); return; } update_option( 'wardpair_last_seen', time() ); Sentinel_Transport::reset_breaker(); // The partner just spoke to us. self::$response_extra = Sentinel_Epoch::reply_fields(); // Everything that arrives here is trusted data from the partner. $apply = function () use ( $payload ) { self::$applying++; try { return ( new self() )->apply_payload( $payload ); } finally { self::$applying--; } }; $applied = $apply(); do_action( 'wardpair_after_sync' ); $reply = array_merge( array( 'ok' => $applied ), self::$response_extra ); if ( $applied ) { Sentinel_Replay::complete( $token, $reply ); } else { Sentinel_Replay::release( $token ); // Not applied: a retry must be processed, not mistaken for a repeat. } wp_send_json( $reply, 200 ); } // phpcs:enable WordPress.Security.NonceVerification.Missing // ========================================================================= // SECONDARY: APPLY PAYLOAD DISPATCHER // ========================================================================= /** * @return bool Whether the payload was actually applied successfully. * Currently only the 'post' case reflects real success/ * failure (apply_post() already had the detection logic — * it just wasn't being surfaced). Other types still ACK * unconditionally pending the same treatment. */ private function apply_payload( array $payload ): bool { switch ( $payload['type'] ) { case 'identity_sync': Sentinel_Identity::store_primary_identity( $payload ); break; case 'epoch_announce': return true; // Epoch already adopted in handle_incoming_payload(). case 'clone_chunk': case 'clone_file_chunk': case 'clone_verify_page': case 'clone_db_fingerprint': case 'clone_repair_files': case 'clone_repair_pull': case 'clone_status': case 'clone_mark_complete': Sentinel_Clone_Sync::handle_clone_payload( $payload ); break; case 'remote_disconnect': self::log( self::DIR_PULL, 'system', self::STATUS_OK, 'Remote disconnect received - clearing local pairing state.' ); update_option( 'wardpair_disconnect_received', time() ); // The other site started this disconnect and is waiting for our answer, so we must // NOT notify it back. Notifying made the two sites call each other in turn (each // still "paired" until its own notice finished), tying up both servers' PHP workers // until the requests timed out: the admin saw "Request failed" and the other site // could be left paired. Sentinel_Pairing::disconnect( false ); break; case 'standby_ping': // The primary asking "are you there?" while it has nothing to sync. Answering ok is the whole job. break; case 'chunk_size_probe': // Probe from primary during chunk size negotiation. // Payload is accepted and discarded - ok=true is sufficient. self::log( self::DIR_PULL, 'clone', self::STATUS_OK, 'Chunk size probe accepted: ' . size_format( (int) ( $payload['probe_size'] ?? 0 ) ) ); break; case 'clone_complete_ack': // Secondary telling primary its clone finished - clears primary's // own "Initial content sync in progress" banner, which nothing // else on primary's side would ever otherwise clear. update_option( 'wardpair_bootstrap_complete', true ); self::log( self::DIR_PULL, 'clone', self::STATUS_OK, 'Secondary confirmed clone completion.' ); Sentinel_Core::log_event( 'clone_complete', 'Secondary confirmed clone completion - bootstrap marked complete on primary.' ); break; default: // Live content syncing (posts, users, options, files, handback) is a paid feature. self::log( self::DIR_PULL, $payload['type'], self::STATUS_SKIP, "Unknown payload type: {$payload['type']}." ); } return true; } // ========================================================================= // SECONDARY: APPLY METHODS // ========================================================================= // ========================================================================= // SECONDARY: PULL FILES FROM PRIMARY // ========================================================================= /** * @param array $files Relative paths to pull from the primary. * @param int $attempt Retry counter. * @param string $root 'uploads' (default, media sync) or 'content' * (differential repair, which covers all of * wp-content, not just the uploads tree). */ /** * The address of the OTHER site, for pulling files from it. A standby pulls from the primary. * During a failover it is the other way round: the original primary pulls from the secondary, * and its own "primary address" is its own server. The first valid address wins. */ public static function partner_origin_ip(): string { $opts = ( 'primary' === get_option( 'wardpair_role', 'secondary' ) ) ? array( 'wardpair_secondary_ip', 'wardpair_partner_ip' ) : array( 'wardpair_primary_ip', 'wardpair_partner_ip' ); foreach ( $opts as $o ) { $v = trim( (string) get_option( $o, '' ) ); if ( '' !== $v && filter_var( $v, FILTER_VALIDATE_IP ) ) { return $v; } } return ''; } /** Every valid address known for the other site, best first (the single-address version is partner_origin_ip()). */ public static function partner_origin_ips(): array { $opts = ( 'primary' === get_option( 'wardpair_role', 'secondary' ) ) ? array( 'wardpair_secondary_ip', 'wardpair_partner_ip' ) : array( 'wardpair_primary_ip', 'wardpair_partner_ip' ); $out = array(); foreach ( $opts as $o ) { $v = trim( (string) get_option( $o, '' ) ); if ( '' !== $v && filter_var( $v, FILTER_VALIDATE_IP ) && ! in_array( $v, $out, true ) ) { $out[] = $v; } } return $out; } /** * Download request to the other site with the same fallback the pushes have: each known * origin address first, then the public domain route. The domain route is skipped only when * this site is the secondary holding authority, because then the partner's domain leads back here. * * @param string[] $ips * @return array|WP_Error */ private static function pull_request( string $url, array $ips, array $args ) { $last = new WP_Error( 'swp_no_route', 'No route to the other site.' ); foreach ( $ips as $ip ) { $last = Sentinel_Transport::origin_request( $url, (string) $ip, $args ); if ( ! is_wp_error( $last ) && (int) wp_remote_retrieve_response_code( $last ) < 500 ) { return $last; } } $domain_leads_here = ( 'secondary' === Sentinel_Epoch::role() && 'secondary' === Sentinel_Epoch::authority() ); if ( $domain_leads_here ) { return $last; } $args['sslverify'] = true; $args['redirection'] = 0; $domain = wp_remote_post( $url, $args ); return $domain; } /** Key that identifies one queued pull, so repeated requests for the same files can be recognised. */ public static function pull_key( string $root, array $files ): string { return 'wardpair_pull_' . md5( ( 'content' === $root ? 'content' : 'uploads' ) . '|' . implode( '|', $files ) ); } public function handle_pull_files( array $files, int $attempt = 1, string $root = 'uploads' ): void { $root = ( 'content' === $root ) ? 'content' : 'uploads'; $primary_ip = self::partner_origin_ip(); $primary_url = get_option( 'wardpair_partner_url', '' ); $primary_domain = wp_parse_url( $primary_url, PHP_URL_HOST ); $slug = get_option( 'wardpair_endpoint_slug', '' ); $shared_secret = get_option( 'wardpair_shared_secret', '' ); if ( 'content' === $root ) { $base_dir = untrailingslashit( Sentinel_Utils::content_dir() ); } else { $upload_dir = wp_upload_dir(); $base_dir = $upload_dir['basedir']; } $ok = $fail = 0; $errs = array(); $retry = array(); // Files that failed for a recoverable (network/HTTP) reason. foreach ( $files as $relative ) { // Retries queued before an exclusion was added must not keep // hammering the primary for archives we deliberately skip. if ( 'content' === $root && is_string( $relative ) && ! Sentinel_Clone_Verify::path_is_comparable( $relative ) ) { $skipped_excluded = ( $skipped_excluded ?? 0 ) + 1; continue; } if ( ! Sentinel_Utils::relative_path_ok( $relative ) ) { // Not recoverable — never retry a rejected path. $fail++; $errs[] = $relative . ' (path traversal rejected)'; continue; } $safe = $relative; // Executables inside uploads are not media. Skipped for good, not retried. if ( 'uploads' === $root && Sentinel_Utils::is_executable_name( $safe ) ) { $skipped_exec = ( $skipped_exec ?? 0 ) + 1; continue; } // The signed string is scoped by root so a signature minted for // an uploads path can't be replayed to read the same relative // path out of wp-content (wp-config.php lives outside wp-content, // but plugin source and drop-ins do not). $signed = ( 'content' === $root ) ? 'content:' . $safe : $safe; $ts = time(); $token = Sentinel_Crypto::sign( $ts, $signed, $shared_secret ); // Download to a temporary file, never straight onto the destination and never into // memory: a dropped connection or a truncated body must not leave a half file in place. $tmp_dir = untrailingslashit( WARDPAIR_BACKUP_DIR ) . '/pull_temp'; wp_mkdir_p( $tmp_dir ); $tmp = $tmp_dir . '/' . wp_generate_password( 12, false, false ) . '.part'; // Verified TLS to the origin, or a pinned certificate (see Sentinel_Transport::origin_request), // then the public domain route when the origin address cannot be reached. $response = self::pull_request( 'https://' . $primary_domain . '/wp-admin/admin-ajax.php', self::partner_origin_ips(), array( 'timeout' => 120, 'stream' => true, 'filename' => $tmp, // A streamed body is stored as received, so ask for it uncompressed. 'headers' => array( 'Accept-Encoding' => 'identity' ), 'body' => array( 'action' => 'swp_' . $slug . '_mf', 'swp_file' => $safe, 'swp_root' => $root, 'swp_ts' => $ts, 'swp_token' => $token, ), ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { wp_delete_file( $tmp ); $fail++; $retry[] = $relative; $errs[] = $safe . ' (' . ( is_wp_error( $response ) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code( $response ) ) . ')'; continue; } // Verify what arrived against what the primary says it sent. $want_md5 = strtolower( (string) wp_remote_retrieve_header( $response, 'x-swp-md5' ) ); $want_size = (string) wp_remote_retrieve_header( $response, 'x-swp-size' ); $got_size = is_file( $tmp ) ? (int) filesize( $tmp ) : -1; $want_sig = strtolower( (string) wp_remote_retrieve_header( $response, 'x-swp-sig' ) ); $is_code = false; // The checksum and size arrive on a connection whose certificate is not verified // (origin route), so on their own they only catch accidents, not tampering. The // primary signs them with the pairing secret; nothing is kept without that // signature, so a file can only come from the paired site, unchanged. A missing // signature is a failure, never a fallback: stripping it must not downgrade us. if ( '' === $want_md5 || '' === $want_size || '' === $want_sig || ! hash_equals( Sentinel_Crypto::sign_file( $ts, $root, $safe, $want_md5, (int) $want_size, $shared_secret ), $want_sig ) ) { wp_delete_file( $tmp ); $fail++; $errs[] = $safe . ' (the primary\'s reply was not signed correctly; both sites must run the same Wardpair version)'; continue; } if ( (int) $want_size !== $got_size || ! hash_equals( $want_md5, (string) md5_file( $tmp ) ) ) { wp_delete_file( $tmp ); $fail++; $retry[] = $relative; // A damaged or cut-off download is worth another try. $errs[] = $safe . ' (checksum or size mismatch)'; continue; } // Content-relative names ("plugins/x/y.php") are resolved where this site really keeps // that folder, and must stay inside it. if ( 'content' === $root ) { $dest = Sentinel_Utils::resolve_content_path( $safe ); $allowed = Sentinel_Utils::content_base_for( $safe ); } else { $dest = $base_dir . '/' . $safe; $allowed = $base_dir; } if ( ! Sentinel_Utils::mkdir_inside( dirname( $dest ), $allowed ) || ! Sentinel_Utils::write_target_ok( $dest, $allowed ) ) { wp_delete_file( $tmp ); $fail++; $errs[] = $safe . ' (resolves outside the allowed folder)'; continue; } if ( @rename( $tmp, $dest ) || ( @copy( $tmp, $dest ) && @unlink( $tmp ) ) ) { $ok++; } else { wp_delete_file( $tmp ); $fail++; $retry[] = $relative; // Write failures (e.g. transient disk issue) are also worth retrying. $errs[] = $safe . ' (write failed)'; } } if ( ! empty( $skipped_held ) ) { self::log( self::DIR_PULL, 'media', self::STATUS_SKIP, "{$skipped_held} plugin/theme file(s) not taken: the code gate is paused on this site." ); } if ( ! empty( $skipped_exec ) ) { self::log( self::DIR_PULL, 'media', self::STATUS_SKIP, "{$skipped_exec} executable file(s) inside uploads were not carried across (not media)." ); } if ( ! empty( $skipped_excluded ) ) { self::log( self::DIR_PULL, 'media', self::STATUS_SKIP, "{$skipped_excluded} excluded path(s) not pulled (backup archives and other skipped folders)." ); } self::log( self::DIR_PULL, 'media', $fail ? self::STATUS_FAILED : self::STATUS_OK, "File pull: {$ok} ok, {$fail} failed" . ( $attempt > 1 ? " (attempt {$attempt})" : '' ) . '.', implode( '; ', array_slice( $errs, 0, 5 ) ) ); if ( empty( $retry ) ) { delete_transient( self::pull_key( $root, $files ) ); } // Bounded retry for recoverable failures — network hiccups and transient // disk errors shouldn't mean a file silently never syncs. if ( ! empty( $retry ) && $attempt < self::MAX_PULL_ATTEMPTS && function_exists( 'as_schedule_single_action' ) ) { as_schedule_single_action( time() + 60, 'wardpair_pull_files', array( $retry, $attempt + 1, $root ), 'wardpair' ); self::log( self::DIR_PULL, 'media', self::STATUS_OK, count( $retry ) . " file(s) re-queued for retry (attempt " . ( $attempt + 1 ) . ')' ); } elseif ( ! empty( $retry ) ) { self::log( self::DIR_PULL, 'media', self::STATUS_FAILED, count( $retry ) . ' file(s) gave up after ' . self::MAX_PULL_ATTEMPTS . ' attempts.' ); } } // ========================================================================= // PRIMARY: SERVE MEDIA FILE // ========================================================================= // phpcs:disable WordPress.Security.NonceVerification.Missing -- Server-to-server request authenticated by an HMAC signature, a timestamp and the shared secret. There is no logged-in user here, so a nonce does not apply. public static function handle_media_file_request(): void { $shared_secret = get_option( 'wardpair_shared_secret', '' ); $token = sanitize_text_field( wp_unslash( $_POST['swp_token'] ?? '' ) ); $ts = absint( wp_unslash( $_POST['swp_ts'] ?? 0 ) ); // The exact string the secondary signed. sanitize_text_field() rewrote names with // %xx sequences or angle brackets, and WordPress adds slashes to $_POST, so a name // with an apostrophe never matched its own signature. Taken verbatim, the signature // check below is what decides whether it is trusted. $relative = isset( $_POST['swp_file'] ) && is_string( $_POST['swp_file'] ) ? wp_unslash( $_POST['swp_file'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Must stay verbatim to match the signature; it is verified with Sentinel_Crypto::verify() and Sentinel_Utils::relative_path_ok() before any use. $root = sanitize_key( wp_unslash( $_POST['swp_root'] ?? 'uploads' ) ); $root = ( 'content' === $root ) ? 'content' : 'uploads'; if ( empty( $token ) || empty( $ts ) || empty( $relative ) ) { wp_die( '', '', array( 'response' => 400 ) ); } // Must match the scoping applied when the signature was minted — // see handle_pull_files(). $signed = ( 'content' === $root ) ? 'content:' . $relative : $relative; if ( ! Sentinel_Crypto::verify( $token, $ts, $signed, $shared_secret ) ) { wp_die( '', '', array( 'response' => 403 ) ); } if ( ! Sentinel_Utils::relative_path_ok( $relative ) ) { wp_die( '', '', array( 'response' => 400 ) ); } $safe = $relative; if ( 'content' === $root ) { // Same allow-list the repair scanner uses, applied again here: // this endpoint is reachable by anything holding a valid // signature, so it validates the path itself rather than // trusting that the caller already did. if ( ! Sentinel_Clone_Verify::path_is_safe( $safe ) ) { wp_die( '', '', array( 'response' => 400 ) ); } $base = untrailingslashit( Sentinel_Utils::content_base_for( $safe ) ); $filepath = Sentinel_Utils::resolve_content_path( $safe ); // Belt and braces: resolve and confirm the real path is still // inside wp-content after symlinks are followed. $real = realpath( $filepath ); if ( false === $real || 0 !== strpos( $real, realpath( $base ) . DIRECTORY_SEPARATOR ) ) { wp_die( '', '', array( 'response' => 400 ) ); } } else { $upload_dir = wp_upload_dir(); $filepath = $upload_dir['basedir'] . '/' . $safe; // Same containment as the content branch: a symlink inside uploads must not // lead the response out of the uploads folder. if ( ! Sentinel_Utils::path_inside( $filepath, $upload_dir['basedir'] ) ) { wp_die( '', '', array( 'response' => 400 ) ); } } if ( ! file_exists( $filepath ) || ! is_file( $filepath ) ) { wp_die( '', '', array( 'response' => 404 ) ); } $size = (int) filesize( $filepath ); $md5 = (string) md5_file( $filepath ); // fileinfo is optional on some hosts; the file is still served as plain bytes without it. $mime = ( function_exists( 'mime_content_type' ) ? mime_content_type( $filepath ) : false ) ?: 'application/octet-stream'; header( 'Content-Type: ' . $mime ); header( 'Content-Length: ' . $size ); // The receiver checks what it downloaded against these before keeping it, and the // signature proves they came from the paired site and were not swapped on the way. header( 'X-SWP-Size: ' . $size ); header( 'X-SWP-MD5: ' . $md5 ); header( 'X-SWP-Sig: ' . Sentinel_Crypto::sign_file( $ts, $root, $safe, $md5, $size, $shared_secret ) ); header( 'X-Content-Type-Options: nosniff' ); nocache_headers(); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Streams a large file to the paired site without loading it into memory; the path passed Sentinel_Utils::relative_path_ok() and the request signature was verified first. readfile( $filepath ); exit; } // phpcs:enable WordPress.Security.NonceVerification.Missing // ========================================================================= // SECONDARY: APPLY POST // ========================================================================= // ========================================================================= // SECONDARY: APPLY USER // ========================================================================= // ========================================================================= // SECONDARY: APPLY TERM // ========================================================================= // ========================================================================= // SECONDARY: APPLY PASSWORD // ========================================================================= // ========================================================================= // HELPERS // ========================================================================= }