atlas-iac/dockerfiles/hermes-dashboard-terminal-input.ts

28 lines
929 B
TypeScript

/** Validate the only mouse reports the dashboard may relay to its PTY. */
// SGR wheel reports use button codes 64/65 plus optional Shift/Alt/Ctrl bits.
// Keep coordinates bounded so arbitrary control text never reaches the PTY.
const SGR_WHEEL_REPORT = /^\x1b\[<(\d{1,2});(\d{1,4});(\d{1,4})M$/;
const MIN_WHEEL_BUTTON = 64;
const MAX_WHEEL_BUTTON = 95;
const MAX_TERMINAL_COORDINATE = 9_999;
/** Return whether ``data`` is one bounded SGR wheel report from xterm. */
export function isSgrWheelReport(data: string): boolean {
const match = SGR_WHEEL_REPORT.exec(data);
if (!match) return false;
const button = Number(match[1]);
const column = Number(match[2]);
const row = Number(match[3]);
return (
button >= MIN_WHEEL_BUTTON &&
button <= MAX_WHEEL_BUTTON &&
(button & 3) <= 1 &&
column >= 1 &&
column <= MAX_TERMINAL_COORDINATE &&
row >= 1 &&
row <= MAX_TERMINAL_COORDINATE
);
}