fix: bound trans stdout and sanitize win ps json
This commit is contained in:
+203
-2
@@ -1,5 +1,8 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { type UniDeskConfig, repoRoot } from "./config";
|
||||
import {
|
||||
decodeApplyPatchV2BulkRead,
|
||||
@@ -88,6 +91,22 @@ export interface SshRuntimeTimeoutHint {
|
||||
note: string;
|
||||
}
|
||||
|
||||
export interface SshStdoutTruncationHint {
|
||||
code: "ssh-stdout-truncated";
|
||||
level: "warning";
|
||||
providerId: string;
|
||||
route: string;
|
||||
transport: "backend-core-broker" | "frontend-websocket";
|
||||
invocationKind: SshInvocationKind;
|
||||
thresholdBytes: number;
|
||||
observedBytesAtTruncation: number;
|
||||
dumpPath: string | null;
|
||||
dumpError: string | null;
|
||||
message: string;
|
||||
action: string;
|
||||
note: string;
|
||||
}
|
||||
|
||||
const argvQuotedSshSubcommands = new Set(["git", "rg", "grep", "sed", "nl", "stat", "du", "ls", "cat", "head", "tail", "wc", "pwd"]);
|
||||
const nativeK3sKubeconfig = "/etc/rancher/k3s/k3s.yaml";
|
||||
const windowsBridgeCwd = "/mnt/c/Windows";
|
||||
@@ -96,6 +115,10 @@ const windowsCmdExeNativePath = "C:\\Windows\\System32\\cmd.exe";
|
||||
const defaultSshSlowWarningMs = 10_000;
|
||||
const defaultSshRuntimeTimeoutMs = 60_000;
|
||||
const maxSshRuntimeTimeoutMs = 60_000;
|
||||
const defaultSshStdoutStreamMaxBytes = 256 * 1024;
|
||||
const minSshStdoutStreamMaxBytes = 4 * 1024;
|
||||
const maxSshStdoutStreamMaxBytes = 16 * 1024 * 1024;
|
||||
const sshStdoutDumpDir = join(tmpdir(), "unidesk-cli-output");
|
||||
export const sshShellCompatibilityPrelude = 'printf(){ if [ "${1+x}" = x ] && [ "$1" = "-v" ] && [ -n "${BASH_VERSION:-}" ]; then command printf "$@"; return $?; fi; if [ "${1+x}" = x ] && [ "$1" = "--" ]; then shift; fi; command printf -- "$@"; }';
|
||||
export const sshUserToolPathPrelude = [
|
||||
'for unidesk_path_dir in "$HOME/.bun/bin" "$HOME/.local/bin" "$HOME/bin" "/root/.bun/bin"; do',
|
||||
@@ -1236,7 +1259,58 @@ function buildWindowsCmdStdinLauncherScript(cwd: string | null): string {
|
||||
].join(" ");
|
||||
}
|
||||
|
||||
function windowsPowerShellScriptPrelude(cwd: string | null): string {
|
||||
const windowsPowerShellJsonSafetyPreludeLines = [
|
||||
"function ConvertTo-UniDeskPlainJsonValue {",
|
||||
" param([object]$Value, [int]$Depth = 6, [int]$Level = 0)",
|
||||
" if ($null -eq $Value) { return $null }",
|
||||
" if ($Value -is [string]) { return [string]$Value }",
|
||||
" if ($Value -is [char]) { return [string]$Value }",
|
||||
" if ($Value -is [bool] -or $Value -is [byte] -or $Value -is [sbyte] -or $Value -is [int16] -or $Value -is [uint16] -or $Value -is [int] -or $Value -is [uint32] -or $Value -is [long] -or $Value -is [uint64] -or $Value -is [single] -or $Value -is [double] -or $Value -is [decimal]) { return $Value }",
|
||||
" if ($Value -is [datetime]) { return $Value.ToUniversalTime().ToString('o') }",
|
||||
" if ($Level -ge $Depth) { return [string]$Value }",
|
||||
" if ($Value -is [System.Collections.IDictionary]) {",
|
||||
" $map = [ordered]@{}",
|
||||
" foreach ($key in $Value.Keys) { $map[[string]$key] = ConvertTo-UniDeskPlainJsonValue -Value $Value[$key] -Depth $Depth -Level ($Level + 1) }",
|
||||
" return $map",
|
||||
" }",
|
||||
" if ($Value -is [System.Collections.IEnumerable] -and -not ($Value -is [string])) {",
|
||||
" $items = New-Object System.Collections.ArrayList",
|
||||
" foreach ($item in $Value) { [void]$items.Add((ConvertTo-UniDeskPlainJsonValue -Value $item -Depth $Depth -Level ($Level + 1))) }",
|
||||
" return $items.ToArray()",
|
||||
" }",
|
||||
" $skip = @('PSPath','PSParentPath','PSChildName','PSDrive','PSProvider','ReadCount')",
|
||||
" $props = @($Value.PSObject.Properties | Where-Object { @('NoteProperty','Property','AliasProperty') -contains $_.MemberType.ToString() -and -not ($skip -contains $_.Name) })",
|
||||
" if ($props.Count -eq 0) { return [string]$Value }",
|
||||
" $out = [ordered]@{}",
|
||||
" foreach ($prop in $props) {",
|
||||
" try { $out[$prop.Name] = ConvertTo-UniDeskPlainJsonValue -Value $prop.Value -Depth $Depth -Level ($Level + 1) }",
|
||||
" catch { $out[$prop.Name] = '<unreadable>' }",
|
||||
" }",
|
||||
" return $out",
|
||||
"}",
|
||||
"function ConvertTo-Json {",
|
||||
" [CmdletBinding()]",
|
||||
" param(",
|
||||
" [Parameter(ValueFromPipeline=$true)] [object]$InputObject,",
|
||||
" [int]$Depth = 2,",
|
||||
" [switch]$Compress,",
|
||||
" [switch]$EnumsAsStrings",
|
||||
" )",
|
||||
" begin { $items = New-Object System.Collections.ArrayList }",
|
||||
" process { [void]$items.Add($InputObject) }",
|
||||
" end {",
|
||||
" if ($items.Count -eq 0) { $value = $null } elseif ($items.Count -eq 1) { $value = $items[0] } else { $value = $items.ToArray() }",
|
||||
" $plain = ConvertTo-UniDeskPlainJsonValue -Value $value -Depth $Depth -Level 0",
|
||||
" if ($Compress) {",
|
||||
" Microsoft.PowerShell.Utility\\ConvertTo-Json -InputObject $plain -Depth $Depth -Compress",
|
||||
" } else {",
|
||||
" Microsoft.PowerShell.Utility\\ConvertTo-Json -InputObject $plain -Depth $Depth",
|
||||
" }",
|
||||
" }",
|
||||
"}",
|
||||
];
|
||||
|
||||
export function windowsPowerShellScriptPrelude(cwd: string | null): string {
|
||||
return [
|
||||
"$ErrorActionPreference = 'Stop'",
|
||||
"$ProgressPreference = 'SilentlyContinue'",
|
||||
@@ -1245,6 +1319,7 @@ function windowsPowerShellScriptPrelude(cwd: string | null): string {
|
||||
"$OutputEncoding = [System.Text.UTF8Encoding]::new()",
|
||||
"$env:PYTHONUTF8 = '1'",
|
||||
"$env:PYTHONIOENCODING = 'utf-8'",
|
||||
...windowsPowerShellJsonSafetyPreludeLines,
|
||||
...(cwd === null ? [] : [`Set-Location -LiteralPath ${powerShellSingleQuote(cwd)}`]),
|
||||
].join("\r\n") + "\r\n";
|
||||
}
|
||||
@@ -2274,6 +2349,118 @@ export function formatSshRuntimeTimeoutHint(hint: SshRuntimeTimeoutHint): string
|
||||
return `UNIDESK_SSH_RUNTIME_TIMEOUT ${JSON.stringify(hint)}\n`;
|
||||
}
|
||||
|
||||
export function sshStdoutStreamMaxBytes(env: NodeJS.ProcessEnv = process.env): number {
|
||||
const raw = env.UNIDESK_SSH_STDOUT_STREAM_MAX_BYTES ?? env.UNIDESK_TRAN_STDOUT_STREAM_MAX_BYTES;
|
||||
const parsed = raw === undefined ? NaN : Number(raw);
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) return defaultSshStdoutStreamMaxBytes;
|
||||
return Math.min(maxSshStdoutStreamMaxBytes, Math.max(minSshStdoutStreamMaxBytes, Math.trunc(parsed)));
|
||||
}
|
||||
|
||||
export function sshStdoutTruncationHint(options: {
|
||||
invocation: ParsedSshInvocation;
|
||||
transport: SshStdoutTruncationHint["transport"];
|
||||
thresholdBytes: number;
|
||||
observedBytesAtTruncation: number;
|
||||
dumpPath: string | null;
|
||||
dumpError?: string | null;
|
||||
}): SshStdoutTruncationHint {
|
||||
return {
|
||||
code: "ssh-stdout-truncated",
|
||||
level: "warning",
|
||||
providerId: safeProviderId(options.invocation.providerId),
|
||||
route: options.invocation.route.raw,
|
||||
transport: options.transport,
|
||||
invocationKind: options.invocation.parsed.invocationKind,
|
||||
thresholdBytes: options.thresholdBytes,
|
||||
observedBytesAtTruncation: options.observedBytesAtTruncation,
|
||||
dumpPath: options.dumpPath,
|
||||
dumpError: options.dumpError ?? null,
|
||||
message: `ssh stdout exceeded ${options.thresholdBytes} bytes; stdout is bounded and the complete stream is written to a local dump when possible.`,
|
||||
action: "Inspect the dump path, or rerun a narrower remote command with tail/paging instead of emitting full logs or huge JSON.",
|
||||
note: "This hint is written to stderr and intentionally does not echo the original remote command.",
|
||||
};
|
||||
}
|
||||
|
||||
export function formatSshStdoutTruncationHint(hint: SshStdoutTruncationHint): string {
|
||||
return `UNIDESK_SSH_STDOUT_TRUNCATED ${JSON.stringify(hint)}\n`;
|
||||
}
|
||||
|
||||
function sshStdoutDumpPath(invocation: ParsedSshInvocation): string {
|
||||
mkdirSync(sshStdoutDumpDir, { recursive: true, mode: 0o700 });
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/gu, "-");
|
||||
const suffix = randomBytes(4).toString("hex");
|
||||
const slug = `${invocation.providerId}-${invocation.route.raw}-${invocation.route.plane}`
|
||||
.replace(/[^A-Za-z0-9._-]+/gu, "-")
|
||||
.replace(/^-+|-+$/gu, "")
|
||||
.slice(0, 80) || "ssh";
|
||||
return join(sshStdoutDumpDir, `${timestamp}-${process.pid}-${suffix}-${slug}.stdout.bin`);
|
||||
}
|
||||
|
||||
export function createSshStdoutForwarder(options: {
|
||||
invocation: ParsedSshInvocation;
|
||||
transport: SshStdoutTruncationHint["transport"];
|
||||
maxBytes?: number;
|
||||
stdout?: NodeJS.WritableStream;
|
||||
}): { write: (chunk: Buffer) => string | null } {
|
||||
const stdout = options.stdout ?? process.stdout;
|
||||
const maxBytes = options.maxBytes ?? sshStdoutStreamMaxBytes();
|
||||
let observedBytes = 0;
|
||||
let forwardedBytes = 0;
|
||||
let truncated = false;
|
||||
let dumpPath: string | null = null;
|
||||
let dumpError: string | null = null;
|
||||
const bufferedChunks: Buffer[] = [];
|
||||
|
||||
const appendDump = (chunk: Buffer): void => {
|
||||
if (dumpError !== null) return;
|
||||
try {
|
||||
if (dumpPath === null) {
|
||||
dumpPath = sshStdoutDumpPath(options.invocation);
|
||||
writeFileSync(dumpPath, Buffer.alloc(0), { mode: 0o600 });
|
||||
for (const buffered of bufferedChunks) appendFileSync(dumpPath, buffered);
|
||||
bufferedChunks.length = 0;
|
||||
}
|
||||
appendFileSync(dumpPath, chunk);
|
||||
} catch (error) {
|
||||
dumpError = error instanceof Error ? error.message : String(error);
|
||||
dumpPath = null;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
write(chunk: Buffer): string | null {
|
||||
observedBytes += chunk.length;
|
||||
if (!truncated && observedBytes <= maxBytes) {
|
||||
bufferedChunks.push(Buffer.from(chunk));
|
||||
stdout.write(chunk);
|
||||
forwardedBytes += chunk.length;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!truncated) {
|
||||
truncated = true;
|
||||
const remaining = Math.max(0, maxBytes - forwardedBytes);
|
||||
if (remaining > 0) {
|
||||
stdout.write(chunk.subarray(0, remaining));
|
||||
forwardedBytes += remaining;
|
||||
}
|
||||
appendDump(chunk);
|
||||
return formatSshStdoutTruncationHint(sshStdoutTruncationHint({
|
||||
invocation: options.invocation,
|
||||
transport: options.transport,
|
||||
thresholdBytes: maxBytes,
|
||||
observedBytesAtTruncation: observedBytes,
|
||||
dumpPath,
|
||||
dumpError,
|
||||
}));
|
||||
}
|
||||
|
||||
appendDump(chunk);
|
||||
return null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function brokerSource(): string {
|
||||
return String.raw`
|
||||
const open = JSON.parse(process.argv[2] || process.argv[1] || "{}");
|
||||
@@ -2753,7 +2940,21 @@ export async function runSsh(config: UniDeskConfig, providerId: string, args: st
|
||||
const text = Buffer.isBuffer(chunk) ? chunk.toString("utf8") : chunk;
|
||||
stderrTail = (stderrTail + text).slice(-16_384);
|
||||
};
|
||||
child.stdout.pipe(process.stdout);
|
||||
if (parsed.remoteCommand === null) {
|
||||
child.stdout.pipe(process.stdout);
|
||||
} else {
|
||||
const stdoutForwarder = createSshStdoutForwarder({
|
||||
invocation,
|
||||
transport: "backend-core-broker",
|
||||
});
|
||||
child.stdout.on("data", (chunk: Buffer) => {
|
||||
const hint = stdoutForwarder.write(chunk);
|
||||
if (hint !== null) {
|
||||
appendStderrTail(hint);
|
||||
process.stderr.write(hint);
|
||||
}
|
||||
});
|
||||
}
|
||||
child.stderr.on("data", (chunk: Buffer) => {
|
||||
appendStderrTail(chunk);
|
||||
process.stderr.write(chunk);
|
||||
|
||||
Reference in New Issue
Block a user