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.
115 lines
5.0 KiB
Go
115 lines
5.0 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|