feat(ssh): implement KnownHostsService TOFU/MITM guard, sudo helper, and multiplexed SSHPool with ProxyJump
This commit is contained in:
parent
df030ba8ba
commit
b742f0ebf8
4 changed files with 1129 additions and 1 deletions
50
src/server/ssh/sudo.ts
Normal file
50
src/server/ssh/sudo.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
export function posixSingleQuote(value: string): string {
|
||||
return `'${value.replace(/'/g, "'\\''")}'`;
|
||||
}
|
||||
|
||||
export function buildEnvPrefix(env?: Record<string, string>): string {
|
||||
if (!env) return '';
|
||||
const parts: string[] = [];
|
||||
for (const [key, val] of Object.entries(env)) {
|
||||
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
|
||||
throw new Error(`Invalid environment variable name: ${key}`);
|
||||
}
|
||||
parts.push(`export ${key}=${posixSingleQuote(val)}`);
|
||||
}
|
||||
return parts.length > 0 ? `${parts.join('; ')}; ` : '';
|
||||
}
|
||||
|
||||
export function prepareSudoExecution(
|
||||
command: string,
|
||||
options: { sudoPassword?: string; stdin?: Buffer | string } = {},
|
||||
): { command: string; stdin?: Buffer } {
|
||||
const quoted = posixSingleQuote(command);
|
||||
if (options.sudoPassword !== undefined && options.sudoPassword.length > 0) {
|
||||
const pwPrefix = Buffer.from(`${options.sudoPassword}\n`, 'utf8');
|
||||
let combined = pwPrefix;
|
||||
if (options.stdin !== undefined) {
|
||||
const extra =
|
||||
typeof options.stdin === 'string'
|
||||
? Buffer.from(options.stdin, 'utf8')
|
||||
: options.stdin;
|
||||
combined = Buffer.concat([pwPrefix, extra]);
|
||||
pwPrefix.fill(0);
|
||||
}
|
||||
return {
|
||||
command: `sudo -S -p '' sh -c ${quoted}`,
|
||||
stdin: combined,
|
||||
};
|
||||
}
|
||||
|
||||
const extraStdin =
|
||||
options.stdin === undefined
|
||||
? undefined
|
||||
: typeof options.stdin === 'string'
|
||||
? Buffer.from(options.stdin, 'utf8')
|
||||
: Buffer.from(options.stdin);
|
||||
|
||||
return {
|
||||
command: `sudo -n sh -c ${quoted}`,
|
||||
stdin: extraStdin,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue