0b8ac34aee
Pre-existing work-in-progress in the tree: karakeep, snippets, skills-health and redaction ingesters, the opencode.db session sweep, gossip and metrics changes. Checkpointed as-is so the adapter fix in the next commit stays reviewable on its own.
59 lines
3.0 KiB
Go
59 lines
3.0 KiB
Go
package ingest
|
|
|
|
import "regexp"
|
|
|
|
// Redact masks credentials in text lifted from sessions: DB URLs, NAME=literal
|
|
// secrets, cloud token shapes, private keys, JWTs, bearer tokens. It is
|
|
// deliberately broad on *literal* values but never touches a value that is a
|
|
// reference or substitution (`$(…)`, `${…}`, `$VAR`) — masking those would
|
|
// corrupt code without hiding anything. Idempotent, so re-running is safe.
|
|
func Redact(s string) string {
|
|
for _, r := range redactRules {
|
|
s = r.re.ReplaceAllString(s, r.repl)
|
|
}
|
|
return s
|
|
}
|
|
|
|
type redactRule struct {
|
|
re *regexp.Regexp
|
|
repl string
|
|
}
|
|
|
|
// secretName matches a variable name that implies a credential. Deliberately
|
|
// excludes the bare words PASS/PWD, which falsely match PASSED, BYPASS, etc.
|
|
const secretName = `(?:PASSWORD|PASSWD|PASSPHRASE|SECRET|TOKEN|APIKEY|API_KEY|ACCESS_KEY|SECRET_KEY|PRIVATE_KEY|CREDENTIAL|AUTH|PSK|CLIENT_SECRET|SESSION_KEY)`
|
|
|
|
// litValue is a quoted-literal value: not a $reference, $(...) or ${...}.
|
|
const litValue = `[A-Za-z0-9_\-./+=]{4,}`
|
|
|
|
// litStrong is an UNQUOTED value that must contain at least one digit or symbol
|
|
// (- / + =) — i.e. it looks like a secret, not an identifier or a property access
|
|
// like `formData.get`. This keeps the bare rules from corrupting
|
|
// `password = read_secret(...)`, `password = formData.get(...)`,
|
|
// `password: string`, or `C1_PASSED=true`.
|
|
const litStrong = `[A-Za-z0-9_\-/+=]*[0-9\-/+=][A-Za-z0-9_\-/+=]*`
|
|
|
|
var redactRules = []redactRule{
|
|
// PEM private key blocks (multiline).
|
|
{regexp.MustCompile(`(?s)-----BEGIN [A-Z ]*PRIVATE KEY-----.*?-----END [A-Z ]*PRIVATE KEY-----`), "[REDACTED PRIVATE KEY]"},
|
|
// userinfo in a URL: scheme://user:password@host (structure preserved).
|
|
{regexp.MustCompile(`(?i)([a-z][a-z0-9+.\-]*://)([^/\s:@]+):([^@\s/]+)@`), "${1}${2}:****@"},
|
|
// NAME="literal" / NAME='literal' (quoted values are unambiguous).
|
|
{regexp.MustCompile(`(?i)\b([A-Z0-9_]*` + secretName + `[A-Z0-9_]*)="(` + litValue + `)"`), `${1}="****"`},
|
|
{regexp.MustCompile(`(?i)\b([A-Z0-9_]*` + secretName + `[A-Z0-9_]*)='(` + litValue + `)'`), `${1}='****'`},
|
|
// NAME=literal (unquoted, must look secret-like).
|
|
{regexp.MustCompile(`(?i)\b([A-Z0-9_]*` + secretName + `[A-Z0-9_]*)=(` + litStrong + `)`), `${1}=****`},
|
|
// password[:=]literal in JSON-ish text (quoted, or secret-like unquoted).
|
|
{regexp.MustCompile(`(?i)("?password"?\s*[:=]\s*)"(` + litValue + `)"`), `${1}"****"`},
|
|
{regexp.MustCompile(`(?i)("?password"?\s*[:=]\s*)'(` + litValue + `)'`), `${1}'****'`},
|
|
{regexp.MustCompile(`(?i)("?password"?\s*[:=]\s*)(` + litStrong + `)`), `${1}****`},
|
|
// Cloud / SaaS token shapes.
|
|
{regexp.MustCompile(`AKIA[0-9A-Z]{16}`), "AKIA****************"},
|
|
{regexp.MustCompile(`gh[pousr]_[A-Za-z0-9]{20,}`), "gh*_****"},
|
|
{regexp.MustCompile(`xox[baprs]-[A-Za-z0-9-]{10,}`), "xox*-****"},
|
|
{regexp.MustCompile(`eyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+`), "****.****.****"},
|
|
{regexp.MustCompile(`(?i)bearer\s+[A-Za-z0-9._\-]{8,}`), "Bearer ****"},
|
|
// curl -u user:password
|
|
{regexp.MustCompile(`(?i)((?:^|\s)(?:-u|--user)\s+)([^\s:]+):([^\s]+)`), "${1}${2}:****"},
|
|
}
|