Files
magpie/internal/redact/package_known_test.go
T
yetone 300b7eee08 redact: a package's name goes as written where it was masked before, and in manifests and errors (dniray on Discord)
dniray, on v0.1.1965 after v0.1.1932's fix: "问题还在", with
import { and, eq } from ' drizzle-orm ' still going out as
from ' {{SECRET_o2tu7vej}} '.

v0.1.1932 let the module an import loads go as written only where the
local model found it on that line (moduleAt in modelFits). A value
masked once is known (knownSpans) and masked wherever it is, with no
look at what is around it. So once the model took drizzle-orm for a
secret anywhere in a request (package.json's dependencies, a tsc
error, a sentence: none of those was exempt), the import line was
masked as known: in the same request (MaskJSON walks the texts in
order) and in every later one.

- knownSpans skips a known value where packageAt says it is a
  package's name.
- packageAt: moduleAt, plus a manifest's dependency pinned at a version
  ("drizzle-orm": "^0.36.4", workspace:*, TOML x = "9";
  dependencyAt), a folder under node_modules or site-packages, the
  module an error could not find (Cannot find module 'x', No module
  named 'x', Can't resolve 'x'; missingModuleAt), a package an install
  names (pnpm add x, npm i -D x, pip install x, cargo add x;
  installedAt, not after a --token-like flag), one pinned with @
  (x@0.36.4, lower case, not an IPv4 address; pinnedAt). A piece of a
  name (drizzle-orm of drizzle-orm/pg-core, of @acme/drizzle-orm) is
  judged as the whole name.
- moduleAt reads past an editor's line number and a diff's mark
  (codeHead): Claude Code's Read gives "     1→import drizzle from
  'drizzle-orm'", and a default import there wasn't an import line.

Tests: package_known_test.go.
TestKnownPackageNameGoesAsWrittenWhereCodeLoadsIt has the reporter's
literal line in a Claude Code tool_result after a sentence with the
name, in one Anthropic body, then alone (Mask, no model) and in a
second body: red on the old code at all three
(from ' {{SECRET_…}} '). TestModelPackageNamesInManifestsAndOutput: 22
shapes, red on the old code. TestPackageShapesKeepSecrets: a JSON key
with a non-version value, a password before @ip, a quoted secret on a
numbered line, prose with "install", a known secret in prose and on a
numbered line are masked still.

Run, at 936bdccfa + this change, on mote:
- go test -count=1 -timeout 45m ./internal/redact/... ./internal/masking/...
  ./internal/otlp/... ./internal/pii/... ./internal/sessions/...
  ./internal/settings/... ./internal/gwmw/...: ok (TestMaskEval's
  recordings pass unchanged)
- go test -run "Redact|Mask|Privacy|Unredact|Known|Restore"
  ./internal/gateway ./internal/gui: ok
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows on
  ./internal/redact/... ./internal/masking/...: ok; gofmt -l: clean
- the real AstrLink Guard and Privacy Filter on mote (zzcache), at
  their default thresholds, over 12 texts: the literal line, a
  numbered Read of a drizzle file, package.json, pnpm add, a tsc
  error, node_modules path, prose, a .env: neither model found
  drizzle-orm anywhere, and the .env's password and JWT secret were
  still masked with the change.

Broken by hand, each red: the packageAt check in knownSpans off
(TestKnownPackageNameGoesAsWrittenWhereCodeLoadsIt, all three);
codeHead off and missingModuleAt/installedAt/pinnedAt off
(TestModelPackageNamesInManifestsAndOutput, 11 cases).

Not tried: what made the model take drizzle-orm for a secret on
dniray's machine. Neither real model does on any text tried, so the
fix is held by the fake model; the known path is the one way found
that v0.1.1932's check is passed by. No eval case added (recordings not
re-recorded); the full ./internal/gateway and ./internal/gui suites;
Windows and Linux runs (vet only).
2026-10-29 08:00:53 +08:00

107 lines
5.5 KiB
Go

package redact
import (
"encoding/json"
"strings"
"testing"
)
// dniray on Discord, v0.1.1965, after v0.1.1932's fix: "问题还在", with
// import { and, eq } from ' drizzle-orm ' going out as
// from ' {{SECRET_o2tu7vej}} '. The module an import loads went as
// written only where the model found it on that line: once the model had
// taken the name for a secret anywhere else in a request (package.json's
// dependencies, a tsc error, a sentence), the name was known, and a known
// value is masked wherever it is, on the import line too.
func TestKnownPackageNameGoesAsWrittenWhereCodeLoadsIt(t *testing.T) {
const line = "import { and, eq } from ' drizzle-orm '"
f := &fakeModel{values: map[string]string{"drizzle-orm": "secret"}}
o := Options{Secrets: true, Personal: true, Model: f.find}
// a sentence the name can't be told apart in, then the import, in one
// request: Claude Code's tool results as it sends them
body, _ := json.Marshal(map[string]any{"model": "m", "messages": []any{
map[string]any{"role": "user", "content": "we keep the drizzle-orm here"},
map[string]any{"role": "assistant", "content": []any{map[string]any{"type": "tool_use", "id": "toolu_01", "name": "Read", "input": map[string]any{"file_path": "/w/src/db.ts"}}}},
map[string]any{"role": "user", "content": []any{map[string]any{"type": "tool_result", "tool_use_id": "toolu_01", "content": line + "\n"}}},
}})
out, _ := MaskJSON(body, o)
if !strings.Contains(string(out), line) {
t.Errorf("the import was masked: %s", out)
}
// and in the next request, with the model finding nothing in it
if out, n := Mask(line, Options{Secrets: true, Personal: true}); n != 0 {
t.Errorf("as known: %q", out)
}
f2 := &fakeModel{values: map[string]string{"drizzle-orm": "secret"}}
o2 := Options{Secrets: true, Personal: true, Model: f2.find}
body2, _ := json.Marshal(map[string]any{"messages": []any{map[string]any{"role": "user", "content": line}}})
if out, _ := MaskJSON(body2, o2); !strings.Contains(string(out), line) {
t.Errorf("as known, in a body: %s", out)
}
}
// Where else a package's name is written in what an agent reads: a
// dependency in package.json, Cargo.toml or a lock file, a path under
// node_modules, a compiler's "Cannot find module", an install, a pin at a
// version, and an import with an editor's line numbers or a diff's mark
// before it. The model took it for a secret in each, and it went as a
// placeholder the agent can't resolve.
func TestModelPackageNamesInManifestsAndOutput(t *testing.T) {
for _, c := range []struct{ text, value string }{
{"{\n \"dependencies\": {\n \"drizzle-orm\": \"^0.36.4\",\n \"jsonwebtoken\": \"~9.0.2\"\n }\n}", "drizzle-orm"},
{"{\"devDependencies\": {\"drizzle-kit\": \"0.28.1\"}}", "drizzle-kit"},
{"\"jsonwebtoken\": \"workspace:*\",", "jsonwebtoken"},
{"\"drizzle-orm\": \">=0.30.0 <1\",", "drizzle-orm"},
{"[dependencies]\njsonwebtoken = \"9\"\n", "jsonwebtoken"},
{" 1→import drizzle from 'drizzle-orm';", "drizzle-orm"},
{" 2\timport sql from 'postgres';\n", "postgres"},
{" 3→import { pgTable } from 'drizzle-orm/pg-core';", "drizzle-orm"},
{"import { z } from '@acme/drizzle-orm';", "drizzle-orm"},
{"12: import { sql } from \"drizzle-orm\";", "drizzle-orm"},
{"+import { and, eq } from ' drizzle-orm '", "drizzle-orm"},
{"-const jwt = require('jsonwebtoken')", "jsonwebtoken"},
{"node_modules/drizzle-orm/pg-core/columns/text.d.ts:12:5 - error TS2322", "drizzle-orm"},
{"src/db.ts:1:27 - error TS2307: Cannot find module 'drizzle-orm' or its corresponding type declarations.", "drizzle-orm"},
{"Error: Cannot find module 'jsonwebtoken'\nRequire stack:", "jsonwebtoken"},
{"ModuleNotFoundError: No module named 'secretstorage'", "secretstorage"},
{"pnpm add drizzle-orm postgres && pnpm add -D drizzle-kit", "drizzle-orm"},
{"npm install --save jsonwebtoken", "jsonwebtoken"},
{"pip install django-rest-passwordreset", "django-rest-passwordreset"},
{"cargo add jsonwebtoken", "jsonwebtoken"},
{"added drizzle-orm@0.36.4 to dependencies", "drizzle-orm"},
{" drizzle-orm@0.36.4:\n resolution: {}", "drizzle-orm"},
} {
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)
}
}
}
// A secret beside those shapes is masked still: one the model finds under
// a field that isn't a dependency, after an install's words in prose, and
// a known one in an ordinary line.
func TestPackageShapesKeepSecrets(t *testing.T) {
for _, c := range []struct{ text, value string }{
{"{\"password\": \"Zq8vR2mT6wLp\"}", "Zq8vR2mT6wLp"},
{"{\"Zq8vR2mT6wLp\": \"hunter2\"}", "Zq8vR2mT6wLp"},
{"the login is Zq8vR2mT6wLp@10.0.0.5", "Zq8vR2mT6wLp"},
{" 1→const token = 'Zq8vR2mT6wLp';", "Zq8vR2mT6wLp"},
{"use the code Zq8vR2mT6wLp to install it", "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)
}
}
// a known secret in a line that isn't code loading it
if out, _ := Mask("DB_PASSWORD=Zq8hunterPass42", Options{Secrets: true}); strings.Contains(out, "Zq8hunterPass42") {
t.Fatalf("not masked to begin with: %q", out)
}
for _, s := range []string{"the password from Zq8hunterPass42 there", " 1→const p = 'Zq8hunterPass42';"} {
if out, _ := Mask(s, Options{Secrets: true}); strings.Contains(out, "Zq8hunterPass42") {
t.Errorf("known secret left in %q", out)
}
}
}