Expand description
Shell allowlist with AST-based command parsing.
Security model (Information Bottleneck principle):
- When allowlist is set: ALL segments of a compound command must be allowed (deny-by-default)
- When empty: all commands pass (backwards-compatible blocklist-only mode)
- Dangerous patterns (subshells, eval, backticks) are blocked in restricted mode
Structs§
- Approval
Miss - A program the command runs that the allowlist does not name.
- Approval
Request - What a user is asked to approve for one command.
- Partial
Run - The statements that may run, and one note per skipped statement.
Enums§
- Separator
- The operator that ends a command segment.
- Shell
Security - Active shell-security posture. Order is least → most permissive only for readability; do not rely on ordinal values.
Constants§
- LEAN_
CTX_ RUN_ CONTROL_ ENV - The
LEAN_CTX_*variables a command line may set: they only switch compression or the hook re-entry guard for that one command.lean-ctx -capplies these from the command text (#1321) and no others. Every otherLEAN_CTX_*variable — the shell security mode, the allowlist, the config and data directories, the path jail — is lean-ctx’s own policy, which a command line must not set for the lean-ctx it starts. - MAX_
VALIDATED_ COMMAND_ BYTES - Longest command the gate validates; a longer one is refused in
enforcemode. Every check re-scans the line, and a hook that runs out of time fails open.
Functions§
- allow_
execute_ languages - Adds
languagesto the globalexecute_allowed_languages: the “always allow” answer to actx_executeapproval prompt. - allow_
programs - Adds
programsto the globalshell_allowlist_extra, aslean-ctx allowdoes, without printing. Returns the entries that were new. Used when the user answers an approval prompt with “always allow”. - approval_
request - The approval
commandneeds, orNonewhen it needs none (it passes) or cannot get one (a hard block, or a shell security mode other thanenforce, where nothing is refused for a missing program). - check_
leanctx_ invocation - The checks for a single, plain
lean-ctx …invocation from a host shell, honouring the activeShellSecuritymode likecheck_shell_allowlist. - check_
shell_ allowlist - Checks whether a command may run, honouring the active
ShellSecuritymode (GL #788). This is the single chokepoint shared by MCPctx_shelland the CLI shell entrypoints, so the mode applies consistently: - check_
shell_ allowlist_ granted super::check_shell_allowlistwithgrantedprograms counted as listed. An empty grant is exactly the ordinary check.- effective_
allowlist_ pub - Public accessor: the fully-resolved allowlist actually enforced by the MCP tools
(base
shell_allowlist+ additiveshell_allowlist_extra+ env), deduplicated. Empty means blocklist-only mode (all commands pass). Used bylean-ctx allowandlean-ctx doctorto show users exactly what the runtime sees. - exceeds_
validation_ limits - True when
commandis beyond what the gate validates: longer thanMAX_VALIDATED_COMMAND_BYTESor nested deeper than the lexer follows. The shell hooks deny such a command (inenforcemode) instead of handing it on. - extract_
all_ commands_ pub - Public accessor for extracting all command segments.
- extract_
base_ command - heredoc_
delims - The delimiters of the heredoc operators in
line, in source order (only quoted ones withquoted_only). A<<inside quotes,${…}or arithmetic is no operator; a<<<here-string has no body. - invalid_
allowlist_ entries_ pub - Public accessor: entries the effective allowlist silently dropped for
invalid
*placement (§4 of the design doc). Used bylean-ctx doctorto surface a count, and by--fixto know which raw config lines to comment out. - is_
valid_ allowlist_ entry - §4 of the design doc: an entry’s only valid
*placement is a single trailing token. Bare*, an embedded*, or more than one*are invalid. - narrowed_
allowlist_ entries_ pub - Public accessor: multi-word entries with no trailing
*— their meaning changed under subcommand scoping (now exact-match only, previously “whole binary”). Used by thelean-ctx doctorupgrade advisory (§2 of the design doc). Always empty whenshell_allowlist_subcommand_scoping = false, since those entries’ meaning didn’t change under the compat path. - partial_
run - Plans a partial run for
command, which the gate refused as a whole.Nonekeeps the whole-call rejection. - passes_
enforced - True when
commandwould pass the allowlist / dangerous-pattern checks inenforcesemantics — independent of the activeShellSecuritymode and without any logging or blocking side effects. - read_
heredoc_ delim - Parse a heredoc delimiter token starting at
start, returning its bare name (quotes/escapes removed), whether any part was quoted, and the index just past the token.Nonewhen no delimiter is present. - refuse_
redaction_ marker - Refuses code that contains a redaction marker such as
[REDACTED:Database credentials]. Redaction applies only to what an agent is shown, never to what runs; an agent that copies a marker out of a redacted view into a command would write the marker itself into files or remote configs (a crash-looping server on 24.10.). No command means one, so it never runs, whatever the shell security mode. - segments_
with_ separators - Every segment together with the separator that ends it; the last segment
has none. Same scanner as
split_on_operators, so the two never disagree about where a command ends. - shadowed_
allowlist_ entries_ pub - All shadowed entries in
entries, paired with the entry that covers each one — used by thelean-ctx doctoradvisory (§2). - shadowing_
entry - §2 “Shadowing warnings”: is
amade useless by a different entry already inentries? Entry A is shadowed by B when B matches every command A matches: B is a single word equal to A’s first word, or B ends in*and B’s words before the*are a leading run of A’s words (ignoring A’s own trailing*). Returns the first covering entry, in list order. Shared bylean-ctx allow(write-time warning) andlean-ctx doctor(read-only advisory) so the rule can’t drift between the two. - shell_
tokenize - Tokenize a shell command segment respecting single/double quotes and backslash escapes.
Returns tokens with outer quotes stripped, matching how the shell would parse them.
E.g.
git -C "Program Files" status→["git", "-C", "Program Files", "status"] - strip_
all_ heredoc_ bodies - Like
strip_quoted_heredoc_bodiesbut strips bodies for all heredocs (quoted and unquoted delimiters). Use for checks that must never interpret heredoc body content as commands or redirects (#931). - warn_
only_ reason - The reason of an enforce-mode block message, without the wording that only holds when the command was actually stopped (#1874).
- with_
run_ cwd - Runs
fwith relative paths resolved againstcwd, the directory the checked command runs in, instead of this process’s own.