shellparse

package
v0.0.0-...-230ac96 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shellparse extracts the policy-relevant binary from a shell-wrapped invocation like `sh -c "shutdown now"`.

Why: 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.

This 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.

This 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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BypassReason

func BypassReason(command string, args []string) string

BypassReason 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.

The exact wording is not part of the API contract — callers should surface it as-is in user-facing messages and not parse it.

func DerivePolicyTarget

func DerivePolicyTarget(command string, args []string) (string, []string, bool)

DerivePolicyTarget 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 `<shell> -c "<simple-cmd>"`.

Returns (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.

The 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.

func IsOpaqueShellC

func IsOpaqueShellC(command string, args []string) bool

IsOpaqueShellC 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 (`;`, `&`, `|`), redirects, subshells, globs, quoting, or parameter substitution — any of which can cause the shell to execute a different binary than the first token suggests.

The 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.

Returns false for invocations DerivePolicyTarget can rewrite and for bypass forms (IsShellCBypassAttempt is the correct API for those).

func IsShellCBypassAttempt

func IsShellCBypassAttempt(command string, args []string) bool

IsShellCBypassAttempt 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.

Returns false for invocations that DerivePolicyTarget would succeed on, so a typical caller checks this only after DerivePolicyTarget reports ok=false.

func OpaqueReason

func OpaqueReason(command string, args []string) string

OpaqueReason 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.

Same non-parseability caveat as BypassReason.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL