Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(redaction): redact curl basic-auth credentials and Cookie headers (#924)

Rudy Mizrahi Celekli675607ac69c5
diff --git a/pkg/asymptoteobserve/privacy.go b/pkg/asymptoteobserve/privacy.go
index 21c98eea..29c2d16d 100644
--- a/pkg/asymptoteobserve/privacy.go
+++ b/pkg/asymptoteobserve/privacy.go
@@ -115,6 +115,10 @@ type redactionRule struct {
// needles are literals the pattern cannot match without: when the text holds none of them,
// the rule is skipped without running the pattern.
needles *redactNeedles
+ // repeat applies the rule until the text stops changing. A rule scoped to a command line
+ // matches from the command to the value, so a second such value on the line lies inside the
+ // first match and is reached only by another pass.
+ repeat bool
}
// redactNeedles is a set of literals a pattern cannot match without. folded needles are lower
@@ -156,6 +160,14 @@ func foldedRule(expr, replacement string, keep func(string) bool, needles ...str
return lazyRule(expr, replacement, keep, newRedactNeedles(true, needles...))
}
+// curlRule is a foldedRule scoped to a curl command line. A flag curl accepts more than once (two
+// -b cookie strings) is redacted at every occurrence on the line.
+func curlRule(expr, replacement string) redactionRule {
+ r := foldedRule(expr, replacement, nil, "curl")
+ r.repeat = true
+ return r
+}
+
// lazyRule compiles its pattern the first time text gets past the needles. A hook is a process
// per event, and most events never need most rules: compiling all of them up front cost every
// hook run a few milliseconds before it had read its input.
@@ -295,6 +307,11 @@ func isRedactedJWTPrefix(value string) bool {
return value == "eyJ"
}
+// isRedactedValue keeps a value an earlier pass already replaced, so redaction stays idempotent.
+func isRedactedValue(value string) bool {
+ return value == redactedValue
+}
+
// isLiteralOrAuthScheme is isAuthScheme, plus the JSON literals that carry no secret.
func isLiteralOrAuthScheme(value string) bool {
switch value {
@@ -337,6 +354,18 @@ var redactionRules = concatRules([]redactionRule{
// usually write arguments: `--password "x y"`, `--token='x'`.
}, quotedRules(quotedFlag, nil), []redactionRule{
{pattern: lazyPattern(`(?i)(` + redactFlag + `)([^\s"'-][^\s"']*)`), replacement: "${1}" + redactedValue, needles: redactFlagNeedles},
+ // curl's basic-auth credentials: `-u user:pass`, `-uuser:pass`, `--user user:pass` and
+ // `--proxy-user user:pass`. The user name and the colon stay. A bare `-u user` (curl prompts for
+ // the password) has nothing to redact, and the rule is limited to curl lines so `docker run -u
+ // 1000:1000` is left alone.
+ curlRule(`(?i)(\bcurl\b[^\n]*?\s(?:-[a-z]*u\s*|(?:--user|--proxy-user)(?:=|\s+))["']?[^:\s"']*:)([^\s"']+)`, "${1}"+redactedValue),
+ // curl's cookie flag with a cookie string: `-b 'sid=x; csrf=y'`, `--cookie sid=x`. Only a value
+ // holding `=` is cookies; anything else names a cookie file, which stays. The quotes stay.
+ curlRule(`(?i)(\bcurl\b[^\n]*?\s(?:-[a-z]*b\s*|--cookie(?:=|\s+)))(?:(')[^'\n]*=[^'\n]*(')|(")[^"\n]*=[^"\n]*(")|[^\s"'=]+=[^\s"']*)`, "${1}${2}${4}"+redactedValue+"${3}${5}"),
+ // A Cookie or Set-Cookie header: the whole value is a session credential, as the field-name rule
+ // in SanitizeMap already treats a structured `cookie` field. Only where a header or key can
+ // start (a line, a quote, curl's -H), so prose that says "cookie:" keeps its sentence.
+ foldedRule(`(?im)((?:^\s*|["']\s*|(?:-H|--header)(?:=|\s+)["']?)(?:set-)?cookie["']?\s*:\s*["']?)([^\n"'\\]+)`, "${1}"+redactedValue, isRedactedValue, "cookie"),
// The password in a URL's userinfo: `postgres://user:pw@host`, `https://x-access-token:[email protected]`.
foldedRule(`(?i)(\b[a-z][a-z0-9+.-]*://[^/\s:@"'<>]+:)([^@/\s"'<>]+)(@)`, "${1}"+redactedValue+"${3}", nil, "://"),
// Credentials recognizable by their own shape, wherever they appear. The type prefix is public
@@ -374,7 +403,12 @@ func concatRules(groups ...[]redactionRule) []redactionRule {
func (rule redactionRule) apply(value string) string {
pattern := rule.pattern()
if rule.keep == nil {
- return pattern.ReplaceAllString(value, rule.replacement)
+ out := pattern.ReplaceAllString(value, rule.replacement)
+ // Each pass redacts at least one more value, and a redacted value matches no further.
+ for pass := 0; rule.repeat && out != value && pass < 16; pass++ {
+ value, out = out, pattern.ReplaceAllString(out, rule.replacement)
+ }
+ return out
}
index := pattern.SubexpIndex("value")
if index < 0 {
diff --git a/pkg/asymptoteobserve/privacy_test.go b/pkg/asymptoteobserve/privacy_test.go
index 64457a92..40b9284a 100644
--- a/pkg/asymptoteobserve/privacy_test.go
+++ b/pkg/asymptoteobserve/privacy_test.go
@@ -134,6 +134,20 @@ func TestRedactStringRedactsCommonCredentialFormats(t *testing.T) {
{"bare database url", "connect to postgres://app:[email protected]:5432/app", "pa55w0rd"},
{"password flag", "mysql --user root --password hunter2hunter2", "hunter2hunter2"},
{"token flag", "tool login --token=opaque-flag-value", "opaque-flag-value"},
+ {"curl basic auth", "curl -u admin:hunter2hunter2 https://api.example.com/v1", "hunter2hunter2"},
+ {"curl basic auth in a flag cluster", "curl -fsSu admin:hunter2hunter2 https://api.example.com", "hunter2hunter2"},
+ {"curl long user flag", "curl --user admin:hunter2hunter2 https://api.example.com", "hunter2hunter2"},
+ {"curl quoted user flag", `curl --user="admin:hunter2hunter2" https://api.example.com`, "hunter2hunter2"},
+ {"curl proxy user", "curl --proxy-user proxy:hunter2hunter2 -x http://p:3128 https://x", "hunter2hunter2"},
+ {"curl attached user flag", "curl -uadmin:hunter2hunter2 https://api.example.com", "hunter2hunter2"},
+ {"curl cookie flag", "curl -b 'sessionid=abcdef123456789; csrftoken=zzz' https://x", "abcdef123456789"},
+ {"curl long cookie flag", "curl --cookie sessionid=abcdef123456789 https://x", "abcdef123456789"},
+ {"curl cookie flag in a cluster", `curl -sSb "sessionid=abcdef123456789" https://x`, "abcdef123456789"},
+ {"curl second cookie flag on a line", "curl -b 'a=1' --cookie sessionid=abcdef123456789 https://x", "abcdef123456789"},
+ {"cookie header", "curl -H 'Cookie: sessionid=abcdef123456789; csrftoken=zzz' https://x", "abcdef123456789"},
+ {"set-cookie header", "Set-Cookie: sid=abcdef123456789; HttpOnly", "abcdef123456789"},
+ {"json cookie header", `{"cookie": "sid=abcdef123456789"}`, "abcdef123456789"},
+ {"yaml cookie key", "headers:\n cookie: sid=abcdef123456789", "abcdef123456789"},
{"basic auth", "Authorization: Basic dXNlcjpwYXNzd29yZA==", "dXNlcjpwYXNzd29yZA=="},
{"slack", "xoxb-1234567890-abcdefghijkl", "abcdefghijkl"},
{"stripe", "sk_live_AbCdEfGhIjKlMnOpQrSt", "AbCdEfGhIjKlMnOpQrSt"},
@@ -186,6 +200,13 @@ func TestRedactStringKeepsLabelsAndOrdinaryText(t *testing.T) {
"see https://github.com/org/repo/pull/12 and [email protected]:org/repo.git",
"PWD=/home/ada/project",
"task-0123456789abcdefghijklmnop",
+ "docker run -u 1000:1000 alpine id",
+ "curl -u admin https://api.example.com",
+ "curl -L -o out.txt https://example.com/u:v",
+ "we set a cookie banner: accept all",
+ "The cookie: chocolate chip recipe is great",
+ "curl -b cookies.txt https://example.com",
+ `curl --user-agent "Mozilla/5.0 (X11: Linux)" https://example.com`,
}
for _, input := range kept {
if got := RedactString(input); got != input {
@@ -222,6 +243,8 @@ func TestRedactStringIsIdempotent(t *testing.T) {
`{"token": "opaque-session-value-9", "password": "hunter2hunter2"}`,
"Authorization: Bearer abc.def.ghi and --password hunter2hunter2",
"postgres://app:pa55w0rd@db/app",
+ "curl -u admin:hunter2hunter2 -H 'Cookie: sid=abcdef123456789' https://x",
+ "curl -b 'sessionid=abcdef123456789' --cookie sid=abcdef123456789 https://x",
}
for _, input := range inputs {
once := RedactString(input)