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 ¶
const Mask = "REDACTED"
Mask replaces a secret.
Variables ¶
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 ¶
Error wraps err so that its message is redacted (Text); errors.Is and errors.As still see the original.
func Text ¶
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 ¶
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.