guardrails

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 3 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 guardrails implements request-content guardrails (G5): regex-PII detection (email/phone/card/API keys) and conservative prompt-injection heuristics. Three enforcement modes driven by config.Guardrails.Mode:

  • "log" — findings are reported to telemetry, the request passes through;
  • "redact" — matched PII in user/system messages is masked with [REDACTED] before forwarding;
  • "block" — any finding rejects the request with 403 guardrail_blocked.

Heuristics are deliberately phrase-level (not word-level) so ordinary developer chat ("ignore the noise in this dataset", "show me the system prompt of your friend") does not false-positive; the eval suite in guardrails_test.go pins that contract.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KindString

func KindString(kinds []Finding) string

KindString joins detected kinds into a stable audit label.

func Redact

func Redact(body []byte, cfg Config) ([]byte, int)

Redact masks matched PII in user and system messages. Returns the rewritten body and the number of masked occurrences. Injection hits are not masked (they carry no secret); run with block mode for those.

Types

type Config

type Config struct {
	Mode string    `json:"mode,omitempty"`
	PII  PIIConfig `json:"pii,omitempty"`
	// Injection enables the prompt-injection heuristics. When zero-valued we
	// inherit from the mode (see Effective).
	Injection bool `json:"injection,omitempty"`
}

Config mirrors the yaml `guardrails:` block. Mode off/empty disables.

func (Config) Effective

func (c Config) Effective() Config

Effective fills defaults: with a mode set, PII flags and Injection default to true unless explicitly disabled.

func (Config) Enabled

func (c Config) Enabled() bool

type Finding

type Finding struct {
	Kind  string `json:"kind"`
	Count int    `json:"count"`
}

Finding describes one guardrail hit.

func Check

func Check(body []byte, cfg Config) []Finding

Check scans user messages for PII and injection heuristics. Returns nil when nothing applies.

type PIIConfig

type PIIConfig struct {
	Email  bool `json:"email,omitempty"`
	Phone  bool `json:"phone,omitempty"`
	Card   bool `json:"card,omitempty"`
	APIKey bool `json:"api_key,omitempty"`
}

Jump to

Keyboard shortcuts

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