security(ssh,sudo,keys): fix Sudo stdin leak, SSHPool Panic Lock/ProxyJump races, multi-algo known_hosts & OpenSSH import

This commit is contained in:
tyuXX 2026-11-27 22:40:40 +00:00
parent 9a27525def
commit 716627afdd
7 changed files with 944 additions and 151 deletions

View file

@ -1,3 +1,5 @@
export const SUDO_PTY_PROMPT = '[gcoord-sudo-prompt] ';
export function posixSingleQuote(value: string): string {
return `'${value.replace(/'/g, "'\\''")}'`;
}
@ -16,13 +18,31 @@ export function buildEnvPrefix(env?: Record<string, string>): string {
export function prepareSudoExecution(
command: string,
options: { sudoPassword?: string; stdin?: Buffer | string } = {},
): { command: string; stdin?: Buffer } {
options: {
sudoPassword?: string;
stdin?: Buffer | string;
ptyPrompt?: boolean;
} = {},
): { command: string; stdin?: Buffer; ptyPassword?: string } {
const quoted = posixSingleQuote(command);
if (options.sudoPassword !== undefined && options.sudoPassword.length > 0) {
if (options.ptyPrompt) {
const extraStdin =
options.stdin === undefined
? undefined
: typeof options.stdin === 'string'
? Buffer.from(options.stdin, 'utf8')
: Buffer.from(options.stdin);
return {
command: `sudo -S -k -p ${posixSingleQuote(SUDO_PTY_PROMPT)} sh -c ${quoted}`,
stdin: extraStdin,
ptyPassword: options.sudoPassword,
};
}
const pwPrefix = Buffer.from(`${options.sudoPassword}\n`, 'utf8');
let combined = pwPrefix;
if (options.stdin !== undefined) {
if (options.stdin !== undefined && options.stdin.length > 0) {
const extra =
typeof options.stdin === 'string'
? Buffer.from(options.stdin, 'utf8')
@ -31,7 +51,7 @@ export function prepareSudoExecution(
pwPrefix.fill(0);
}
return {
command: `sudo -S -p '' sh -c ${quoted}`,
command: `sudo -S -k -p '' sh -c ${quoted}`,
stdin: combined,
};
}