policy

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0 Imports: 6 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 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

View Source
const Sub = "$(...)"

Sub stands in a word for a command substitution: $( ), ` `, <( ) or >( ).

Variables

This section is empty.

Functions

func FillParams added in v0.3.0

func FillParams(script string, params []string) string

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

func Invalid(extra []string) map[int]string

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 IsCFlag added in v0.3.0

func IsCFlag(w string) bool

IsCFlag is -c or a group of short flags with c in it (-ec, -lc).

func IsDescriptor added in v0.3.0

func IsDescriptor(w string) bool

IsDescriptor: the 2 in >&2, or - (close).

func IsShell added in v0.3.0

func IsShell(name string) bool

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

func Lex(s string) ([][]Token, string)

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

func Normalize(cmd string) string

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

func PSOnlyReads(script string) bool

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

func PSReadOnly(script string) string

PSReadOnly is why a PowerShell script is not accepted under --read-only, or "" when it is in the read-only subset.

func Pattern

func Pattern(r Rule) string

func Plain added in v0.3.0

func Plain(cmd string) string

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

func ShellScript(name string, args []string) (script string, params []string, ok bool)

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

func Tokens(s string) [][]Token

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

func Check(cmd string, extra []string) []Rule

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 Rules

func Rules() []Rule

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
}

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 ";".

Jump to

Keyboard shortcuts

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