UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

122 lines (121 loc) • 10.5 kB
/** * The TIGHTENING reader's view of a command line — "which programs could this command line run, and can the text * even be read?" — for the deny/ask direction of the permission-rule lane (`permission-rule-model.ts` * `programRunReachOf`) and the read-only allow member (`read-only-shell.ts`). It reads the ONE shell lexer's result * (`shell/lex.ts`) and adds only this reader's own domain: which pieces of a line are run candidates, the leading * keyword of a control structure, and the additive peel. The command line a command/process substitution runs — in an * argument, a redirection operand, an unquoted heredoc body, an arithmetic text — is read by this same reader and carried * on the segment ({@link ShellSegment.substituted}), down to {@link MAX_SUBSTITUTION_DEPTH} levels; a segment that may run * a program no carried line shows (a substitution inside a parameter's operand, a name the arithmetic evaluator reads, * a substitution past the depth) is one whose program runs are not fully known — the reader answers `unreadable` for * every rule such a segment does not otherwise reach, never silence; the leading assignments and the wrapper programs of {@link SHELL_WRAPPER_TABLE} are peeled ADDITIVELY — a segment keeps * its spelled argv as one run candidate and gains one per peeled layer, so `sudo env X=1 nohup rm -r x` is a run of * `sudo …`, of `env …`, of `nohup …` and of `rm -r x`, and a deny on any of them reaches. * * What it answers is deliberately THREE-valued per segment: the argv words it could read, each marked literal or * expansion-bearing, or an `unreadable` reason where the program runs cannot be known from the text (a command the * lexer cannot delimit, a syntax error, a control structure whose commands need a syntax tree, a wrapper handed its * command as one string or an option of unknown arity). The reader above turns "unreadable" into a fail-closed ask * rather than into silence — reading it as "no match" is how a deny went blind on `rm -r x; echo "$HOME"`. Where the * reader stops before the end of the text — the lexer cannot delimit it, or it runs past {@link MAX_SHELL_READ_CHARS} — what * it read stands as read and the text keeps the mark that it was not read whole ({@link ShellCommandShape.stopped}): the segments * before the one it stopped in are candidates (bash runs every complete line before the one it cannot parse), and so are the * statements past a place the lexer could not delimit that it can still read within the limit (`segmentsPastStop`: a shell may * run them; past the limit nothing is read); the segment it stopped in keeps the words it read whole as candidates — a program * word is a fact once read — each followed by one word that stands for the text it did not read (it may be no word or many), its * runs otherwise not known; with no word read it is the unreadable one. * * The ALLOW direction's rule lane never reads this module: a standing yes covers the exact spelling a person read * (`ruleAdmitsCommand`), and its whole-string refusal of every shell construct stays where it is. */ import { type LexedWord, type StartedShell } from "../shell/lex.js"; /** One argv word as the reader projects it. */ export interface ShellWord { /** The word's value with quotes and escapes removed — meaningful only when `expands` is `false`. */ readonly text: string; /** The unquoted source spelling (assignment and wrapper-flag tests read this, never `text`). */ readonly raw: string; /** `false`: a literal word. `"one"`: carries an expansion but yields exactly ONE word (it was double-quoted). * `"many"`: an unquoted expansion, glob or brace pattern, an ANSI-C quote, or a quoted `"$@"` / array expansion — * may yield zero, one or several words. */ readonly expands: false | "one" | "many"; } /** One redirection of a segment, as read: the operator and its operand word (a heredoc's operand is its delimiter; * a descriptor duplication's is the descriptor; the IO number or `{name}` before the operator is folded into the * operator's meaning and not kept). The tightening reader never reads these — a rule speaks about program runs; the * read-only reader does (an output redirection is a write). */ export interface ShellRedirection { readonly op: string; readonly target: ShellWord; } /** One program run the reader found, after keyword stripping. */ export interface ShellSegment { /** The run as SPELLED (a leading keyword removed): the first candidate a rule is compared against. */ readonly argv: readonly ShellWord[]; /** The segment's redirections in source order (see {@link ShellRedirection}); empty when it has none. */ readonly redirections: readonly ShellRedirection[]; /** The deeper candidates, one per peeled layer (leading assignments, then each wrapper of the table): * `sudo -u root rm -r x` ⇒ `[[rm, -r, x]]`. Empty when nothing peeled. */ readonly peeled: readonly (readonly ShellWord[])[]; /** Present when this segment's program runs cannot be known from the text at all; `argv` then holds * whatever was read before the failure and must not be matched as a run. */ readonly unreadable?: string; /** Present when the segment's program runs are NOT fully known although its candidates could be read: * a peel that stopped before the wrapped program could be named (its options carry an expansion, a * command string, an option of unknown arity), a run the segment may perform that no line in * {@link substituted} shows (a substitution in a parameter's operand, a name the arithmetic evaluator * reads, a substitution nested past {@link MAX_SUBSTITUTION_DEPTH}), or the reader stopped in this segment * ({@link ShellCommandShape.stopped}, whose sentence this is then). The candidates stand — a rule they * reach is reached — and every other rule reads `unreadable`. */ readonly peelUnreadable?: string; /** The command lines the segment's command and process substitutions run, each read by this same reader * (`echo "$(rm -r x)"` carries the line `rm -r x`): a rule reaches the segment when it reaches a run of * one of them. Present only when the segment carries at least one; a segment may then spell no run of its * own (`[ -n "$(…)" ]`, `> "$(…)"`, a here-document body on an empty piece). */ readonly substituted?: readonly ShellCommandShape[]; } /** A command line as the tightening reader sees it. */ export interface ShellCommandShape { readonly segments: readonly ShellSegment[]; /** The connector BETWEEN each consecutive pair of segments (`;` `&&` `||` `|` `|&` `&` `\n`). */ readonly connectors: readonly string[]; /** A subshell, group, control-structure keyword or dropped empty piece bounded the segments: the * connector list is not one flat chain a compound rule could spell. */ readonly grouped: boolean; /** A `&` connector anywhere — between two segments or after the last one (`ls &`): some segment runs in the * background, past any approval-time reading of the line. The connector list alone cannot say so for a * trailing `&` (a connector rides BETWEEN segments). */ readonly backgrounded: boolean; /** A redirection on a piece that runs NO program (`> x` alone, `[ -f a ] > x`, a keyword's own redirection): * such a piece is dropped from `segments` (the tightening reader asks about program runs, and it runs * none) — but the shell still opens the target, so a reader that asks about EFFECTS must refuse the line. */ readonly strayRedirection: boolean; /** Why the reader stopped before the end of the text (the lexer cannot delimit what follows, or the text runs past * {@link MAX_SHELL_READ_CHARS}), when it did — the mark that the text was not read whole. The segment it stopped in carries the * stop's sentence as its `peelUnreadable` (with no word read, its `unreadable`), and each of its run candidates ends in a word no * text spells, `expands: "many"`, standing for the text not read; it is the last segment unless statements past a parse stop * follow it (read within the limit), and a parse stop inside a text the limit cut is a second such segment (this field is then * the limit's sentence). A reader that needs the WHOLE text read (a rule body, a read-only verdict) refuses the shape on this * mark, not on any segment's position. */ readonly stopped?: string; } export { SHELL_WRAPPER_TABLE, type ShellWrapperName } from "./shell-wrapper-table.js"; export declare const DURATION_RE: RegExp; /** The reader reads a command's text up to this many characters, counted from its first character that is not white space * (what a trim removes: bash skips the blanks and newlines there, and a run behind lines of the others — each a program * that does not exist — still runs, so none of them may push it past the limit; the text there is read all the same); it * reads nothing past them. What it read stands as read: the reader stopped there ({@link ShellCommandShape.stopped}), and a * word the limit cuts through is not read (it may go on past the limit). Upstream's own reader stops at the same size. */ export declare const MAX_SHELL_READ_CHARS = 10000; export declare function lexedWordOf(w: ShellWord): LexedWord | undefined; /** Nesting levels of command substitution the reader reads (the outermost line is level 0); a substitution nested deeper * is a run this reader does not read. Upstream's own reader stops at the same depth. */ export declare const MAX_SUBSTITUTION_DEPTH = 16; /** Read `source` as the tightening reader must: every program run the text could perform, or why it cannot be read. */ export declare function readShellCommand(source: string): ShellCommandShape; /** {@link readShellCommand} of a text `shell` runs (the engine's readers take the call's started shell). */ export declare function readShellCommandIn(shell: StartedShell, source: string): ShellCommandShape; /** Every program run of `shape` can be NAMED from the text: no segment is unreadable, no peel stopped, * every run candidate's program word is literal, and so is every run of every line a segment's substitutions * run ({@link ShellSegment.substituted}). The offer/mint side's "judgeable" question. */ export declare function isFullyReadable(shape: ShellCommandShape): boolean;