):array */ private readonly ?\Closure $testInvoker; /** @param null|callable(array):array $testInvoker */ public function __construct( private readonly ?string $binaryPath = null, ?callable $testInvoker = null ) { $this->testInvoker = $testInvoker === null ? null : \Closure::fromCallable($testInvoker); } /** @param array $request @return array */ public function invoke(array $request): array { $protocol = $request['protocol'] ?? null; $operationId = $request['operation_id'] ?? null; if (!in_array($protocol, ['object-replica-v1', 'object-replica-verify-v1'], true) || !is_string($operationId) || preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1) { throw new \InvalidArgumentException( 'Storage replica helper request envelope is invalid' ); } if ($this->testInvoker !== null) { return self::normalizeReceipt( ($this->testInvoker)($request), $protocol, $operationId ); } $payload = json_encode( $request, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ); if (strlen($payload) > self::MAX_REQUEST_BYTES) { throw new \InvalidArgumentException( 'Storage replica helper request is too large' ); } $path = $this->binaryPath ?? (string) Config::get( 'cloud_storage_replicator_path', self::DEFAULT_PATH ); if (!str_starts_with($path, '/') || !is_file($path) || !is_executable($path)) { return self::unknownReceipt( $protocol, $operationId, 'replicator_unavailable' ); } $pipes = []; $process = @proc_open( [$path], [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'], ], $pipes, null, ['LANG' => 'C', 'LC_ALL' => 'C'], ['bypass_shell' => true] ); if (!is_resource($process) || count($pipes) !== 3) { return self::unknownReceipt( $protocol, $operationId, 'replicator_start_failed' ); } $stdout = ''; $stderrBytes = 0; $timedOut = false; $stdinFailed = false; $observedExit = null; $closedExit = -1; try { foreach ($pipes as $pipe) { stream_set_blocking($pipe, false); } $startedAt = microtime(true); $deadlineAt = is_int($request['deadline_at'] ?? null) ? $request['deadline_at'] : time() + 60; $stopAt = min( $startedAt + 21605.0, max($startedAt + 1.0, (float) $deadlineAt + 5.0) ); $offset = 0; $payloadBytes = strlen($payload); $stdinOpen = true; do { $remaining = $stopAt - microtime(true); if ($remaining <= 0) { $timedOut = true; self::terminateProcess($process); break; } $read = []; if (is_resource($pipes[1]) && !feof($pipes[1])) { $read[] = $pipes[1]; } if (is_resource($pipes[2]) && !feof($pipes[2])) { $read[] = $pipes[2]; } $write = $stdinOpen && $offset < $payloadBytes ? [$pipes[0]] : []; $except = []; if ($read !== [] || $write !== []) { $seconds = (int) floor(min($remaining, 0.1)); $microseconds = (int) floor( (min($remaining, 0.1) - $seconds) * 1000000 ); $selected = @stream_select( $read, $write, $except, $seconds, max(1, $microseconds) ); if ($selected === false) { $stdinFailed = true; self::terminateProcess($process); break; } } else { usleep((int) min(20000, max(1, $remaining * 1000000))); } foreach ($read as $stream) { if ($stream === $pipes[1]) { $chunk = stream_get_contents( $stream, max(1, self::MAX_RECEIPT_BYTES + 1 - strlen($stdout)) ); if (is_string($chunk)) { $stdout .= $chunk; } } else { $chunk = stream_get_contents( $stream, max(1, 65537 - $stderrBytes) ); if (is_string($chunk)) { $stderrBytes += strlen($chunk); } } } if ($write !== []) { $written = @fwrite( $pipes[0], substr($payload, $offset, 65536) ); if ($written === false) { $stdinFailed = true; self::terminateProcess($process); break; } $offset += $written; } if ($stdinOpen && $offset === $payloadBytes) { fclose($pipes[0]); $stdinOpen = false; } $status = proc_get_status($process); if (is_array($status) && is_int($status['exitcode'] ?? null) && $status['exitcode'] >= 0) { $observedExit = $status['exitcode']; } if (!is_array($status) || !($status['running'] ?? false)) { if ($offset !== $payloadBytes) { $stdinFailed = true; } break; } if (strlen($stdout) > self::MAX_RECEIPT_BYTES || $stderrBytes > 65536) { self::terminateProcess($process); break; } } while (true); if (is_resource($pipes[1])) { $tail = stream_get_contents( $pipes[1], max(1, self::MAX_RECEIPT_BYTES + 1 - strlen($stdout)) ); if (is_string($tail)) { $stdout .= $tail; } } } finally { foreach ($pipes as $pipe) { if (is_resource($pipe)) { fclose($pipe); } } $closedExit = proc_close($process); } $exit = $closedExit >= 0 ? $closedExit : $observedExit; if ($stdinFailed || $timedOut || strlen($stdout) > self::MAX_RECEIPT_BYTES || $exit !== 0 || $stderrBytes > 65536) { return self::unknownReceipt( $protocol, $operationId, $stdinFailed ? 'replicator_stdin_failed' : ($timedOut ? 'replicator_timeout' : 'replicator_invalid_exit') ); } try { $receipt = json_decode( $stdout, true, 64, JSON_THROW_ON_ERROR ); } catch (\JsonException) { return self::unknownReceipt( $protocol, $operationId, 'replicator_invalid_receipt' ); } if (!is_array($receipt)) { return self::unknownReceipt( $protocol, $operationId, 'replicator_invalid_receipt' ); } return self::normalizeReceipt($receipt, $protocol, $operationId); } /** A failed or timed-out pipe exchange must not leave the helper alive. */ private static function terminateProcess($process): void { @proc_terminate($process, 15); usleep(100000); $status = @proc_get_status($process); if (is_array($status) && ($status['running'] ?? false)) { @proc_terminate($process, 9); } } /** @param array $receipt @return array */ private static function normalizeReceipt( array $receipt, string $protocol, string $operationId ): array { $allowed = [ 'protocol', 'operation_id', 'outcome', 'effect_resolution', 'size_bytes', 'sha256', 'etag', 'version_id', 'error_code', ]; if (array_diff(array_keys($receipt), $allowed) !== [] || ($receipt['protocol'] ?? null) !== $protocol || ($receipt['operation_id'] ?? null) !== $operationId || !in_array($receipt['outcome'] ?? null, ['succeeded', 'failed'], true) || !in_array($receipt['effect_resolution'] ?? null, ['applied', 'not_applied', 'unknown'], true)) { return self::unknownReceipt( $protocol, $operationId, 'replicator_invalid_receipt' ); } if ($receipt['outcome'] === 'succeeded') { $versionId = $receipt['version_id'] ?? ''; if ($receipt['effect_resolution'] !== 'applied' || (isset($receipt['error_code']) && $receipt['error_code'] !== '') || !is_int($receipt['size_bytes'] ?? null) || $receipt['size_bytes'] < 1 || !is_string($receipt['sha256'] ?? null) || preg_match('/^[a-f0-9]{64}$/D', $receipt['sha256']) !== 1 || !self::providerIdentity($receipt['etag'] ?? null, 256) || !self::providerVersionIdentity( $versionId, 1024 )) { return self::unknownReceipt( $protocol, $operationId, 'replicator_invalid_receipt' ); } $receipt['version_id'] = $versionId; $receipt['error_code'] = ''; } else { $code = $receipt['error_code'] ?? null; if (!in_array($receipt['effect_resolution'], ['not_applied', 'unknown'], true) || !is_string($code) || preg_match('/^[a-z][a-z0-9_]{0,95}$/D', $code) !== 1) { return self::unknownReceipt( $protocol, $operationId, 'replicator_invalid_receipt' ); } } return $receipt; } private static function providerIdentity($value, int $maximum): bool { return is_string($value) && $value !== '' && strlen($value) <= $maximum && preg_match('/[\x00-\x1f\x7f]/', $value) !== 1; } private static function providerVersionIdentity( $value, int $maximum ): bool { return is_string($value) && strlen($value) <= $maximum && preg_match('/[\x00-\x1f\x7f]/', $value) !== 1; } /** @return array */ private static function unknownReceipt( string $protocol, string $operationId, string $code ): array { return [ 'protocol' => $protocol, 'operation_id' => $operationId, 'outcome' => 'failed', 'effect_resolution' => 'unknown', 'error_code' => $code, ]; } }