mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 09:51:28 +00:00
* 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>
262 lines
9.7 KiB
JavaScript
262 lines
9.7 KiB
JavaScript
"use strict";
|
||
|
||
const ESCAPE_SEQUENCE = "\\x" + "1b";
|
||
const BELL_SEQUENCE = "\\x" + "07";
|
||
const ANSI_CONTROL_PATTERN = new RegExp(`${ESCAPE_SEQUENCE}\\[[0-?]*[ -/]*[@-~]`, "gu");
|
||
const OSC_CONTROL_PATTERN = new RegExp(
|
||
`${ESCAPE_SEQUENCE}\\][^${BELL_SEQUENCE}]*(?:${BELL_SEQUENCE}|${ESCAPE_SEQUENCE}\\\\)`,
|
||
"gu",
|
||
);
|
||
const MAX_PROMPT_SECURITY_TAIL_CHARS = 2_048;
|
||
const PROMPT_MARKER_LINE_PATTERN = /^[#$%]$/u;
|
||
const BRACKETED_PROMPT_IDENTITY_PATTERN =
|
||
/^(?:\([^()\r\n]+\)\s+)?\[([^\s@\]]+)@([^\s\]:#$%]+)(?:\s+[^\]\r\n]+)?\](?:[$#%]|\s+[A-Za-z][A-Za-z0-9_.-]{0,31}\s*=\s*(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))?$/u;
|
||
const SPACED_PROMPT_IDENTITY_PATTERN =
|
||
/^#\s+([^\s@]+)\s+@\s+([^\s\]]+)/u;
|
||
const POSIX_PROMPT_IDENTITY_PATTERN =
|
||
/^(?:\([^()\r\n]+\)\s+)?([^\s@():\]]+)@([^\s\]:#$%]+)(?::[^\r\n#$%]*)?[$#%]?$/u;
|
||
const ZSH_PROMPT_IDENTITY_PATTERN =
|
||
/^(?:\([^()\r\n]+\)\s+)?([^\s@]+)@([^\s:%#$]+)\s+[^\s%#$][^%#$\r\n]*\s+[#$%]$/u;
|
||
const ALPINE_PROMPT_IDENTITY_PATTERN =
|
||
/^([A-Za-z0-9_][A-Za-z0-9_.-]*):(?:~(?:\/[^#$>\\\r\n]*)?|\/[^#$>\\\r\n]*)[#$]?$/u;
|
||
const MACOS_ROOT_PROMPT_IDENTITY_PATTERN =
|
||
/^([A-Za-z0-9_][A-Za-z0-9_.-]*):(?:~|\/[^#$>\\\s]*)\s+[A-Za-z0-9_.-]+[#$]?$/u;
|
||
const JUMP_SERVER_MENU_PROMPT_PATTERN = /^(?:Opt|\[Host\])>$/u;
|
||
const PROMPT_HOST_PATTERN =
|
||
/^(?:\[[A-Fa-f0-9:.]+\]|[A-Za-z0-9_][A-Za-z0-9_.-]*)$/u;
|
||
|
||
const SENSITIVE_ENGLISH_LABEL = [
|
||
String.raw`pass(?:word|phrase|code)`,
|
||
String.raw`one[\s-]?time(?:\s+(?:password|passcode|code|token))?`,
|
||
String.raw`\botp\b`,
|
||
String.raw`verification(?:\s+(?:code|token|passcode))?`,
|
||
String.raw`authentication\s+(?:code|token|passcode)`,
|
||
String.raw`security\s+(?:code|token|passcode|pin)`,
|
||
String.raw`\bpin\b`,
|
||
String.raw`\btoken\b`,
|
||
String.raw`2fa`,
|
||
String.raw`two[\s-]?factor`,
|
||
String.raw`multi[\s-]?factor`,
|
||
String.raw`\bmfa\b`,
|
||
String.raw`second\s+factor`,
|
||
String.raw`secondary(?:\s+\w+){0,3}\s+passw(?:ord)?`,
|
||
String.raw`second(?:\s+\w+){0,3}\s+passw(?:ord)?`,
|
||
String.raw`additional(?:\s+\w+){0,3}\s+passw(?:ord)?`,
|
||
String.raw`re[-\s]?enter\s+passw(?:ord)?`,
|
||
String.raw`confirm\s+passw(?:ord)?`,
|
||
String.raw`\bedr\b`,
|
||
String.raw`\bduo\b`,
|
||
].join("|");
|
||
|
||
const SENSITIVE_CJK_LABEL = [
|
||
"密码", "口令", "动态", "一次性", "验证码", "验证信息", "令牌", "双因素", "多因素",
|
||
"短信验证", "手机验证", "二次", "安全密码", "挑战码",
|
||
].join("|");
|
||
|
||
const SENSITIVE_LABEL_PATTERN = new RegExp(
|
||
`(?:${SENSITIVE_ENGLISH_LABEL}|${SENSITIVE_CJK_LABEL})`,
|
||
"iu",
|
||
);
|
||
|
||
function stripTerminalControlSequences(value) {
|
||
return String(value || "").replace(OSC_CONTROL_PATTERN, "").replace(ANSI_CONTROL_PATTERN, "");
|
||
}
|
||
|
||
function lastLogicalLine(value) {
|
||
const plain = stripTerminalControlSequences(value);
|
||
const boundary = Math.max(plain.lastIndexOf("\n"), plain.lastIndexOf("\r"));
|
||
return plain.slice(boundary + 1).slice(-MAX_PROMPT_SECURITY_TAIL_CHARS);
|
||
}
|
||
|
||
function isSensitiveTerminalChallenge(value) {
|
||
const line = lastLogicalLine(value).trim();
|
||
if (!line) return false;
|
||
const label = SENSITIVE_LABEL_PATTERN.exec(line);
|
||
if (!label) return false;
|
||
const prefix = line.slice(0, label.index || 0).trim();
|
||
const suffix = line.slice((label.index || 0) + label[0].length);
|
||
if (suffix.trim().length === 0) {
|
||
return prefix.length === 0
|
||
|| /(?:^|\s)(?:enter|input|provide|type|scan|please|your|current|new|old)\s*$/iu.test(prefix)
|
||
|| /(?:输入|请输入|请)\s*$/u.test(prefix);
|
||
}
|
||
if (/^\s+(?:for|of)\s+[^\r\n]{1,96}$/iu.test(suffix)) return true;
|
||
return /^[^\r\n::>›»]{0,96}[::>›»]\s*[^\r\n]{0,1024}$/u.test(suffix);
|
||
}
|
||
|
||
function isConfirmedTerminalShellPrompt(promptText, options = {}) {
|
||
const prompt = lastLogicalLine(promptText).trim();
|
||
if (!prompt || isSensitiveTerminalChallenge(prompt)) return false;
|
||
if (/[❯❮→➜➤⟩»›]/u.test(prompt)) return true;
|
||
for (const character of prompt) {
|
||
const code = character.charCodeAt(0);
|
||
if (code >= 0xE000 && code <= 0xF8FF) return true;
|
||
}
|
||
if (/[$#%]$/u.test(prompt)) return true;
|
||
// POSIX sh/bash/ksh default PS2. Trusted only with heredoc context (#3796).
|
||
if (prompt === ">") return isHeredocPs2Continuation(promptText, options);
|
||
if (!prompt.endsWith(">")) return false;
|
||
if (/^(?:PS\s+)?[A-Za-z]:[\\/].*>$/u.test(prompt)) return true;
|
||
if (/[@\\/~:]\S*>$/u.test(prompt)) return true;
|
||
return options.allowHostStyleGreaterThan === true
|
||
&& /^[A-Za-z0-9_.-]+(?:\([^)]{1,128}\))?>$/u.test(prompt);
|
||
}
|
||
|
||
function splitTerminalLogicalLines(value) {
|
||
const lines = [];
|
||
let start = 0;
|
||
for (let i = 0; i < value.length; i += 1) {
|
||
const ch = value[i];
|
||
if (ch !== "\n" && ch !== "\r") continue;
|
||
lines.push(value.slice(start, i));
|
||
if (ch === "\r" && value[i + 1] === "\n") i += 1;
|
||
start = i + 1;
|
||
}
|
||
lines.push(value.slice(start));
|
||
return lines;
|
||
}
|
||
|
||
function isBashPs2ContinuationLine(line) {
|
||
const trimmed = line.trim();
|
||
return trimmed === ">" || trimmed.startsWith("> ");
|
||
}
|
||
|
||
function commandAfterConfirmedShellPrompt(line, options) {
|
||
const text = line.trim();
|
||
for (let i = 0; i < text.length - 1; i += 1) {
|
||
const ch = text[i];
|
||
if (ch !== "$" && ch !== "#" && ch !== "%" && ch !== ">") continue;
|
||
const rest = text.slice(i + 1);
|
||
if (!/^\s+\S/u.test(rest)) continue;
|
||
const candidate = text.slice(0, i + 1);
|
||
if (!isConfirmedTerminalShellPrompt(candidate, options)) continue;
|
||
if ((ch === "$" || ch === "#" || ch === "%") && !isPlausibleShellPromptBody(candidate.slice(0, -1))) {
|
||
continue;
|
||
}
|
||
return rest;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// Bare ">" is bash PS2 only inside a heredoc opened by a confirmed shell
|
||
// command (#3796). read -s -p '> ' and other origins stay fail-closed.
|
||
function isHeredocPs2Continuation(value, options) {
|
||
const lines = splitTerminalLogicalLines(stripTerminalControlSequences(value));
|
||
let index = lines.length - 2;
|
||
while (index >= 0 && isBashPs2ContinuationLine(lines[index])) index -= 1;
|
||
if (index < 0) return false;
|
||
const command = commandAfterConfirmedShellPrompt(lines[index], options);
|
||
return command != null && command.includes("<<");
|
||
}
|
||
|
||
function isPlausibleShellPromptBody(body) {
|
||
const trimmed = body.trim();
|
||
if (!trimmed) return true;
|
||
if (/@|[/\\~:]/u.test(trimmed)) return true;
|
||
if (/^(?:bash|zsh|sh|fish|ksh|csh|tcsh|dash)(?:-[\d.]+)?$/iu.test(trimmed)) return true;
|
||
return /^[a-z0-9_.-]+$/u.test(trimmed);
|
||
}
|
||
|
||
function promptHostnameFromLine(line) {
|
||
const text = String(line || "");
|
||
for (const [pattern, hostnameGroup] of [
|
||
[BRACKETED_PROMPT_IDENTITY_PATTERN, 2],
|
||
[SPACED_PROMPT_IDENTITY_PATTERN, 2],
|
||
[POSIX_PROMPT_IDENTITY_PATTERN, 2],
|
||
[ZSH_PROMPT_IDENTITY_PATTERN, 2],
|
||
[MACOS_ROOT_PROMPT_IDENTITY_PATTERN, 1],
|
||
[ALPINE_PROMPT_IDENTITY_PATTERN, 1],
|
||
]) {
|
||
const match = pattern.exec(text);
|
||
if (!match) continue;
|
||
const hostname = match[hostnameGroup].replace(/[.,;]+$/u, "");
|
||
if (PROMPT_HOST_PATTERN.test(hostname)) return hostname;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function isJumpServerMenuPrompt(value) {
|
||
const line = lastLogicalLine(value).trim();
|
||
if (!line) return false;
|
||
return JUMP_SERVER_MENU_PROMPT_PATTERN.test(line);
|
||
}
|
||
|
||
function extractTerminalPromptHostname(value) {
|
||
const plain = stripTerminalControlSequences(value);
|
||
if (!plain) return null;
|
||
if (isSensitiveTerminalChallenge(plain)) return null;
|
||
|
||
const lines = splitTerminalLogicalLines(plain);
|
||
const lastLineIndex = lines.length - 1;
|
||
let markerIndex = lastLineIndex;
|
||
while (markerIndex >= 0 && lines[markerIndex].trim().length === 0) {
|
||
markerIndex -= 1;
|
||
}
|
||
if (markerIndex < 0) return null;
|
||
|
||
const hasTrailingLineBreaks = markerIndex < lastLineIndex;
|
||
const markerLine = lines[markerIndex].trim();
|
||
if (!PROMPT_MARKER_LINE_PATTERN.test(markerLine)) {
|
||
if (hasTrailingLineBreaks) return null;
|
||
return /[#$%]$/u.test(markerLine)
|
||
? promptHostnameFromLine(markerLine)
|
||
: null;
|
||
}
|
||
|
||
let identityIndex = markerIndex - 1;
|
||
while (
|
||
identityIndex >= 0
|
||
&& (
|
||
lines[identityIndex].trim().length === 0
|
||
|| lines[identityIndex].trim() === markerLine
|
||
)
|
||
) {
|
||
identityIndex -= 1;
|
||
}
|
||
if (identityIndex < 0) return null;
|
||
const identityLine = lines[identityIndex].trim();
|
||
if (isSensitiveTerminalChallenge(identityLine)) return null;
|
||
return promptHostnameFromLine(identityLine);
|
||
}
|
||
|
||
function hasTypedInputAfterConfirmedPrompt(line, options = {}) {
|
||
for (let i = 0; i < line.length - 1; i += 1) {
|
||
const ch = line[i];
|
||
if (ch !== "$" && ch !== "#" && ch !== "%" && ch !== ">") continue;
|
||
const rest = line.slice(i + 1);
|
||
if (!/^\s+\S/u.test(rest)) continue;
|
||
const candidate = line.slice(0, i + 1);
|
||
if (!isConfirmedTerminalShellPrompt(candidate, options)) continue;
|
||
if ((ch === "$" || ch === "#" || ch === "%") && !isPlausibleShellPromptBody(candidate.slice(0, -1))) {
|
||
continue;
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function isUntrustedTerminalInputPrompt(value, options = {}) {
|
||
const prompt = lastLogicalLine(value).trim();
|
||
if (!prompt) return false;
|
||
if (isSensitiveTerminalChallenge(prompt)) return true;
|
||
// Vim command-mode entry on the alternate screen. A bare `:` on the
|
||
// primary screen stays fail-closed (`sudo -p ':'`).
|
||
if (options.alternateScreen && /^:\s*$/.test(prompt)) return false;
|
||
if (!/[::>›»]\s*$/u.test(prompt)) return false;
|
||
if (hasTypedInputAfterConfirmedPrompt(prompt, options)) return false;
|
||
// Pass the original text so a bare ">" can see heredoc lines above it.
|
||
return !isConfirmedTerminalShellPrompt(value, options);
|
||
}
|
||
|
||
module.exports = {
|
||
MAX_PROMPT_SECURITY_TAIL_CHARS,
|
||
stripTerminalControlSequences,
|
||
isSensitiveTerminalChallenge,
|
||
isConfirmedTerminalShellPrompt,
|
||
splitTerminalLogicalLines,
|
||
isBashPs2ContinuationLine,
|
||
isUntrustedTerminalInputPrompt,
|
||
isJumpServerMenuPrompt,
|
||
extractTerminalPromptHostname,
|
||
};
|