{"modulePath":"github.com/chainloop-dev/chainloop","version":"v1.138.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package redaction\n\nPackage redaction removes detected secrets from JSON documents before they leave the machine.\n\nDetection and rewriting are deliberately split. Secrets are detected against the document rendered as indented JSON, because the patterns that catch generic credentials rely on seeing a key next to its value. The rewrite then happens structurally, on the decoded value tree, so the output cannot be malformed JSON. A convergence loop re-scans after every rewrite, which makes idempotency a property of the algorithm rather than of the placeholder format.\n\n## Constants\n\n```go\nconst DefaultMaxPasses = 4\n```\nDefaultMaxPasses bounds the convergence loop. A successful redaction costs two passes: one that finds the secrets and one that confirms none are left.\n\nRedaction is deliberately unbounded in size and time. A session that cannot be scanned cannot be stored, and failing an attestation over a large-but-honest transcript is worse than taking a while over it. Callers that need a bound can impose one through the context they pass to Redact.\n\n```go\nconst RulesetVersion = 5\n```\nRulesetVersion identifies what the scanner detects. Bump it whenever betterleaks.toml, the betterleaks version, or the handling of findings in this file changes what gets redacted, so that policies can tell placeholders from different rulesets apart. Version 1 is the plain default ruleset.\n\n## Variables\n\n```go\nvar (\n\t// ErrNotConverged is returned when repeated passes keep detecting secrets,\n\t// which in practice means a placeholder is itself matching a rule.\n\tErrNotConverged = errors.New(\"secret redaction did not converge\")\n\t// ErrInvalidJSON is returned when the document is not a single JSON object.\n\tErrInvalidJSON = errors.New(\"document is not a JSON object\")\n\t// ErrDuplicateKey is returned when an object repeats a key. Decoding keeps\n\t// only the last value for a repeated key, so a secret in an earlier one would\n\t// never be scanned; redaction refuses the document rather than pass it\n\t// through unexamined.\n\tErrDuplicateKey = errors.New(\"document contains a duplicate object key\")\n)\n```\n\n## Functions\n\n```go\nfunc DefaultPlaceholder(ruleID string) string\n```\nDefaultPlaceholder is the replacement text for a redacted secret. It is deterministic, so redacting the same document twice yields the same digest, and it names the rule so a reviewer can tell what kind of credential was present without being able to recover it.\n\n```go\nfunc IsDefaultPlaceholder(s string) bool\n```\nIsDefaultPlaceholder reports whether s is a placeholder DefaultPlaceholder could have produced.\n\n## Types\n\n```go\ntype Finding struct {\n\t// RuleID names the rule that matched. It ends up in the placeholder, so it\n\t// must never contain secret material.\n\tRuleID string\n\t// Secret is the matched credential, verbatim as it appeared in the scanned\n\t// text.\n\tSecret string\n}\n```\nFinding is a located secret. It is deliberately decoupled from any particular scanning engine's types.\n\n```go\ntype OpaqueFilter func(path string, obj map[string]any, key string) bool\n```\nOpaqueFilter reports whether the string at obj\\[key] is opaque data, for example base64 media, that is neither scanned nor rewritten. path is the path of obj. Scanning such data is slow, its matches are false, and a rewrite breaks it.\n\n```go\ntype Option func(*Redactor)\n```\nOption customises a Redactor.\n\n```go\nfunc WithAllowMarkers() Option\n```\nWithAllowMarkers leaves in place a secret whose line carries an inline \"gitleaks:allow\" or \"betterleaks:allow\" marker, as a repository scan with either tool would. The default redacts it.\n\nA line is a line of the decoded string, such as one line of a file in a tool result, so the marker only covers the secrets next to it. An occurrence that spans more than one line is always redacted.\n\n```go\nfunc WithMaxPasses(n int) Option\n```\nWithMaxPasses bounds the convergence loop.\n\n```go\nfunc WithOpaque(f OpaqueFilter) Option\n```\nWithOpaque leaves out of the scan, and out of the rewrite, the string leaves that f reports as opaque data. The default has none.\n\n```go\nfunc WithPathFilter(f PathFilter) Option\n```\nWithPathFilter restricts which string leaves may be rewritten. The default allows every leaf.\n\n```go\nfunc WithPlaceholder(format func(ruleID string) string, matches func(string) bool) Option\n```\nWithPlaceholder overrides the replacement text derived from a rule id.\n\nThe two functions are supplied together on purpose. Some rules match a position rather than a value, so they report a placeholder as though it were a secret; recognising our own output is what stops redaction from rewriting it and keeps redacting an already-redacted document a no-op. A format without a matching recogniser would silently lose that.\n\n```go\ntype PathFilter func(path string) bool\n```\nPathFilter reports whether the string leaf at the given path may be rewritten. Paths look like \"/data/raw\\_session/main/0/content\": a leading slash, object keys and array indices separated by slashes.\n\n```go\ntype Redactor struct {\n\t// contains filtered or unexported fields\n}\n```\nRedactor rewrites JSON documents, replacing detected secrets with placeholders. It is safe for concurrent use as long as the Scanner is.\n\n```go\nfunc New(s Scanner, opts ...Option) *Redactor\n```\nNew builds a Redactor around the given Scanner.\n\n```go\nfunc (r *Redactor) Redact(ctx context.Context, doc []byte) ([]byte, *Report, error)\n```\nRedact returns a copy of doc with every detected secret replaced. When nothing is detected the input is returned verbatim, so that a document without secrets keeps its original digest.\n\n```go\nfunc (r *Redactor) RedactText(ctx context.Context, text string) (string, *Report, error)\n```\nRedactText returns a copy of text with every detected secret replaced. Redact rewrites the string leaves of a JSON document, so the text is carried as the single leaf of one. When nothing is detected the input is returned verbatim, byte for byte.\n\n```go\ntype Report struct {\n\t// Replacements is the total number of substitutions performed.\n\tReplacements int\n\t// ByRule counts substitutions per rule id.\n\tByRule map[string]int\n\t// Unlocated counts, per rule id, secrets the scanner reported but which\n\t// could not be attributed to any eligible leaf. That happens when the match\n\t// lies in a path the filter protects, or when it spans the JSON punctuation\n\t// between two adjacent leaves.\n\tUnlocated map[string]int\n\t// Allowed counts, per rule id, occurrences left in place because their line\n\t// carries an allow marker. Empty unless the Redactor honours markers.\n\tAllowed map[string]int\n\t// Passes is the number of detection passes performed.\n\tPasses int\n}\n```\nReport summarises a Redact call. It never contains secret material, so it is safe to log and to surface as material annotations.\n\n```go\nfunc (r *Report) Changed() bool\n```\nChanged reports whether anything was redacted.\n\n```go\nfunc (r *Report) RuleIDs() []string\n```\nRuleIDs returns the sorted, deduplicated ids of the rules that actually redacted something.\n\n```go\ntype Scanner interface {\n\tScan(ctx context.Context, text string) ([]Finding, error)\n}\n```\nScanner detects secrets in a text fragment.\n\n```go\nfunc DefaultScanner() (Scanner, error)\n```\nDefaultScanner returns the process-wide betterleaks-backed scanner. Constructing a detector compiles several hundred regexes and builds a keyword trie, so it is built once, lazily, and only for attestations that need it.\n\n","path":"github.com/chainloop-dev/chainloop/internal/redaction","name":"redaction","synopsis":"Package redaction removes detected secrets from JSON documents before they leave the machine.","isRedistributable":true}
