redact

package
v0.1.443 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package redact keeps secrets on the machine. What the gateway sends a vendor has its secrets (API keys, private keys, tokens, passwords in connection strings and assignments), and when asked personal data and the user's own words, swapped for placeholders like {{API_KEY_k3v9x2mq}}; what the vendor says back has them swapped back, streams included, so the agent and the user see the real thing while the vendor never does.

A placeholder is the same for the same value every time (a keyed hash of it), so a conversation's history reads the same turn after turn — what a vendor cached of it stays good, and a thinking block's signature still matches the text it was made for — and one placeholder is never two values. The values are held in memory only, by placeholder, as requests bring them; a restart loses nothing, since the next request brings them again.

Index

Constants

View Source
const (
	MaxRules = 32
)

How many rules, and how long each part of one, a user may have.

Variables

This section is empty.

Functions

func Known

func Known() bool

Known says there are values to put back.

func Mask

func Mask(s string, o Options) (string, int)

Mask swaps what o covers in s for placeholders, and says how many.

func MaskJSON

func MaskJSON(body []byte, o Options) ([]byte, int)

MaskJSON masks the strings of a request body, and says how many values it masked. A body that isn't JSON is masked as text.

func Restore

func Restore(s string, escaped bool) string

Restore puts the values back in s. escaped is for text that is itself JSON (a tool call's arguments), where a value goes in escaped.

func RestoreJSON

func RestoreJSON(body []byte) []byte

RestoreJSON puts the values back in a response body.

func RuleKind added in v0.1.388

func RuleKind(kind string) string

RuleKind is kind as a placeholder can have it: upper case letters, digits and _, starting with a letter, at most 24 long; "" is CUSTOM.

func SetKeyPath added in v0.1.187

func SetKeyPath(path string)

SetKeyPath chooses where the placeholder key is kept across restarts; "" keeps it in memory only. The gateway's owner sets it before requests arrive. Once the key is loaded, both it and its path stay fixed.

Types

type Options

type Options struct {
	Secrets  bool     // API keys, private keys, tokens, passwords
	Personal bool     // emails, phone numbers, ID and bank card numbers
	Words    []string // the user's own: names, codenames, hosts
	Rules    []Rule   // the user's own rules, masked with the secrets
}

Options says what to mask.

type Rule added in v0.1.388

type Rule struct {
	Kind   string `json:"kind"`
	Prefix string `json:"prefix,omitempty"`
	// Regex is an RE2 pattern; the value is its group 1 when it has one,
	// else all of what it matches, as with magpie's own rules.
	Regex string `json:"regex,omitempty"`
}

Rule is one the user added (Settings → Privacy) for a secret magpie's own rules don't know, a gateway's key say (#195): what its placeholders are called, and the prefix its values start with or a pattern for them. Its values are masked with the secrets, as they are: the same placeholder for the same value, put back in what the vendor answers.

func CheckRules added in v0.1.388

func CheckRules(in []Rule) ([]Rule, error)

CheckRules tidies the user's rules (the kind as a placeholder has it, spaces trimmed, the empty and the repeated dropped) and says what is wrong with one that can't be used: a pattern that doesn't compile, or matches nothing at all, a prefix too short, too many or too long.

type Writer

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

Writer puts the values back in what a vendor answers, on its way to the agent. A stream is put back event by event, as it comes; anything else when it is done, at Finish.

A placeholder a stream splits between two events — {{API_ in one text delta, KEY_k3v9x2mq}} in the next — is put back whole: an event whose text ends in what may be the start of one is held until the next, and if that one goes on with the same text the start moves over to it. Nothing is added or dropped, so the text the agent puts together is the same.

func NewWriter

func NewWriter(w http.ResponseWriter) *Writer

NewWriter wraps w; Finish must be called when the handler is done.

func (*Writer) Finish

func (rw *Writer) Finish()

Finish writes what is still held.

func (*Writer) Flush

func (rw *Writer) Flush()

func (*Writer) Header

func (rw *Writer) Header() http.Header

func (*Writer) Unwrap

func (rw *Writer) Unwrap() http.ResponseWriter

Unwrap is for http.ResponseController.

func (*Writer) Write

func (rw *Writer) Write(p []byte) (int, error)

func (*Writer) WriteHeader

func (rw *Writer) WriteHeader(code int)

Jump to

Keyboard shortcuts

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