policy

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 5 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 holds the policy data model and pure decision evaluator. The package is agent-agnostic: adapters consume only the resulting Decision; the package never imports adapter code.

The active policy is the embedded default at policy/builtin/policy.json. A future revision will replace Builtin() with a fetch from the StepSecurity backend; call sites consume Policy values and need not change. There is intentionally no on-disk override.

Index

Constants

View Source
const GenericBlockMessage = "Blocked by your organization's administrator."

GenericBlockMessage is the literal phrase shown to the agent on any block. It does not name files, registries, or packages — that detail goes to JSONL only, so the agent cannot guide the user to a bypass.

Variables

This section is empty.

Functions

func IsKnown

func IsKnown(e Ecosystem) bool

IsKnown reports whether e is one of the ecosystems the runtime recognizes.

Types

type Decision

type Decision struct {
	Allow          bool
	Code           DecisionCode
	UserMessage    string
	InternalDetail string
}

Decision is the evaluator's output. Adapters consume only Allow and UserMessage; Code and InternalDetail are JSONL-only.

func AllowDecision

func AllowDecision(code DecisionCode, detail string) Decision

AllowDecision builds an explicit allow decision with the given code.

func BlockDecision

func BlockDecision(code DecisionCode, detail string) Decision

BlockDecision builds a block decision with the generic user message.

func Eval

func Eval(p Policy, req Request) Decision

Eval is a pure function over Policy + Request. The runtime persists the returned Decision both on the JSONL event and (via the adapter) on the stdout response.

type DecisionCode

type DecisionCode string

DecisionCode names a structured reason. Codes go to the JSONL audit record; the agent only sees Decision.UserMessage, which is intentionally generic.

const (
	CodeAllowed            DecisionCode = "allowed"
	CodeRegistryNotAllowed DecisionCode = "registry_not_allowed"
	CodeRegistryFlag       DecisionCode = "registry_flag_override"
	CodeRegistryEnv        DecisionCode = "registry_env_override"
	CodeUserconfigFlag     DecisionCode = "userconfig_override"
	CodeManagedKeyMutation DecisionCode = "managed_key_mutation"
	CodeManagedKeyEdit     DecisionCode = "managed_key_edit"
	CodeInsufficientData   DecisionCode = "insufficient_data"
	CodePolicyDisabled     DecisionCode = "policy_disabled"
	CodeNotInstallCommand  DecisionCode = "not_install_command"
)

type Ecosystem

type Ecosystem string

Ecosystem names a language family the runtime can enforce policy on. One ecosystem subsumes multiple package-manager binaries (e.g. npm, pnpm, yarn, and bun all live under EcosystemNPM). Future entries: pypi, cargo, go.

const (
	EcosystemNPM Ecosystem = "npm"
)

func EcosystemFor

func EcosystemFor(binary string) Ecosystem

EcosystemFor maps an observed PM binary to its ecosystem. It returns "" when the binary does not belong to any ecosystem the runtime enforces today; callers treat that as "not policy-relevant" and fall through to allow.

This is the single binary→ecosystem dispatch in the codebase; do not reproduce the table elsewhere.

func KnownEcosystems

func KnownEcosystems() []Ecosystem

KnownEcosystems lists the ecosystems the runtime recognizes.

type EcosystemPolicy

type EcosystemPolicy struct {
	Enabled  bool           `json:"enabled"`
	Registry RegistryPolicy `json:"registry"`
}

EcosystemPolicy carries the per-ecosystem enforcement settings. Today every ecosystem is registry-pin only; future fields land here without changing the surrounding shape.

type Mode

type Mode string

Mode controls what the runtime does with a policy violation.

  • ModeAudit: evaluate, persist a finding describing what *would* have blocked, but always emit an allow response to the agent.
  • ModeBlock: evaluate, persist the finding, and on an explicit violation flip the response to block.

Mode is policy-wide; there is no per-ecosystem override. Endpoint-level behavior is the call the org wants to make uniformly across ecosystems.

const (
	ModeAudit Mode = "audit"
	ModeBlock Mode = "block"
)

func ResolveMode

func ResolveMode(p Policy) Mode

ResolveMode returns p.Mode if it is a known value; otherwise ModeAudit. Unknown or empty strings collapse to audit so a malformed policy can never silently switch the endpoint into block mode.

type ParsedCommand

type ParsedCommand struct {
	InlineEnv      map[string]string
	Binary         string
	Args           []string
	RegistryFlag   string
	UserconfigFlag string
	// ConfigOp is "" unless argv looks like `<pm> config <set|delete|edit> ...`.
	ConfigOp    string
	ConfigKey   string
	ConfigValue string
}

ParsedCommand is the pre-policy view of a shell argv: the leading `KEY=VAL` env, the package manager binary, the residual args, plus the flags we care about.

func ParseShell

func ParseShell(cmd string) ParsedCommand

ParseShell tokenizes cmd and pulls out the pieces the policy evaluator cares about. Unknown commands return Binary="" and the caller should treat the parse as a no-op.

type Policy

type Policy struct {
	Version    int                           `json:"version"`
	Mode       Mode                          `json:"mode,omitempty"`
	Ecosystems map[Ecosystem]EcosystemPolicy `json:"ecosystems"`
}

Policy is the active policy document. Per-ecosystem enforcement lives under Ecosystems; a missing or disabled block means the runtime allows that ecosystem unconditionally and emits no policy_decision.

func Builtin

func Builtin() Policy

Builtin returns the embedded policy. The embedded JSON is checked at build time by the test suite; a parse failure here is a programmer error, not a runtime condition.

type RegistryPolicy

type RegistryPolicy struct {
	// Allowlist is the set of permitted registry URLs. Matching is
	// prefix-based after trailing-slash normalization.
	Allowlist []string `json:"allowlist"`
}

RegistryPolicy expresses the secure-registry pinning policy.

type Request

type Request struct {
	Ecosystem        Ecosystem         // resolved by EcosystemFor(parsed.Binary)
	PackageManager   string            // raw binary observed: "npm" | "pnpm" | "yarn" | "bun" | "npx" | ...
	CommandKind      string            // "install" | "config_set" | "config_delete" | "config_edit" | "exec" | ...
	Registry         string            // resolved per cwd, e.g. "https://registry.npmjs.org/"
	RegistryFlag     string            // value of --registry= if present on argv
	UserconfigFlag   string            // value of --userconfig= if present on argv
	InlineEnv        map[string]string // KEY=VAL prefix env vars on argv
	ConfigKeyMutated string            // for config_set/config_delete: which key
	ConfigValue      string            // for config_set: the new value
}

Request is what the runtime hands to Eval after parsing a hook payload. All fields are optional except Ecosystem + CommandKind; missing data tends toward Allow (fail-open).

Jump to

Keyboard shortcuts

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