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 DockerRating(args []string) string
- func EssentialPackage(name string) bool
- func EssentialPackagePattern() string
- func FillParams(script string, params []string) string
- func HandedText(cmd string) (texts []string, ok bool)
- func HasPrivateKey(text string) bool
- 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) (lines [][]Token, bad 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 DockerRun
- type Encoded
- type Rule
- type SubLine
- 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 DockerRating ¶ added in v0.18.0
DockerRating says why docker's own rating counts a command (its words after `docker`) as destructive: "rm" (deletes), "prune", "kill" (stops or changes the swarm) or "host" (hands a container the server); "" when it does not. Exec and compose exec are not rated: the command they run in the container is checked like any other.
func EssentialPackage ¶ added in v0.18.0
EssentialPackage reports whether name, written any way a package manager takes it (OpenSSH-Server, openssh-server:amd64, sudo=1.9.15, systemd/bookworm, openssh-server-8.0p1, sudo.x86_64), is an essential package. A name with a dot, * ? or [ is a pattern to apt, dnf or yum ("openssh.server", "ssh*"): it counts when it would pick one.
func EssentialPackagePattern ¶ added in v0.18.0
func EssentialPackagePattern() string
EssentialPackagePattern is essentialPkgs as an extended regular expression, for the scripts that check a package manager's plan on the server (grep -E).
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. Text that grows past the reading budget of the script and its arguments is unreadableMark: the readers refuse it, and it is not built first.
func HandedText ¶ added in v0.18.0
HandedText is the text the command line cmd hands to a shell, or to a program that runs it in one, one level down: what scripts finds in each of its lines. ok is false when komando cannot read that text with confidence, as for Check: the reader crashed, a lone \r or a here-document whose end the shell may see elsewhere, a text with a part it cannot read, or texts past the reading budget of cmd.
func HasPrivateKey ¶ added in v0.17.0
HasPrivateKey reports whether text holds a private key: its BEGIN line is enough (an END line can be built later), or a PuTTY key file header.
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; the word after << holds the body). It never fails: text it cannot read stays words, which the checks treat with suspicion; Lex says what it could not read.
Types ¶
type DockerRun ¶ added in v0.18.0
type DockerRun struct {
Args []string // the words after docker; docker-compose reads as compose
Dir string // the folder a cd before it moved to; "" is the login folder
// DirKnown is false when komando cannot tell the folder or the compose
// files: a cd to a variable or ~user, a subshell, COMPOSE_ settings
DirKnown bool
}
DockerRun is a docker command a shell line runs.
func DockerRuns ¶ added in v0.18.0
DockerRuns are the docker commands in cmd, with the folder each runs in, so the compose files they start containers from can be read.
type Encoded ¶ added in v0.17.0
type Encoded struct {
Kind string // base64, base32, hex, hex escapes, octal escapes, private key
Chars int // encoded characters
Decoded int // about how many bytes they decode to
First int
Last int
}
Encoded is one block of encoded data in a script or command: lines First to Last (from 1) that are mostly one kind of encoding.
func ScanEncoded ¶ added in v0.17.0
ScanEncoded finds the encoded data in text, by run and by block: lines in a row that are mostly encoded (a heredoc, a+="…" pieces) are one block, and a block or a single run of encodedMin characters or more counts. Not counted: one 64-hex-digit hash alone on a line that says it is a hash, and public PEM blocks (a certificate, a public key). A private key block counts as its own kind.
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.
func CheckScript ¶ added in v0.18.0
CheckScript is Check for a script komando wrote whole for one of its own commands (pkg security, docker prune): the rules that copy those commands' checks are left out, since the command rated itself already.
type SubLine ¶ added in v0.18.0
type SubLine struct {
Toks []Token
File bool // <( ): the word names a file that holds the output
// contains filtered or unexported fields
}
SubLine is the line in a substitution that is a whole word. What it prints is worked out once, however many readers ask (subScript).
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
// Body is, on the word after <<, the text of the here-document as the
// program reads it: in an unquoted one, \$ \` \\ and line joins undone
Body string
// Proc is, on a word that is one substitution ($( ), "$( )" or <( )),
// its line
Proc *SubLine
// Fd is, on a redirect, the descriptor word before it: 2 in 2>f
Fd string
}
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 ";".