redact

package
v0.22.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 7 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 removes secrets from text an app logs, shows or exports (spec §13, v1.0.9): API keys and tokens in URL queries, signatures of presigned URLs, passwords in proxy and other URLs, and bearer tokens. Apps pass every log line, error detail and diagnostics file through Text (or wrap errors with Error) before writing it.

log.Print(redact.Text(err.Error()))
u := redact.URL("https://x.googleapis.com/v2?key=AIza…&q=hi") // …?key=REDACTED&q=hi

Index

Constants

View Source
const Mask = "REDACTED"

Mask replaces a secret.

Variables

View Source
var SecretParams = []string{
	"key", "api_key", "apikey", "api-key", "token", "access_token", "refresh_token", "id_token",
	"auth", "authorization", "password", "passwd", "pwd", "secret", "client_secret",
	"sig", "signature", "x-amz-signature", "x-amz-credential", "x-amz-security-token",
	"x-goog-signature", "x-goog-credential", "pin", "code",
	"accesstoken", "refreshtoken", "idtoken", "clientsecret", "session_token", "sessiontoken",
}

SecretParams are the query parameters (lower case) whose values are secrets. Matching ignores case.

Functions

func Error

func Error(err error) error

Error wraps err so that its message is redacted (Text); errors.Is and errors.As still see the original.

func Text

func Text(s string) string

Text redacts every URL in s (URL), secret parameters outside URLs (token=…; key=, auth=, code=, pin= only after ? or &), secret JSON string fields ("api_key": "…"), Authorization and Proxy-Authorization headers (the scheme word stays: "Authorization: Bearer REDACTED"), API-key and Cookie headers, bearer tokens, and well-known key shapes (Google AIza…, OpenAI/Anthropic sk-…, Stripe sk_live_…, Hugging Face hf_…, GitHub gh*_… and github_pat_…, GitLab glpat-…, Slack xox*-…, JWTs).

func URL

func URL(raw string) string

URL redacts one URL: the password of its user info (user:pass@ → user:REDACTED@; a lone user, which proxies use as a token, too) and the values of SecretParams in its query. A URL that net/url rejects (a bad escape such as %zz or p%ss, a broken host) is redacted as raw text: its user info and secret query values by position, then the rules Text uses outside URLs. Text without a scheme goes through Text.

Types

type Personal added in v0.21.0

type Personal struct {
	// Home is the user's home folder ("/Users/ann").
	Home string
	// Names are words to replace with "USER" wherever they stand alone:
	// the login and full name, the Mac's name. Names shorter than 3
	// characters are ignored (they would match too much), except
	// two-character names in other scripts ("张三").
	Names []string
	// KeepEmails are addresses left as they are (the one the user typed as
	// their contact, which the site sees anyway).
	KeepEmails []string
}

Personal removes what identifies the person from text an app sends away (feedback diagnostics, crash reports), after Text has removed the secrets: the home folder (→ "~"), other users' folders (/Users/<name> → /Users/USER), the user's and the Mac's names, email addresses (r***@gmail.com), and IP addresses (the last two parts of an IPv4 address, all of an IPv6 one but loopback). The zero value only masks emails, folders of /Users and addresses; ThisUser fills Home and Names for the running user.

func ThisUser added in v0.21.0

func ThisUser(computerNames ...string) Personal

ThisUser is Personal for the running user: their home, login and full name, and the host and computer names given.

func (Personal) Text added in v0.21.0

func (p Personal) Text(s string) string

Text masks the personal parts of s.

Jump to

Keyboard shortcuts

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