{"modulePath":"github.com/diffsec/agentmon","version":"v0.0.0-20261111001054-230ac962f687","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package shellparse\n\nPackage shellparse extracts the policy-relevant binary from a shell-wrapped invocation like \\`sh -c \"shutdown now\"\\`.\n\nWhy: the server's policy pre-check runs CheckCommand(cmd, args). When the client invokes a shell with \\`-c\\`, the command is the shell binary and the script is opaque to the policy engine. A rule like \\`deny bin=shutdown\\` never fires for \\`sh -c \"shutdown now\"\\` unless we peek inside the script.\n\nThis package implements that peek for the narrow case where the script is a single external command with no shell features (pipes, globs, redirects, builtins, quoting, compound operators, …). For anything more complex we return ok=false and the caller falls back to checking the shell itself — a safe default because the shell's own rule (or per-builtin rule via env\\_inject) still applies.\n\nThis is a best-effort pre-check aid, NOT a sandbox. The actual exec still runs via the shell; seccomp/landlock/env\\_inject remain the primary enforcement layers. This package only widens what the policy pre-check can see.\n\n## Functions\n\n```go\nfunc BypassReason(command string, args []string) string\n```\nBypassReason returns a short human-readable reason explaining why command+args is classified as a wrapper-bypass attempt by IsShellCBypassAttempt, or \"\" if it isn't. The reason names the specific construct that triggered the classification (offending flag, wrapper, or env-assignment) so the policy engine can put it in the deny hint and operators don't have to guess what shape of invocation tripped the check.\n\nThe exact wording is not part of the API contract — callers should surface it as-is in user-facing messages and not parse it.\n\n```go\nfunc DerivePolicyTarget(command string, args []string) (string, []string, bool)\n```\nDerivePolicyTarget reports the underlying binary and args that should be used for a policy pre-check when command+args represents a simple shell invocation of the form \\`\\\u003cshell\u003e -c \"\\\u003csimple-cmd\u003e\"\\`.\n\nReturns (derivedCmd, derivedArgs, true) for invocations the caller can safely substitute for policy evaluation, or (\"\", nil, false) otherwise — in which case the caller MUST use the original command/args UNLESS IsShellCBypassAttempt reports true, which indicates a known command-exec wrapper in a form we can't safely parse; the caller should fail closed.\n\nThe input command may be an absolute path (\"/bin/sh\"), a relative path (\"./sh\"), or a bare name (\"bash\"); the basename is lowercased before matching the known-shell set so that case-insensitive filesystems (e.g. stock macOS) don't produce a bypass where \\`/BIN/SH\\` is recognized by the policy engine but not by this derivation. Both \\`/\\` and \\`\\\\\\` are treated as path separators regardless of GOOS so cross-compiled Windows builds parse Unix-style exec paths the same way a Linux server would.\n\n```go\nfunc IsOpaqueShellC(command string, args []string) bool\n```\nIsOpaqueShellC reports whether command+args is a shell-c invocation whose script is a multi-command program our parser can't safely reduce to a single binary. Opaque scripts contain shell features like metacharacters (\\`;\\`, \\`\u0026\\`, \\`|\\`), redirects, subshells, globs, quoting, or parameter substitution — any of which can cause the shell to execute a different binary than the first token suggests.\n\nThe caller (policy engine) should fail closed on true ONLY when the policy contains at least one restrictive command rule (deny, redirect, soft\\_delete, approve, audit). Policies that only allow are, by the operator's own expression, not relying on command-level restrictions — denying opaque scripts there would break legitimate shell use without closing any bypass. See Engine.hasRestrictiveCommandRule for the gate.\n\nReturns false for invocations DerivePolicyTarget can rewrite and for bypass forms (IsShellCBypassAttempt is the correct API for those).\n\n```go\nfunc IsShellCBypassAttempt(command string, args []string) bool\n```\nIsShellCBypassAttempt reports whether command+args is a shell-c invocation that exposes a known command-exec wrapper (nohup, nice, exec, command) in a form we can't safely collapse to a single binary — for example \\`sh -c \"exec -a foo shutdown\"\\` or \\`sh -c \"nohup --preserve-status rm /\"\\`. The caller (policy engine) should treat a true return as grounds to fail closed: the operator's allow-shell rule would otherwise cover a bypass attempt that DerivePolicyTarget declined to rewrite.\n\nReturns false for invocations that DerivePolicyTarget would succeed on, so a typical caller checks this only after DerivePolicyTarget reports ok=false.\n\n```go\nfunc OpaqueReason(command string, args []string) string\n```\nOpaqueReason returns a short human-readable reason explaining why command+args is classified as opaque by IsOpaqueShellC, or \"\" if it isn't. The reason names the construct (metacharacter, glob, expansion trigger, unterminated quote, ...) that prevented safe tokenization.\n\nSame non-parseability caveat as BypassReason.\n\n","path":"github.com/diffsec/agentmon/internal/shellparse","name":"shellparse","synopsis":"Package shellparse extracts the policy-relevant binary from a shell-wrapped invocation like `sh -c \"shutdown now\"`.","isRedistributable":true}
