Files
Netcatty/domain/terminalPromptSecurity.ts
T
6cdf6b836b feat(terminal): show live jump hosts and fix AI prompt readiness (#4316)
* feat(terminal): extract hostnames from shell prompts

* fix(ai): accept decorated two-line shell prompts

* feat(tabs): add prompt-host title presentation

* feat(session): track prompt-host transitions

* feat(terminal): publish live prompt hostnames to tabs

* fix(session): include prompt host action in publisher runtime

* fix(ai): accept redrawn JumpServer shell prompts

* fix(tabs): prefer live prompt host behind user labels

* build(windows): normalize cross-built native modules

* fix(build): avoid config lint global shadowing

* fix(session): constrain live prompt hostname parsing

* fix(tabs): refresh workspace labels from live prompt hosts

* build(windows): validate cross-built native payloads

* fix(session): cover strict zsh and jump host prompts

* test(tabs): type live workspace prompt fixtures

* build(windows): scan packed native modules safely

* build(windows): add explicit cross-build script

* build(windows): make cross packaging self-contained

* fix(terminal): address review findings on JumpServer prompts and Windows cross-pack

* fix(windows): preserve child-process detection in cross-built packages

* fix(windows): require patched conpty.node during cross-packaging

---------

Co-authored-by: zjio26 <zjao2026@outlook.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-11-24 10:50:25 +08:00

88 lines
3.2 KiB
TypeScript

import {
MAX_PROMPT_SECURITY_TAIL_CHARS,
extractTerminalPromptHostname,
isBashPs2ContinuationLine,
isConfirmedTerminalShellPrompt,
isJumpServerMenuPrompt,
isSensitiveTerminalChallenge,
isUntrustedTerminalInputPrompt,
splitTerminalLogicalLines,
stripTerminalControlSequences,
type ConfirmedPromptOptions,
} from '@/domain/terminalPromptSecurity.shared.cjs';
const ESCAPE_SEQUENCE = "\\x" + "1b";
const CURSOR_POSITION_PATTERN = new RegExp(`${ESCAPE_SEQUENCE}\\[\\d*(?:;(\\d*))?[Hf]`, 'gu');
/**
* Keep enough raw output to recognize authentication prompts split across
* transport chunks without retaining terminal history or unbounded data.
*/
export function appendTerminalPromptSecurityTail(previous: string, chunk: string): string {
const combined = `${previous}${chunk}`;
const boundary = Math.max(combined.lastIndexOf('\n'), combined.lastIndexOf('\r'));
return combined.slice(boundary + 1).slice(-MAX_PROMPT_SECURITY_TAIL_CHARS);
}
function nextHeredocPromptOrigin(origin: string, completedLine: string): string {
const plain = stripTerminalControlSequences(completedLine).trim();
// A blank line ends the continuation. A PS2 echo (">" or "> ...") does not.
if (isBashPs2ContinuationLine(plain)) return origin;
return plain.slice(0, MAX_PROMPT_SECURITY_TAIL_CHARS);
}
/**
* Current logical line with controls stripped for classification, plus the
* heredoc opener when the line is a bare ">". `current` stays raw so partial
* control sequences can be completed by the next transport chunk.
*/
export function foldTerminalPromptSecurityOutput(
state: { origin: string; current: string; pendingCr?: boolean },
chunk: string,
): { origin: string; current: string; promptText: string; pendingCr: boolean } {
let pendingCr = state.pendingCr === true;
let input = chunk;
if (pendingCr && input.startsWith('\n')) {
input = input.slice(1);
pendingCr = false;
}
const combined = `${state.current}${input}`.replace(
CURSOR_POSITION_PATTERN,
(sequence, column: string | undefined) => Number(column || 1) <= 1 ? '\r' : sequence,
);
let origin = state.origin;
for (const line of splitTerminalLogicalLines(combined).slice(0, -1)) {
origin = nextHeredocPromptOrigin(origin, line);
}
const current = appendTerminalPromptSecurityTail('', combined);
const plain = stripTerminalControlSequences(current);
const promptText = plain.trim() === '>' && origin
? `${origin}\n${plain}`
: plain;
if (input.endsWith('\r')) pendingCr = true;
else if (input.length > 0) pendingCr = false;
return { origin, current, promptText, pendingCr };
}
export function shouldUsePluginTerminalCompletionProvider(input: {
sensitiveInputActive: boolean;
promptText: string;
allowHostStyleGreaterThan?: boolean;
}): boolean {
return !input.sensitiveInputActive
&& !isSensitiveTerminalChallenge(input.promptText)
&& isConfirmedTerminalShellPrompt(input.promptText, {
allowHostStyleGreaterThan: input.allowHostStyleGreaterThan,
});
}
export {
MAX_PROMPT_SECURITY_TAIL_CHARS,
extractTerminalPromptHostname,
isConfirmedTerminalShellPrompt,
isJumpServerMenuPrompt,
isSensitiveTerminalChallenge,
isUntrustedTerminalInputPrompt,
type ConfirmedPromptOptions,
};