redact

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 4 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 redact performs outbound redaction of sensitive values from byte payloads (tool results, JSON-RPC errors, and notifications) before they leave the gateway or are written to the audit log.

It removes two classes of data:

  • Injected secret values, matched by exact value. All secrets are matched in a single pass with an Aho-Corasick automaton, so cost is independent of the number of secrets rather than O(secrets) repeated scans.
  • PII, matched by a literal-gated regular expression. Each pattern's compiled RE2 regexp is consulted only when a cheap byte-substring pre-screen for its anchor literal succeeds.

A Redactor is built once with New and is immutable thereafter: it holds only read-only state, so Redact is safe for concurrent use by multiple goroutines. Redact never mutates its input; it returns a freshly allocated slice. Callers therefore treat tool results and call arguments as immutable.

Overlap policy: secret occurrences are merged into maximal spans. Overlapping or directly adjacent secret matches (for example secrets "abc" and "bcd" in "abcd", or a secret that is a substring of another) collapse into one redacted span replaced by a single placeholder, so the output is never corrupted and no byte is redacted twice.

Out of scope (future work): encoding canonicalization. This package matches only the literal bytes it is given; it does not decode base64, percent-encoding, or other transforms before matching. A secret that appears only in an encoded form is not redacted by this implementation.

Index

Constants

View Source
const Placeholder = "[REDACTED]"

Placeholder is the text substituted for every redacted secret value and every literal-gated PII match.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pattern

type Pattern struct {
	Name   string
	Regex  string
	Anchor string
}

Pattern is a literal-gated PII pattern. The application layer maps configured patterns onto this type.

Regex is an RE2 regular expression (see the regexp package). Anchor is a plain literal substring that must be present in the input for the regex to be run at all; it is a constant-factor pre-screen, not a correctness requirement, so it should be a literal that necessarily appears in every match (often a fixed prefix or separator of the pattern). Name identifies the pattern for operators and is not used during matching.

type Redactor

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

Redactor removes secret values and PII from byte payloads. It is immutable after construction and safe for concurrent use.

func New

func New(secrets []string, patterns []Pattern) (*Redactor, error)

New builds a Redactor over the given secret values and PII patterns.

Secrets are loaded into a single Aho-Corasick automaton for single-pass, multi-string exact matching; empty secret strings are ignored. Each pattern's regular expression is compiled with regexp.Compile, and the first uncompilable regex makes New return a non-nil error. The returned Redactor is immutable and safe for concurrent use.

func (*Redactor) Redact

func (r *Redactor) Redact(data []byte) (redacted []byte, count int)

Redact returns a new slice in which every occurrence of a registered secret value and every literal-gated PII match has been replaced by Placeholder. The returned count is the total number of replacements: one per merged secret span (see the package overlap policy) plus one per PII match. The input slice is never modified.

Jump to

Keyboard shortcuts

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