hook

package
v1.11.5 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: Apache-2.0 Imports: 18 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 hook implements the bounded, fail-open hot path invoked by `stepsecurity-dev-machine-guard _hook <agent> <hookEvent>`. Every stage MUST be capped, redaction-first, and resilient to internal errors: the agent waits for stdout, so a failure here can never become a non-zero exit or a stalled response.

Persistence-by-design omission: this package does NOT write events.jsonl. The only on-disk artifact is the errors log appended through the LogError seam; the event itself is either delivered to UploadEvent or dropped.

Index

Constants

View Source
const (
	CapHook       = 15 * time.Second
	CapPM         = 10 * time.Second
	CapMCP        = 10 * time.Second
	CapSecretMin  = 30 * time.Second
	CapSecretMax  = 60 * time.Second
	MaxStdinBytes = 5 * 1024 * 1024 // 5 MiB
)

Every hook invocation MUST honor these caps.

CapHook bounds the worst-case agent stall on a hung invocation. It is 15s to absorb the 1s identity probe and a 5s upload under load. The agent's own hook timeout (Claude Code defaults to 60s) is the absolute ceiling above us.

View Source
const UploadTimeout = 5 * time.Second

UploadTimeout is the per-invocation cap on the synchronous upload stage. Mirrors ingest.DefaultHookUploadTimeout; kept here so the runtime stays decoupled from the ingest package's HTTP client.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runtime

type Runtime struct {
	Adapter adapter.Adapter
	Exec    executor.Executor
	Stdin   io.Reader
	Stdout  io.Writer
	Stderr  io.Writer
	Now     func() time.Time

	// UploadEvent is the synchronous backend ingestion seam. nil means
	// upload is disabled — the local-only behavior the runtime falls
	// back to whenever enterprise config is missing. Production wires
	// this to an ingest.Client closure via cli.newUploader; tests
	// inject a capture function. The event passed in already carries
	// customer_id, device_id, and user_identity stamped from the same
	// identity.Resolve call, so the seam intentionally does not take a
	// separate identity argument.
	UploadEvent func(ctx context.Context, ev event.Event) error

	// LogError is the errors.jsonl appender seam. nil means errors are
	// silently dropped — fail-open is the contract; logging is best
	// effort. Production wires this to cli.AppendError; tests can
	// capture the calls by setting their own function. Signature
	// matches cli.AppendError(stage, code, message, eventID).
	LogError func(stage, code, message, eventID string)
}

Runtime wires every dependency the hot path needs.

All fields are exported so tests and the CLI handler can construct a Runtime by struct literal. Production code prefers NewRuntime, which fills in defaults (real executor, os.Std{in,out,err}, UTC clock).

func NewRuntime

func NewRuntime(a adapter.Adapter) *Runtime

NewRuntime constructs the default runtime for the given adapter. The hook package no longer knows about any concrete adapter; agent selection is the CLI's job.

func (*Runtime) Run

func (rt *Runtime) Run(parent context.Context, hookType event.HookEvent) error

Run executes one hook invocation. It always writes an adapter-compatible response to stdout, even on internal failure. The default verdict is allow; only an explicit policy match flips it to block.

The returned error, if any, is purely informational for the CLI exit path: the CLI swallows it so the process exit code stays 0.

Jump to

Keyboard shortcuts

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