inspect

package
v0.0.0-...-230ac96 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 9 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 inspect runs content through inspection providers and resolves the policy engine's `inspect` decision against what they find.

The policy engine cannot resolve an inspect decision itself: it matches rules against a path, an argv or a destination, never the content those refer to (see internal/policy/engine.go, wrapRuleDecision). It therefore emits a Decision whose EffectiveDecision is deny with an InspectInfo attached. This package is what a content-holding caller uses to turn that into a real verdict -- see Resolve.

Index

Constants

View Source
const (
	KindFile      = "file"
	KindCommand   = "command"
	KindNetwork   = "network"
	KindProxyBody = "proxy_body"
	KindMCPArgs   = "mcp_args"
)

Kind describes where inspected content came from. Providers use it to pick a strictness, and it is recorded on the audit trail.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

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

Checker runs inspection profiles against content. It satisfies policy.InspectChecker, which is how it reaches the engine via Engine.SetInspector.

func NewChecker

func NewChecker(cfg Config) (*Checker, error)

NewChecker builds a Checker. Duplicate provider names are a configuration error rather than a last-one-wins surprise: two providers answering to the same name means half the profiles silently reach the wrong one.

func (*Checker) Inspect

Inspect runs every named profile against the content and merges the results. It satisfies policy.InspectChecker.

Any failure -- an unknown profile, an unconfigured provider, a privacy refusal, a provider error, a timeout -- returns an error rather than a clean verdict. Content that was not inspected must never be reported as content that was inspected and found clean; Resolve routes the error to the rule's on_failure, which defaults to fail_closed.

func (*Checker) InspectWith

func (c *Checker) InspectWith(ctx context.Context, req policy.InspectRequest, redactor Redactor) (policy.InspectVerdict, error)

InspectWith is Inspect with a caller-supplied redaction strategy. A nil redactor uses PlaceholderRedactor. It satisfies RedactingChecker, which is how Resolve passes a caller's redactor down without widening policy.InspectChecker -- that interface stays narrow so the policy package keeps no dependency on this one.

func (*Checker) Missing

func (c *Checker) Missing(profiles []string) []string

Missing reports the profiles a Checker cannot run, and why. It exists so a caller can refuse to install a policy up front rather than discovering at the first request that every inspect rule fails closed.

type Config

type Config struct {
	// Profiles is the policy's inspection.profiles block.
	Profiles map[string]policy.InspectionProfile
	// Providers are the available inspectors, keyed on Name().
	Providers []Provider
	// Privacy gates what may reach a non-local provider. Nil permits
	// nothing remote.
	Privacy *PrivacyConfig
	// ProviderTimeout bounds one provider call. Zero uses
	// defaultProviderTimeout.
	ProviderTimeout time.Duration
}

Config configures a Checker.

type Finding

type Finding struct {
	// Profile is the profile that produced this finding.
	Profile string
	// Category is the taxonomy label, e.g. private_email or secret. For a
	// query-based provider it is the query's id.
	Category string
	// Start and End are byte offsets into the inspected content. Both are
	// zero for a finding with no span, such as a query answer.
	Start int
	End   int
	// Score is the provider's confidence in [0,1]. Zero means the provider
	// does not report one.
	Score float64
}

Finding is one detected span or one answered query.

A Finding deliberately does NOT carry the matched text. That text is exactly the sensitive material inspection exists to keep from spreading, and findings end up in logs, audit events and error messages. Callers that need the content have it already; everyone else gets offsets.

func (Finding) HasSpan

func (f Finding) HasSpan() bool

HasSpan reports whether the finding identifies a byte range that can be redacted. A query answer ("does this exfiltrate credentials?") has no span, so on_violation: redact cannot act on it.

type LocalProvider

type LocalProvider interface {
	IsLocal() bool
}

LocalProvider is an optional interface for providers that run entirely in-process and make no network calls. The privacy gate lets those see content unconditionally; everything else has to be permitted explicitly. It mirrors pkgcheck.LocalProvider, which draws the same distinction for package names.

type Option

type Option func(*options)

Option customises a Resolve call.

func WithRedactor

func WithRedactor(r Redactor) Option

WithRedactor supplies the redaction strategy for this call.

It is per-call rather than per-Checker because a Checker is built from the policy, which is shared, while the redaction machinery belongs to one caller: the proxy's token store is scoped to a session, and handing a session's store to a Checker shared across sessions would let one session's values be restored into another's traffic.

type PlaceholderRedactor

type PlaceholderRedactor struct{}

PlaceholderRedactor blanks a finding out, keeping only its category.

This is the default because it is the only strategy that is safe without knowing anything about the caller: it retains nothing. The cost is that the value is destroyed for everything downstream, not just for the model.

func (PlaceholderRedactor) Replace

func (PlaceholderRedactor) Replace(category, _ string) string

Replace implements Redactor.

type PrivacyConfig

type PrivacyConfig struct {
	// AllowRemote permits providers that are not LocalProvider to see
	// content at all. False -- the zero value -- means only in-process
	// providers run.
	AllowRemote bool

	// RemoteKinds restricts which content kinds may reach a remote
	// provider, using the Kind* constants. Empty with AllowRemote set means
	// every kind may. It exists because the kinds differ enormously in
	// sensitivity: a command argv is usually a path and a flag, while a
	// proxy body is whatever the agent was about to send anywhere.
	RemoteKinds []string
}

PrivacyConfig gates what may leave the machine.

This is the load-bearing difference between inspection and every other check in this codebase. pkgcheck sends package *names* to third parties; inspection sends the content itself, and the content is the sensitive material -- the request body, the file being written, the argv. Sending PII to a remote service to ask whether it contains PII is the failure that makes the feature worse than not having it.

The zero value permits nothing remote. Remote egress is opt-in, per kind.

type PrivacyGate

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

PrivacyGate decides whether a given provider may see a given request.

func NewPrivacyGate

func NewPrivacyGate(cfg *PrivacyConfig) *PrivacyGate

NewPrivacyGate builds a gate from configuration. A nil config gates everything remote, which is the safe reading of "not configured".

func (*PrivacyGate) Allows

func (g *PrivacyGate) Allows(p Provider, kind string) (bool, string)

Allows reports whether p may inspect content of the given kind, and why not when it may not. The reason is for the audit log and for the operator; it names the kind and the provider, never the content.

type Provider

type Provider interface {
	// Name returns the provider's identifier, matching the `provider:` field
	// of an inspection profile.
	Name() string

	// Categories returns the finding categories this provider can produce.
	// A profile naming a category no provider covers is a configuration
	// error the caller should surface, not a silent clean result.
	Categories() []string

	// Inspect examines req.Content and returns findings.
	Inspect(ctx context.Context, req Request) (*Response, error)
}

Provider inspects content and reports findings. Implementations live in internal/inspect/provider.

type ProviderError

type ProviderError struct {
	Provider string
	Profile  string
	Err      error
}

ProviderError records a failure from a single provider.

func (ProviderError) Error

func (e ProviderError) Error() string

func (ProviderError) Unwrap

func (e ProviderError) Unwrap() error

type RedactingChecker

type RedactingChecker interface {
	InspectWith(ctx context.Context, req policy.InspectRequest, r Redactor) (policy.InspectVerdict, error)
}

RedactingChecker is optionally implemented by inspectors that accept a caller-supplied redaction strategy. *Checker implements it.

type Redactor

type Redactor interface {
	// Replace returns the text to substitute for one finding.
	//
	// matched is the exact bytes the finding covers. An implementation that
	// stores it is storing exactly the sensitive material inspection exists
	// to contain, and must be scoped and cleared accordingly.
	Replace(category, matched string) string
}

Redactor decides what replaces a finding's matched text.

It exists because the right replacement depends on the caller, not on the policy. The proxy can mint a reversible pseudonym and restore the real value on the way back, so the model never sees it but everything downstream still works. A caller with no such machinery can only blank the value out. Both are "redact" as far as a rule is concerned.

type Registry

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

Registry hands out a Checker for a given policy.

A Checker is per-policy, because the profiles it runs come from the policy's own inspection block, while the providers come from host configuration. Attaching one to each engine at construction would mean finding every place an engine is built -- there are eight outside the policy package, and the equivalent Tor wiring reaches only three of them (internal/api/session_policy.go:91 documents that gap). A registry keyed on the policy is immune to that: a session running a named policy file, and an engine swapped in by a live policy push, both get a correct Checker without anyone remembering to attach it.

func NewRegistry

func NewRegistry(providers []Provider, privacy *PrivacyConfig, timeout time.Duration) *Registry

NewRegistry builds a Registry over the host's configured providers.

func (*Registry) For

func (r *Registry) For(p *policy.Policy) (*Checker, error)

For returns the Checker for p.

The result is memoised on the most recent policy only. Building a Checker is two small maps, so a miss is cheap, and a single-entry memo can never grow: keeping a map keyed on policy pointers would retain every policy ever swapped in for the life of the process.

func (*Registry) Providers

func (r *Registry) Providers() []string

Providers returns the configured provider names, for logging and for `agentmon detect`.

type Request

type Request struct {
	// Profile is the name the policy gave this profile.
	Profile string
	// Spec is the profile definition from the policy's inspection block.
	Spec policy.InspectionProfile
	// Kind is one of the Kind* constants.
	Kind string
	// Content is the text to inspect.
	Content string
}

Request is one profile applied to one piece of content.

type Response

type Response struct {
	Provider string
	Findings []Finding
	Metadata ResponseMetadata
}

Response is one provider's answer for one request.

type ResponseMetadata

type ResponseMetadata struct {
	Duration time.Duration
	Error    string
}

ResponseMetadata holds operational details about a provider response.

type Result

type Result struct {
	// Decision is the resolved decision. Its EffectiveDecision is now
	// terminal -- allow, deny or approve -- and safe for an enforcement
	// backend to act on.
	Decision policy.Decision
	// Content is what the caller should use going forward. It differs from
	// the input only when Rewritten is true.
	Content string
	// Rewritten is true when on_violation was redact and spans were
	// replaced.
	Rewritten bool
	// Verdict is what the inspector reported. Zero when inspection failed.
	Verdict policy.InspectVerdict
	// Err is the inspection failure that sent this through on_failure. It
	// is nil on both a clean result and a violation; a non-nil Err means
	// the content was NOT successfully inspected.
	Err error
}

Result is the outcome of resolving an inspect-bearing decision.

func Fail

func Fail(dec policy.Decision, content string, err error) Result

Fail routes a caller-side failure through the rule's on_failure, without calling an inspector.

It exists because some reasons the content cannot be inspected are known before an inspector would be reached -- a body over the caller's size cap, a stream that could not be buffered. Those must reach the same on_failure the rule declares, not a bare deny and not a skip: a request body too large to inspect is uninspected content, exactly like a provider timeout.

func Resolve

func Resolve(ctx context.Context, checker policy.InspectChecker, dec policy.Decision, kind, content string, opts ...Option) Result

Resolve turns an unresolved inspect decision into a terminal one.

The policy engine cannot do this: it matches rules against a path, an argv or a destination and never holds the content, so it emits a decision whose EffectiveDecision is deny with the inspection spec attached. Every caller that does hold the content -- a proxy body hook, the DLP pass, an MCP argument check -- routes it through here.

A decision with no inspection spec is returned untouched, so callers can funnel every decision through Resolve without first testing for one.

Directories

Path Synopsis
Package provider holds inspection provider implementations.
Package provider holds inspection provider implementations.
onnxrt
Package onnxrt calls ONNX Runtime from Go without cgo.
Package onnxrt calls ONNX Runtime from Go without cgo.
onnxrt/gen command
Command gen-ort-api extracts the OrtApi struct member order from onnxruntime_c_api.h and writes it as Go constants.
Command gen-ort-api extracts the OrtApi struct member order from onnxruntime_c_api.h and writes it as Go constants.
privacyfilter
Package privacyfilter decodes OpenAI Privacy Filter output into spans.
Package privacyfilter decodes OpenAI Privacy Filter output into spans.
privacyfilter/modelcache
Package modelcache downloads and verifies model files.
Package modelcache downloads and verifies model files.
privacyfilter/tokenizer
Package tokenizer implements the byte-level BPE tokenizer that openai/privacy-filter was trained with.
Package tokenizer implements the byte-level BPE tokenizer that openai/privacy-filter was trained with.

Jump to

Keyboard shortcuts

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