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 ¶
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.
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 ¶
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 ¶
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.