redact

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 7 Imported by: 3 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 credentials from text before it is logged, stored or shown.

Values removes secrets a program knows, such as the token it just sent, as they are and in the forms they take inside a request: URL-escaped and JSON-escaped. It does not find a secret inside a larger encoded value, such as the Base64 string of HTTP Basic authentication.

Patterns removes the credentials it recognizes by their shape, for text from elsewhere whose secrets are not known: labeled assignments such as password=…, Bearer tokens, and tokens whose prefix names their issuer. It is a safety net, not a substitute for Values.

Both replace what they find with Marker. The package depends on the standard library only, so a logger can redact without pulling in a keyring or a terminal.

Index

Examples

Constants

View Source
const Marker = "[REDACTED]"

Marker is what replaces a secret in text passed through Values or Patterns.

Variables

This section is empty.

Functions

func Patterns

func Patterns(text string) string

Patterns returns text with the credentials it can recognize by their shape replaced by Marker. Use it where the secrets are not known in advance, as they are for Values: on what a server, a model or a log file says, before it is stored or shown. It replaces

  • the value of a labeled assignment, in JSON, YAML, an environment file or a query string, whose name ends in api_key, access_token, token, secret, password, credential, authorization or auth, in any case and with "-" or "_" between the words;
  • the credential after "Bearer ";
  • tokens whose prefix names their issuer: sk-, ghp_, gho_, ghu_, ghs_, ghr_, glpat- and the AKIA of an AWS access key ID.

The value of an assignment is read the way the formats write it, so that none of it is left behind and the text keeps its structure:

  • A quoted value keeps its quotes: {"api_key":"sk-…"} becomes {"api_key":"[REDACTED]"}. Backslash escapes and the doubled quotes of YAML ('it”s') are part of the value.
  • An unquoted value is replaced without quotes, DB_PASSWORD=hunter2 becomes DB_PASSWORD=[REDACTED], unless it is the value of a quoted JSON name, {"token": 12345}, which becomes {"token": "[REDACTED]"} so that JSON stays JSON. It runs up to white space, a comma, a semicolon, a closing brace or a quote, so the rest of a query string after a credential goes with it, and takes the scheme of an HTTP authorization along: "Authorization: Basic …" becomes "Authorization: [REDACTED]".
  • An array or an object is replaced as a whole, nested ones and brackets inside its strings included: {"token": ["a", "b"]} becomes {"token": "[REDACTED]"}.
  • An HTTP authorization with a scheme, such as "Authorization: Digest username=…, response=…", is replaced up to the end of its line.
  • A YAML block scalar, password: | or >, and a value on the lines indented below its name, are replaced with all of those lines. A name without a value on its line or below it, as in an empty PASSWORD= followed by the next setting, is left alone.
  • The value of a JSON name may stand on a later line. The tags and anchors of a YAML value, password: !!str hunter2, go with it, and a comment after the name stays while the value below it goes.
  • An assignment inside a JSON string, as in {"message":"token=abc def"}, is replaced inside the string, and its value ends at the latest where the string does. A value in the escaped quotes of JSON inside a JSON string keeps them.
  • A quoted value, an array or an object that is never closed, in text cut short, is replaced up to the end of the text, or of the JSON string it is in.

A secret without a label or a known prefix looks like any other word and stays. Patterns is a safety net for text from elsewhere, not a substitute for Values with the secrets a program holds itself.

Example
package main

import (
	"fmt"

	"github.com/mittwald/cli-kit/redact"
)

func main() {
	fmt.Println(redact.Patterns(`{"user":"alice","password":"hunter2"}`))
	fmt.Println(redact.Patterns("https://example.com/hook?token=abc123&page=2"))
}
Output:
{"user":"alice","password":"[REDACTED]"}
https://example.com/hook?token=[REDACTED]

func Values

func Values(text string, secrets ...string) string

Values returns text with every occurrence of each secret replaced by Marker. Use it on anything that may carry a secret before it is logged or shown: an HTTP dump, an error from a server that echoes its input. Empty secrets are ignored.

A secret is found as it is and in the forms it takes on its way through a request: escaped for a URL query or a form (with a space as "+" or as "%20"), escaped for a URL path, and escaped for a JSON string. All occurrences are looked for in text as it was passed in and removed together, so secrets that overlap, or contain one another, leave no part of either behind.

Values cannot find a secret that is part of a larger encoded value. The credentials of HTTP Basic authentication are the user name and the password encoded as one Base64 string, and a token inside a Base64 or compressed body looks like nothing Values knows. Remove such headers and bodies before text reaches a log, or pass the encoded value as a secret of its own.

Example
package main

import (
	"fmt"

	"github.com/mittwald/cli-kit/redact"
)

func main() {
	token := "glpat-abc123"
	fmt.Println(redact.Values("GET /api?private_token=glpat-abc123", token))
}
Output:
GET /api?private_token=[REDACTED]

Types

This section is empty.

Jump to

Keyboard shortcuts

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