redact: a package's name goes as written, an import's module first (dniray on Discord)
dniray (v0.1.1917): "脱敏会把包名给替换了", with the case
import { and, eq } from ' drizzle-orm ' going out as
from ' {{SECRET_…}} '. The SECRET came from the local model; the rules
left it. AstrLink Guard reproduces the class on mote with
require( ' jsonwebtoken ' ) -> {{SECRET_…}} and a requirements.txt
django-rest-passwordreset==1.4.0 -> {{SECRET_…}}==1.4.0; Privacy Filter
took a pnpm-lock integrity hash (sha512-…==}) for one, eating the }.
A probe of ~60 package shapes (npm scoped and pinned, Java/Android ids,
Go module paths, Python modules, purls, Debian, Cargo, Gradle, Maven,
CocoaPods, lock files) through Preview, rules alone and with both real
models, default kinds and all kinds, found these and fixed each class:
- model, any kind: the module a line of code loads is never PII
(moduleAt): a quoted specifier after from on an import/export line or
after } or *, import 'x', require('x'), import('x'), and unquoted
from x import / import x / use x:: / extern crate x at a line's start.
A from in prose ("the token from 'Zq8v…'", "copy it from Zq8v…") is
judged as before.
- model SECRET: a name a version is pinned after (==, >=, ~=, !=, <=,
===; pinnedAfter); @ is left out so s3cretPass@10.0.0.5 still masks.
- model SECRET: an SRI hash (sriRe in namesThing).
- secret-named field rule (fieldAt, keyAt, modelTakes): a version or a
range is no secret value: "js-tokens": "^4.0.0-beta.1",
jsonwebtoken = "9.2.0-beta1", secretVersion = '2.1.0-alpha03'
(versionValue).
- user@host (off by default): js-tokens@4.0.0: in pnpm-lock.yaml went
as a user and a host (notPackagePin); alice@10.0.0.5 still masks.
Left as is: SECRET_PACKAGE=@acme/secret-store-v2 and
PASSWORD_LIB=passlib-1.7.4 (a value under a secret's name, not told
apart from a password), and Privacy Filter's "Dagger Hilt" as a person.
Tests: package_names_test.go. TestPackageNamesGoAsWritten (rules,
default and all kinds), TestPackagePinIsNoLogin,
TestModelPackageNamesNotMasked (dniray's literal bytes first, through
fakeModel), TestPackageNameChecksKeepRealValues (emails, an ssh host,
password=, API_KEY=, a secret-named field with a real value, alice@ip,
a model secret in prose after from, before ==, before @ip).
Run, at b692de9c8 + this change, on mote:
- go test -count=1 -timeout 45m ./internal/redact/... ./internal/masking/...: ok
- go test ./internal/otlp/... ./internal/pii/... ./internal/sessions/...
./internal/settings/...: ok; -run "Redact|Mask|Privacy|Unredact"
./internal/gateway ./internal/gui: ok
- go vet ./internal/redact/... darwin, GOOS=linux -tags nogui, GOOS=windows: ok
- gofmt -l internal/redact: clean
- probe with real AstrLink Guard and Privacy Filter (MAGPIE_EVAL_CACHE):
every shape above as written; emails, ssh host and the author's email
still masked.
Broken by hand, each red: moduleAt off (TestModelPackageNamesNotMasked:
drizzle-orm, reflect-metadata, jsonwebtoken), notPackagePin always true
(TestPackagePinIsNoLogin), versionValue false
(TestPackageNamesGoAsWritten), pinnedAfter off and sriRe off
(TestModelPackageNamesNotMasked).
Not tried: dniray's own text didn't make either model fire alone on
mote (the model reads context), so the fix is held by the fake-model
test and the jsonwebtoken case the real model does fire on; the eval
recordings were not re-recorded (no eval case added); the full
./internal/gateway and ./internal/gui suites.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -26,6 +26,12 @@ import (
|
||||
// 10-28: function names were masked). A key's characters are random;
|
||||
// a name made of words, one of them token, secret or password, is a
|
||||
// function's or a variable's.
|
||||
// - a package's version: a dependency whose name has token, secret or
|
||||
// password in it is pinned in a manifest the same way, "js-tokens":
|
||||
// "^4.0.0-beta.1", jsonwebtoken = "9.2.0-beta1", tokenizer =
|
||||
// "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).
|
||||
//
|
||||
// A secret written down still masks: password=hunter2pass9,
|
||||
// API_KEY=AbC123dEf456GhI789, token: t0k3nValue2, a password of one word
|
||||
@@ -41,9 +47,24 @@ func fieldAt(s string, a, b int) bool {
|
||||
if v[0] == '=' {
|
||||
return false // Go's := with no space after it: secretKey:=base64Encode(raw)
|
||||
}
|
||||
return !fileField(s, a) && !lineRef(v) && !pathValue(v) && !namesASecret(v) && !skillName(s, a, b)
|
||||
return !fileField(s, a) && !lineRef(v) && !pathValue(v) && !namesASecret(v) && !skillName(s, a, b) && !versionValue(v)
|
||||
}
|
||||
|
||||
// versionRangeRe is a release's version as a manifest or a lock file
|
||||
// pins it: a range's operator (^, ~, >=, ==), numbers joined by dots,
|
||||
// then a pre-release's or a build's parts, each a word releases are
|
||||
// tagged with and its number (-beta.1, -rc1, -alpha03, .post2, b1,
|
||||
// .RELEASE, +build.5) or a number alone. One letter on its own is a
|
||||
// part only with a number after it (b1, M2), so 3.14159abc is no
|
||||
// version.
|
||||
var versionRangeRe = regexp.MustCompile(`^(?:[~^]|[<>=!~]=?|==)?[vV]?\d+(?:\.\d+)+` +
|
||||
`(?:[-+._]?(?:(?i:alpha|beta|rc|pre|preview|post|dev|canary|next|nightly|snapshot|release|final|ga|stable|build|incubating)\d*|[abcmABCM]\d+|\d+))*$`)
|
||||
|
||||
// versionValue says v is a release's version, which no secret is: the
|
||||
// SECRET rules found it after a package's name with token or secret in
|
||||
// it, the local model in a manifest.
|
||||
func versionValue(v string) bool { return versionRangeRe.MatchString(v) }
|
||||
|
||||
// skillName says s[a:b] is the second half of plugin:skill, as Claude
|
||||
// Code names a plugin's skill and command (design-tokens:generate-v2,
|
||||
// secret-scan:audit-x7k2p9q1): words in lower case joined by hyphens on
|
||||
|
||||
@@ -121,7 +121,7 @@ func delimiter(c byte) bool { return isSpace(c) || strings.IndexByte(`"',;:`, c)
|
||||
func modelTakes(kind, v string) bool {
|
||||
switch kind {
|
||||
case "SECRET":
|
||||
return len(v) >= 8 && !madeID(v) && !namesThing(v)
|
||||
return len(v) >= 8 && !madeID(v) && !namesThing(v) && !versionValue(v)
|
||||
case "PERSON":
|
||||
// (an ordinary Chinese word, 文案 or 关键词, is left where it is
|
||||
// read: zhWordNotName)
|
||||
@@ -161,7 +161,7 @@ func namesThing(v string) bool {
|
||||
if u, n, ok := strings.Cut(v, ":"); ok && n != "" && strings.Trim(n, digits) == "" {
|
||||
v = u
|
||||
}
|
||||
if uuidRe.MatchString(v) || digestRe.MatchString(v) || slugRe.MatchString(v) || fileNameRe.MatchString(v) || modelName(v) {
|
||||
if uuidRe.MatchString(v) || digestRe.MatchString(v) || sriRe.MatchString(v) || slugRe.MatchString(v) || fileNameRe.MatchString(v) || modelName(v) {
|
||||
return true
|
||||
}
|
||||
return !strings.ContainsAny(v, " \t\n") && (strings.HasPrefix(v, "/") || strings.HasPrefix(v, "~/") ||
|
||||
@@ -201,6 +201,11 @@ var (
|
||||
// a file's name in lower case (make-icons.sh, run_all.py)
|
||||
fileNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]*(?:\.[a-z0-9_-]+)*\.[a-z][a-z0-9]{0,4}$`)
|
||||
digestRe = regexp.MustCompile(`^(?i)(?:md5|sha-?(?:1|224|256|384|512)|blake2b?|blake3)[ :=-]+[0-9a-f]{32,128}$`)
|
||||
// a package's digest as a lock file pins it (Subresource Integrity:
|
||||
// package-lock.json's and pnpm-lock.yaml's "integrity"), sha512- and
|
||||
// the hash in base64, with the } or quote the model took in after it.
|
||||
// Privacy Filter took each one for a secret.
|
||||
sriRe = regexp.MustCompile(`^sha(?:256|384|512)-[A-Za-z0-9+/]{43,86}={0,2}[}"',;)\]]*$`)
|
||||
)
|
||||
|
||||
// madeID says v is an id of the shape vendors and tools give theirs: a
|
||||
@@ -314,6 +319,9 @@ func modelFits(s string, a, b int, kind string) bool {
|
||||
// 10-28). A key is 64 hex digits; one called a key is masked still.
|
||||
return kind == "SECRET" && secretCued(s, a)
|
||||
}
|
||||
if moduleAt(s, a, b) {
|
||||
return false
|
||||
}
|
||||
switch kind {
|
||||
case "PERSON":
|
||||
return personFits(s, a, b, v)
|
||||
@@ -348,6 +356,9 @@ func modelFits(s string, a, b int, kind string) bool {
|
||||
if f := strings.Fields(v); len(f) > 1 && hexRe.MatchString(f[0]) || modCacheRe.MatchString(v) {
|
||||
return false
|
||||
}
|
||||
if pinnedAfter(s, b) {
|
||||
return false
|
||||
}
|
||||
return !hexRe.MatchString(v) || secretCued(s, a)
|
||||
case "EMAIL":
|
||||
// a URL's user:password@host is no address, as for the rule's (#3538)
|
||||
@@ -356,6 +367,83 @@ func modelFits(s string, a, b int, kind string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// pinnedAfter says the value that ends at s[b] is a package's name, as
|
||||
// a requirement pins its version after it: django-rest-passwordreset==1.4.0,
|
||||
// tokenizers>=0.15, a-lib~=2.1 (pip's comparisons). AstrLink Guard took a
|
||||
// package with password in its name for a secret, and the agent read
|
||||
// {{SECRET_…}}==1.4.0 in requirements.txt (dniray on Discord, v0.1.1917).
|
||||
// No secret has a version pinned after it; a password before @ and an
|
||||
// address (s3cret@10.0.0.5) is no pin.
|
||||
func pinnedAfter(s string, b int) bool {
|
||||
rest := s[b:]
|
||||
for _, op := range []string{"===", "==", ">=", "<=", "~=", "!="} {
|
||||
if strings.HasPrefix(rest, op) {
|
||||
rest = strings.TrimLeft(rest[len(op):], " ")
|
||||
return rest != "" && rest[0] >= '0' && rest[0] <= '9'
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// moduleRe is what code names a module by: drizzle-orm, @scope/name/sub,
|
||||
// node:fs, ./db/schema, golang.org/x/net, serde_json::Value.
|
||||
var moduleRe = regexp.MustCompile(`^(?:[a-z]+:)?@?[A-Za-z0-9_.~-]+(?:(?:/|::)[A-Za-z0-9_.~@-]+)*$`)
|
||||
|
||||
// moduleAt says s[a:b] is the module a line of code loads: import { eq }
|
||||
// from 'drizzle-orm', require(' jsonwebtoken '), import('x'), from
|
||||
// django.contrib import auth, import torch, use jsonwebtoken::encode.
|
||||
// AstrLink Guard took a package with a word like token in its name, or
|
||||
// one alone in its quotes, for a secret (dniray on Discord, v0.1.1917:
|
||||
// from ' {{SECRET_…}} '), and the agent read an import it could not
|
||||
// resolve. A secret is never the module code loads. Prose is not code:
|
||||
// "the token from 'a1b2…'" and "copy it from a1b2…" are judged as before,
|
||||
// as a from counts only on an import or export line or after } or *.
|
||||
func moduleAt(s string, a, b int) bool {
|
||||
if !moduleRe.MatchString(s[a:b]) {
|
||||
return false
|
||||
}
|
||||
line := strings.LastIndexByte(s[:a], '\n') + 1
|
||||
head := strings.TrimLeft(s[line:a], " \t")
|
||||
end := strings.IndexByte(s[b:], '\n')
|
||||
if end < 0 {
|
||||
end = len(s) - b
|
||||
}
|
||||
tail := strings.TrimLeft(s[b:b+end], " \t")
|
||||
if q := strings.TrimRight(head, " \t"); q != "" && strings.ContainsRune("'\"`", rune(q[len(q)-1])) {
|
||||
// quoted, and ended in the same quote: from ' drizzle-orm '
|
||||
if tail == "" || tail[0] != q[len(q)-1] {
|
||||
return false
|
||||
}
|
||||
q = strings.TrimRight(q[:len(q)-1], " \t")
|
||||
if c := strings.TrimSuffix(q, "("); c != q {
|
||||
q = strings.TrimRight(c, " \t")
|
||||
return wordEnd(q, "require") || wordEnd(q, "import")
|
||||
}
|
||||
if !wordEnd(q, "from") {
|
||||
// import 'reflect-metadata'
|
||||
return q == "import"
|
||||
}
|
||||
q = strings.TrimRight(strings.TrimSuffix(q, "from"), " \t")
|
||||
return strings.HasPrefix(q, "import") || strings.HasPrefix(q, "export") ||
|
||||
strings.HasSuffix(q, "}") || strings.HasSuffix(q, "*") || q == ""
|
||||
}
|
||||
// unquoted, the word starts the line: Python's and Rust's
|
||||
switch strings.TrimRight(head, " \t") {
|
||||
case "from":
|
||||
return strings.HasPrefix(tail, "import ")
|
||||
case "import":
|
||||
return tail == "" || tail[0] == ',' || tail[0] == ';' || strings.HasPrefix(tail, "as ")
|
||||
case "use", "extern crate":
|
||||
return strings.HasPrefix(tail, "::") || tail == "" || tail[0] == ';' || tail[0] == '{'
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// wordEnd says s ends in the word w.
|
||||
func wordEnd(s, w string) bool {
|
||||
return strings.HasSuffix(s, w) && (len(s) == len(w) || !wordByte(s[len(s)-len(w)-1]))
|
||||
}
|
||||
|
||||
func wordByte(c byte) bool {
|
||||
return c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'
|
||||
}
|
||||
|
||||
+37
-3
@@ -123,10 +123,12 @@ var moreRules = []rule{
|
||||
fold: []string{"users", "documents and settings"}, back: 12, ahead: 100},
|
||||
|
||||
// user@host as a shell prompt has it (user@host:~$, [user@host ~]$) or
|
||||
// as ssh, scp and rsync are given it; git@github.com is no one's
|
||||
{cat: "userhost", groups: []string{"USERNAME", "HOSTNAME"}, groupOK: []func(string) bool{userOK, hostOK}, markers: []string{"@"},
|
||||
// as ssh, scp and rsync are given it; git@github.com is no one's; a
|
||||
// package pinned at its version,
|
||||
// js-tokens@4.0.0: in pnpm-lock.yaml, is no login (notPackagePin)
|
||||
{cat: "userhost", groups: []string{"USERNAME", "HOSTNAME"}, groupOK: []func(string) bool{userOK, hostOK}, markers: []string{"@"}, at: notPackagePin,
|
||||
re: regexp.MustCompile(`(?m)(?:^|[^A-Za-z0-9._%+-])(` + unixUser + `)@(` + hostRe + `)(?::[~/]|:[ \t]*$|[ \t]+[~/]|[ \t]*[$%#>][ \t]|[ \t]*[$%#>]$)`)},
|
||||
{cat: "userhost", groups: []string{"USERNAME", "HOSTNAME"}, groupOK: []func(string) bool{userOK, hostOK}, markers: []string{"@"},
|
||||
{cat: "userhost", groups: []string{"USERNAME", "HOSTNAME"}, groupOK: []func(string) bool{userOK, hostOK}, markers: []string{"@"}, at: notPackagePin,
|
||||
re: regexp.MustCompile(`(?i:\b(?:ssh|scp|sftp|rsync|mosh|ssh-copy-id))\b[^\n@]{0,80}?[ \t"'](` + unixUser + `)@(` + hostRe + `)`)},
|
||||
// hostname: x, HOSTNAME=x, a line with the value alone after it; or
|
||||
// ssh config's HostName x, indented under its Host
|
||||
@@ -350,6 +352,38 @@ func inCode(v string) bool {
|
||||
return codeObjects[first] || notUsers[last] || notNames[last] || publicHosts[last] || last == "login" || last == "username"
|
||||
}
|
||||
|
||||
// notPackagePin says the user or the host of user@host at s[a:b] is not
|
||||
// half of a package pinned at its version: pnpm-lock.yaml lists each
|
||||
// package as js-tokens@4.0.0: on a line of its own, which is a shell
|
||||
// prompt's shape, and the package and its version went as a user and a
|
||||
// host (dniray on Discord, v0.1.1917). A host that is an address
|
||||
// (alice@10.0.0.5) is a host still.
|
||||
func notPackagePin(s string, a, b int) bool {
|
||||
if b < len(s) && s[b] == '@' {
|
||||
// the user: what follows the @ is the host
|
||||
e := b + 1
|
||||
for e < len(s) && strings.IndexByte(alnum+"-.", s[e]) >= 0 {
|
||||
e++
|
||||
}
|
||||
return !versionHost(strings.TrimRight(s[b+1:e], "."))
|
||||
}
|
||||
return !(a > 0 && s[a-1] == '@' && versionHost(s[a:b]))
|
||||
}
|
||||
|
||||
// versionHostRe is a release's version as a host's characters can
|
||||
// spell it: 4.0.0, 18.2.0-rc.0.
|
||||
var versionHostRe = regexp.MustCompile(`^v?\d+(?:\.\d+)+(?:-[0-9A-Za-z.-]+)?$`)
|
||||
|
||||
// versionHost says h, after a user's @, is a version and not a host: no
|
||||
// host's name is numbers and dots alone, but an address is.
|
||||
func versionHost(h string) bool {
|
||||
if !versionHostRe.MatchString(h) {
|
||||
return false
|
||||
}
|
||||
_, err := netip.ParseAddr(h)
|
||||
return err != nil
|
||||
}
|
||||
|
||||
func lineUserOK(v string) bool { return userOK(v) && !inCode(v) && !chatWords[strings.ToLower(v)] }
|
||||
func lineHostOK(v string) bool { return hostOK(v) && !inCode(v) }
|
||||
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A package's name, in every shape code and manifests write one, goes as
|
||||
// written by the rules, with the default categories and with all of them
|
||||
// (dniray on Discord, v0.1.1917: "脱敏会把包名给替换了").
|
||||
func TestPackageNamesGoAsWritten(t *testing.T) {
|
||||
for _, s := range []string{
|
||||
// npm, scoped and pinned
|
||||
"npm install @babel/core @types/node @vue/cli-service --save-dev",
|
||||
"pnpm add @tanstack/react-query@5.59.0 @scope/name@1.2.3 lodash@4.17.21",
|
||||
"import { and, eq } from ' drizzle-orm '",
|
||||
"import { and, eq } from 'drizzle-orm'",
|
||||
"const jwt = require( ' jsonwebtoken ' );",
|
||||
`"node_modules/@babel/code-frame": {"version": "7.23.5"}`,
|
||||
// Java and Android
|
||||
"package com.example.app;\nimport org.springframework.boot.SpringApplication;",
|
||||
"包名是 com.dniray.tool,帮我改成 com.zhangsan.demo",
|
||||
"implementation 'com.squareup.retrofit2:retrofit:2.9.0'",
|
||||
// Go, Python, purl, Debian, Cargo
|
||||
"go get github.com/spf13/cobra@v1.8.0 golang.org/x/net",
|
||||
"from sklearn.model_selection import train_test_split",
|
||||
"pip install requests==2.31.0 scikit-learn pandas>=2.0",
|
||||
"purl: pkg:npm/%40angular/animation@12.3.1 pkg:pypi/django@1.11.1",
|
||||
"sudo apt-get install -y libssl-dev libffi-dev python3-dev",
|
||||
"cargo add serde_json tokio; use jsonwebtoken::encode;",
|
||||
// a dependency with a secret's word in its name, at a prerelease
|
||||
`{"dependencies": {"js-tokens": "^4.0.0-beta.1", "tokenizers": "0.15.0-rc1", "next-auth": "5.0.0-beta.25"}}`,
|
||||
"[dependencies]\njsonwebtoken = \"9.2.0-beta1\"\npassword-hash = \"0.5.0-pre.1\"",
|
||||
"secretVersion = '2.1.0-alpha03'\ntokenizer = \"2.4.0-beta02\"",
|
||||
"tokenizers==0.15.0rc1\npassword_strength==0.0.3.post2\ncredentials-manager==0.2.0b1",
|
||||
} {
|
||||
for _, o := range []Options{{Secrets: true, Personal: true}, allKinds()} {
|
||||
if out, n := Mask(s, o); n != 0 {
|
||||
t.Errorf("all=%v: %q masked as %q", o.Kinds != nil, s, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pnpm-lock.yaml lists each package pinned at its version on a line of
|
||||
// its own, a shell prompt's shape: with user@host on, the package and
|
||||
// its version went as a user and a host.
|
||||
func TestPackagePinIsNoLogin(t *testing.T) {
|
||||
for _, s := range []string{
|
||||
"packages:\n js-tokens@4.0.0:\n resolution: {}",
|
||||
" react-dom@18.2.0-rc.0:\n",
|
||||
"ssh deploy@1.2.3-beta ~/x",
|
||||
} {
|
||||
if out, n := Mask(s, allKinds()); n != 0 {
|
||||
t.Errorf("%q masked as %q", s, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// What the local model took for a secret in a package's name: the
|
||||
// module an import loads (dniray's literal bytes first), a requirement's
|
||||
// name, a lock file's integrity hash.
|
||||
func TestModelPackageNamesNotMasked(t *testing.T) {
|
||||
for _, c := range []struct{ text, value string }{
|
||||
{"import { and, eq } from ' drizzle-orm '", "drizzle-orm"},
|
||||
{"import { and, eq } from 'drizzle-orm';", "drizzle-orm"},
|
||||
{"} from \"drizzle-orm/pg-core\";", "drizzle-orm/pg-core"},
|
||||
{"export * from '@acme/secret-store';", "@acme/secret-store"},
|
||||
{"import 'reflect-metadata'", "reflect-metadata"},
|
||||
{"const jwt = require( ' jsonwebtoken ' );", "jsonwebtoken"},
|
||||
{"const m = await import('node:crypto')", "node:crypto"},
|
||||
{"from secretstorage import collection", "secretstorage"},
|
||||
{"import jsonwebtoken as jwt", "jsonwebtoken"},
|
||||
{"use jsonwebtoken::{encode, Header};", "jsonwebtoken"},
|
||||
{"django-rest-passwordreset==1.4.0\n", "django-rest-passwordreset"},
|
||||
{"tokenizers>=0.15\n", "tokenizers"},
|
||||
{"resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}",
|
||||
"sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}"},
|
||||
} {
|
||||
f := &fakeModel{values: map[string]string{c.value: "secret"}}
|
||||
if out, n := Mask(c.text, Options{Secrets: true, Personal: true, Model: f.find}); n != 0 {
|
||||
t.Errorf("%q: masked %q", c.value, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Real values beside the shapes above are masked still.
|
||||
func TestPackageNameChecksKeepRealValues(t *testing.T) {
|
||||
for _, c := range []struct{ text, value string }{
|
||||
{"Contact zhang.wei@acme-corp.cn about it", "zhang.wei@acme-corp.cn"},
|
||||
{"ssh deploy@build01.corp.acme-internal.net ~/run.sh", "build01.corp.acme-internal.net"},
|
||||
{"password=hunter2pass9", "hunter2pass9"},
|
||||
{"API_KEY=AbC123dEf456GhI789", "AbC123dEf456GhI789"},
|
||||
{"secret_version = 'Zq8vR2mT6wLp'", "Zq8vR2mT6wLp"},
|
||||
{"alice@10.0.0.5:~$ ls", "alice"},
|
||||
} {
|
||||
if out, _ := Mask(c.text, allKinds()); strings.Contains(out, c.value) {
|
||||
t.Errorf("%q left in %q", c.value, out)
|
||||
}
|
||||
}
|
||||
// the model's secret in prose, after a password, before an address
|
||||
for _, c := range []struct{ text, value string }{
|
||||
{"I copied the token from 'Zq8vR2mT6wLp' yesterday", "Zq8vR2mT6wLp"},
|
||||
{"copy it from Zq8vR2mT6wLp now", "Zq8vR2mT6wLp"},
|
||||
{"the vault phrase is Zq8vR2mT6wLp", "Zq8vR2mT6wLp"},
|
||||
{"mysql -p s3cretPass@10.0.0.5", "s3cretPass"},
|
||||
{"pw Zq8vR2mT6wLp==", "Zq8vR2mT6wLp"},
|
||||
} {
|
||||
f := &fakeModel{values: map[string]string{c.value: "secret"}}
|
||||
if out, _ := Mask(c.text, Options{Secrets: true, Personal: true, Model: f.find}); strings.Contains(out, c.value) {
|
||||
t.Errorf("%q left in %q", c.value, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -301,8 +301,10 @@ func keyValue(v string) bool {
|
||||
// time.
|
||||
func keyAt(s string, a, b int) bool {
|
||||
v := s[a:b]
|
||||
if pathValue(v) {
|
||||
return false // where a key is kept: 私钥 ~/.ssh/id_ed25519 (fieldvalue.go)
|
||||
if pathValue(v) || versionValue(v) {
|
||||
// where a key is kept: 私钥 ~/.ssh/id_ed25519, or a release's
|
||||
// version: token 是 0.15.0-rc1 (fieldvalue.go)
|
||||
return false
|
||||
}
|
||||
if keyValue(v) {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user