Documentation
¶
Overview ¶
Package policy detects destructive commands so an agent cannot wreck a server by accident. Blocked commands need an explicit --allow-dangerous. It is a guardrail against mistakes, not a sandbox: a determined user can always write a command it does not recognise.
Index ¶
- Constants
- func CustomID(pattern string) string
- func FillParams(script string, params []string) string
- func Invalid(extra []string) map[int]string
- func IsCFlag(w string) bool
- func IsDescriptor(w string) bool
- func IsShell(name string) bool
- func Lex(s string) ([][]Token, string)
- func Normalize(cmd string) string
- func PSOnlyReads(script string) bool
- func PSReadOnly(script string) string
- func Pattern(r Rule) string
- func Plain(cmd string) string
- func ShellScript(name string, args []string) (script string, params []string, ok bool)
- func Tokens(s string) [][]Token
- type Rule
- type Token
Constants ¶
const Sub = "$(...)"
Sub stands in a word for a command substitution: $( ), ` `, <( ) or >( ).
Variables ¶
This section is empty.
Functions ¶
func CustomID ¶ added in v0.12.0
CustomID is the id of a custom deny pattern: made from the pattern, so it stays the same when another rule is removed (a position would move).
func FillParams ¶ added in v0.3.0
FillParams puts the arguments a script gets ($0, $1, ${2}, $@, $*) into its text, for checking it as it runs. A missing one is empty.
func Invalid ¶ added in v0.3.0
Invalid returns, for each custom pattern that does not compile, what is wrong with it and how to fix it (for doctor and policy list).
func IsDescriptor ¶ added in v0.3.0
IsDescriptor: the 2 in >&2, or - (close).
func IsShell ¶ added in v0.3.0
IsShell: sh, bash, su ... run the text after -c (or a flag group with c in it) as commands, and otherwise a script from a file or their input.
func Lex ¶ added in v0.3.0
Lex is Tokens, and also says what it could not read: an unclosed quote, ` or $( ). Bash refuses such a line, or reads it in a way the checks may not, so a check that must be sure treats it as unknown.
func Normalize ¶ added in v0.2.0
Normalize prepares a command for matching: line breaks become ";" (the shell treats both as command separators), runs of white space become one space, and a word that is entirely quoted ("/" or '/') loses its quotes.
func PSOnlyReads ¶ added in v0.9.0
PSOnlyReads reports a script whose every command komando knows to only read, so a protected server skips its --yes. The subset PSReadOnly accepts holds only such commands, so PSOnlyReads is exactly as strict.
func PSReadOnly ¶ added in v0.9.0
PSReadOnly is why a PowerShell script is not accepted under --read-only, or "" when it is in the read-only subset.
func Plain ¶ added in v0.3.0
Plain is the command line as the shell runs it, for matching: quotes and backslashes removed, every redirect that writes a file shown as >, absolute paths cleaned (//etc, /./etc and /tmp/../etc are /etc), and the text of command substitutions, sh -c '...' and eval as lines of their own. A word with blanks or operators in it keeps quotes, so text in an argument (grep "rm -rf /") does not look like a command.
func ShellScript ¶ added in v0.3.0
ShellScript reads the arguments of a shell (sh, bash, su ...): the text it runs after -c, and the words after that text, which the script gets as $0, $1 ... Options may come before and after -c (bash -c -e 'x', bash -o pipefail -c -- 'x'). ok is false when there is no -c: the shell runs a script from a file or its input.
func Tokens ¶ added in v0.3.0
Tokens splits a shell command line the way bash reads it, closely enough for the safety checks: quotes and backslashes are removed, $'...' is decoded, and the text of each command substitution is a command line of its own, returned after the first one (the word that held it shows Sub). Comments are left out, and so is the body of a here-document (only the $( ) and ` ` in an unquoted one are kept, as lines of their own). It never fails: text it cannot read stays words, which the checks treat with suspicion; Lex says what it could not read.
Types ¶
type Rule ¶
type Rule struct {
ID string `json:"id"`
Reason string `json:"reason"`
// contains filtered or unexported fields
}
func Check ¶
Check returns the rules a command matches. extra are user patterns from settings (raw regexps). Each rule is tried on the command as written (Normalize), as the shell reads it (Plain) and without PowerShell's backticks, so quotes, backslashes and backticks inside a word (r\m -rf /e""tc, Re`move-Item) do not hide it. A custom pattern that does not compile matches every command: a broken rule must not quietly stop protecting anything.
type Token ¶ added in v0.3.0
type Token struct {
Text string
Op bool
Quoted bool // the word had quotes or backslashes in it
// Expands: the word has a $ or ` the shell expands when it runs (a
// variable, $1, a command substitution), so its value is not Text
Expands bool
// Brace: the word has an unquoted {a,b} or {1..3}, which bash expands
// into several words before the command runs
Brace bool
// Code: the word has an expansion bash may evaluate as code: anything
// in ${ } but a plain name (${x@P}, ${x:=v}, ${a[i]}, ${x:n}) or $[ ]
Code bool
}
Token is one word of a shell command line, with its quotes removed, or an operator: ; | || & && ( ) or a redirect (> >> >| &> &>> <> >& < << <<< <&). A line break and ;; are both ";".