permission

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package permission implements glob-based command permission checking.

Matching semantics

Rules are evaluated against three normalized projections of every shell command found anywhere in the input — including pipelines, command substitution ($(...) / `...`), process substitution (<(...) / >(...)), arithmetic expansion ($((...))), and redirect targets:

  1. The full normalized command (env-prefix included), e.g. "KUBECONFIG=x kubectl get pods".
  2. The same command with leading variable assignments stripped, e.g. "kubectl get pods". This prevents trivial bypass via env prefix and prevents false-deny when a rule lists only the program form.
  3. Redirect targets are projected as synthetic commands (">/path", ">>/path", "&>/path", "&>>/path", ">|/path") so that rules can gate writes the same way they gate executions. Read redirects (<, <<, <<<, <>, here-docs) are not checked beyond their inner command (here-doc body and here-string undergo expansion).

Both rule patterns and the command projections are run through the shell printer's canonical formatter, so "kubectl get pods" matches the rule "kubectl get pods" regardless of spacing.

Precedence

Rules are evaluated in specificity order, and the first match wins. Specificity is "longer literal prefix before any '*' wins"; the catch-all "*" is always tried last. A deny rule that matches first beats a more general allow rule, and vice-versa.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRules

func DefaultRules() map[string]string

DefaultRules returns a set of default rules that trust model capabilities.

func KubernetesReadOnlyRules

func KubernetesReadOnlyRules() map[string]string

KubernetesReadOnlyRules returns rules for read-only Kubernetes operations.

func SafeReadOnlyRules

func SafeReadOnlyRules() map[string]string

SafeReadOnlyRules returns rules that allow common read-only operations.

Notably absent compared to a naive list:

  • "find *" — allowed `find . -delete` and `find . -exec rm`. Specific safe variants are listed instead, none of which can mutate state.
  • "echo *" — allowed `echo x > /etc/passwd`. Write redirects are now gated by checkRedirect, but plain "echo" is rarely needed for diagnostics, so it is dropped to stay tight by default.

Write redirects are denied unless an explicit allow rule for the synthetic "> target" form is added (e.g. "> /tmp/*").

Types

type Action

type Action string

Action represents the permission action.

const (
	ActionAllow Action = "allow"
	ActionDeny  Action = "deny"
)

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker checks command permissions against configured rules.

func NewChecker

func NewChecker(patterns map[string]string) *Checker

NewChecker creates a new permission checker from a map of patterns to actions. Rules are sorted so that the most specific rule (longest literal prefix before any '*') is tried first; "*" is always the fallback.

func (*Checker) Check

func (c *Checker) Check(command string) error

Check checks if a command is allowed. Returns nil if allowed, error with reason if denied.

func (*Checker) IsAllowed

func (c *Checker) IsAllowed(command string) bool

IsAllowed is a convenience method that returns true if command is allowed.

type Rule

type Rule struct {
	Pattern string
	Action  Action
	// contains filtered or unexported fields
}

Rule represents a single permission rule.

Jump to

Keyboard shortcuts

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