security

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 16 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 security enforces security outside the LLM (§43): redaction, path guards, HTML sanitization, trust classification, and the local-API token.

Index

Constants

This section is empty.

Variables

View Source
var ErrPathEscape = errors.New("security: path escapes allowed root")

ErrPathEscape is returned when a path resolves outside its allowed root.

Functions

func FindCredential

func FindCredential(s string) string

FindCredential returns the first credential-like pattern found in s, or "" if none. It is the detection counterpart to Redact: the same catalog, used as a hard rejection signal (§45) rather than a redaction.

func Fingerprint added in v0.6.0

func Fingerprint(value string) string

Fingerprint returns a secret value's fingerprint using an empty HMAC key (review H3: kept for callers with no per-project key -- tests, and any use outside a project's own Redactor). FingerprintWithKey is what every project-scoped call site should use instead, so the same secret does not fingerprint identically across every installation that ever saw it.

func FingerprintWithKey added in v0.6.0

func FingerprintWithKey(key []byte, value string) string

FingerprintWithKey returns the stable HMAC-SHA256 hex digest of a secret value under key, used as the "not a secret" allowlist key (review changes #1, review H3: an HMAC keyed per-project replaces the original bare sha256, which was the same, precomputable value for the same secret on every installation). It is computed over the value's capture group, never the raw match text (which may include a vendor prefix or surrounding quotes), so the same secret fingerprints the same way regardless of quoting or whitespace. A nil/empty key is a valid (degraded, but never an error) case: the HMAC is still deterministic, just not keyed to a project.

func LoadOrCreateToken

func LoadOrCreateToken(path string) (string, error)

LoadOrCreateToken returns the per-install API token, creating it (0600) if absent. The token authenticates every local-API request (§15).

func NewRedactingHandler added in v0.7.0

func NewRedactingHandler(inner slog.Handler, redact func(string) string) slog.Handler

NewRedactingHandler wraps inner so that every log record passes through redact before inner formats or writes it: the message, every attribute value (string, error, fmt.Stringer, or any other value), nested groups, and the attributes bound earlier with Logger.With.

It exists because the daemon log was the one persisted surface the redaction choke point did not reach (audit 3 / S4): daemon.log is written to disk and served by GET /api/v1/logs, and the daemon logs wrapped errors that can carry a provider's response body, a DSN, or a command's output verbatim.

Redaction happens per value, not on the formatted line, so it works under any inner handler (text or JSON) without parsing its output. That loses one shape a line-level pass would see: the generic credential-assignment rule keys on the NAME next to the value ("password=..."), and an attribute's key and value only meet in the formatted output. redactAttr reconstructs that pairing explicitly (key + "=" + value) so `log.Info("x", "password", "hunter2xyz")` is masked the same way the same text in a message would be.

A value whose redaction changes nothing is passed through untouched, with its original kind, so a clean []string or struct keeps the inner handler's native formatting (a JSON array stays an array). Only a value that actually carried a credential is replaced, by its redacted string form.

Group names and attribute keys are not redacted: they are constants in the daemon's own source, never data.

func Redact

func Redact(s string) string

Redact applies pattern-based redaction only (no known values). Used as a convenience for callers without resolved secrets.

func ResolveWithin

func ResolveWithin(root, rel string) (string, error)

ResolveWithin joins rel to root, canonicalizes it, and guarantees the result (after symlink resolution of the deepest existing ancestor) stays within root. It is the single path guard for blob/config/cache/log access (§15).

func SanitizeMarkdown

func SanitizeMarkdown(s string) string

SanitizeMarkdown neutralizes raw HTML and dangerous URLs in artifact bodies before they are served to the UI (§15, stored-XSS containment). The frontend additionally renders markdown with a renderer that escapes raw HTML.

func TokenEqual

func TokenEqual(a, b string) bool

TokenEqual compares two tokens in constant time.

func TrustFor

func TrustFor(selector string) domain.TrustLevel

TrustFor classifies context trust deterministically from its selector (§38). Trust is assigned by deterministic code here — never declared by model output.

Types

type CredentialFinding added in v0.6.0

type CredentialFinding struct {
	Path        string
	Line        int
	Kind        string
	Fingerprint string
	Overridable bool
	Preview     string
	MaskedLine  string
	// Advisory marks a match downgraded to non-blocking (ICP re-run defect
	// #1): a generic "credential assignment" match found in a test/fixture
	// path (isTestFixturePath) -- never a provider-specific format or a
	// configured secret value, and never outside a test/fixture path. The
	// caller (workflow.Engine) is responsible for actually treating this as
	// non-blocking; this field only carries the classification.
	Advisory bool
}

CredentialFinding is one detected credential occurrence, located but never valued: the file and line it was found on, the kind of secret matched, and its stable fingerprint. Overridable reports whether a human may disposition this specific match "not a secret" (true for a pattern-shaped match; false for a value drawn from the project's own configured secrets, which are never eligible for that override). Preview and MaskedLine (review H3) show what a not_a_secret disposition would allowlist without ever carrying the raw value: Preview is the value alone, masked (see maskValue); MaskedLine is the full source line with the value replaced by that same mask.

func FindCredentialsInFile added in v0.6.0

func FindCredentialsInFile(path, content string) []CredentialFinding

FindCredentialsInFile scans one file's content for every known credential pattern, returning each match's line, kind and fingerprint -- never the matched value itself. It is the location-aware counterpart to FindCredential, used so a denial can name path:line and the kind of secret instead of just rejecting silently (§ D2 secret sweep, review changes #1/#7).

Uses an empty HMAC key (see Fingerprint); (*Redactor)(nil).FindPatternsInFile is the per-project-keyed equivalent used inside a project's own pipeline.

type PIIFinding added in v0.7.0

type PIIFinding struct {
	Path    string
	Line    int
	Kind    string
	Preview string
	// Advisory is true in a test/fixture path (isTestFixturePath): shown to
	// the reviewer, never blocking. Fixture data is the one place a
	// realistic-looking card number or SSN is routine.
	Advisory bool
}

PIIFinding is one detected occurrence of personal data in a file, located but never carrying the value: Preview is masked (a card number keeps only its last four digits, the way a receipt does; an SSN keeps nothing).

func FindPIIInFile added in v0.7.0

func FindPIIInFile(path, content string) []PIIFinding

FindPIIInFile scans one file's content for payment card numbers and US Social Security numbers (see the catalog comment above for exactly what is and is not detected).

type Redactor

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

Redactor redacts known secret values and credential patterns from content.

func NewRedactor

func NewRedactor(knownValues ...string) *Redactor

NewRedactor builds a Redactor with the given known secret values and no per-project fingerprint key (fingerprints fall back to Fingerprint's unkeyed, degraded form). Prefer NewRedactorWithKey wherever a project's own store is available, so its findings' fingerprints are HMAC-keyed per project (review H3).

func NewRedactorWithKey added in v0.6.0

func NewRedactorWithKey(key []byte, knownValues ...string) *Redactor

NewRedactorWithKey is NewRedactor with an explicit per-project HMAC key (review H3) -- typically store.DB.SecretFingerprintKey's result -- so every fingerprint this Redactor computes (known-value and pattern matches alike) is scoped to the project rather than a bare, cross-installation-comparable sha256 of the value.

func (*Redactor) ContainsCredential

func (r *Redactor) ContainsCredential(s string) bool

ContainsCredential reports whether s contains any known secret value or credential pattern. It is the detection counterpart to RedactString, used as a hard-reject signal (nil-safe: a nil Redactor falls back to patterns only).

func (*Redactor) FindKnownInFile added in v0.6.0

func (r *Redactor) FindKnownInFile(path, content string) []CredentialFinding

FindKnownInFile locates every configured secret value (r.known) within one file's content, by line. Unlike FindCredentialsInFile's pattern matches, these are never Overridable: a value drawn from the project's own resolved secrets (a provider key, a .env value) is a real credential by construction, not a shape that might be a false positive.

func (*Redactor) FindPatternsInFile added in v0.6.0

func (r *Redactor) FindPatternsInFile(path, content string) []CredentialFinding

FindPatternsInFile is FindCredentialsInFile's per-Redactor, per-project-key equivalent (review H3): scans content for every known credential pattern, fingerprinting each match with this Redactor's key (nil-safe: a nil *Redactor falls back to an empty key, exactly matching the package-level FindCredentialsInFile).

func (*Redactor) Redact

func (r *Redactor) Redact(content []byte) ([]byte, error)

Redact implements the blob.Redactor contract ([]byte in, []byte out).

func (*Redactor) RedactString

func (r *Redactor) RedactString(s string) string

RedactString redacts a string. It is safe to call concurrently.

Every catalog pattern's regex match is redacted unconditionally UNLESS the pattern defines its own narrow redactGate (review C3): this is the blob store's own redactor (daemon/runtime.go), so anything it lets through reaches persisted model artifacts, findings and agent output verbatim. A prior version instead redacted only the matches that passed each pattern's p.valid gate -- the same validity gate FindPatternsInFile/ContainsCredential use to decide whether to raise a FINDING, which for most patterns includes a placeholder-vocabulary/low-entropy check -- which meant a real secret that merely LOOKED like a placeholder (a Slack token whose numeric ID segments produced a 4-digit run by chance, a password containing "latest") was never masked at all, not even silently: it passed through in the clear. p.valid stays exactly where it belongs -- FindPatternsInFile, ContainsCredential, FindCredential -- deciding whether a match is worth surfacing to a reviewer or treating as a hard-reject signal, a question entirely separate from "must this be masked before it's ever persisted". redactGate is reserved for a narrower, non-placeholder structural check (the generic assignment rule's own "is this actually credential-shaped at all, or a variable reference/function call/template placeholder" gate) -- see credentialPattern's doc comment.

Jump to

Keyboard shortcuts

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