'0..3', 'warning' => '4', 'notice' => '5', 'info' => '6', 'debug' => '7', ]; private const PRIORITY_LABELS = [ 0 => '紧急', 1 => '警报', 2 => '严重', 3 => '错误', 4 => '警告', 5 => '通知', 6 => '信息', 7 => '调试', ]; public static function page(array $actor, array $input): array { self::authorize($actor); return (new self())->read($input); } /** * Read one immutable journal entry by its opaque journal cursor. * Cursors are only accepted from the fixed unit allowlist and the returned * cursor is checked again after journalctl has read the entry. */ public static function detail(array $actor, array $input): array { self::authorize($actor); $cursor = self::cleanCursor($input['cursor'] ?? ''); if ((string) ($input['cursor'] ?? '') === '' || $cursor === '') { throw new CloudError('日志游标无效', 400); } $unit = trim((string) ($input['unit'] ?? '')); if (!in_array($unit, self::UNITS, true)) { throw new CloudError('日志服务无效,请刷新列表后重试。', 400); } $timestamp = self::cleanJournalTimestamp($input['timestamp'] ?? ''); if ($timestamp === '') { throw new CloudError('日志时间无效,请刷新列表后重试。', 400); } $args = [ '--no-pager', '--output=json', '--utc', '--all', '--system', // journalctl cursor seeks are version-sensitive when combined // with filters. The list carries the exact UTC timestamp and // unit, so use a one-timestamp window and verify __CURSOR after // parsing. This remains exact even when the cursor was rotated // or a deployed systemd release advances once after seeking. '--since=' . $timestamp, '--until=' . $timestamp, '--lines=all', '--unit=' . $unit, ]; $result = (new self())->run($args, self::MAX_DETAIL_OUTPUT_BYTES); self::assertReadableResult($result); $entry = self::parseJournalDetailJson($result['stdout'], $cursor, [$unit]); if ($entry === null) { throw new CloudError('日志已轮转或不存在,请刷新列表后重试。', 404); } return ['item' => $entry, 'generated_at' => time()]; } /** * Parse journalctl's JSON-lines output. Kept public for contract tests and * for deployments that want to validate a captured journal fixture. */ public static function parseJournalJson(string $output, ?array $allowedUnits = null, bool $includeSearchMessage = false): array { $allowed = array_fill_keys($allowedUnits ?? self::UNITS, true); $rows = []; foreach (preg_split('/\r?\n/', $output) ?: [] as $line) { if ($line === '') { continue; } try { $entry = json_decode($line, true, 32, JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR); } catch (\Throwable $ignored) { continue; } if (!is_array($entry)) { continue; } $unit = self::cleanText($entry['_SYSTEMD_UNIT'] ?? ''); if ($unit === '' || !isset($allowed[$unit])) { continue; } $priority = filter_var($entry['PRIORITY'] ?? null, FILTER_VALIDATE_INT); if ($priority === false || $priority === null || $priority < 0 || $priority > 7) { $priority = 6; } $micros = filter_var($entry['__REALTIME_TIMESTAMP'] ?? null, FILTER_VALIDATE_INT); $timestamp = $micros !== false && $micros !== null && $micros > 0 ? self::timestamp((int) $micros) : null; [$message, $messageTruncated] = self::cleanBounded( $entry['MESSAGE'] ?? '', self::MAX_MESSAGE_LENGTH ); [$message, $messageRedacted] = self::redactDetailValue($message); $row = [ 'timestamp' => $timestamp, 'unit' => $unit, 'priority' => $priority, 'level' => self::PRIORITY_LABELS[$priority], 'message' => $message, 'message_truncated' => $messageTruncated, 'message_redacted' => $messageRedacted, 'pid' => self::cleanText($entry['_PID'] ?? ''), 'process' => self::cleanText($entry['_COMM'] ?? ($entry['SYSLOG_IDENTIFIER'] ?? '')), 'hostname' => self::cleanText($entry['_HOSTNAME'] ?? ''), 'cursor' => self::cleanCursor($entry['__CURSOR'] ?? ''), ]; if ($includeSearchMessage) { // Keep matching independent from the short table preview; // the internal value is removed before the response leaves // this service. $row['_search_message'] = self::cleanBounded( $entry['MESSAGE'] ?? '', self::MAX_DETAIL_VALUE_LENGTH )[0]; $searchFields = []; foreach ($entry as $fieldName => $fieldValue) { $fieldName = (string) $fieldName; if (!preg_match('/^[A-Z_][A-Z0-9_]{0,127}$/D', $fieldName) || self::omitDetailField($fieldName)) { continue; } $searchFields[] = $fieldName . ' ' . self::cleanBounded($fieldValue, 2048)[0]; if (count($searchFields) >= self::MAX_DETAIL_FIELDS) { break; } } $row['_search_fields'] = self::cleanBounded( implode(' ', $searchFields), self::MAX_DETAIL_VALUE_LENGTH )[0]; } $rows[] = $row; } return $rows; } /** * Parse one detail response and retain the useful journald metadata. The * command is still restricted to the application unit allowlist; command * lines, executable paths and core-dump fields are deliberately omitted. */ public static function parseJournalDetailJson(string $output, string $cursor, ?array $allowedUnits = null): ?array { $allowed = array_fill_keys($allowedUnits ?? self::UNITS, true); foreach (preg_split('/\r?\n/', $output) ?: [] as $line) { if ($line === '') { continue; } try { $entry = json_decode($line, true, 32, JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR); } catch (\Throwable $ignored) { continue; } if (!is_array($entry)) { continue; } $entryCursor = self::cleanCursor($entry['__CURSOR'] ?? ''); $unit = self::cleanText($entry['_SYSTEMD_UNIT'] ?? ''); if ($entryCursor !== $cursor || $unit === '' || !isset($allowed[$unit])) { continue; } $priority = filter_var($entry['PRIORITY'] ?? null, FILTER_VALIDATE_INT); if ($priority === false || $priority === null || $priority < 0 || $priority > 7) { $priority = 6; } [$message, $messageTruncated] = self::cleanBounded( $entry['MESSAGE'] ?? '', self::MAX_DETAIL_VALUE_LENGTH ); [$message, $messageRedacted] = self::redactDetailValue($message); $fields = []; $omittedFields = []; $truncatedFields = []; $redactedFields = []; foreach ($entry as $name => $value) { $name = (string) $name; if (!preg_match('/^[A-Z_][A-Z0-9_]{0,127}$/D', $name)) { continue; } if (self::omitDetailField($name)) { $omittedFields[] = $name; continue; } if (count($fields) >= self::MAX_DETAIL_FIELDS) { $omittedFields[] = $name; continue; } [$clean, $truncated] = self::cleanBounded($value, self::MAX_DETAIL_VALUE_LENGTH); if (self::redactDetailField($name)) { $clean = '[已隐藏]'; $truncated = false; $redactedFields[] = $name; } else { [$clean, $valueRedacted] = self::redactDetailValue($clean); if ($valueRedacted) { $redactedFields[] = $name; } } $fields[$name] = $clean; if ($truncated) { $truncatedFields[] = $name; } } ksort($fields); sort($omittedFields); sort($truncatedFields); sort($redactedFields); $micros = filter_var($entry['__REALTIME_TIMESTAMP'] ?? null, FILTER_VALIDATE_INT); return [ 'timestamp' => $micros !== false && $micros !== null && $micros > 0 ? self::timestamp((int) $micros) : null, 'monotonic_timestamp' => self::cleanText($entry['__MONOTONIC_TIMESTAMP'] ?? ''), 'unit' => $unit, 'priority' => $priority, 'level' => self::PRIORITY_LABELS[$priority], 'message' => $message, 'message_truncated' => $messageTruncated, 'message_redacted' => $messageRedacted, 'pid' => self::cleanText($entry['_PID'] ?? ''), 'process' => self::cleanText($entry['_COMM'] ?? ($entry['SYSLOG_IDENTIFIER'] ?? '')), 'hostname' => self::cleanText($entry['_HOSTNAME'] ?? ''), 'cursor' => $entryCursor, 'fields' => $fields, 'omitted_fields' => array_values(array_unique($omittedFields)), 'truncated_fields' => array_values(array_unique($truncatedFields)), 'redacted_fields' => array_values(array_unique($redactedFields)), ]; } return null; } private function read(array $input): array { $filters = $this->filters($input); $units = $filters['unit'] === '' ? self::UNITS : [$filters['unit']]; // journalctl rejects --since together with a cursor. Cursor pages // therefore read a bounded older window and apply the already // validated time bounds below in PHP. $fetchLines = $filters['cursor'] !== '' ? self::MAX_FETCH_LINES : min(self::MAX_FETCH_LINES, $filters['limit'] + 1); $args = [ '--no-pager', '--output=json', '--utc', '--all', '--system', '--reverse', '--lines=' . $fetchLines, ]; foreach ($units as $unit) { $args[] = '--unit=' . $unit; } if ($filters['priority'] !== '') { $args[] = '--priority=' . self::PRIORITIES[$filters['priority']]; } if ($filters['cursor'] === '') { if ($filters['since'] !== '') { $args[] = '--since=@' . $this->epoch($filters['since']); } if ($filters['until'] !== '') { $args[] = '--until=@' . $this->epoch($filters['until']); } } elseif ($filters['until'] !== '') { // --until is compatible with --cursor and narrows the reverse // scan; --since is applied by filterRowsByTime() below because // systemd treats it as mutually exclusive with a cursor. $args[] = '--until=@' . $this->epoch($filters['until']); } if ($filters['cursor'] !== '') { // With --reverse, --cursor seeks to the boundary and walks toward // older records. The boundary is normally excluded by journalctl; // the parser also removes it defensively if an older release // emits it. This avoids the known --after-cursor + --unit skip on // older systemd releases. $args[] = '--cursor=' . $filters['cursor']; } $result = $this->run($args); self::assertReadableResult($result); $rawRows = self::parseJournalJson($result['stdout'], $units, $filters['query'] !== ''); $boundaryEmitted = false; if ($filters['cursor'] !== '' && $rawRows !== []) { // journalctl seeks to the cursor and advances once before it // starts output. Some older releases emitted the boundary when // filters were present, so discard it defensively when it does // appear; never require it to be present for a valid page. foreach ($rawRows as $row) { if (($row['cursor'] ?? '') === $filters['cursor']) { $boundaryEmitted = true; break; } } $rawRows = array_values(array_filter($rawRows, static fn (array $row): bool => ($row['cursor'] ?? '') !== $filters['cursor'])); } $rawCount = count($rawRows); $rawTail = $rawRows === [] ? null : $rawRows[array_key_last($rawRows)]; $rawTailCursor = is_array($rawTail) ? ($rawTail['cursor'] ?? null) : null; $sinceEpoch = $filters['since'] === '' ? null : $this->epoch($filters['since']); $rawTailEpoch = is_array($rawTail) ? $this->rowEpoch($rawTail) : null; $reachedSince = $sinceEpoch !== null && $rawTailEpoch !== null && $rawTailEpoch < $sinceEpoch; $rows = $this->filterRowsByTime($rawRows, $filters['since'], $filters['until']); if ($filters['query'] !== '') { $needle = function_exists('mb_strtolower') ? mb_strtolower($filters['query'], 'UTF-8') : strtolower($filters['query']); $rows = array_values(array_filter($rows, static function (array $row) use ($needle): bool { $haystack = $row['unit'] . ' ' . $row['process'] . ' ' . ($row['_search_message'] ?? $row['message']) . ' ' . ($row['_search_fields'] ?? ''); $haystack = function_exists('mb_strtolower') ? mb_strtolower($haystack, 'UTF-8') : strtolower($haystack); return str_contains($haystack, $needle); })); } // A cursor page normally excludes its boundary before output. Keep // the full bounded window in the continuation calculation; subtracting // one here would advertise another page when exactly the limit was // returned. $windowSize = $fetchLines; $windowFull = ($rawCount + ($boundaryEmitted ? 1 : 0)) >= $windowSize; $scanMayContinue = ($filters['query'] !== '' || $filters['cursor'] !== '') && $windowFull && !$reachedSince; $hasMore = count($rows) > $filters['limit'] || $scanMayContinue; if ($hasMore) { $rows = array_slice($rows, 0, $filters['limit']); } $nextCursor = $hasMore ? (($rows !== [] ? ($rows[array_key_last($rows)]['cursor'] ?? null) : null) ?: $rawTailCursor) : null; // A journal without cursors cannot be paged safely; report the // bounded first window instead of presenting a dead “load more” link. $hasMore = $hasMore && $nextCursor !== null && $nextCursor !== ''; foreach ($rows as &$row) { unset($row['_search_message']); unset($row['_search_fields']); } unset($row); $errorCount = 0; $warningCount = 0; foreach ($rows as $row) { if ((int) $row['priority'] <= 3) { $errorCount++; } elseif ((int) $row['priority'] === 4) { $warningCount++; } } return [ 'items' => $rows, 'units' => self::UNITS, 'summary' => [ 'total' => count($rows), 'error_count' => $errorCount, 'warning_count' => $warningCount, 'latest_at' => $rows[0]['timestamp'] ?? null, ], 'filters' => [ 'unit' => $filters['unit'], 'priority' => $filters['priority'], 'query' => $filters['query'], 'since' => $filters['since'], 'until' => $filters['until'], 'limit' => $filters['limit'], ], 'pagination' => ['next_cursor' => $nextCursor, 'has_more' => $hasMore], 'generated_at' => time(), 'limited' => $hasMore, ]; } private function filterRowsByTime(array $rows, string $since, string $until): array { if ($since === '' && $until === '') { return $rows; } $sinceEpoch = $since === '' ? null : $this->epoch($since); $untilEpoch = $until === '' ? null : $this->epoch($until); return array_values(array_filter($rows, function (array $row) use ($sinceEpoch, $untilEpoch): bool { $epoch = $this->rowEpoch($row); if ($epoch === null) { return false; } return ($sinceEpoch === null || $epoch >= $sinceEpoch) && ($untilEpoch === null || $epoch <= $untilEpoch); })); } private function rowEpoch(array $row): ?int { if (!is_string($row['timestamp'] ?? null) || $row['timestamp'] === '') { return null; } try { return (new DateTimeImmutable($row['timestamp']))->getTimestamp(); } catch (\Throwable $ignored) { return null; } } private function filters(array $input): array { $unit = trim((string) ($input['unit'] ?? '')); if ($unit !== '' && !in_array($unit, self::UNITS, true)) { throw new CloudError('日志服务筛选项无效', 400); } $priority = strtolower(trim((string) ($input['priority'] ?? ''))); if ($priority !== '' && !isset(self::PRIORITIES[$priority])) { throw new CloudError('日志级别筛选项无效', 400); } $query = trim((string) ($input['query'] ?? '')); if (strlen($query) > self::MAX_QUERY_LENGTH) { throw new CloudError('日志关键词过长', 400); } if (preg_match('//u', $query) !== 1 || preg_match('/[\x00-\x1F\x7F]/', $query) === 1) { throw new CloudError('日志关键词包含不可用字符', 400); } $limit = (int) ($input['limit'] ?? 100); if (!in_array($limit, [50, 100, 200], true)) { throw new CloudError('日志条数只能为 50、100 或 200', 400); } $since = $this->dateFilter($input['since'] ?? ''); $until = $this->dateFilter($input['until'] ?? ''); if ($since !== '' && $until !== '' && $this->epoch($since) > $this->epoch($until)) { throw new CloudError('日志开始时间不能晚于结束时间', 400); } $cursor = self::cleanCursor($input['cursor'] ?? ''); if ((string) ($input['cursor'] ?? '') !== '' && $cursor === '') { throw new CloudError('日志游标无效', 400); } return compact('unit', 'priority', 'query', 'limit', 'since', 'until', 'cursor'); } private function dateFilter($value): string { $value = trim((string) $value); if ($value === '') { return ''; } if (!preg_match('/^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2})?$/D', $value)) { throw new CloudError('日志时间格式无效', 400); } $value = str_replace(' ', 'T', $value); $format = strlen($value) === 16 ? '!Y-m-d\\TH:i' : '!Y-m-d\\TH:i:s'; $zone = new DateTimeZone(date_default_timezone_get()); $date = DateTimeImmutable::createFromFormat($format, $value, $zone); $errors = DateTimeImmutable::getLastErrors(); if (!$date || ($errors !== false && ($errors['warning_count'] || $errors['error_count']))) { throw new CloudError('日志时间无效', 400); } // Keep the canonical value in the panel timezone so datetime-local // controls round-trip correctly. epoch() performs the UTC conversion // only when building journalctl arguments. return $date->format('Y-m-d\\TH:i'); } private function epoch(string $value): int { $date = new DateTimeImmutable($value, new DateTimeZone(date_default_timezone_get())); return $date->getTimestamp(); } private function run(array $args, int $maxOutputBytes = self::MAX_OUTPUT_BYTES): array { $binary = is_executable('/usr/bin/journalctl') ? '/usr/bin/journalctl' : (is_executable('/bin/journalctl') ? '/bin/journalctl' : ''); if ($binary === '') { throw new CloudError('系统日志工具 journalctl 不可用', 503); } $stdin = @fopen('/dev/null', 'r'); if (!is_resource($stdin)) { throw new CloudError('系统日志读取暂不可用', 503); } $descriptors = [0 => $stdin, 1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; $process = @proc_open(array_merge([$binary], $args), $descriptors, $pipes, null, null, ['bypass_shell' => true]); if (!is_resource($process)) { @fclose($stdin); throw new CloudError('系统日志读取进程无法启动', 503); } foreach ([1, 2] as $index) { if (isset($pipes[$index]) && is_resource($pipes[$index])) { stream_set_blocking($pipes[$index], false); } } $stdout = ''; $stderr = ''; $status = ['running' => true, 'exitcode' => -1]; $observedExitCode = null; $timedOut = false; $deadline = hrtime(true) + (self::MAX_PROCESS_SECONDS * 1000000000); try { while (true) { if (function_exists('connection_aborted') && connection_aborted()) { throw new CloudError('日志读取已取消', 499); } $observed = @proc_get_status($process); if (is_array($observed)) { $status = $observed; if (($observed['exitcode'] ?? -1) >= 0) { $observedExitCode = (int) $observed['exitcode']; } } $read = []; foreach ([1, 2] as $index) { if (isset($pipes[$index]) && is_resource($pipes[$index]) && !feof($pipes[$index])) { $read[] = $pipes[$index]; } } if (!($status['running'] ?? false) && $read === []) { break; } if (hrtime(true) >= $deadline) { $timedOut = true; $this->terminateProcess($process); break; } if ($read === []) { usleep(1000); continue; } $write = null; $except = null; $remaining = max(0, $deadline - hrtime(true)); $seconds = (int) min(1, intdiv($remaining, 1000000000)); $microseconds = (int) min(999999, intdiv($remaining - ($seconds * 1000000000), 1000)); $ready = @stream_select($read, $write, $except, $seconds, $microseconds); if ($ready === false || $ready === 0) { continue; } foreach ($read as $stream) { $chunk = @fread($stream, 65536); if ($chunk === false || $chunk === '') { continue; } if ($stream === ($pipes[1] ?? null)) { $stdout .= $chunk; if (strlen($stdout) > $maxOutputBytes) { throw new CloudError('系统日志输出过大,请缩小时间范围后重试。', 422); } } elseif (strlen($stderr) < self::MAX_STDERR_BYTES) { $remainingBytes = self::MAX_STDERR_BYTES - strlen($stderr); $stderr .= substr($chunk, 0, $remainingBytes); } } } } finally { if ($timedOut || ($status['running'] ?? false)) { $this->terminateProcess($process); } foreach ([1, 2] as $index) { if (isset($pipes[$index]) && is_resource($pipes[$index])) { @fclose($pipes[$index]); } } $exitCode = @proc_close($process); @fclose($stdin); } // PHP can return -1 from proc_close after proc_get_status() already // observed the child exit. Preserve that reliable status code. if ($exitCode === -1 && $observedExitCode !== null) { $exitCode = $observedExitCode; } if ($timedOut) { throw new CloudError('读取系统日志超时,请缩小时间范围后重试。', 503); } return ['exit_code' => $exitCode, 'stdout' => $stdout, 'stderr' => $stderr]; } private function terminateProcess($process): void { if (!is_resource($process)) { return; } @proc_terminate($process, 9); $deadline = hrtime(true) + 250000000; do { $status = @proc_get_status($process); if (!is_array($status) || !($status['running'] ?? false)) { return; } usleep(20000); } while (hrtime(true) < $deadline); } private static function authorize(array $actor): void { if (!($actor['is_admin'] ?? false)) { throw new CloudError('需要管理员权限', 403); } } private static function assertReadableResult(array &$result): void { $stderr = strtolower((string) ($result['stderr'] ?? '')); $permissionHint = preg_match( '/not seeing messages|systemd-journal|insufficient permissions|permission denied|access denied|failed to open.*journal/', $stderr ) === 1; // journalctl may still return a partial user journal with exit code 0 // while warning that system records are inaccessible. Treat that as // an error instead of silently presenting an incomplete page. if ($permissionHint) { throw new CloudError( '系统日志读取权限不足,请将 PHP-FPM 用户加入 systemd-journal 组后重启 PHP-FPM。', 503 ); } if ($result['exit_code'] === 0) { return; } if (str_contains($stderr, 'no journal files')) { $result['stdout'] = ''; return; } if (str_contains($stderr, 'unknown option') || str_contains($stderr, 'unrecognized option')) { throw new CloudError('系统日志工具不支持当前查询参数,请升级 systemd 后重试。', 503); } if (str_contains($stderr, 'cursor')) { throw new CloudError('日志已轮转或游标无效,请刷新列表后重试。', 404); } throw new CloudError( '系统日志暂时无法读取,请确认 PHP-FPM 用户有 systemd journal 读取权限。', 503 ); } private static function timestamp(int $micros): string { $seconds = intdiv($micros, 1000000); $fraction = $micros % 1000000; return gmdate('Y-m-d\\TH:i:s', $seconds) . sprintf('.%06dZ', $fraction); } private static function cleanMessage($value): string { return self::cleanBounded($value, self::MAX_MESSAGE_LENGTH)[0]; } private static function cleanText($value): string { return self::cleanMessage($value); } private static function cleanCursor($value): string { $value = trim((string) $value); return strlen($value) <= self::MAX_CURSOR_LENGTH && preg_match('/^[A-Za-z0-9_.:;=+@\\/-]+$/D', $value) === 1 ? $value : ''; } private static function cleanJournalTimestamp($value): string { $value = trim((string) $value); if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{6}Z$/D', $value)) { return ''; } try { $date = new DateTimeImmutable($value, new DateTimeZone('UTC')); } catch (\Throwable $ignored) { return ''; } $canonical = $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\\TH:i:s.u\\Z'); return hash_equals($canonical, $value) ? $canonical : ''; } private static function cleanBounded($value, int $limit): array { $value = is_scalar($value) || $value === null ? (string) $value : (string) json_encode($value, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); $value = preg_replace( '/(?:\\x1B\\[[0-?]*[ -\\/]*[@-~])|[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]/', '', $value ) ?? ''; $length = function_exists('mb_strlen') ? mb_strlen($value, 'UTF-8') : strlen($value); $truncated = $length > $limit; if ($truncated) { $value = function_exists('mb_substr') ? mb_substr($value, 0, $limit, 'UTF-8') : substr($value, 0, $limit); } return [$value, $truncated]; } private static function omitDetailField(string $name): bool { return in_array($name, ['_CMDLINE', '_EXE', 'CMDLINE', 'EXE'], true) || str_starts_with($name, 'COREDUMP'); } private static function redactDetailField(string $name): bool { return preg_match( '/(?:PASSWORD|PASSWD|SECRET|TOKEN|PRIVATE(?:[_-]?KEY)?|API[_-]?KEY|AUTH(?:ORIZATION|ENTICATION)?|BEARER|COOKIE|CREDENTIAL|DATABASE[_-]?(?:URL|DSN|PASSWORD)?|(?:^|_)ENV(?:IRONMENT)?(?:$|_)|HMAC|JWT|ACCESS[_-]?KEY|CLIENT[_-]?SECRET|SIGN(?:ING)?[_-]?KEY|ENCRYPT(?:ION)?[_-]?KEY|PEM)/i', $name ) === 1; } /** * Mask obvious credential material embedded in otherwise useful messages * or diagnostic fields. This is deliberately pattern based: arbitrary log * prose cannot be proven secret-free, but common bearer/assignment forms * should never be copied into an administrator response verbatim. * * @return array{0:string,1:bool} */ private static function redactDetailValue(string $value): array { $original = $value; $value = preg_replace( '/-----BEGIN (?:[A-Z]+ )?PRIVATE KEY-----.*?(?:-----END (?:[A-Z]+ )?PRIVATE KEY-----|$)/is', '[私密内容已隐藏]', $value ) ?? $value; $value = preg_replace('/\\bBearer\\s+[^\\s,;]+/i', 'Bearer [凭据已隐藏]', $value) ?? $value; $value = preg_replace('/\\bsk-[A-Za-z0-9_-]{8,}\\b/i', '[凭据已隐藏]', $value) ?? $value; $value = preg_replace_callback( '/(\\b(?:password|passwd|password_hash|api[_ -]?key|api[_ -]?token|access[_ -]?token|refresh[_ -]?token|token|secret|client_secret|authorization|credential|private[_ -]?key)\\s*[:=]\\s*)(?:"[^"]*"|\'[^\']*\'|[^\\s,;]+)/i', static fn (array $match): string => $match[1] . '[凭据已隐藏]', $value ) ?? $value; $value = preg_replace( '#(https?://[^:/\\s]+:)[^@\\s/]+@#i', '$1[凭据已隐藏]@', $value ) ?? $value; $value = preg_replace( '/\\beyJ[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+\\b/', '[凭据已隐藏]', $value ) ?? $value; return [$value, $value !== $original]; } }