redact: a field set to nothing takes nothing from the next one (mx_albert)
mx_albert on Discord (#feedback): with Secrets masking on, this command
line went to the vendor with 127.0.0.1 replaced:
env -i PATH HOME UI_USER= UI_PASSWORD= UI_ADDR=127.0.0.1:39117 DATA_DIR=/tmp/x ./syncer serve
The SECRET rule (a name with password/secret/token/api_key, then = or :)
lets a space stand after the separator, for `password = hunter2` in a
config. UI_PASSWORD is set to nothing, so the rule took the next word,
UI_ADDR=127.0.0.1, as its value and masked it as {{SECRET_…}}:39117;
once masked it was a known value and went masked wherever it came again.
The other field rules (*_KEY / *_PASS, key, 密码, 密钥, "token 是") had the
same shape.
Now a value that is an assignment of its own after a space (UI_ADDR=,
addr=, --listen=; an = with something other than = after it, so a
base64 value's padding is never one) is the next field: the match is
dropped and the search goes on from that value, which is read with its
own name (fieldMatches, nextField). UI_ADDR's address goes as written;
UI_PASSWORD= UI_TOKEN=t0k3nValue2 still masks the token, as UI_TOKEN's.
Old data: what was masked before is known only in memory, so after the
update's restart 127.0.0.1 in that line goes as written for every user;
a conversation carrying it sends it as written from then on (one prompt
cache miss).
Tests: TestEmptyFieldTakesNotTheNext (the reporter's literal line, plus
0.0.0.0, [::1], localhost:port, logfmt, --flag=, and the CJK and
"token 是" rules, every category on) and TestSecretBesideEmptyFieldMasks
(hunter2pass9, a token after the empty field, a *_KEY, a base64 value,
password = with spaces).
Run at this head on mote: go test ./internal/redact ./internal/masking
./internal/otlp ./internal/pii ./internal/sessions ./internal/settings
./plugins/redact all ok; ./internal/gateway and ./internal/gui (nogui)
-run 'Redact|Mask|Privacy|Secret' ok, gateway
TestLoopGuardLeavesOrdinaryRepliesAlone ok (it reads the doc page);
GOOS=linux and GOOS=windows go vet ./internal/redact ok; gofmt -l clean.
Broken by hand: findAll with the field check turned off. Both new tests
go red: the reporter's line masks {{SECRET_…}}:39117, and 17 of the
other cases fail (UI_TOKEN= after an empty field masked with its name,
UI_ADDR=127.0.0.1 then masked as known in the next case).
Not tried: the full gateway and gui packages (another session's suite
was on mote), the GUI, a real request through a running gateway.
UI_PASSWORD=hunter2 itself is not masked by a rule, before or after
this: the rule asks 8 characters of a value, hunter2 has 7.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -32,6 +32,15 @@ import (
|
||||
// "2.4.0-beta02" (dniray on Discord, v0.1.1917: package names and
|
||||
// their versions were replaced). A release's version is published for
|
||||
// anyone to read (versionValue).
|
||||
// - the next field, when the one named has no value: a command line
|
||||
// sets UI_PASSWORD to nothing in env -i UI_PASSWORD= UI_ADDR=127.0.0.1:39117
|
||||
// ./syncer serve, and the SECRET rule, which lets a space stand after
|
||||
// = (password = hunter2 in a config), took UI_ADDR=127.0.0.1 for
|
||||
// UI_PASSWORD's value (mx_albert on Discord). A value that starts as
|
||||
// an assignment of its own, after a space, is the next field
|
||||
// (nextField), and that field is read on its own: UI_ADDR's address
|
||||
// goes as written, and UI_PASSWORD= UI_TOKEN=t0k3nValue2 still masks
|
||||
// the token.
|
||||
//
|
||||
// A secret written down still masks: password=hunter2pass9,
|
||||
// API_KEY=AbC123dEf456GhI789, token: t0k3nValue2, a password of one word
|
||||
@@ -227,3 +236,48 @@ func identWords(v string) []string {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// nextFieldRe is a field being set at a value's start, as a command
|
||||
// line, a logfmt line or a flag sets one: an env var's name in capitals
|
||||
// (UI_ADDR=), one in lower case (addr=, data-dir=), or a flag
|
||||
// (--listen=); then =, and something other than another =, so a base64
|
||||
// value's padding (dGVzdA==) is never taken for one.
|
||||
var nextFieldRe = regexp.MustCompile(`^(?:[A-Z_][A-Z0-9_]*|[a-z_][a-z0-9_-]*|--?[A-Za-z][A-Za-z0-9_-]*)=[^=]`)
|
||||
|
||||
// nextField says the value at s[a:b] is not the value of the field before
|
||||
// it but the next field: the name before it was set to nothing (UI_PASSWORD=
|
||||
// UI_ADDR=127.0.0.1:39117), as a space stands between the separator and
|
||||
// a value that is itself an assignment.
|
||||
func nextField(s string, a, b int) bool {
|
||||
return a > 0 && (s[a-1] == ' ' || s[a-1] == '\t') && nextFieldRe.MatchString(s[a:b])
|
||||
}
|
||||
|
||||
// fieldMatches are re's matches in s as FindAllStringSubmatchIndex has
|
||||
// them, but where a match's value (group 1) is the next field (nextField)
|
||||
// the match is dropped and the search goes on from that value, so the
|
||||
// next field is read with its own name: UI_PASSWORD= UI_TOKEN=t0k3nValue2
|
||||
// masks the token as UI_TOKEN's, and UI_PASSWORD= UI_ADDR=127.0.0.1 masks
|
||||
// nothing. The search from the value starts where a space is before it,
|
||||
// so a pattern's ^ there is the start of a word, as it is after a space.
|
||||
func fieldMatches(re *regexp.Regexp, s string) [][]int {
|
||||
var out [][]int
|
||||
for at := 0; ; {
|
||||
again := -1
|
||||
for _, m := range re.FindAllStringSubmatchIndex(s[at:], -1) {
|
||||
for x := range m {
|
||||
if m[x] >= 0 {
|
||||
m[x] += at
|
||||
}
|
||||
}
|
||||
if len(m) > 3 && m[2] > at && nextField(s, m[2], m[3]) {
|
||||
again = m[2]
|
||||
break
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
if again < 0 {
|
||||
return out
|
||||
}
|
||||
at = again
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// everything is every category of masking on, the ones off by default
|
||||
// (the IP address, the user and host) too.
|
||||
func everything() Options {
|
||||
o := Options{Secrets: true, Personal: true, Kinds: map[string]bool{}}
|
||||
for _, c := range Categories {
|
||||
o.Kinds[c.ID] = true
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// A field set to nothing takes nothing from the next one: mx_albert on
|
||||
// Discord, the Secrets category masked 127.0.0.1 in this command line, as
|
||||
// the SECRET rule took UI_ADDR=127.0.0.1 for UI_PASSWORD's value. An
|
||||
// address, a port, a path or a flag after an empty field goes as written,
|
||||
// on any category.
|
||||
func TestEmptyFieldTakesNotTheNext(t *testing.T) {
|
||||
for _, in := range []string{
|
||||
"env -i PATH HOME UI_USER= UI_PASSWORD= UI_ADDR=127.0.0.1:39117 DATA_DIR=/tmp/x ./syncer serve",
|
||||
"UI_PASSWORD= UI_ADDR=0.0.0.0:39117 ./syncer serve",
|
||||
"UI_TOKEN= LISTEN=[::1]:39117 ./syncer",
|
||||
"API_KEY= ADDR=localhost:39117 ./syncer",
|
||||
"DB_PASS= DB_HOST=127.0.0.1 DB_PORT=5432 psql",
|
||||
"SERVICE_KEY=\tBIND_ADDR=127.0.0.1:8080",
|
||||
"level=info password= addr=127.0.0.1:39117 data_dir=/tmp/x1",
|
||||
"client_secret: listen_addr=127.0.0.1:8080",
|
||||
"./syncer --password= --listen=127.0.0.1:39117",
|
||||
"secret= --data-dir=/var/lib/syncer2",
|
||||
"key= UPSTREAM_URL2=http://127.0.0.1:39117/v1",
|
||||
"密码= UI_ADDR=127.0.0.1:39117",
|
||||
"密钥: UI_ADDR=127.0.0.1:39117 ./syncer",
|
||||
"token 是 UI_ADDR=127.0.0.1:39117",
|
||||
} {
|
||||
if out, n := Mask(in, everything()); n != 0 {
|
||||
t.Errorf("%q: %d masked: %q", in, n, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A secret still masks beside an empty field: its own, after its own
|
||||
// name, and a value after a space that is no assignment (password =
|
||||
// hunter2pass9, a base64 value with its padding).
|
||||
func TestSecretBesideEmptyFieldMasks(t *testing.T) {
|
||||
for _, c := range []struct{ in, secret, kept string }{
|
||||
{"env -i UI_USER= UI_PASSWORD=hunter2pass9 UI_ADDR=127.0.0.1:39117 ./syncer serve", "hunter2pass9", "UI_ADDR=127.0.0.1:39117"},
|
||||
{"UI_PASSWORD= UI_TOKEN=t0k3nValue2 UI_ADDR=127.0.0.1:39117", "t0k3nValue2", "UI_ADDR=127.0.0.1:39117"},
|
||||
{"UI_PASSWORD= SERVICE_KEY=AbC123dEf456GhI789 ./syncer", "AbC123dEf456GhI789", "SERVICE_KEY="},
|
||||
{"password= api_key=AbC123dEf456GhI789", "AbC123dEf456GhI789", "api_key="},
|
||||
{"password = hunter2pass9", "hunter2pass9", "password = "},
|
||||
{"password: dGVzdDEyMzQ1Ng==", "dGVzdDEyMzQ1Ng==", "password: "},
|
||||
{"UI_PASSWORD= hunter2pass9", "hunter2pass9", "UI_PASSWORD= "},
|
||||
{"密码= UI_TOKEN=t0k3nValue2", "t0k3nValue2", "UI_TOKEN="},
|
||||
} {
|
||||
out, n := Mask(c.in, everything())
|
||||
if n == 0 || strings.Contains(out, c.secret) || !strings.Contains(out, c.kept) {
|
||||
t.Errorf("%q: %d masked: %q, want %q masked and %q kept", c.in, n, out, c.secret, c.kept)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,6 +136,10 @@ type rule struct {
|
||||
back, ahead int
|
||||
// cat is what turns the rule on (Options.covers): "" the secrets
|
||||
cat string
|
||||
// field is a rule that takes a value after a field's name and = or :,
|
||||
// a space allowed between them: a value that is the next field's
|
||||
// assignment is not this one's (fieldMatches)
|
||||
field bool
|
||||
// yours is a rule the user wrote (customRules)
|
||||
yours bool
|
||||
// anyGroup takes the first group that matched, for a pattern with a
|
||||
@@ -175,7 +179,7 @@ var rules = append([]rule{
|
||||
{kind: "PASSWORD", re: regexp.MustCompile(`[A-Za-z][A-Za-z0-9+.-]*://[^\s:@/?#"'<>]*:([^\s@/?#"'<>]{3,})@`), markers: []string{"://"}, ok: notAVariable, at: hiddenInURL},
|
||||
// password = …, API_KEY: "…", as .env files and configs have them
|
||||
{kind: "SECRET", re: regexp.MustCompile(`(?i)[A-Za-z0-9_.-]*(?:` + secretNames.pattern() + `)[A-Za-z0-9_.-]*` + quote + `[ \t]*[:=][ \t]*` + quote + `(` + secretCh + `{8,})`),
|
||||
markers: secretNames.markers(), ok: secretValue, at: fieldAt},
|
||||
markers: secretNames.markers(), ok: secretValue, at: fieldAt, field: true},
|
||||
// an env var's name says what it holds in the upper case: a
|
||||
// SERVICE_X_KEY, a DB_PASS (#2946). The names above have a word for the
|
||||
// secret in them; these only end in one, and only in the upper case is
|
||||
@@ -183,7 +187,7 @@ var rules = append([]rule{
|
||||
// code's: a sort key, a cache key, a public key (notAPlainKey turns the
|
||||
// upper-case ones of those away too).
|
||||
{kind: "SECRET", re: regexp.MustCompile(`(?:^|[^A-Za-z0-9_.-])[A-Za-z0-9_.-]*_(?:KEY|PASS)` + quote + `[ \t]*[:=][ \t]*` + quote + `(` + secretCh + `{8,})`),
|
||||
markers: fieldNames{{"key", "key"}, {"pass", "pass"}}.markers(), ok: secretValue, at: namedSecretAt},
|
||||
markers: fieldNames{{"key", "key"}, {"pass", "pass"}}.markers(), ok: secretValue, at: namedSecretAt, field: true},
|
||||
// an Authorization or Cookie header's value is a secret whatever it
|
||||
// looks like: an opaque bearer token, a session value (#2946)
|
||||
{kind: "SECRET", re: regexp.MustCompile(`(?im)^[ \t]*(?:authorization|cookie)[ \t]*:[ \t]*(\S[^\r\n]{7,})`),
|
||||
@@ -192,24 +196,24 @@ var rules = append([]rule{
|
||||
// "oc_sk_…" (#195). Only a long value of letters and digits both, since
|
||||
// code calls anything a key: a map's "key": "value", sort_key, keyboard.
|
||||
{kind: "SECRET", re: regexp.MustCompile(`(?i)(?:^|[^A-Za-z0-9_.-])` + quote + `key` + quote + `[ \t]*[:=][ \t]*` + quote + `(` + secretCh + `{16,})`),
|
||||
markers: fieldNames{{"key", "key"}}.markers(), ok: secretValue, at: fieldAt},
|
||||
markers: fieldNames{{"key", "key"}}.markers(), ok: secretValue, at: fieldAt, field: true},
|
||||
// a password after its name in Chinese, Japanese or Korean, as people
|
||||
// write one into a message: 密码:786kih, 密碼是 hunter2, パスワード: … .
|
||||
// Unlike password = in a config, nothing in code is called 密码, so
|
||||
// any value of four or more is taken, digits only too (CyberCaius on
|
||||
// Discord); a sentence's closing stop is no part of it.
|
||||
{kind: "PASSWORD", re: cjkNamed(cjkPasswordNames), fold: lowerAll(cjkPasswordNames), back: 64, ahead: 100, ok: notAVariable},
|
||||
{kind: "PASSWORD", re: cjkNamed(cjkPasswordNames), fold: lowerAll(cjkPasswordNames), back: 64, ahead: 100, ok: notAVariable, field: true},
|
||||
// a key, a secret or a token after its name the same way: 密钥为 …,
|
||||
// 令牌是 …, 金鑰: … . A key of digits only is taken after 为, 是, :
|
||||
// or = when it has six or more and counts nothing, so 密钥为12231241
|
||||
// is masked and 密钥为 2048 位 is left.
|
||||
{kind: "SECRET", re: cjkNamed(cjkKeyNames), fold: lowerAll(cjkKeyNames), back: 64, ahead: 100, at: keyAt},
|
||||
{kind: "SECRET", re: cjkNamed(cjkKeyNames), fold: lowerAll(cjkKeyNames), back: 64, ahead: 100, at: keyAt, field: true},
|
||||
// an English name for one before 为, 為, 是 or :, as a Chinese message
|
||||
// has it: apikey 为 …, token 是 …, API Key:… (CyberCaius on Discord).
|
||||
// password = … and API_KEY: … in a config are the SECRET rule's above.
|
||||
{kind: "SECRET", re: regexp.MustCompile(`(?i)(?:` + secretNames.pattern() + `|pwd|api[ ]key|access[ ]key|secret[ ]key|key|\bpin(?:[ ]?code)?)` + quote +
|
||||
`[ \t]*(?:[为為是][ \t]*[::]?|:)[ \t]*(?:\\?["'“「])?(` + secretCh + `{3,}[A-Za-z0-9_\-/+=~!@#%^&*])`),
|
||||
fold: cjkCopulas, back: 24, ahead: 260, at: keyAt},
|
||||
fold: cjkCopulas, back: 24, ahead: 260, at: keyAt, field: true},
|
||||
|
||||
{kind: "EMAIL", re: regexp.MustCompile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}`), markers: []string{"@"}, bound: alnum + "_%+-", ok: realEmail, at: emailAt, cat: catPersonal},
|
||||
{kind: "ID_CARD", re: regexp.MustCompile(`[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]`), bound: alnum, ok: chineseID, cat: catPersonal},
|
||||
@@ -1449,7 +1453,7 @@ func (r rule) by() string {
|
||||
// ahead set, only around its fold markers.
|
||||
func (r rule) matches(s, lower string) [][]int {
|
||||
if r.ahead == 0 {
|
||||
return r.re.FindAllStringSubmatchIndex(s, -1)
|
||||
return r.findAll(s)
|
||||
}
|
||||
var wins [][2]int
|
||||
for _, f := range r.fold {
|
||||
@@ -1488,7 +1492,7 @@ func (r rule) matches(s, lower string) [][]int {
|
||||
k++
|
||||
w[1] = max(w[1], wins[k][1])
|
||||
}
|
||||
for _, m := range r.re.FindAllStringSubmatchIndex(s[w[0]:w[1]], -1) {
|
||||
for _, m := range r.findAll(s[w[0]:w[1]]) {
|
||||
for x := range m {
|
||||
if m[x] >= 0 {
|
||||
m[x] += w[0]
|
||||
@@ -1500,6 +1504,16 @@ func (r rule) matches(s, lower string) [][]int {
|
||||
return out
|
||||
}
|
||||
|
||||
// findAll are r's matches in s: each of them, or, for a rule that reads
|
||||
// a field's value after its name (field), none whose value is the next
|
||||
// field (fieldMatches).
|
||||
func (r rule) findAll(s string) [][]int {
|
||||
if r.field {
|
||||
return fieldMatches(r.re, s)
|
||||
}
|
||||
return r.re.FindAllStringSubmatchIndex(s, -1)
|
||||
}
|
||||
|
||||
// lowerASCII is s with its ASCII letters in lower case, as long as s, so a
|
||||
// place in one is the same place in the other (a byte of a character
|
||||
// outside ASCII is never an ASCII letter). It is s itself, not a copy,
|
||||
|
||||
Reference in New Issue
Block a user