Three reports on X (10-28), each a value the SECRET rule took (Insights >
Masking: 机密, 规则). The rule takes a name with token, secret, password
or api_key in it, then : or =, then 8 characters or more with a letter and
a digit. Prompts and code have that shape where nothing is secret:
- AlpsNowX: L643-669. was masked, in two requests. A file whose name has
one of those words, then :L643-669. (internal/auth/token.go:L643-669.)
read as a field token.go and its value, and once masked the lines went
as {{SECRET_…}} wherever they came again (by known). Now a name that is
a source file's (.go, .ts, .py, .md, .json …, not .txt/.env/.key) or a
path's last part is no field, and a line or range (L643, L643-669,
L12-L40, 643:12) is no value. Siblings: token.go:643-669 and #L643-L669
were never masked (no letter / no colon); file.go:L12-40 was, and isn't.
- binary4cat: function names masked. access_token = get_access_token_v2,
max_tokens=max_tokens_v3, secret: env.SECRET_V2, refreshToken2;,
fn(token=token_v2), secretKey:=base64Encode(raw) all masked the name.
A value written as code writes a name (lower-case start, or _ or .
between words) of two words or more, one of them token/secret/password/
key/credential, is a name now; a value starting with = is Go's :=.
- AkinoNya: folders and skills with a random string replaced. Found two
routes: a path after such a name (PRIVATE_KEY_PATH=~/.ssh/id_ed25519,
token_dir=/private/tmp/claude-501/x7k2p9q1-skill,
GOOGLE_APPLICATION_CREDENTIALS=/Users/…/legacy_credentials/x7k2p9q1/…,
私钥 ~/.ssh/…), and Claude Code's plugin:skill when the plugin's name
has one of the words (design-tokens:generate-v2, secret-scan:
audit-x7k2p9q1). A path from ~/, ./, ../ or a root folder (/Users,
/tmp, /private …) with no + or = goes as written, in the CJK key rules
too; slug:slug with no space is a skill. The ~1100 real folder and skill
names on this machine (~/.claude, ~/.codex, $TMPDIR, Claude's
claude-501 dirs) in five contexts masked nothing before or after, so
the reporter's exact names are not reproduced.
Still masked (TestSecretsBesideLinesAndNames): password=hunter2pass9,
API_KEY=AbC123dEf456GhI789, token=t0k3nValue2, Passw0rd123, password123,
SuperSecret2024 (PascalCase stays a password), token: Lk8x9QmP2vR4,
secret.txt:hunter2pass9, x-api-key:k9x2mp4qr7st1vw3 and :ab12cd34ef56,
Password=Abc12345; in a connection string, 密钥为12231241, 私钥 ab12…,
django-insecure-…; vendorchecks and the existing code tests unchanged.
Run (mote): go vet ./internal/redact for darwin, linux, windows; go test
-count=1 ./internal/redact ./internal/masking ./internal/pii/...
./internal/otlp ./internal/sessions ./internal/settings ./internal/gateway
at base ea53eb126: all ok (gateway 551s). After the rebase onto
bc3ddc661 (zhname): vet (3 OSes) and redact, masking ok. gofmt -l clean.
Broken by hand: the two SECRET rules back on notCode and keyAt without
the path check: 28 of the new tests' cases red (every reported shape),
the still-masked test green.
Not tried: the full ./... suite (mote at load ~50 with two other full
suites); the reporters' own prompts; the local-model path, unchanged.
Tradeoff: a password written as code writes a name (myPassword1,
MY_SECRET_2) now goes as written.
105 lines
4.0 KiB
Go
105 lines
4.0 KiB
Go
package redact
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A place in a file goes as written: AlpsNowX on X (10-28) saw L643-669.
|
|
// listed under Secrets, found by a rule, in two requests. A file whose
|
|
// name has token, secret or password in it, a colon and the lines were
|
|
// a field, its name and its value; and once masked, the lines were masked
|
|
// wherever they came again.
|
|
func TestLineRangeIsNoSecret(t *testing.T) {
|
|
o := Options{Secrets: true, Personal: true}
|
|
for _, in := range []string{
|
|
"看一下 internal/auth/token.go:L643-669.",
|
|
"L643-669.",
|
|
"参考 src/secret_store.ts:L643-669. 和 L12-40",
|
|
"见 tokenizer.rs:L643-669。",
|
|
"cmd/apikey/main.go:L643-669, cmd/apikey/main.go:L12-40.",
|
|
"see internal/redact/password_test.go:643-669 and #L643-L669",
|
|
"src/credentials.py:L12-L40",
|
|
"token_test.go:p2fa9c1d",
|
|
"token.go:-1-2-3a",
|
|
"internal/auth/token.go:L12-40",
|
|
} {
|
|
if out, n := Mask(in, o); n != 0 {
|
|
t.Errorf("%q: %d masked: %q", in, n, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Code naming a secret goes as written: binary4cat on X (10-28), function
|
|
// names were masked. A name made of words, one of them token, secret or
|
|
// password, after = or : is a function's or a variable's.
|
|
func TestNameInCodeIsNoSecret(t *testing.T) {
|
|
for _, in := range []string{
|
|
"const accessToken2 = refreshToken2;",
|
|
"let secretV2 = loadSecretV2;",
|
|
"secretKey:=base64Encode(raw)",
|
|
"password = hashPassword256;",
|
|
"self.token = generate_token_v2",
|
|
`TOKEN_V2 = "TOKEN_V2"`,
|
|
"tokenCount = countTokens4o",
|
|
" secret: env.SECRET_V2",
|
|
"max_tokens=max_tokens_v3",
|
|
"access_token = get_access_token_v2",
|
|
"passwordInput.value = newPassword2",
|
|
"fn(token=token_v2)",
|
|
`const TOKEN_KEY = "access_token_v2"`,
|
|
} {
|
|
if out, n := Mask(in, Options{Secrets: true}); n != 0 {
|
|
t.Errorf("%q: %d masked: %q", in, n, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A path to where a secret is kept goes as written, and so does a
|
|
// plugin's skill: AkinoNya on X (10-28), folders and skills with a random
|
|
// string in their name were replaced.
|
|
func TestPathAndSkillAreNoSecret(t *testing.T) {
|
|
for _, in := range []string{
|
|
"PRIVATE_KEY_PATH=~/.ssh/id_ed25519",
|
|
"GOOGLE_APPLICATION_CREDENTIALS=/Users/alice/.config/gcloud/legacy_credentials/x7k2p9q1/adc.json",
|
|
`"credentials_file": "/Users/alice/.aws/sso/cache/3f9a2c7b1e.json"`,
|
|
"私钥 ~/.ssh/id_ed25519_2024",
|
|
"token_dir=/private/tmp/claude-501/x7k2p9q1-skill",
|
|
"--token-file=/tmp/tok-a8f3k2x9/token",
|
|
"skills/secret-scanner:x7k2p9q1",
|
|
"- design-tokens:generate-v2: Generate tokens",
|
|
"/secret-scan:audit-x7k2p9q1",
|
|
} {
|
|
if out, n := Mask(in, Options{Secrets: true}); n != 0 {
|
|
t.Errorf("%q: %d masked: %q", in, n, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A secret of the same shape still masks: a value of one word and digits,
|
|
// a random one, one starting with L, a file of secrets' content, a key in
|
|
// a header with no space, a password in a connection string.
|
|
func TestSecretsBesideLinesAndNames(t *testing.T) {
|
|
for in, v := range map[string]string{
|
|
"password=hunter2pass9": "hunter2pass9",
|
|
"API_KEY=AbC123dEf456GhI789": "AbC123dEf456GhI789",
|
|
"token=t0k3nValue2": "t0k3nValue2",
|
|
"password: Passw0rd123": "Passw0rd123",
|
|
"DB_PASSWORD=password123": "password123",
|
|
"PASSWORD=SuperSecret2024": "SuperSecret2024",
|
|
"token: Lk8x9QmP2vR4": "Lk8x9QmP2vR4",
|
|
"secret.txt:hunter2pass9": "hunter2pass9",
|
|
"x-api-key:k9x2mp4qr7st1vw3": "k9x2mp4qr7st1vw3",
|
|
"x-api-key:ab12cd34ef56": "ab12cd34ef56",
|
|
"Server=db;Password=Abc12345;User Id=sa": "Abc12345",
|
|
"api_token = 'tok-8f3a9c2e7b1d'": "tok-8f3a9c2e7b1d",
|
|
"密钥为12231241": "12231241",
|
|
"私钥 ab12cd34ef56": "ab12cd34ef56",
|
|
"SECRET_KEY=django-insecure-a1b2c3d4e5f6": "django-insecure-a1b2c3d4e5f6",
|
|
} {
|
|
if out, n := Mask(in, Options{Secrets: true}); n != 1 || strings.Contains(unmarked(out), v) {
|
|
t.Errorf("%q: %d masked: %q", in, n, out)
|
|
}
|
|
}
|
|
}
|