@nightloom/isy 1.0.6-next.0 → 1.0.7-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +7 -5
  2. package/dist/agents/codex-records.js +10 -1
  3. package/dist/agents/codex-records.js.map +1 -1
  4. package/dist/commands/commit.js +21 -1
  5. package/dist/commands/commit.js.map +1 -1
  6. package/dist/commands/upload.d.ts +1 -0
  7. package/dist/commands/upload.js +13 -5
  8. package/dist/commands/upload.js.map +1 -1
  9. package/dist/index.js +8 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/parser.js +1 -0
  12. package/dist/parser.js.map +1 -1
  13. package/dist/redact.js +12 -5
  14. package/dist/redact.js.map +1 -1
  15. package/dist/subagents.js +7 -5
  16. package/dist/subagents.js.map +1 -1
  17. package/package.json +3 -2
  18. package/src/agents/codex-records.ts +10 -1
  19. package/src/commands/commit.ts +22 -1
  20. package/src/commands/upload.ts +13 -6
  21. package/src/index.ts +8 -2
  22. package/src/parser.ts +1 -0
  23. package/src/redact.ts +13 -5
  24. package/src/subagents.ts +7 -5
  25. package/dist/test/analyze.test.d.ts +0 -1
  26. package/dist/test/analyze.test.js +0 -412
  27. package/dist/test/analyze.test.js.map +0 -1
  28. package/dist/test/check.test.d.ts +0 -1
  29. package/dist/test/check.test.js +0 -215
  30. package/dist/test/check.test.js.map +0 -1
  31. package/dist/test/cli.test.d.ts +0 -1
  32. package/dist/test/cli.test.js +0 -142
  33. package/dist/test/cli.test.js.map +0 -1
  34. package/dist/test/codex.test.d.ts +0 -1
  35. package/dist/test/codex.test.js +0 -666
  36. package/dist/test/codex.test.js.map +0 -1
  37. package/dist/test/commit.test.d.ts +0 -1
  38. package/dist/test/commit.test.js +0 -1898
  39. package/dist/test/commit.test.js.map +0 -1
  40. package/dist/test/e2e-harness.test.d.ts +0 -1
  41. package/dist/test/e2e-harness.test.js +0 -53
  42. package/dist/test/e2e-harness.test.js.map +0 -1
  43. package/dist/test/git.test.d.ts +0 -1
  44. package/dist/test/git.test.js +0 -145
  45. package/dist/test/git.test.js.map +0 -1
  46. package/dist/test/health.test.d.ts +0 -1
  47. package/dist/test/health.test.js +0 -156
  48. package/dist/test/health.test.js.map +0 -1
  49. package/dist/test/hook.test.d.ts +0 -1
  50. package/dist/test/hook.test.js +0 -343
  51. package/dist/test/hook.test.js.map +0 -1
  52. package/dist/test/init.test.d.ts +0 -1
  53. package/dist/test/init.test.js +0 -162
  54. package/dist/test/init.test.js.map +0 -1
  55. package/dist/test/kimi-cli.test.d.ts +0 -1
  56. package/dist/test/kimi-cli.test.js +0 -231
  57. package/dist/test/kimi-cli.test.js.map +0 -1
  58. package/dist/test/kimi-code.test.d.ts +0 -1
  59. package/dist/test/kimi-code.test.js +0 -302
  60. package/dist/test/kimi-code.test.js.map +0 -1
  61. package/dist/test/kimi-config.test.d.ts +0 -1
  62. package/dist/test/kimi-config.test.js +0 -246
  63. package/dist/test/kimi-config.test.js.map +0 -1
  64. package/dist/test/kimi-hook.test.d.ts +0 -1
  65. package/dist/test/kimi-hook.test.js +0 -163
  66. package/dist/test/kimi-hook.test.js.map +0 -1
  67. package/dist/test/kimi-listing.test.d.ts +0 -1
  68. package/dist/test/kimi-listing.test.js +0 -233
  69. package/dist/test/kimi-listing.test.js.map +0 -1
  70. package/dist/test/kimi.test.d.ts +0 -1
  71. package/dist/test/kimi.test.js +0 -418
  72. package/dist/test/kimi.test.js.map +0 -1
  73. package/dist/test/mask-paths.test.d.ts +0 -1
  74. package/dist/test/mask-paths.test.js +0 -275
  75. package/dist/test/mask-paths.test.js.map +0 -1
  76. package/dist/test/notes.test.d.ts +0 -1
  77. package/dist/test/notes.test.js +0 -41
  78. package/dist/test/notes.test.js.map +0 -1
  79. package/dist/test/pairing.test.d.ts +0 -1
  80. package/dist/test/pairing.test.js +0 -138
  81. package/dist/test/pairing.test.js.map +0 -1
  82. package/dist/test/parser.test.d.ts +0 -1
  83. package/dist/test/parser.test.js +0 -1041
  84. package/dist/test/parser.test.js.map +0 -1
  85. package/dist/test/plan.test.d.ts +0 -1
  86. package/dist/test/plan.test.js +0 -35
  87. package/dist/test/plan.test.js.map +0 -1
  88. package/dist/test/redact.test.d.ts +0 -1
  89. package/dist/test/redact.test.js +0 -3053
  90. package/dist/test/redact.test.js.map +0 -1
  91. package/dist/test/settings.test.d.ts +0 -1
  92. package/dist/test/settings.test.js +0 -32
  93. package/dist/test/settings.test.js.map +0 -1
  94. package/dist/test/stage0.test.d.ts +0 -1
  95. package/dist/test/stage0.test.js +0 -1304
  96. package/dist/test/stage0.test.js.map +0 -1
  97. package/dist/test/subagents.test.d.ts +0 -1
  98. package/dist/test/subagents.test.js +0 -290
  99. package/dist/test/subagents.test.js.map +0 -1
  100. package/dist/test/sweep.test.d.ts +0 -1
  101. package/dist/test/sweep.test.js +0 -434
  102. package/dist/test/sweep.test.js.map +0 -1
  103. package/dist/test/update.test.d.ts +0 -1
  104. package/dist/test/update.test.js +0 -26
  105. package/dist/test/update.test.js.map +0 -1
  106. package/dist/test/upload.test.d.ts +0 -1
  107. package/dist/test/upload.test.js +0 -393
  108. package/dist/test/upload.test.js.map +0 -1
@@ -1,3053 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { existsSync } from "node:fs";
3
- import { copyFile, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
4
- import { homedir, tmpdir } from "node:os";
5
- import { join } from "node:path";
6
- import { test } from "node:test";
7
- import { parseLines, parseTranscriptFile, shellEditPaths } from "../parser.js";
8
- import { BUILTIN_RULES, REDACTION_MARKER, applyRules, marker, redactLineStream, redactLines, redactTranscriptFile } from "../redact.js";
9
- function redactOne(text, extraPatterns) {
10
- const record = { type: "user", message: { role: "user", content: text } };
11
- const { lines } = redactLines([JSON.stringify(record)], { extraPatterns });
12
- const parsed = JSON.parse(lines[0]);
13
- return parsed.message.content;
14
- }
15
- function summaryFor(records) {
16
- return redactLines(records.map((record) => JSON.stringify(record)));
17
- }
18
- /** One tool call and its result as Claude Code records them, and what came out of each. */
19
- function throughCall(name, input, output = "MAILER_PHRASE=Zq8vR2mK9xLp") {
20
- const { lines } = summaryFor([
21
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name, input }] } },
22
- { type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: output }] }, toolUseResult: { stdout: output } },
23
- ]);
24
- const [call, result] = lines.map((line) => JSON.parse(line));
25
- return { input: call.message.content[0].input, content: result.message.content[0].content, toolUseResult: result.toolUseResult };
26
- }
27
- test("masks OpenAI-style provider keys", () => {
28
- const out = redactOne("export OPENAI=sk-abcdefghij0123456789XYZ done");
29
- assert.equal(out, `export OPENAI=${marker("openai_key")} done`);
30
- });
31
- test("masks Anthropic keys with their own label", () => {
32
- const out = redactOne("key sk-ant-api03-AAaaBBbbCCccDDddEEeeFF");
33
- assert.equal(out, `key ${marker("anthropic_key")}`);
34
- });
35
- test("does not treat an ordinary hyphenated word as a provider key", () => {
36
- const out = redactOne("the task-abcdefghij0123456789 identifier");
37
- assert.equal(out, "the task-abcdefghij0123456789 identifier");
38
- });
39
- test("masks every GitHub token prefix", () => {
40
- const body = "a".repeat(36);
41
- for (const prefix of ["ghp", "gho", "ghs", "ghu"]) {
42
- assert.equal(redactOne(`${prefix}_${body}`), marker("github_token"));
43
- }
44
- assert.equal(redactOne(`github_pat_${"b".repeat(24)}`), marker("github_token"));
45
- });
46
- test("masks AWS access key ids", () => {
47
- assert.equal(redactOne(`id AKIA${"A".repeat(16)}`), `id ${marker("aws_key")}`);
48
- assert.equal(redactOne(`id ASIA${"1".repeat(16)}`), `id ${marker("aws_key")}`);
49
- });
50
- test("masks Google API keys", () => {
51
- assert.equal(redactOne(`AIza${"x".repeat(35)}`), marker("google_key"));
52
- });
53
- test("masks Stripe secret keys, Slack and npm tokens, and an opaque bearer token", () => {
54
- const body = "a1B2c3D4e5F6g7H8i9J0k1L2";
55
- assert.equal(redactOne(`sk_live_${body}`), marker("stripe_key"));
56
- assert.equal(redactOne(`rk_test_${body}`), marker("stripe_key"));
57
- assert.equal(redactOne(`SLACK=xoxb-123456789012-1234567890123-${body}`), `SLACK=${marker("slack_token")}`);
58
- assert.equal(redactOne(`npm_${"a1".repeat(18)}`), marker("npm_token"));
59
- assert.equal(redactOne(`Authorization: Bearer ${body}`), `Authorization: Bearer ${marker("bearer_token")}`);
60
- assert.equal(redactOne(`Bearer ghp_${"a".repeat(36)}`), `Bearer ${marker("github_token")}`);
61
- assert.equal(redactOne("Bearer tokens travel in the Authorization header"), "Bearer tokens travel in the Authorization header");
62
- });
63
- test("masks a token by the prefix its service issues it under", () => {
64
- const body = "a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8s9T0u1V2w3X4y5Z6a7B8c9D0e1F2";
65
- const hex = "0123456789abcdef".repeat(4);
66
- const m = marker("service_token");
67
- const tokens = [
68
- `glpat-${body.slice(0, 20)}`,
69
- `gldt-${body.slice(0, 20)}`,
70
- `xapp-1-A0123456789-1234567890123-${hex}`,
71
- `hooks.slack.com/services/T00000000/B00000000/${body.slice(0, 24)}`,
72
- `discord.com/api/webhooks/123456789012345678/${body}`,
73
- `discordapp.com/api/webhooks/123456789012345678/${body}`,
74
- `gsk_${body.slice(0, 52)}`,
75
- `xai-${body}`,
76
- `ya29.${body}`,
77
- `1//0${body}`,
78
- `whsec_${body.slice(0, 32)}`,
79
- `SG.${body.slice(0, 22)}.${body.slice(0, 43)}`,
80
- `key-${hex.slice(0, 32)}`,
81
- `SK${hex.slice(0, 32)}`,
82
- `shpat_${hex.slice(0, 32)}`,
83
- `shpss_${hex.slice(0, 32)}`,
84
- `dop_v1_${hex}`,
85
- `hf_${body.slice(0, 34)}`,
86
- `pypi-${body}`,
87
- `rubygems_${hex.slice(0, 48)}`,
88
- `dckr_pat_${body.slice(0, 27)}`,
89
- `sbp_${hex.slice(0, 40)}`,
90
- `vercel_${body.slice(0, 24)}`,
91
- `ATATT3xFfGF0${body}`,
92
- `lin_api_${body.slice(0, 40)}`,
93
- `ntn_${body.slice(0, 46)}`,
94
- `secret_${body.slice(0, 43)}`,
95
- `figd_${body.slice(0, 40)}`,
96
- `r8_${body.slice(0, 37)}`,
97
- `sk_${hex.slice(0, 48)}`,
98
- `AGE-SECRET-KEY-1${body.slice(0, 58).toUpperCase()}`,
99
- ];
100
- for (const token of tokens) {
101
- const said = `your new key is ${token} (save it)`;
102
- assert.equal(redactOne(said), `your new key is ${m} (save it)`);
103
- assert.equal(redactOne(`your new key is ${m} (save it)`), `your new key is ${m} (save it)`, "a second pass changes nothing");
104
- }
105
- assert.equal(redactOne(`ghr_${body.slice(0, 36)}`), marker("github_token"));
106
- // What is left is an id, which is public: a fal.ai key's, a Telegram bot's, a Discord bot's in base64.
107
- for (const [id, rest] of [
108
- ["12345678-1234-4123-8123-123456789abc", `:${hex.slice(0, 32)}`],
109
- ["1234567890", `:AA${body.slice(0, 33)}`],
110
- [body.slice(0, 24), `.${body.slice(0, 6)}.${body.slice(0, 38)}`],
111
- ]) {
112
- assert.equal(redactOne(`token ${id}${rest} here`), `token ${id}${m} here`);
113
- }
114
- for (const text of [
115
- "hf_model_name",
116
- "gsk_short",
117
- "glob-pattern-with-a-very-long-name-here",
118
- "key-value",
119
- "key-0123456789",
120
- "monkey-0123456789abcdef0123456789abcdef",
121
- "secret_santa",
122
- "vercel_deployment_configuration_file",
123
- "r8_format",
124
- "SG.example.com",
125
- "see 1//comment here",
126
- "ATATT",
127
- "the-settings-page-component-a.module.the-settings-page-component-styles",
128
- "123e4567-e89b-12d3-a456-426614174000",
129
- ]) {
130
- assert.equal(redactOne(text), text);
131
- }
132
- });
133
- test("masks a whole PEM private key block", () => {
134
- const pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA\nabc\n-----END RSA PRIVATE KEY-----";
135
- assert.equal(redactOne(`before ${pem} after`), `before ${marker("private_key")} after`);
136
- });
137
- test("masks a truncated private key that never reaches its end marker", () => {
138
- const out = redactOne("-----BEGIN OPENSSH PRIVATE KEY-----\nMIIEowIBAAKCAQEA");
139
- assert.equal(out, marker("private_key"));
140
- });
141
- test("masks JWTs", () => {
142
- const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEjXk";
143
- assert.equal(redactOne(`Authorization: Bearer ${jwt}`), `Authorization: Bearer ${marker("jwt")}`);
144
- });
145
- test("masks only the password inside a connection string", () => {
146
- const out = redactOne("postgres://appuser:hunter2000@db.internal:5432/isy");
147
- assert.equal(out, `postgres://appuser:${marker("connection_string")}@db.internal:5432/isy`);
148
- });
149
- test("masks connection string passwords for every supported scheme", () => {
150
- for (const scheme of ["postgresql", "mysql", "mongodb", "redis", "amqp"]) {
151
- const out = redactOne(`${scheme}://user:secretpass@host/db`);
152
- assert.equal(out, `${scheme}://user:${marker("connection_string")}@host/db`);
153
- }
154
- });
155
- test("masks the password in a URL of any scheme, and an armored PGP private key", () => {
156
- const masked = marker("connection_string");
157
- assert.equal(redactOne("rediss://default:hunter2000@cache.internal:6380"), `rediss://default:${masked}@cache.internal:6380`);
158
- assert.equal(redactOne("amqps://app:secretpass@mq.internal/vhost"), `amqps://app:${masked}@mq.internal/vhost`);
159
- assert.equal(redactOne("curl https://admin:hunter2000@10.0.0.5:8443/api"), `curl https://admin:${masked}@10.0.0.5:8443/api`);
160
- assert.equal(redactOne("REDIS_URL=redis://:hunter2000@localhost:6379/0"), `REDIS_URL=redis://:${masked}@localhost:6379/0`);
161
- assert.equal(redactOne("redis-cli -u redis://:hunter2000@localhost:6379 ping"), `redis-cli -u redis://:${masked}@localhost:6379 ping`);
162
- assert.equal(redactOne("amqp://:hunter2000@mq/"), `amqp://:${masked}@mq/`);
163
- assert.equal(redactOne(`redis://:${masked}@localhost:6379/0`), `redis://:${masked}@localhost:6379/0`, "a second pass changes nothing");
164
- // A port is not a password: nothing in this URL is masked.
165
- assert.equal(redactOne("https://example.com:8080/a/@scope/pkg"), "https://example.com:8080/a/@scope/pkg");
166
- assert.equal(redactOne("https://:8080/a/@scope/pkg"), "https://:8080/a/@scope/pkg");
167
- const pgp = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nlQOYBF0abcd\n=AbCd\n-----END PGP PRIVATE KEY BLOCK-----";
168
- assert.equal(redactOne(`key:\n${pgp}\ndone`), `key:\n${marker("private_key")}\ndone`);
169
- });
170
- test("masks the value of a sensitive assignment and keeps the key", () => {
171
- assert.equal(redactOne('password = "hunter2000"'), `password = "${marker("assignment")}"`);
172
- assert.equal(redactOne("API_KEY: abcdefgh12345"), `API_KEY: ${marker("assignment")}`);
173
- assert.equal(redactOne("private-key=abcdefghijkl"), `private-key=${marker("assignment")}`);
174
- });
175
- test("masks a password passed on a command line, and nothing else on it", () => {
176
- const m = marker("assignment");
177
- const cut = [
178
- ["app --password Zq8vR2mK9xLp --port 80", `app --password ${m} --port 80`],
179
- [`app --password "Zq8vR2mK9xLp"`, `app --password "${m}"`],
180
- ["app --password 'Zq8vR2mK9xLp'", `app --password '${m}'`],
181
- ["app --token Zq8vR2mK9xLp", `app --token ${m}`],
182
- ["app --api-key Zq8vR2mK9xLp", `app --api-key ${m}`],
183
- ["app --apiKey Zq8vR2mK9xLp", `app --apiKey ${m}`],
184
- ["app --client-secret Zq8vR2mK9xLp", `app --client-secret ${m}`],
185
- ["Connect-App -Password Zq8vR2mK9xLp", `Connect-App -Password ${m}`],
186
- ["curl -u admin:Zq8vR2 http://127.0.0.1/", `curl -u admin:${m} http://127.0.0.1/`],
187
- ["curl -uadmin:Zq8vR2 http://127.0.0.1/", `curl -uadmin:${m} http://127.0.0.1/`],
188
- [`curl -u "admin:Zq8vR2" http://127.0.0.1/`, `curl -u "admin:${m}" http://127.0.0.1/`],
189
- ["curl --user admin:Zq8vR2 http://127.0.0.1/", `curl --user admin:${m} http://127.0.0.1/`],
190
- ["curl --user=admin:Zq8vR2 http://127.0.0.1/", `curl --user=admin:${m} http://127.0.0.1/`],
191
- [`curl -s "http://127.0.0.1/?a=1&b=2" -u admin:Zq8vR2`, `curl -s "http://127.0.0.1/?a=1&b=2" -u admin:${m}`],
192
- ["curl -U proxy:Zq8vR2 http://127.0.0.1/", `curl -U proxy:${m} http://127.0.0.1/`],
193
- ["curl -u me@example.com:Zq8vR2 http://127.0.0.1/", `curl -u ${marker("email")}:${m} http://127.0.0.1/`],
194
- ["mysql -h db -uroot -pZq8vR2 app", `mysql -h db -uroot -p${m} app`],
195
- ["mysqldump -pZq8vR2 app", `mysqldump -p${m} app`],
196
- ["sshpass -p Zq8vR2 ssh host", `sshpass -p ${m} ssh host`],
197
- ["sshpass -pZq8vR2 ssh host", `sshpass -p${m} ssh host`],
198
- ["redis-cli -h cache -a Zq8vR2 ping", `redis-cli -h cache -a ${m} ping`],
199
- ["docker login -u me -p Zq8vR2 ghcr.io", `docker login -u me -p ${m} ghcr.io`],
200
- // Short flags run together.
201
- ["curl -ku admin:hunter2000 https://x", `curl -ku admin:${m} https://x`],
202
- ["curl -sSu admin:hunter2000 https://x", `curl -sSu admin:${m} https://x`],
203
- ["curl -fsSLu admin:hunter2000 https://x", `curl -fsSLu admin:${m} https://x`],
204
- ["curl -sSuadmin:hunter2000 https://x", `curl -sSuadmin:${m} https://x`],
205
- ["curl -kU proxy:hunter2000 https://x", `curl -kU proxy:${m} https://x`],
206
- // A quoted value runs to its closing quote, spaces and the other quote included.
207
- ["curl -u 'admin:pa ss wo rd' https://x", `curl -u 'admin:${m}' https://x`],
208
- [`curl -u "admin:it's a pa ss" https://x`, `curl -u "admin:${m}" https://x`],
209
- ["curl --user 'admin:pa ss wo rd' https://x", `curl --user 'admin:${m}' https://x`],
210
- ["curl -ku 'admin:pa ss wo rd' https://x", `curl -ku 'admin:${m}' https://x`],
211
- ["curl -u admin:'pa ss wo rd' https://x", `curl -u admin:'${m}' https://x`],
212
- [`curl -u admin:"pa ss wo rd" https://x`, `curl -u admin:"${m}" https://x`],
213
- ["sshpass -p 'pa ss word 1234' ssh x", `sshpass -p '${m}' ssh x`],
214
- [`sshpass -p "pa ss word 1234" ssh x`, `sshpass -p "${m}" ssh x`],
215
- ["sshpass -p'pa ss word 1234' ssh x", `sshpass -p'${m}' ssh x`],
216
- ["docker login -u me -p 'pa ss word' ghcr.io", `docker login -u me -p '${m}' ghcr.io`],
217
- ["redis-cli -a 'pa ss word' ping", `redis-cli -a '${m}' ping`],
218
- ["mysql -h db -p'pa ss word' app", `mysql -h db -p'${m}' app`],
219
- ];
220
- for (const [text, expected] of cut) {
221
- assert.equal(redactOne(text), expected);
222
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
223
- }
224
- for (const text of [
225
- "app --password $APP_PASSWORD",
226
- `app --password "$APP_PASSWORD"`,
227
- "app --password-file /run/secrets/app",
228
- "docker login --password-stdin",
229
- "app --password --verbose",
230
- "app --token short",
231
- "app --max-tokens 1000000",
232
- `spawn("app", ["--password", password])`,
233
- "mkdir -p src/lib/deep",
234
- "ssh -p 2222 host",
235
- "mysql -p app",
236
- "docker run -p 8080:80 app",
237
- `curl -u "$API_USER:$API_PASS" http://127.0.0.1/`,
238
- `curl -ku "$API_USER:$API_PASS" http://127.0.0.1/`,
239
- `curl -u "admin:$API_PASS" http://127.0.0.1/`,
240
- `curl -u admin:"$API_PASS" http://127.0.0.1/`,
241
- `curl -u "admin:" http://127.0.0.1/`,
242
- `sshpass -p "$SSH_PASS" ssh x`,
243
- "sshpass -f secrets.txt ssh x",
244
- "curl -fsSL https://x",
245
- "curl -sS -o out.txt https://x",
246
- `curl -H "Accept: json" -X PUT https://x`,
247
- "curl -u admin: http://127.0.0.1/",
248
- "curl -u admin http://127.0.0.1/",
249
- "curl http://127.0.0.1/ && grep -p x",
250
- "tar -xf a.tar -C out -p",
251
- "redis-cli -h cache ping",
252
- ]) {
253
- assert.equal(redactOne(text), text);
254
- }
255
- });
256
- test("masks a credential in the shapes code and config files spell it in", () => {
257
- const m = marker("assignment");
258
- const value = "hG7xQ2pLm9Wz4Rt8";
259
- const cut = [
260
- // A camelCase name with a quoted value is data, whatever stands between them.
261
- [`clientSecret: "${value}"`, `clientSecret: "${m}"`],
262
- [`authToken: "${value}",`, `authToken: "${m}",`],
263
- [`accessToken: '${value}',`, `accessToken: '${m}',`],
264
- [`const dbPass = "${value}";`, `const dbPass = "${m}";`],
265
- [`userPassword = "${value}"`, `userPassword = "${m}"`],
266
- [`accessToken="${value}"`, `accessToken="${m}"`],
267
- [`this.authToken = "${value}";`, `this.authToken = "${m}";`],
268
- // What is code as often as data, unless what follows is in quotes.
269
- [`'password' => '${value}'`, `'password' => '${m}'`],
270
- [`"password" => "${value}"`, `"password" => "${m}"`],
271
- [`:password => "${value}"`, `:password => "${m}"`],
272
- [`'dbPassword' => '${value}'`, `'dbPassword' => '${m}'`],
273
- [`password := "${value}"`, `password := "${m}"`],
274
- [`password:="${value}"`, `password:="${m}"`],
275
- [`password == "${value}"`, `password == "${m}"`],
276
- [`password === "${value}"`, `password === "${m}"`],
277
- [`password != "${value}"`, `password != "${m}"`],
278
- [`if (password !== '${value}') {`, `if (password !== '${m}') {`],
279
- [`os.environ["DB_PASSWORD"] = "${value}"`, `os.environ["DB_PASSWORD"] = "${m}"`],
280
- [`os.environ['API_KEY']='${value}'`, `os.environ['API_KEY']='${m}'`],
281
- [`ENV["STRIPE_KEY"] = "${value}"`, `ENV["STRIPE_KEY"] = "${m}"`],
282
- // SQL.
283
- [`CREATE USER a WITH PASSWORD '${value}'`, `CREATE USER a WITH PASSWORD '${m}'`],
284
- [`ALTER USER a PASSWORD '${value}';`, `ALTER USER a PASSWORD '${m}';`],
285
- [`create user a with password '${value}'`, `create user a with password '${m}'`],
286
- [`GRANT ALL ON *.* TO a IDENTIFIED BY '${value}'`, `GRANT ALL ON *.* TO a IDENTIFIED BY '${m}'`],
287
- [`identified by "${value}"`, `identified by "${m}"`],
288
- // XML.
289
- [`<password>${value}</password>`, `<password>${m}</password>`],
290
- [`<Password>${value}</Password>`, `<Password>${m}</Password>`],
291
- [`<apiKey>${value}</apiKey>`, `<apiKey>${m}</apiKey>`],
292
- [`<clientSecret>${value}</clientSecret>`, `<clientSecret>${m}</clientSecret>`],
293
- // `.netrc`, on a line or over three.
294
- [`machine h login a password ${value}`, `machine h login a password ${m}`],
295
- [`machine h\n login a\n password ${value}\n`, `machine h\n login a\n password ${m}\n`],
296
- // A connection string, which runs on past the `;`.
297
- [`AccountKey=${value};`, `AccountKey=${m}`],
298
- [`DefaultEndpointsProtocol=https;AccountName=a;AccountKey=${value};EndpointSuffix=x`, `DefaultEndpointsProtocol=https;AccountName=a;AccountKey=${m}`],
299
- [`Pwd=${value};`, `Pwd=${m}`],
300
- // A signed URL, which a request's other parameters do not sign.
301
- [`https://a.blob.core.windows.net/c/b?sv=2024&sig=${value}&se=2025`, `https://a.blob.core.windows.net/c/b?sv=2024&sig=${m}&se=2025`],
302
- [`?sig=${value}`, `?sig=${m}`],
303
- [`https://b.s3.amazonaws.com/k?X-Amz-Signature=${value}`, `https://b.s3.amazonaws.com/k?X-Amz-Signature=${m}`],
304
- [`https://x/y?a=1&x-goog-signature=${value}#top`, `https://x/y?a=1&x-goog-signature=${m}#top`],
305
- [`https://x/y?Signature=${value}&Expires=1`, `https://x/y?Signature=${m}&Expires=1`],
306
- ];
307
- for (const [text, expected] of cut) {
308
- assert.equal(redactOne(text), expected);
309
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
310
- }
311
- for (const text of [
312
- "password => password.trim()",
313
- "token => token.length",
314
- "const check = secret => secret.value;",
315
- `password := os.Getenv("DB_PASSWORD")`,
316
- "token := jwt.New()",
317
- "password == confirmPassword",
318
- "password === confirmPassword",
319
- "token != null",
320
- "auth === undefined",
321
- "adminPassword = someVariable",
322
- "dbPass: process.env.DB_PASS",
323
- "authToken: getToken()",
324
- "this.authToken = req.headers.token;",
325
- 'clientSecret: "${process.env.CLIENT_SECRET}"',
326
- 'clientSecret: "$CLIENT_SECRET"',
327
- 'clientSecret: "<your secret>"',
328
- 'authToken: "short"',
329
- "CREATE USER a WITH PASSWORD :pw",
330
- "ALTER USER a PASSWORD $1",
331
- "IDENTIFIED BY password",
332
- "identified by $1",
333
- "the password 'short'",
334
- "<password>${DB_PASSWORD}</password>",
335
- "<password>{{ password }}</password>",
336
- "<password>short</password>",
337
- "<password></password>",
338
- "<dbname>production_database</dbname>",
339
- "https://api.telegram.org/bot<token>/getUpdates",
340
- "machine h login a password $NETRC_PASSWORD",
341
- "machine learning models login with password",
342
- "Pwd=$DB_PASSWORD;",
343
- "PWD=/srv/someone/projects",
344
- "pwd=/srv/someone/projects",
345
- "AccountKey=$STORAGE_KEY;",
346
- "https://x/y?design=abcdefghijklm",
347
- "https://x/y?a=1&assign=abcdefghijklm",
348
- "https://x/y?sig=short",
349
- "https://x/y?sig=$SIGNATURE",
350
- "https://x/y?signature=",
351
- "grep password src/auth/login.ts",
352
- ]) {
353
- assert.equal(redactOne(text), text);
354
- }
355
- });
356
- test("masks a password where the common clients take one on their command line", () => {
357
- const m = marker("assignment");
358
- const value = "hG7xQ2pLm9Wz";
359
- const cut = [
360
- [`mongosh -u root -p ${value} --authenticationDatabase admin`, `mongosh -u root -p ${m} --authenticationDatabase admin`],
361
- [`mongo -u root -p ${value} admin`, `mongo -u root -p ${m} admin`],
362
- [`mongodump -u root -p ${value} --db app`, `mongodump -u root -p ${m} --db app`],
363
- [`mongorestore -u root -p '${value}' dump/`, `mongorestore -u root -p '${m}' dump/`],
364
- [`cqlsh -u cassandra -p ${value} host`, `cqlsh -u cassandra -p ${m} host`],
365
- [`sqlcmd -S db -U sa -P ${value} -Q "select 1"`, `sqlcmd -S db -U sa -P ${m} -Q "select 1"`],
366
- [`sqlcmd -S db -U sa -P '${value}'`, `sqlcmd -S db -U sa -P '${m}'`],
367
- [`bcp t out f -S db -U sa -P ${value}`, `bcp t out f -S db -U sa -P ${m}`],
368
- [`sqlplus system/${value}@//db:1521/XE`, `sqlplus system/${m}`],
369
- [`sqlplus -S system/${value}@db`, `sqlplus -S system/${m}`],
370
- [`rabbitmqctl add_user app ${value}`, `rabbitmqctl add_user app ${m}`],
371
- [`rabbitmqctl change_password app ${value}`, `rabbitmqctl change_password app ${m}`],
372
- [`plink -pw ${value} root@host`, `plink -pw ${m} root@host`],
373
- [`pscp -pw ${value} a root@host:/`, `pscp -pw ${m} a root@host:/`],
374
- [`lftp -u user,${value} sftp://host`, `lftp -u user,${m} sftp://host`],
375
- [`smbclient //host/share -U user%${value}`, `smbclient //host/share -U user%${m}`],
376
- [`zip -P ${value} a.zip b`, `zip -P ${m} a.zip b`],
377
- [`unzip -P ${value} a.zip`, `unzip -P ${m} a.zip`],
378
- [`7z a -p${value} a.7z b`, `7z a -p${m} a.7z b`],
379
- [`ssh-keygen -t ed25519 -N '${value}' -f k`, `ssh-keygen -t ed25519 -N '${m}' -f k`],
380
- [`keytool -genkey -storepass ${value} -keypass ${value} -keystore k`, `keytool -genkey -storepass ${m} -keypass ${m} -keystore k`],
381
- [`jarsigner -storepass ${value} a.jar alias`, `jarsigner -storepass ${m} a.jar alias`],
382
- [`htpasswd -b .htpasswd admin ${value}`, `htpasswd -b .htpasswd admin ${m}`],
383
- [`htpasswd -bc .htpasswd admin ${value}`, `htpasswd -bc .htpasswd admin ${m}`],
384
- [`htpasswd -nb admin ${value}`, `htpasswd -nb admin ${m}`],
385
- [`curl -b "session=${value}" https://h`, `curl -b "${m}" https://h`],
386
- [`curl -s --cookie 'a=1; session=${value}' https://h`, `curl -s --cookie '${m}' https://h`],
387
- [`http -a admin:${value} https://h`, `http -a admin:${m} https://h`],
388
- [`xh -a admin:${value} https://h`, `xh -a admin:${m} https://h`],
389
- [`helm registry login ghcr.io -u a -p ${value}`, `helm registry login ghcr.io -u a -p ${m}`],
390
- [`az login -u a -p ${value}`, `az login -u a -p ${m}`],
391
- [`az login --service-principal -u id -p ${value} --tenant t`, `az login --service-principal -u id -p ${m} --tenant t`],
392
- [`twine upload -u a -p ${value} dist/x`, `twine upload -u a -p ${m} dist/x`],
393
- [`aws configure set aws_secret_access_key ${value}`, `aws configure set aws_secret_access_key ${m}`],
394
- [`npm config set //registry.npmjs.org/:_authToken ${value}`, `npm config set //registry.npmjs.org/:_authToken ${m}`],
395
- [`yarn config set npmAuthToken ${value}`, `yarn config set npmAuthToken ${m}`],
396
- [`setx API_KEY ${value}`, `setx API_KEY ${m}`],
397
- [`setenv API_KEY ${value}`, `setenv API_KEY ${m}`],
398
- [`set -x API_KEY ${value}`, `set -x API_KEY ${m}`],
399
- [`set -gx DB_PASSWORD ${value}`, `set -gx DB_PASSWORD ${m}`],
400
- [`vault login ${value}`, `vault login ${m}`],
401
- [`psql -c "ALTER USER a PASSWORD '${value}'"`, `psql -c "ALTER USER a PASSWORD '${m}'"`],
402
- [`mysql -e "CREATE USER a IDENTIFIED BY '${value}'"`, `mysql -e "CREATE USER a IDENTIFIED BY '${m}'"`],
403
- [`mysql -e "SET PASSWORD FOR 'a'@'%' = '${value}'"`, `mysql -e "SET PASSWORD FOR 'a'@'%' = '${m}'"`],
404
- // A script of the project's own says nothing by its flags, and what follows has to look generated.
405
- [`./run.sh -p ${value}`, `./run.sh -p ${m}`],
406
- [`./deploy --key ${value}`, `./deploy --key ${m}`],
407
- [`./deploy --pwd ${value}`, `./deploy --pwd ${m}`],
408
- [`bash ./scripts/deploy.sh --env prod -p "${value}"`, `bash ./scripts/deploy.sh --env prod -p "${m}"`],
409
- [`mongosh --username root -p ${value}`, `mongosh --username root -p ${m}`],
410
- [`lftp -u 'user,${value}' sftp://host`, `lftp -u 'user,${m}' sftp://host`],
411
- [`smbclient //host/share -U 'user%${value}'`, `smbclient //host/share -U 'user%${m}'`],
412
- [`smbclient //host/share ${value} -U user`, `smbclient //host/share ${m} -U user`],
413
- [`net use x: //host/share /user:admin ${value}`, `net use x: //host/share /user:admin ${m}`],
414
- [`http -a 'admin:${value}' https://h`, `http -a 'admin:${m}' https://h`],
415
- [`mysql -e "ALTER USER a IDENTIFIED WITH mysql_native_password BY '${value}'"`, `mysql -e "ALTER USER a IDENTIFIED WITH mysql_native_password BY '${m}'"`],
416
- [`ngrok config add-authtoken ${value}`, `ngrok config add-authtoken ${m}`],
417
- [`ngrok authtoken ${value}`, `ngrok authtoken ${m}`],
418
- [`gh secret set API_KEY --body ${value}`, `gh secret set API_KEY --body ${m}`],
419
- [`doctl auth init -t ${value}`, `doctl auth init -t ${m}`],
420
- [`wp config set DB_PASSWORD ${value}`, `wp config set DB_PASSWORD ${m}`],
421
- [`poetry config pypi-token.pypi ${value}`, `poetry config pypi-token.pypi ${m}`],
422
- [`poetry config http-basic.x user ${value}`, `poetry config http-basic.x user ${m}`],
423
- [`composer config http-basic.h user ${value}`, `composer config http-basic.h user ${m}`],
424
- [`security add-generic-password -a me -s svc -w ${value}`, `security add-generic-password -a me -s svc -w ${m}`],
425
- [`[Environment]::SetEnvironmentVariable("API_KEY", "${value}", "User")`, `[Environment]::SetEnvironmentVariable("API_KEY", "${m}", "User")`],
426
- [`sonar-scanner -Dsonar.login=${value}`, `sonar-scanner -Dsonar.login=${m}`],
427
- [`echo ${value} | docker login -u a --password-stdin`, `echo ${m} | docker login -u a --password-stdin`],
428
- [`echo "${value}" | gh auth login --with-token`, `echo "${m}" | gh auth login --with-token`],
429
- [`gh auth login --with-token <<< ${value}`, `gh auth login --with-token <<< ${m}`],
430
- [`wrangler secret put X <<< "${value}"`, `wrangler secret put X <<< "${m}"`],
431
- ];
432
- for (const [text, expected] of cut) {
433
- assert.equal(redactOne(text), expected);
434
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
435
- }
436
- for (const text of [
437
- "docker run -d --name mongo -p 27017:27017 mongo",
438
- "docker run --name cqlsh -p 9042:9042 cassandra",
439
- "mongosh -u root -p --authenticationDatabase admin",
440
- "mongosh --port 27017",
441
- "wget https://example.com/a.zip -P /tmp/downloads",
442
- "docker run -P nginx",
443
- "unzip -p a.zip file.txt",
444
- "gzip -P9 file",
445
- "7z x a.7z -o./out -y",
446
- "sqlcmd -S db -E",
447
- "sqlplus / as sysdba",
448
- "sqlplus -S /nolog",
449
- "sqlplus @scripts/run.sql",
450
- "ssh-keygen -t ed25519 -N '' -f k",
451
- `ssh-keygen -t ed25519 -N "" -f k`,
452
- `ssh-keygen -f k -N "$KEY_PASS"`,
453
- "htpasswd -c .htpasswd admin",
454
- "htpasswd -b .htpasswd admin $HTPASS",
455
- "keytool -list -storepass:env STORE_PASS -keystore k",
456
- "curl -b cookies.txt -c cookies.txt https://h",
457
- "http GET https://h -a",
458
- "wget http://h/a -a /var/log/wget.log",
459
- "helm registry login ghcr.io -u a --password-stdin",
460
- "az login",
461
- "az login --use-device-code",
462
- "aws configure set region eu-west-1",
463
- "npm config set registry https://registry.npmjs.org/",
464
- "setx PATH /usr/local/bin",
465
- "set -x",
466
- "set -x; echo token",
467
- "vault login",
468
- "vault login -method=github",
469
- "vault login $VAULT_TOKEN",
470
- "the vault login failed",
471
- "rabbitmqctl list_users",
472
- "./run.sh -p 8080",
473
- "./run.sh -p /tmp/out",
474
- "./deploy --key id_ed25519",
475
- "./deploy --key ~/.ssh/id_rsa",
476
- "./deploy --pwd /srv/app",
477
- "./gradlew build -p subprojects",
478
- "git log -p 3f49db0abc12",
479
- "mkdir -p a1b2c3d4e5f6",
480
- "./run.sh -p build/abc123def456/out",
481
- "docker run --name mongo -u 1000 -p 27017:27017 mongo",
482
- "htpasswd -n admin > users",
483
- "the keypass database",
484
- "the vault login succeeded",
485
- "cp a.7z -p dir",
486
- "echo $TOKEN | docker login -u a --password-stdin",
487
- `echo "$TOKEN" | gh auth login --with-token`,
488
- "cat key.txt | docker login --password-stdin",
489
- "echo hello | wc -c",
490
- "gh secret set API_KEY < key.txt",
491
- `gh pr create --body "fixes login"`,
492
- "smbclient -L host -U user",
493
- "net use",
494
- "ngrok http 8080",
495
- `grep foo <<< "$TEXT"`,
496
- "wc -c <<< hello",
497
- "security find-generic-password -a me -w",
498
- "doctl auth init",
499
- "poetry config virtualenvs.in-project true",
500
- "composer config minimum-stability dev",
501
- "wp config set WP_DEBUG true",
502
- ]) {
503
- assert.equal(redactOne(text), text);
504
- }
505
- });
506
- test("masks an assigned secret whole when it holds a dollar sign or a bracket", () => {
507
- const m = marker("assignment");
508
- const cut = [
509
- ["PASSWORD='abc$REST12345678'", `PASSWORD='${m}'`],
510
- ["PASSWORD=abcdefghij$TAILKEEP12", `PASSWORD=${m}`],
511
- ["export ADMIN_PASSWORD='abc$Qw7rTy2uIo9pAs4dFg1h'", `export ADMIN_PASSWORD='${m}'`],
512
- ["PASSWORD=abc(def)ghi12345", `PASSWORD=${m}`],
513
- ["PASSWORD='abc(defghij12345'", `PASSWORD='${m}'`],
514
- ["PASSWORD='$ecretPassw0rd'", `PASSWORD='${m}'`],
515
- [`{"password": "abc$def(ghi)12345"}`, `{"password": "${m}"}`],
516
- ["app --password abc$Qw7rTy2uIo9pAs4", `app --password ${m}`],
517
- [`{"accessToken":"abc$Qw7rTy2uIo9pAs4"}`, `{"accessToken":"${m}"}`],
518
- // With no space after the `=`, and in quotes, what is inside is the value.
519
- ["PASSWORD=Xy7{aB!9]kL2<pQ", `PASSWORD=${m}`],
520
- ["PASSWORD=abc{def}ghi12345", `PASSWORD=${m}`],
521
- ["PASSWORD=abc[def]ghi12345", `PASSWORD=${m}`],
522
- ["PASSWORD=abc<def>ghi12345", `PASSWORD=${m}`],
523
- ["API_KEY=ab]cdefghijklmnop", `API_KEY=${m}`],
524
- ["accessToken=abc{def}ghi12345", `accessToken=${m}`],
525
- ["PASSWORD='abc{def}ghi12345'", `PASSWORD='${m}'`],
526
- [`PASSWORD="abc{def}ghi12345"`, `PASSWORD="${m}"`],
527
- [`password = "abc[def]ghi12345"`, `password = "${m}"`],
528
- ["export DB_PASSWORD='p@ss[w0rd]{123}'", `export DB_PASSWORD='${m}'`],
529
- [`PASSWORD="it's-a-secret-1234"`, `PASSWORD="${m}"`],
530
- [`PASSWORD='say-"hi"-secret-1234'`, `PASSWORD='${m}'`],
531
- [`{"accessToken":"abc{def}ghi12345"}`, `{"accessToken":"${m}"}`],
532
- [`app --password "abc{def}ghi12345"`, `app --password "${m}"`],
533
- // A `$` a shell would not expand: a hash in JSON, a password that opens with one.
534
- [`"password": "$2b$10$abcdefghijklmnopqrstuv"`, `"password": "${m}"`],
535
- [`SECRET="$tr0ngPassw0rd99"`, `SECRET="${m}"`],
536
- [`SECRET="$Tr0ngPassw0rd99"`, `SECRET="${m}"`],
537
- // What closes around the assignment stays out of it.
538
- ["{PASSWORD=abcdefgh12345}", `{PASSWORD=${m}}`],
539
- ["[token=abcdefgh12345] done", `[token=${m}] done`],
540
- ["<input password=abcdefgh12345>", `<input password=${m}>`],
541
- ];
542
- for (const [text, expected] of cut) {
543
- assert.equal(redactOne(text), expected);
544
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
545
- }
546
- for (const text of [
547
- "PASSWORD=$PASSWORD",
548
- `PASSWORD="$DB_PASS"`,
549
- "TOKEN=$(cat f)",
550
- "password=prefix${X}",
551
- "password = hash(input)",
552
- "token: foo(bar),",
553
- "secret: env('SECRET')",
554
- "password = md5($x);",
555
- `SECRET="$DB_PASS_2"`,
556
- `SECRET="\${DB_PASS}"`,
557
- `SECRET="$(cat f1)"`,
558
- `SECRET="$HOME/s3cret/x1"`,
559
- `SECRET="$secret"`,
560
- "PASSWORD=$tr0ngPassw0rd99",
561
- "token = tokens[index]",
562
- "token: tokens[index],",
563
- "password = values<string>",
564
- "if (token==tokens[index]) {}",
565
- "PASSWORD=<your-password>",
566
- "PASSWORD={{ password }}",
567
- "PASSWORD=[hidden]",
568
- `"password": "<your-password>"`,
569
- `"token": "{{ token }}"`,
570
- `"token": "{token}"`,
571
- `"password": "[hidden]"`,
572
- ]) {
573
- assert.equal(redactOne(text), text);
574
- }
575
- // A call's long name goes, and what it is called with stays.
576
- assert.equal(redactOne("token: getToken()"), `token: ${m}()`);
577
- });
578
- test("reads a chain of calls as one call, and a bracket with a letter after it as a password", () => {
579
- const m = marker("assignment");
580
- for (const text of [
581
- "password = hash(input).digest('hex')",
582
- "password = hash(input).digest();",
583
- "password=hash(input).digest()",
584
- "secret: foo.bar(baz).qux()",
585
- "secret = foo.bar(baz).qux(),",
586
- "password = input().strip()",
587
- "password = String(input).trim()",
588
- "password = foo().bar",
589
- "password = foo().bar.baz()",
590
- "token = foo(bar(baz))",
591
- "token = foo(bar(baz)),",
592
- "wrap(password = hash(input))",
593
- "wrap(password = hash(input));",
594
- "token = foo(x)!",
595
- "token = foo(x)?.bar",
596
- "token = foo(x)[0]",
597
- "token = foo(x)+1",
598
- ]) {
599
- assert.equal(redactOne(text), text);
600
- }
601
- // A call's long name goes, and what it is called with stays.
602
- const cut = [
603
- ["token = getToken().trim()", `token = ${m}().trim()`],
604
- ["token=getToken().trim()", `token=${m}().trim()`],
605
- ["token: getToken().trim(),", `token: ${m}().trim(),`],
606
- ["const token = jwt.sign(payload).toString();", `const token = ${m}(payload).toString();`],
607
- ["const token = jwt.sign(payload,key).toString();", `const token = ${m}(payload,key).toString();`],
608
- ["token: this.auth.getToken().pipe(take(1)),", `token: ${m}().pipe(take(1)),`],
609
- ["password = generate(16).toString(36).slice(2)", `password = ${m}(16).toString(36).slice(2)`],
610
- [`secret = Buffer.from(x).toString("hex")`, `secret = ${m}(x).toString("hex")`],
611
- ["token: useToken().value", `token: ${m}().value`],
612
- ["PASSWORD=abc(def)ghi12345", `PASSWORD=${m}`],
613
- ["password = abc(def)ghi12345", `password = ${m}`],
614
- ["PASSWORD=abc(def)ghi(j)k", `PASSWORD=${m}`],
615
- [`password = "hash(input).digest"`, `password = "${m}"`],
616
- ];
617
- for (const [text, expected] of cut) {
618
- assert.equal(redactOne(text), expected);
619
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
620
- }
621
- });
622
- test("masks a value under PASS, PWD and the other names a credential goes by, not under words that hold them", () => {
623
- const m = marker("assignment");
624
- const cut = [
625
- ["DB_PASS=Zq8vR2mK9xLp some-command", `DB_PASS=${m} some-command`],
626
- ["SMTP_PASS=Zq8vR2mK9xLp", `SMTP_PASS=${m}`],
627
- ["db-pass: Zq8vR2mK9xLp", `db-pass: ${m}`],
628
- ["pass=Zq8vR2mK9xLp", `pass=${m}`],
629
- ["PASS=Zq8vR2mK9xLp", `PASS=${m}`],
630
- ["dbPass=Zq8vR2mK9xLp", `dbPass=${m}`],
631
- [`{"dbPass":"Zq8vR2mK9xLp"}`, `{"dbPass":"${m}"}`],
632
- ["DB_PWD=Zq8vR2mK9xLp", `DB_PWD=${m}`],
633
- ["MYSQL_PWD=Zq8vR2mK9xLp mysql app", `MYSQL_PWD=${m} mysql app`],
634
- ["Server=db;Uid=sa;Pwd=Zq8vR2mK9xLp", `Server=db;Uid=sa;Pwd=${m}`],
635
- ["db-pwd=Zq8vR2mK9xLp", `db-pwd=${m}`],
636
- ["jdbc.pwd=Zq8vR2mK9xLp", `jdbc.pwd=${m}`],
637
- ["AUTH=Zq8vR2mK9xLp", `AUTH=${m}`],
638
- ["CREDENTIALS=Zq8vR2mK9xLp", `CREDENTIALS=${m}`],
639
- ["SERVICE_CREDENTIAL=Zq8vR2mK9xLp", `SERVICE_CREDENTIAL=${m}`],
640
- ["GPG_PASSPHRASE=Zq8vR2mK9xLp", `GPG_PASSPHRASE=${m}`],
641
- ["SESSION_COOKIE=Zq8vR2mK9xLp", `SESSION_COOKIE=${m}`],
642
- [`{"auth":"Zq8vR2mK9xLp"}`, `{"auth":"${m}"}`],
643
- ];
644
- for (const [text, expected] of cut) {
645
- assert.equal(redactOne(text), expected);
646
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
647
- }
648
- for (const text of [
649
- "compass=northbynorthwest",
650
- "COMPASS=northbynorthwest",
651
- "encompass: northbynorthwest",
652
- "bypass: northbynorthwest",
653
- "BYPASS=northbynorthwest",
654
- "byPass=northbynorthwest",
655
- "passes: northbynorthwest",
656
- "PWD=/srv/app/current",
657
- "OLDPWD=/srv/app/current",
658
- "--pwd /srv/app/current",
659
- "./pwd=/srv/app/current",
660
- `{"pwd":"/srv/app/current"}`,
661
- "AUTHOR=northbynorthwest",
662
- "author: northbynorthwest",
663
- "AUTH_URL=https://example.com/login",
664
- "authenticated: northbynorthwest",
665
- `credentials: "include"`,
666
- "cookies: northbynorthwest",
667
- ]) {
668
- assert.equal(redactOne(text), text);
669
- }
670
- });
671
- test("one of these words alone, spaced from a plain word, names nothing: test output, prose and code stay", () => {
672
- for (const text of [
673
- "--- PASS: TestParserKeepsHeredocs (0.01s)",
674
- " --- PASS: TestV2Parser/heredoc_case (0.00s)",
675
- "PASS: src/foo/bar.test.ts",
676
- "PASS: test-suite-12345678",
677
- "First pass: everything compiles",
678
- "tests pass: everything green",
679
- "pass: undefined,",
680
- "pass = compute_something(x)",
681
- "pass: process.env.SMTP_PASS",
682
- "auth: requireAuthentication,",
683
- "const auth = getAuthentication();",
684
- "export const auth = betterAuth({",
685
- "Auth: Required",
686
- "auth:\n required_everywhere",
687
- "credential = getCredential()",
688
- "credentials: path/to/file.json",
689
- "cookie: SerializedCookie | undefined,",
690
- "const cookie = parseCookie(req);",
691
- "cookie = req.headers.cookie;",
692
- "const cookie = req.headers.cookie ?? '';",
693
- "if (cookie == null) return next();",
694
- "if (cookie === undefined) return next();",
695
- "Cookie: required for the session flow",
696
- ]) {
697
- assert.equal(redactOne(text), text);
698
- }
699
- const m = marker("assignment");
700
- const cut = [
701
- ["pass: hunter2000", `pass: ${m}`],
702
- [`pass: "sunshine"`, `pass: "${m}"`],
703
- ["pass: 'sunshine'", `pass: '${m}'`],
704
- ["pass=sunshine", `pass=${m}`],
705
- ["PASS=sunshine", `PASS=${m}`],
706
- ["smtp_pass: sunshine", `smtp_pass: ${m}`],
707
- ["auth: dXNlcjpwYXNz1", `auth: ${m}`],
708
- [`"auth": "dXNlcjpwYXNz"`, `"auth": "${m}"`],
709
- ["AUTH=dXNlcjpwYXNz", `AUTH=${m}`],
710
- [`credentials: "dXNlcjpwYXNz"`, `credentials: "${m}"`],
711
- ["cookie: abcdefgh1", `cookie: ${m}`],
712
- [`"cookie": "abcdefgh"`, `"cookie": "${m}"`],
713
- ];
714
- for (const [text, expected] of cut) {
715
- assert.equal(redactOne(text), expected);
716
- }
717
- });
718
- test("a NAME_KEY goes when what it holds looks generated, and stays when it holds a name", () => {
719
- const m = marker("assignment");
720
- const value = "hG7xQ2pLm9Wz4Rt8";
721
- for (const name of ["STRIPE_KEY", "APP_KEY", "ENCRYPTION_KEY", "SIGNING_KEY", "MASTER_KEY", "OPENAI_KEY", "ANTHROPIC_KEY", "SENDGRID_KEY", "HMAC_KEY", "SUPABASE_SERVICE_ROLE_KEY", "KEY"]) {
722
- const cut = [
723
- [`${name}=${value}`, `${name}=${m}`],
724
- [`export ${name}=${value}`, `export ${name}=${m}`],
725
- [`${name}: ${value}`, `${name}: ${m}`],
726
- [`"${name}": "${value}"`, `"${name}": "${m}"`],
727
- [`kubectl create secret generic s --from-literal=${name}=${value}`, `kubectl create secret generic s --from-literal=${name}=${m}`],
728
- [`process.env.${name} = "${value}"`, `process.env.${name} = "${m}"`],
729
- ];
730
- for (const [text, expected] of cut) {
731
- assert.equal(redactOne(text), expected);
732
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
733
- }
734
- }
735
- assert.equal(redactOne("APP_KEY=base64:Zq8vR2mK9xLpWq7="), `APP_KEY=${m}`);
736
- for (const text of [
737
- "SORT_KEY=name",
738
- "SORT_KEY=created_at",
739
- "PRIMARY_KEY=customer_identifier",
740
- "CACHE_KEY=organizationSettings",
741
- "SSH_KEY=~/.ssh/id_ed25519",
742
- "PARTITION_KEY: $TENANT_ID",
743
- "APP_KEY=${APP_KEY}",
744
- `FOREIGN_KEY = "organization_identifier"`,
745
- "MONKEY=hG7xQ2pLm9Wz4Rt8",
746
- "sort_key=hG7xQ2pLm9Wz4Rt8",
747
- ]) {
748
- assert.equal(redactOne(text), text);
749
- }
750
- // A field holds it the same as a line of text does.
751
- const { input } = throughCall("mcp__x__deploy", { env: { STRIPE_KEY: value, SORT_KEY: "created_at_desc" } }, "ok");
752
- assert.deepEqual(input.env, { STRIPE_KEY: m, SORT_KEY: "created_at_desc" });
753
- });
754
- test("masks what an Authorization or Cookie header carries, whatever its scheme or length", () => {
755
- const auth = marker("authorization");
756
- const bearer = marker("bearer_token");
757
- const cookie = marker("cookie");
758
- const cut = [
759
- ["curl -H 'Authorization: Bearer abcdefghijklmnop' http://127.0.0.1/", `curl -H 'Authorization: Bearer ${auth}' http://127.0.0.1/`],
760
- ["curl -H 'Authorization: Bearer Zq8vR2mK9xLpWq7E' http://127.0.0.1/", `curl -H 'Authorization: Bearer ${bearer}' http://127.0.0.1/`],
761
- ["Authorization: Token abcdefghijkl", `Authorization: Token ${auth}`],
762
- ["authorization: Basic dXNlcjpwYXNz", `authorization: Basic ${auth}`],
763
- ["Proxy-Authorization: Basic dXNlcjpwYXNz", `Proxy-Authorization: Basic ${auth}`],
764
- [`"Authorization": "Basic dXNlcjpwYXNz"`, `"Authorization": "Basic ${auth}"`],
765
- [`headers: { Authorization: \`Basic dXNlcjpwYXNz\` }`, `headers: { Authorization: \`Basic ${auth}\` }`],
766
- ["Authorization: dXNlcjpwYXNz", `Authorization: ${marker("assignment")}`],
767
- ["Bearer Zq8vR2mK", `Bearer ${bearer}`],
768
- ["Cookie: sid=Zq8vR2mK9xLp; theme=dark", `Cookie: ${cookie}`],
769
- ["Set-Cookie: sid=Zq8vR2mK9xLp; Path=/; HttpOnly", `Set-Cookie: ${cookie}`],
770
- ["curl -H 'Cookie: sid=Zq8vR2mK9xLp; theme=dark' http://127.0.0.1/", `curl -H 'Cookie: ${cookie}' http://127.0.0.1/`],
771
- [`{"cookie": "sid=Zq8vR2mK9xLp; theme=dark"}`, `{"cookie": "${cookie}"}`],
772
- ];
773
- for (const [text, expected] of cut) {
774
- assert.equal(redactOne(text), expected);
775
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
776
- }
777
- for (const text of [
778
- "Bearer $TOKEN",
779
- "Bearer ${token}",
780
- "Bearer <token>",
781
- "Bearer authentication is what the docs call it",
782
- "Basic authentication is off",
783
- "Authorization: Bearer $TOKEN",
784
- "Authorization: Bearer ${token}",
785
- "Authorization: Bearer <token>",
786
- `curl -H "Authorization: Token $API_TOKEN" http://127.0.0.1/`,
787
- "the Authorization header carries it",
788
- "Cookie: $COOKIE",
789
- "Cookie: ${cookie}",
790
- "Cookie: <value>",
791
- "a cookie jar",
792
- ]) {
793
- assert.equal(redactOne(text), text);
794
- }
795
- const input = { headers: { Authorization: "Basic dXNlcjpwYXNz", Cookie: "sid=Zq8vR2mK9xLp; theme=dark" } };
796
- assert.deepEqual(throughCall("mcp__http__fetch", input, "plain output").input, {
797
- headers: { Authorization: marker("assignment"), Cookie: marker("assignment") },
798
- });
799
- });
800
- test("a cookie's value does not take a name and leave what the name holds", () => {
801
- const value = "Zq8vR2mK9xLpWq7E";
802
- const cookie = marker("cookie");
803
- const assignment = marker("assignment");
804
- const cut = [
805
- [`curl --cookie=k=v https://x && export TOKEN='${value}'`, `curl --cookie=${cookie}'${assignment}'`],
806
- [`curl --cookie=k=v --password='${value}' https://x`, `curl --cookie=${cookie}'${assignment}' https://x`],
807
- [`Cookie: k=v --password='${value}'`, `Cookie: ${cookie}'${assignment}'`],
808
- [`--cookie=k=v; export TOKEN='${value}'`, `--cookie=${cookie}'${assignment}'`],
809
- [`Cookie: a=b; token = "${value}"`, `Cookie: ${cookie}"${assignment}"`],
810
- [`Cookie: a=b db.password='${value}'`, `Cookie: ${cookie}'${assignment}'`],
811
- [`Cookie: a=b mysql://u:'${value}'@h`, `Cookie: ${cookie}`],
812
- // The name rules read `Cookie` as a name too and take the first pair, so the rest is left to this one.
813
- [`Cookie: sid=${value}; theme=${value}`, `Cookie: ${cookie}`],
814
- [`Cookie: token=${value}; theme=${value}`, `Cookie: ${cookie}`],
815
- ];
816
- for (const [text, expected] of cut) {
817
- assert.equal(redactOne(text), expected);
818
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
819
- }
820
- });
821
- test("a value that ends on a secret's name takes what that name holds too", () => {
822
- const value = "Zq8vR2mK9xLpWq7E";
823
- const m = marker("assignment");
824
- const cut = [
825
- [`curl -i https://x | grep 'Set-Cookie: session_token="${value}"; Path=/'`, `curl -i https://x | grep 'Set-Cookie: ${marker("cookie")}'`],
826
- [`curl -H 'Authorization: access_token="${value}"' https://x`, `curl -H 'Authorization: ${m}' https://x`],
827
- [`export DB_PASS=DB_PASSWORD="${value}"`, `export DB_PASS=${m}`],
828
- [`curl -H 'Authorization: OAuth oauth_token="${value}", oauth_nonce="n"' https://x`, `curl -H 'Authorization: OAuth ${marker("authorization")}, oauth_nonce="n"' https://x`],
829
- [`auth: password="${value}"`, `auth: ${m}`],
830
- [`db_credentials:\n password: ${value}`, `db_credentials:\n ${m}`],
831
- [`db_auth: password = ${value}`, `db_auth: ${m}`],
832
- [`export TOKEN='abc12345&"api_key": "${value}"'`, `export TOKEN='${m}'`],
833
- [`--token auth-cookie: api_key= -p${value}`, `--token ${m} ${m}`],
834
- [`--password session.cookie=password= k=${value}`, `--password ${m}${m}`],
835
- [`--token x-cookie: ${"x".repeat(200)}token= l=${value}`, `--token ${m} ${m}`],
836
- // A name with no separator of its own after it holds nothing, and the next pair is its own.
837
- [`{"auth": "password", "token": "${value}"}`, `{"auth": "${m}", "token": "${m}"}`],
838
- ];
839
- for (const [text, expected] of cut) {
840
- assert.equal(redactOne(text), expected);
841
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
842
- }
843
- });
844
- test("a quote after a name's separator opens what the name holds, and the run goes on into it", () => {
845
- const value = "Zq8vR2mK9xLpWq7E";
846
- const m = marker("assignment");
847
- const cut = [
848
- [`token='access_token: "auth_token="${value}""'`, `token='${m}"'`],
849
- [`password=api_token: "abc12345,api_key="${value}"`, `password=${m}`],
850
- [`Authorization: OAuth oauth_consumer_key="ck_abcdefgh,oauth_token="${value}"`, `Authorization: OAuth ${marker("authorization")}`],
851
- [`Cookie: _authToken":"auth_token="${value})`, `Cookie: ${m})`],
852
- [`Cookie: secret=refresh_token="my-token="${value}"`, `Cookie: ${m}`],
853
- [`token=SECRET_KEY_BASE'='api-key='${value}`, `token=${m}`],
854
- ];
855
- for (const [text, expected] of cut) {
856
- assert.equal(redactOne(text), expected);
857
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
858
- }
859
- });
860
- test("a bearer token that takes a name goes on over what the name holds", () => {
861
- const value = "Zq8vR2mK9xLpWq7E";
862
- const m = marker("bearer_token");
863
- const cut = [
864
- [`TOKEN_TYPE=Bearer\nAWS_S3_SECRET=${value}`, `TOKEN_TYPE=Bearer\n${m}`],
865
- [`Authorization: Bearer x1_password="${value}"`, `Authorization: Bearer ${m}`],
866
- [`Bearer 2api-key:${value}`, `Bearer ${m}`],
867
- [`bearer 2API_KEY==${value}'`, `bearer ${m}'`],
868
- ];
869
- for (const [text, expected] of cut) {
870
- assert.equal(redactOne(text), expected);
871
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
872
- }
873
- });
874
- test("a token shape open at its end that takes a name goes on over what the name holds", () => {
875
- const value = "Zq8vR2mK9xLpWq7E";
876
- const cut = [
877
- [`ghp_${"a".repeat(36)}passwd=${value}`, marker("github_token")],
878
- [`ghp_${"a".repeat(36)}2token:${value}]`, `${marker("github_token")}]`],
879
- [`ghp_${"a".repeat(36)}2api-key==${value},`, marker("github_token")],
880
- [`glpat-${"a".repeat(20)}-apikey= ${value}`, marker("service_token")],
881
- [`xapp-1-${"a".repeat(20)}-api_key= ${value}}`, `${marker("service_token")}}`],
882
- [`glpat-${"a".repeat(20)}TOKEN=\`${value}]`, `${marker("service_token")}]`],
883
- // A name that ends otherwise, taken by the shape whole or in part.
884
- [`secret: glpat-${"a".repeat(20)}2auth=${value}`, `secret: ${marker("service_token")}`],
885
- [`GITLAB_TOKEN=glpat-${"a".repeat(20)}-pwd:${value}`, `GITLAB_TOKEN=${marker("service_token")}`],
886
- [`"secret": xapp-1-${value}zpqABCDEFGHIJTOKEN_PROD:=${value}_`, `"secret": ${marker("service_token")}`],
887
- // A value before the token stops at its marker, so the token is what takes the name's value.
888
- [`accessToken='accessToken': '${value},xapp-1-${value}zpqABCDEFGHIJpwd:=${value}`, `accessToken='${marker("assignment")}${marker("service_token")}`],
889
- [`Bearer t0ken99_secret=x-api-key=${value}/+,xapp-1-${value}ABCDEFGHIJauth-cookie=:${value}`, `Bearer ${marker("bearer_token")}${marker("service_token")}`],
890
- [`Cookie: password=${value}_,glpat-${value}abcdefghij_DB_PASS=${value}zpq`, `Cookie: ${marker("assignment")}${marker("service_token")}`],
891
- // The name's value stops short of a bearer token, which its own rule cuts after the token's.
892
- [`sk_${"0a".repeat(24)}.Cookie : x\`authorization:Bearer ${value}`, `${marker("service_token")}Bearer ${marker("bearer_token")}`],
893
- [`sk_${"0a".repeat(24)}-accessToken:\t\`${value}&Proxy-Authorization=Bearer ${value}`, `${marker("service_token")}Bearer ${marker("bearer_token")}`],
894
- ];
895
- for (const [text, expected] of cut) {
896
- assert.equal(redactOne(text), expected);
897
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
898
- }
899
- });
900
- test("a backtick after a separator in an Authorization credential opens a value, and a camelCase name's value goes on over a name it ends on", () => {
901
- const value = "Zq8vR2mK9xLpWq7E";
902
- const a = marker("authorization");
903
- const m = marker("assignment");
904
- const cut = [
905
- [`Authorization: Basic secret==\`${value}\``, `Authorization: Basic ${a}`],
906
- [`Authorization: Basic SECRET=:credentials=\`${value}'`, `Authorization: Basic ${a}'`],
907
- ["headers: { Authorization: `Basic dXNlcjpwYXNz` }", `headers: { Authorization: \`Basic ${a}\` }`],
908
- // Padding is no name's separator, a backtick no end to the value, and a name that ends the value takes what it holds with it.
909
- [`curl -H "Authorization: Basic secret=:credentials=\`${value}==\`" https://x`, `curl -H "Authorization: Basic ${a}" https://x`],
910
- [`Authorization: Basic password=x,secret_value=\`${value}=\``, `Authorization: Basic ${a}`],
911
- [`Authorization: Token secret=refresh_token=\`x-api-key=${value}\`,${value}`, `Authorization: Token ${a}`],
912
- [`Authorization: Token password=\`${value},auth=\`${value}\``, `Authorization: Token ${a}`],
913
- [`curl -H "Authorization: Token password=\`${value},pass=\`${value}\`"`, `curl -H "Authorization: Token ${a}"`],
914
- [`Authorization: Basic PASSWORD=\`Pwd="${value}"`, `Authorization: Basic ${a}"`],
915
- [`Authorization: Basic PASSWORD=\`Pwd = ${value}`, `Authorization: Basic ${a}`],
916
- [`apiKeyProd: 'accessToken": '${value}'`, `apiKeyProd: '${m}`],
917
- [`adminPassword'='accessToken": "${value}"`, `adminPassword'='${m}`],
918
- [`export _authToken='accessToken": "${value}"`, `export _authToken='${m}`],
919
- ];
920
- for (const [text, expected] of cut) {
921
- assert.equal(redactOne(text), expected);
922
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
923
- }
924
- });
925
- test("a value with a token in it goes as a whole, the rest around the token's marker too", () => {
926
- const value = "Zq8vR2mK9xLpWq7E";
927
- const m = marker("assignment");
928
- const cut = [
929
- [`my_SECRET_KEY" = glpat-${value}abcdefghij,${value}_ `, `my_SECRET_KEY" = ${m} `],
930
- [`private_key: glpat-${value}abcdefghij&\`${value} `, `private_key: ${m} `],
931
- [`password: \`hf_${value}bbbbbbbbbbbbbbbbbbbbbbbbb\`\`&glpat-${value}/+abcdefghij-token `, `password: ${m} `],
932
- [`--api-key ghp_${"a".repeat(36)},${value}`, `--api-key ${m}`],
933
- // The same where a value runs on over a name into one (RUNS_ON).
934
- [`password=abc_secret: ghp_${"a".repeat(36)},${value}`, `password=${m}`],
935
- // A value that ends on its marker was cut whole, and stays as it is.
936
- [`token=${marker("jwt")}`, `token=${marker("jwt")}`],
937
- [`"token": "${marker("service_token")}"`, `"token": "${marker("service_token")}"`],
938
- ];
939
- for (const [text, expected] of cut) {
940
- assert.equal(redactOne(text), expected);
941
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
942
- }
943
- });
944
- test("a cookie's marker goes on only into pairs, and leaves a flag, a URL or the next command to their rules", () => {
945
- const value = "Zq8vR2mK9xLpWq7E";
946
- const m = marker("assignment");
947
- const cut = [
948
- [`curl --cookie=cookies.txt -u "bob:${value}" https://x`, `curl --cookie=${m} -u "bob:${m}" https://x`],
949
- [`curl --cookie=cookies.txt --user "bob:${value}" https://x`, `curl --cookie=${m} --user "bob:${m}" https://x`],
950
- [`curl --cookie=cookies.txt -b "k=${value}" https://x`, `curl --cookie=${m} -b "${m}" https://x`],
951
- [`curl --cookie=cookies.txt https://x/?a=b -u "bob:${value}"`, `curl --cookie=${m} https://x/?a=b -u "bob:${m}"`],
952
- [`curl --cookie=cookies.txt https://x && sshpass -p "${value}" ssh h`, `curl --cookie=${m} https://x && sshpass -p "${m}" ssh h`],
953
- [`curl --cookie=cookies.txt https://x && mysql -u root -p"${value}" db`, `curl --cookie=${m} https://x && mysql -u root -p"${m}" db`],
954
- [`curl --cookie=session_id https://x && docker login -u a -p "${value}" r`, `curl --cookie=${m} https://x && docker login -u a -p "${m}" r`],
955
- [`curl --cookie=session_id https://x && git reset --hard`, `curl --cookie=${m} https://x && git reset --hard`],
956
- // The pairs after a marker are still the cookie's.
957
- [`Cookie: session_token=${value}; theme=${value}`, `Cookie: ${marker("cookie")}`],
958
- [`Cookie: a@b.co=${value}; s=${value}`, `Cookie: ${marker("cookie")}`],
959
- [`Set-Cookie: session_token=${value}; Path=/; HttpOnly`, `Set-Cookie: ${marker("cookie")}`],
960
- ];
961
- for (const [text, expected] of cut) {
962
- assert.equal(redactOne(text), expected);
963
- assert.equal(redactOne(expected), expected, "a second pass changes nothing");
964
- }
965
- });
966
- test("leaves short assignment values alone", () => {
967
- assert.equal(redactOne("password = short"), "password = short");
968
- });
969
- test("masks a snake_case key but not a camelCase variable in code", () => {
970
- assert.equal(redactOne("access_token = AbCdEf1234567890"), `access_token = ${marker("assignment")}`);
971
- assert.equal(redactOne("const adminPassword = someLongVariableName"), "const adminPassword = someLongVariableName");
972
- });
973
- test("masks a secret's name however a file spells it: quoted, with affixes, glued", () => {
974
- const masked = marker("assignment");
975
- const cases = [
976
- ['{"password": "hunter2000"}', `{"password": "${masked}"}`],
977
- ['"client_secret": "abcdefgh1234"', `"client_secret": "${masked}"`],
978
- ['{"accessToken":"abcdefgh1234"}', `{"accessToken":"${masked}"}`],
979
- ["SECRET_KEY_BASE=abcdef0123456789", `SECRET_KEY_BASE=${masked}`],
980
- ["STRIPE_SECRET_KEY=whsec_abcdefgh", `STRIPE_SECRET_KEY=${masked}`],
981
- ["AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG", `AWS_SECRET_ACCESS_KEY=${masked}`],
982
- ["aws_secret_access_key = wJalrXUtnFEMI/K7MDENG", `aws_secret_access_key = ${masked}`],
983
- ["PGPASSWORD=hunter2000 psql -h db", `PGPASSWORD=${masked} psql -h db`],
984
- ["//registry.npmjs.org/:_authToken=abcdef123456", `//registry.npmjs.org/:_authToken=${masked}`],
985
- ["curl -H 'x-api-key: abcdef123456'", `curl -H 'x-api-key: ${masked}'`],
986
- ];
987
- for (const [input, expected] of cases)
988
- assert.equal(redactOne(input), expected, input);
989
- });
990
- test("leaves what is said about a secret alone, and code that only names one", () => {
991
- for (const text of [
992
- "tokenHash = computeTheHash(input)",
993
- "TOKENS_PER_ANALYSIS = 1_000_000",
994
- "POSTGRES_PASSWORD_FILE=/run/secrets/pg",
995
- "this.signingSecret = signingSecret",
996
- "accessTokenUrl: https://example.com/oauth",
997
- '"token": 1234567890, "isSecret": true',
998
- ]) {
999
- assert.equal(redactOne(text), text);
1000
- }
1001
- });
1002
- test("a secret's name stays one through its value, its environment and its version", () => {
1003
- const masked = marker("assignment");
1004
- const value = "aB3dE6gH9jK2mN5p";
1005
- for (const name of [
1006
- "DB_PASSWORD_PROD",
1007
- "DB_PASSWORD_PRODUCTION",
1008
- "API_KEY_PROD",
1009
- "AUTH_TOKEN_DEV",
1010
- "SESSION_SECRET_OLD",
1011
- "JWT_SECRET_NEW",
1012
- "stripe_secret_key_live",
1013
- "STRIPE_SECRET_KEY_TEST",
1014
- "SMTP_PASSWORD_STAGING",
1015
- "SMTP_PASSWORD_STAGE",
1016
- "ADMIN_TOKEN_LOCAL",
1017
- "SECRET_KEY_BASE_OLD",
1018
- "GITHUB_TOKEN_2",
1019
- "API_KEY_PROD_2",
1020
- "SECRET_VALUE",
1021
- "token_value",
1022
- "api_key_value",
1023
- "secret-old",
1024
- "DB_PWD_PROD",
1025
- "accessTokenProd",
1026
- "apiKeyProd",
1027
- "dbPasswordOld",
1028
- "secretOld",
1029
- "tokenValue",
1030
- "accessToken2",
1031
- "apiKey2",
1032
- ]) {
1033
- assert.equal(redactOne(`${name}=${value}`), `${name}=${masked}`, name);
1034
- assert.equal(redactOne(`${name}: "${value}"`), `${name}: "${masked}"`, name);
1035
- const record = redactOne(JSON.stringify({ [name]: value }));
1036
- assert.equal(record, JSON.stringify({ [name]: masked }), name);
1037
- }
1038
- assert.deepEqual(throughCall("mcp__vault__put", { DB_PASSWORD_PROD: value, accessToken2: value, tokenValue: value }, "plain output").input, {
1039
- DB_PASSWORD_PROD: masked,
1040
- accessToken2: masked,
1041
- tokenValue: masked,
1042
- });
1043
- // A plain name takes a space and no quote, and a hump is as often code.
1044
- assert.equal(redactOne(`DB_PASSWORD_PROD = ${value}`), `DB_PASSWORD_PROD = ${masked}`);
1045
- assert.equal(redactOne(`export API_KEY_PROD=${value} && psql`), `export API_KEY_PROD=${masked} && psql`);
1046
- assert.equal(redactOne(`--token-prod ${value}`), `--token-prod ${masked}`);
1047
- for (const text of [
1048
- "const tokenValue = someLongVariableName",
1049
- "secretValue: someLongVariableName",
1050
- `tokenValue = ${value}`,
1051
- `accessTokenProd: ${value}`,
1052
- ]) {
1053
- assert.equal(redactOne(text), text);
1054
- }
1055
- });
1056
- test("what is said about a secret still ends in a word of its own", () => {
1057
- for (const name of [
1058
- "PASSWORD_HASH",
1059
- "passwordHash",
1060
- "TOKEN_COUNT",
1061
- "token_program",
1062
- "destination_token_program",
1063
- "owner_token_x",
1064
- "tokens_old",
1065
- "token_prodigy",
1066
- "secret_newsletter",
1067
- "token_2022",
1068
- "klend-repay-token2022",
1069
- "token0",
1070
- "token1",
1071
- "uniswapV2_token0",
1072
- "oauth2",
1073
- "password2",
1074
- ]) {
1075
- const text = `${name}=aB3dE6gH9jK2mN5p`;
1076
- assert.equal(redactOne(text), text);
1077
- }
1078
- });
1079
- test("a camelCase name ending in a secret's word, spaced from a bare value, counts when the value looks generated", () => {
1080
- const masked = marker("assignment");
1081
- for (const name of ["accessToken", "refreshToken", "clientSecret", "dbPassword", "_authToken"]) {
1082
- assert.equal(redactOne(`${name}: aB3dE6gH9jK2mN5p`), `${name}: ${masked}`, name);
1083
- assert.equal(redactOne(` ${name}: aB3dE6gH9jK2mN5p,`), ` ${name}: ${masked}`, name);
1084
- }
1085
- for (const text of [
1086
- "accessToken: string",
1087
- "accessToken: request.headers.authorization",
1088
- "clientSecret: process.env.CLIENT_SECRET",
1089
- "dbPassword: documentation",
1090
- "accessToken: 202410201230",
1091
- "accessToken = aB3dE6gH9jK2mN5p",
1092
- "accessToken:aB3dE6gH9jK2mN5p",
1093
- "accessTokenUrl: aB3dE6gH9jK2mN5p",
1094
- "myApiKey: aB3dE6gH9jK2mN5p",
1095
- ]) {
1096
- assert.equal(redactOne(text), text);
1097
- }
1098
- });
1099
- test("a secret made of digits is still a secret", () => {
1100
- const masked = marker("assignment");
1101
- const cases = [
1102
- ["DB_PASSWORD=1234567890123", `DB_PASSWORD=${masked}`],
1103
- ["MYSQL_ROOT_PASSWORD: 12345678", `MYSQL_ROOT_PASSWORD: ${masked}`],
1104
- [`SECRET=${"1234567890".repeat(3)}12`, `SECRET=${masked}`],
1105
- ['{"password": "12345678"}', `{"password": "${masked}"}`],
1106
- ];
1107
- for (const [input, expected] of cases)
1108
- assert.equal(redactOne(input), expected, input);
1109
- });
1110
- test("an assistant record keeps its usage numbers, as JSON and as text", () => {
1111
- const usage = { input_tokens: 3, cache_creation_input_tokens: 12345678, cache_read_input_tokens: 123456789, output_tokens: 87654321, max_tokens: 64000000 };
1112
- const record = {
1113
- type: "assistant",
1114
- uuid: "a",
1115
- message: { role: "assistant", model: "claude-opus-5-5", content: [{ type: "text", text: `usage was ${JSON.stringify(usage)}` }], usage: { ...usage, service_tier: "standard" } },
1116
- };
1117
- // The last line of a transcript cut off mid-write is redacted as text.
1118
- const { lines } = redactLines([JSON.stringify(record), JSON.stringify(record).slice(0, -2)]);
1119
- const parsed = JSON.parse(lines[0]);
1120
- assert.deepEqual(parsed.message.usage, record.message.usage);
1121
- assert.equal(parsed.message.content[0].text, record.message.content[0].text);
1122
- assert.equal(lines[1], JSON.stringify(record).slice(0, -2));
1123
- });
1124
- test("leaves a value that is plainly code rather than a literal secret", () => {
1125
- const jsx = "<OrdersTab accessToken={accessToken} />";
1126
- assert.equal(redactOne(jsx), jsx);
1127
- assert.equal(redactOne("token: string,"), "token: string,");
1128
- });
1129
- test("still masks an environment reference, erring toward over-redaction", () => {
1130
- assert.equal(redactOne("api_key = process.env.OPENAI_KEY"), `api_key = ${marker("assignment")}`);
1131
- });
1132
- test("does not redact an already redacted value a second time", () => {
1133
- const out = redactOne(`api_key = sk-abcdefghij0123456789XYZ`);
1134
- assert.equal(out, `api_key = ${marker("openai_key")}`);
1135
- });
1136
- test("masks a sensitive value carried as a JSON object key", () => {
1137
- const { lines } = summaryFor([
1138
- { type: "user", message: { role: "user", content: [{ type: "text", text: "x" }] }, toolUseResult: { token: "abcdefghijklmnop" } },
1139
- ]);
1140
- const parsed = JSON.parse(lines[0]);
1141
- assert.equal(parsed.toolUseResult.token, marker("assignment"));
1142
- });
1143
- test("cuts the body of a tool_result that read a dotenv file", () => {
1144
- const { lines, summary } = summaryFor([
1145
- {
1146
- type: "assistant",
1147
- uuid: "a",
1148
- message: {
1149
- role: "assistant",
1150
- content: [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/repo/.env.local" } }],
1151
- },
1152
- },
1153
- {
1154
- type: "user",
1155
- uuid: "b",
1156
- message: {
1157
- role: "user",
1158
- content: [{ type: "tool_result", tool_use_id: "t1", content: "STRIPE_LIVE=pk_live_totally_real" }],
1159
- },
1160
- toolUseResult: { stdout: "STRIPE_LIVE=pk_live_totally_real" },
1161
- },
1162
- ]);
1163
- const result = JSON.parse(lines[1]);
1164
- assert.equal(result.message.content[0]?.content, marker("dotenv"));
1165
- assert.equal(result.toolUseResult, marker("dotenv"));
1166
- assert.ok(summary.counts.dotenv >= 2);
1167
- });
1168
- test("cuts dotenv content written through a Write tool call", () => {
1169
- const { lines } = summaryFor([
1170
- {
1171
- type: "assistant",
1172
- uuid: "a",
1173
- message: {
1174
- role: "assistant",
1175
- content: [
1176
- {
1177
- type: "tool_use",
1178
- id: "t1",
1179
- name: "Write",
1180
- input: { file_path: "/repo/.env", content: "STRIPE_LIVE=pk_live_totally_real" },
1181
- },
1182
- ],
1183
- },
1184
- },
1185
- ]);
1186
- const record = JSON.parse(lines[0]);
1187
- assert.equal(record.message.content[0]?.input.content, marker("dotenv"));
1188
- });
1189
- test("cuts the result of a shell command that reads a dotenv file, and keeps the command", () => {
1190
- const commands = [
1191
- "cat .env.local",
1192
- "grep -v '^#' config/.env.production | head -5",
1193
- "set -a && source ./.env && npm run dev",
1194
- "git show HEAD:.env",
1195
- "ls -la .env*",
1196
- ];
1197
- const records = commands.flatMap((command, i) => [
1198
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: `t${i}`, name: "Bash", input: { command } }] } },
1199
- {
1200
- type: "user",
1201
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: `t${i}`, content: "STRIPE_LIVE=pk_live_totally_real" }] },
1202
- toolUseResult: { stdout: "STRIPE_LIVE=pk_live_totally_real", stderr: "", interrupted: false },
1203
- },
1204
- ]);
1205
- const out = summaryFor(records).lines.map((line) => JSON.parse(line));
1206
- for (let i = 0; i < commands.length; i += 1) {
1207
- assert.equal(out[i * 2].message.content[0].input.command, commands[i]);
1208
- const cut = i < commands.length - 1;
1209
- assert.equal(out[i * 2 + 1].message.content[0].content === marker("dotenv"), cut, commands[i]);
1210
- assert.equal(out[i * 2 + 1].toolUseResult === marker("dotenv"), cut, commands[i]);
1211
- }
1212
- });
1213
- test("cuts the result of any shell command that names a dotenv file, and keeps the command", () => {
1214
- const commands = [
1215
- `node -e "console.log(require('fs').readFileSync('.env','utf8'))"`,
1216
- `python3 -c "print(open('.env').read())"`,
1217
- `python3 -c "\nimport re\nprint(re.findall('X', open('config/.env.local').read()))\n"`,
1218
- "cut -d= -f2 .env.local",
1219
- "tr -d x < .env",
1220
- "sort .env",
1221
- "base64 config/.env",
1222
- "ssh host cat /app/.env",
1223
- "docker exec app cat /app/.env",
1224
- "if [ -f .env ]; then cat .env; fi",
1225
- "(cat .env)",
1226
- "timeout 5 cat .env",
1227
- "time cat .env",
1228
- "sudo -u app cat .env",
1229
- "git show origin/main:.env",
1230
- "git show HEAD:config/.env.local",
1231
- "while read -r line; do echo \"$line\"; done < .env",
1232
- "for f in .env*; do cut -d= -f2 \"$f\"; done",
1233
- "F=.env; node -p \"require('dotenv').parse(require('fs').readFileSync('$F'))\"",
1234
- "docker compose --env-file .env config",
1235
- "node --env-file=.env -p process.env.MAILER_PHRASE",
1236
- "echo `cat .env`",
1237
- "echo \"$(tail -1 .env)\"",
1238
- "Select-String -Path .env -Pattern PHRASE",
1239
- "cat $(ls .env*)",
1240
- "ls .env* | xargs cat",
1241
- "echo .env | xargs -n1 cut -d= -f2",
1242
- "echo \"$(<.env)\"",
1243
- "echo \"$(grep X \"$(dirname $0)/.env\")\"",
1244
- ];
1245
- for (const command of commands) {
1246
- const out = throughCall("Bash", { command });
1247
- assert.equal(out.content, marker("dotenv"), command);
1248
- assert.equal(out.toolUseResult, marker("dotenv"), command);
1249
- assert.equal(out.input.command, command);
1250
- }
1251
- assert.equal(throughCall("Monitor", { command: "tail -f .env" }).content, marker("dotenv"));
1252
- assert.equal(throughCall("PowerShell", { command: "Get-Content .env" }).content, marker("dotenv"));
1253
- assert.equal(throughCall("REPL", { code: "fs.readFileSync('.env', 'utf8')" }).content, marker("dotenv"));
1254
- assert.equal(throughCall("exec", { script: "print(open('config/.env').read())" }).content, marker("dotenv"));
1255
- assert.equal(throughCall("mcp__shell__run", { cmd: "cut -d= -f2 .env" }).content, marker("dotenv"));
1256
- assert.equal(throughCall("Workflow", { script: "agent('cat .env and report')" }, "plain output").content, "plain output");
1257
- });
1258
- test("keeps the result of a command that names a dotenv file and prints none of it, or only names the word", () => {
1259
- const commands = [
1260
- "ls -la .env*",
1261
- "ls -la .env* && git status",
1262
- "ls -la config/.env.local",
1263
- "stat .env",
1264
- "file .env",
1265
- "wc -l .env",
1266
- "test -f .env",
1267
- "[ -f .env ] && echo yes",
1268
- "if [ -f .env ]; then echo yes; fi",
1269
- "if ! test -f .env; then echo no; fi",
1270
- "echo .env >> .gitignore",
1271
- "printf '%s\\n' .env",
1272
- "git add .env.example",
1273
- "git rm --cached .env",
1274
- "git commit -m 'stop tracking .env'",
1275
- "git check-ignore -v .env",
1276
- "git add .env.example && git commit -m \"$(date)\"",
1277
- "test -f .env && echo \"$(date)\"",
1278
- "git status --short .env",
1279
- "git ls-files .env",
1280
- "chmod 600 .env && npm test",
1281
- "cp .env.example .env && npm test",
1282
- "mv .env .env.bak; npm test; mv .env.bak .env",
1283
- "rm -f .env && npm test",
1284
- "touch .env && npm run build",
1285
- `node -e "console.log(process.env.MAILER_PHRASE)"`,
1286
- "npm test -- src/env.test.ts",
1287
- "cat src/load-env.ts",
1288
- "grep -rn 'import.meta.env.VITE_X' src",
1289
- "rg '\\.env' src",
1290
- ];
1291
- for (const command of commands) {
1292
- const out = throughCall("Bash", { command }, "plain output");
1293
- assert.equal(out.content, "plain output", command);
1294
- assert.deepEqual(out.toolUseResult, { stdout: "plain output" }, command);
1295
- assert.equal(out.input.command, command);
1296
- }
1297
- });
1298
- /** A session that says the value everywhere it can be said, and shows a dotenv file between. */
1299
- function sessionAround(dotenvCall) {
1300
- const value = "Zq8vR2mK9xLp";
1301
- const said = (uuid) => [
1302
- { type: "user", uuid: `${uuid}-prompt`, message: { role: "user", content: `use ${value} for the mailer` } },
1303
- { type: "assistant", uuid: `${uuid}-text`, message: { role: "assistant", content: [{ type: "text", text: `The phrase is ${value}.` }] } },
1304
- { type: "assistant", uuid: `${uuid}-call`, message: { role: "assistant", content: [{ type: "tool_use", id: `${uuid}-t`, name: "Bash", input: { command: `node send.js ${value}` } }] } },
1305
- { type: "user", uuid: `${uuid}-out`, message: { role: "user", content: [{ type: "tool_result", tool_use_id: `${uuid}-t`, content: `sent with ${value}` }] }, toolUseResult: { stdout: `sent with ${value}` } },
1306
- ];
1307
- return { value, records: [...said("before"), ...dotenvCall, ...said("after")] };
1308
- }
1309
- const callAndResult = (name, input, output) => [
1310
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "env", name, input }] } },
1311
- { type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "env", content: output }] }, toolUseResult: output },
1312
- ];
1313
- test("a value a dotenv file showed goes from the whole session, before the read and after it", () => {
1314
- const dotenv = "MAILER_PHRASE=Zq8vR2mK9xLp\nNODE_ENV=production\n";
1315
- const ways = [
1316
- ["Read", callAndResult("Read", { file_path: "/repo/.env" }, ` 1\u2192MAILER_PHRASE=Zq8vR2mK9xLp\n 2\u2192NODE_ENV=production\n`)],
1317
- ["Read in blocks", callAndResult("Read", { file_path: "/repo/.env" }, [{ type: "text", text: dotenv }])],
1318
- ["Grep", callAndResult("Grep", { pattern: "PHRASE", path: ".env", output_mode: "content" }, "MAILER_PHRASE=Zq8vR2mK9xLp")],
1319
- ["cat", callAndResult("Bash", { command: "cat .env" }, dotenv)],
1320
- ["node -e", callAndResult("Bash", { command: `node -e "console.log(fs.readFileSync('.env','utf8'))"` }, dotenv)],
1321
- ["quoted", callAndResult("Bash", { command: "cat .env" }, `export MAILER_PHRASE="Zq8vR2mK9xLp"\n`)],
1322
- ["cut, a value with no name", callAndResult("Bash", { command: "cut -d= -f2 .env" }, "Zq8vR2mK9xLp\nproduction\n")],
1323
- ["Write", callAndResult("Write", { file_path: "/repo/.env", content: dotenv }, "ok")],
1324
- ["Edit", callAndResult("Edit", { file_path: "/repo/.env", old_string: "x", new_string: dotenv }, "ok")],
1325
- ["MultiEdit", callAndResult("MultiEdit", { file_path: "/repo/.env", edits: [{ old_string: "x", new_string: dotenv }] }, "ok")],
1326
- ["echo into it", callAndResult("Bash", { command: `echo "MAILER_PHRASE=Zq8vR2mK9xLp" >> .env` }, "")],
1327
- ["heredoc into it", callAndResult("Bash", { command: `cat > .env <<'EOF'\n${dotenv}EOF` }, "")],
1328
- ["printf into it", callAndResult("Bash", { command: "printf 'MAILER_PHRASE=Zq8vR2mK9xLp\\nA=1\\n' > .env" }, "")],
1329
- ["a ! command", [
1330
- { type: "user", message: { role: "user", content: "<bash-input>cat .env</bash-input>" } },
1331
- { type: "user", message: { role: "user", content: `<bash-stdout>${dotenv}</bash-stdout><bash-stderr></bash-stderr>` } },
1332
- ]],
1333
- ];
1334
- for (const [way, call] of ways) {
1335
- const { value, records } = sessionAround(call);
1336
- const { lines, summary } = summaryFor(records);
1337
- const out = lines.join("\n");
1338
- assert.ok(!out.includes(value), `${way} left the value in the session`);
1339
- assert.equal(out.split(marker("dotenv")).length - 1 >= 8, true, way);
1340
- assert.ok((summary.counts.dotenv ?? 0) >= 8, way);
1341
- const parsed = lines.map((line) => JSON.parse(line));
1342
- assert.equal(parsed[0].message.content, `use ${marker("dotenv")} for the mailer`, way);
1343
- const again = redactLines(lines);
1344
- assert.deepEqual(again.lines, lines, `${way}: a second pass changes nothing`);
1345
- }
1346
- });
1347
- test("a command that only names a dotenv file teaches nothing from its own text", () => {
1348
- const value = "Zq8vR2mK9xLp";
1349
- for (const command of [
1350
- `python3 - <<'PY'\nMAILER_PHRASE="${value}"\nprint(open('.env').read())\nPY`,
1351
- `MAILER_PHRASE=${value} node --env-file=.env send.js`,
1352
- ]) {
1353
- const { lines } = summaryFor([
1354
- ...callAndResult("Bash", { command }, "plain output"),
1355
- { type: "user", message: { role: "user", content: `use ${value} for the mailer` } },
1356
- ]);
1357
- assert.ok(lines.at(-1).includes(value), command);
1358
- }
1359
- });
1360
- test("a value learned as a stream goes the same way", async () => {
1361
- const { value, records } = sessionAround(callAndResult("Read", { file_path: "/repo/.env" }, "MAILER_PHRASE=Zq8vR2mK9xLp"));
1362
- const stream = (async function* () {
1363
- for (const record of records)
1364
- yield JSON.stringify(record);
1365
- })();
1366
- const { lines } = await redactLineStream(stream);
1367
- assert.ok(!lines.join("\n").includes(value));
1368
- assert.equal(lines.length, records.length);
1369
- });
1370
- test("what a dotenv file holds in plain view does not shred the session", () => {
1371
- const dotenv = [
1372
- "NODE_ENV=production",
1373
- "PORT=3000",
1374
- "BASE_URL=http://localhost:3000",
1375
- "API_URL=https://api.example.com/v1",
1376
- "APP_DIR=/srv/app/current/release/public",
1377
- "POSTGRES_PASSWORD=password",
1378
- "ADMIN_PASSWORD=changeme",
1379
- "DEBUG=true",
1380
- "ROLE=assistant",
1381
- "PAGE_TITLE=Transcript",
1382
- "SHORT=Zq8vR2m",
1383
- "ANTHROPIC_MODEL=claude-opus-5-5",
1384
- "CHECKOUT_DIR=/repo/src/payments-module",
1385
- `START_CMD="npm run build"`,
1386
- "TEST_COMMAND='npm run test -- --ci'",
1387
- `APP_NAME="My Cool Application"`,
1388
- "PIN_CODE=1234567890123",
1389
- "CLIENT_REGION=eu-central-1",
1390
- `const MARKER = "<!-- isy:comment:v1 -->";`,
1391
- ].join("\n");
1392
- const text = "Run npm run build, then npm run test -- --ci for My Cool Application. In production the password is read at http://localhost:3000 and https://api.example.com/v1 for /srv/app/current/release/public, unless true: changeme, Transcript, Zq8vR2m, 3000, 1234567890123, claude-opus-5-5 in eu-central-1 under <!-- isy:comment:v1 -->.";
1393
- const records = [
1394
- { type: "user", uuid: "u1", sessionId: "s1", gitBranch: "claude-opus-5-5", cwd: "/repo", message: { role: "user", content: text } },
1395
- { type: "assistant", uuid: "a1", sessionId: "s1", message: { role: "assistant", model: "claude-opus-5-5", content: [{ type: "tool_use", id: "t1", name: "Edit", input: { file_path: "/repo/src/payments-module/a.ts", old_string: "a", new_string: "b" } }] } },
1396
- ...callAndResult("Read", { file_path: "/repo/.env" }, dotenv),
1397
- ];
1398
- const { lines } = summaryFor(records);
1399
- const parsed = lines.map((line) => JSON.parse(line));
1400
- assert.equal(parsed[0].message.content, text);
1401
- assert.equal(parsed[0].gitBranch, "claude-opus-5-5");
1402
- assert.equal(parsed[1].type, "assistant");
1403
- assert.equal(parsed[1].message.role, "assistant");
1404
- assert.equal(parsed[1].message.model, "claude-opus-5-5");
1405
- assert.equal(parsed[1].message.content[0].input.file_path, "src/payments-module/a.ts");
1406
- });
1407
- test("a long ordinary value under a name nothing knows stays, and a generated one goes", () => {
1408
- const m = marker("dotenv");
1409
- const quoted = (line) => {
1410
- const { lines } = summaryFor([...callAndResult("Read", { file_path: "/repo/.env" }, line), { type: "user", uuid: "u", message: { role: "user", content: `the value ${line.slice(line.indexOf("=") + 1)} here` } }]);
1411
- return JSON.parse(lines.at(-1)).message.content;
1412
- };
1413
- for (const line of [
1414
- "DOCS_URL=https://documentation.example.com",
1415
- "ORIGIN=https://www.transportation.example.org",
1416
- "BUCKET=s3://organisation-production-backups",
1417
- "APP_NAME=MyCoolApplication",
1418
- "DB_NAME=PRODUCTION_DATABASE",
1419
- "ALLOWED_METHODS=GET,POST,PUT,DELETE",
1420
- "FONT_STACK=Helvetica,Arial,sans-serif",
1421
- "CLOUD_REGION=northamerica-northeast1",
1422
- "SERVICE=transcription-service",
1423
- "IMAGE=registry.example.com/backend:202410201230",
1424
- "NODE_OPTIONS=--max-old-space-size=4096",
1425
- "CACHE_CONTROL=public,max-age=31536000",
1426
- "CIPHER=TLS_AES_256_GCM_SHA384",
1427
- ]) {
1428
- const value = line.slice(line.indexOf("=") + 1);
1429
- assert.equal(quoted(line), `the value ${value} here`);
1430
- }
1431
- for (const line of [
1432
- "MAILER_PHRASE=Zq8vR2mK9xLp",
1433
- "UPSTREAM=abcd1234efgh5678",
1434
- "SESSION_ID=550e8400-e29b-41d4-a716-446655440abc",
1435
- "HOOK=https://hooks.example.com/services/T0000/B0000/XXXXabcd1234",
1436
- "WEIRD=p@ss-w0rd!x#9zz",
1437
- "BLOB=dGVzdGluZ3Rlc3Rpbmc=",
1438
- ]) {
1439
- assert.equal(quoted(line), `the value ${m} here`);
1440
- }
1441
- });
1442
- test("cuts what a shell command writes into a dotenv file, and what it printed", () => {
1443
- const m = marker("dotenv");
1444
- const cases = [
1445
- ["cat > .env << 'EOF'\nDB_PASS=Zq8vR2mK9xLp\nAPP_KEY=base64:Zq8vR2mK9xLpWq7=\nEOF", `${m} > .env`],
1446
- ["python3 - <<'PY'\nopen('.env', 'w').write('DB_PASS=Zq8vR2mK9xLp')\nPY", `python3 - <<'PY'\n${m}\nPY`],
1447
- ['echo "DB_PASS=Zq8vR2mK9xLp" > .env', `${m} > .env`],
1448
- // The edit beside the write is still an edit.
1449
- ["cd app && sed -i 's/a/b/' src/a.ts && printf 'DB_PASS=%s\\n' Zq8vR2mK9xLp >> config/.env.local", `cd app && sed -i 's/a/b/' src/a.ts && ${m} > config/.env.local`],
1450
- ["echo DB_PASS=Zq8vR2mK9xLp | sudo tee -a .env", `${m} > .env`],
1451
- ["Set-Content -Path .env -Value 'DB_PASS=Zq8vR2mK9xLp'", `${m} > .env`],
1452
- // The values sit outside the statement that names the file.
1453
- ["{ echo DB_PASS=Zq8vR2mK9xLp; echo APP=Zq8vR2mK9xLp; } > .env", `${m} > .env`],
1454
- ["(echo DB_PASS=Zq8vR2mK9xLp; echo APP=Zq8vR2mK9xLp) | tee .env", `${m} > .env`],
1455
- ["{\n echo DB_PASS=Zq8vR2mK9xLp\n echo APP=Zq8vR2mK9xLp\n} > .env", `${m} > .env`],
1456
- ['echo "DB_PASS=Zq8vR2mK9xLp\nAPP=Zq8vR2mK9xLp" > .env', `${m} > .env`],
1457
- // `tee` behind whatever runs it, and a target with a space in it.
1458
- ["echo DB_PASS=Zq8vR2mK9xLp | sudo -u app tee .env", `${m} > .env`],
1459
- ["echo DB_PASS=Zq8vR2mK9xLp | ssh host /usr/bin/tee app/.env", `${m} > app/.env`],
1460
- ["'DB_PASS=Zq8vR2mK9xLp' | Tee-Object -FilePath .env", `${m} > .env`],
1461
- ['echo "DB_PASS=Zq8vR2mK9xLp" > "My Projects/app/.env"', `${m} > My Projects/app/.env`],
1462
- ["echo DB_PASS=Zq8vR2mK9xLp > my\\ app/.env", `${m} > my app/.env`],
1463
- // A loop, a condition, a line that goes on in the next.
1464
- ["for kv in DB_PASS=Zq8vR2mK9xLp APP=Zq8vR2mK9xLp; do echo $kv; done > .env", `${m} > .env`],
1465
- ["if true; then echo DB_PASS=Zq8vR2mK9xLp; fi > .env", `${m} > .env`],
1466
- ['echo "DB_PASS=Zq8vR2mK9xLp" \\\n >> .env', `${m} > .env`],
1467
- // The value sits where the write only names it.
1468
- ['VALUE=Zq8vR2mK9xLp\necho "K=$VALUE" > .env', `${m} > .env`],
1469
- ['VALUE=Zq8vR2mK9xLp; echo "K=$VALUE" > .env', `${m} > .env`],
1470
- ['export VALUE=Zq8vR2mK9xLp && printf "K=%s" "${VALUE}" >> .env', `${m} > .env`],
1471
- ["export VALUE=Zq8vR2mK9xLp\ncat > .env <<EOF\nK=$VALUE\nEOF", `${m} > .env`],
1472
- // A redirect that overrides `noclobber`.
1473
- ["echo DB_PASS=Zq8vR2mK9xLp >| .env", `${m} > .env`],
1474
- // The file in a variable, or a loop's.
1475
- ["F=.env; echo K=Zq8vR2mK9xLp > $F", `${m} > .env`],
1476
- ['F=.env; echo K=Zq8vR2mK9xLp > "$F"', `${m} > .env`],
1477
- ['F=".env.local"; echo K=Zq8vR2mK9xLp > ${F}', `${m} > .env.local`],
1478
- ["F=.env; cat > $F <<EOF\nK=Zq8vR2mK9xLp\nEOF", `${m} > .env`],
1479
- ["for f in .env .env.local; do echo K=Zq8vR2mK9xLp >> $f; done", `${m} > .env`],
1480
- ['for v in Zq8vR2mK9xLp; do echo "K=$v" > .env; done', `${m} > .env`],
1481
- // The values where the write does not name them.
1482
- ["exec 3>.env; echo K=Zq8vR2mK9xLp >&3", `${m} > .env`],
1483
- ["export X=Zq8vR2mK9xLp; envsubst < tpl > .env", `${m} > .env`],
1484
- // A heredoc's delimiter quoted the other way.
1485
- ["cat > .env <<\\EOF\nK=Zq8vR2mK9xLp\nEOF", `${m} > .env`],
1486
- // A script fed to a shell, the value set outside it.
1487
- ['X=Zq8vR2mK9xLp; ssh host <<EOF\necho "K=$X" >> /app/.env\nEOF', `${m} > /app/.env`],
1488
- // A write in a quoted remote command.
1489
- ["ssh host 'tee /app/.env' <<EOF\nK=Zq8vR2mK9xLp\nEOF", `${m} > /app/.env`],
1490
- ['echo K=Zq8vR2mK9xLp | ssh host "tee -a /app/.env"', `${m} > /app/.env`],
1491
- // An interpreter's one-liner, which opens the file to write it.
1492
- [`python3 -c "open('.env','w').write('K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1493
- [`python3 -c "with open('.env', mode='a') as f: f.write('K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1494
- [`python3 -c "open(\\".env\\", \\"w\\").write(\\"K=Zq8vR2mK9xLp\\")"`, `${m} > .env`],
1495
- [`python3 -c "from pathlib import Path; Path('.env').write_text('K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1496
- [`node -e "fs.writeFileSync('.env','K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1497
- [`node -e "require('fs').appendFileSync('.env','K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1498
- [`ruby -e "File.write('.env','K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1499
- [`php -r "file_put_contents('.env','K=Zq8vR2mK9xLp');"`, `${m} > .env`],
1500
- [`F=.env; node -e "fs.writeFileSync('$F','K=Zq8vR2mK9xLp')"`, `${m} > .env`],
1501
- // What comes on stdin, copied there.
1502
- ["dd of=.env <<< K=Zq8vR2mK9xLp", `${m} > .env`],
1503
- ["cp /dev/stdin .env <<< K=Zq8vR2mK9xLp", `${m} > .env`],
1504
- ["install /dev/stdin .env <<< K=Zq8vR2mK9xLp", `${m} > .env`],
1505
- ['node -e "console.log(process.env.DB_PASS)" > env.txt', 'node -e "console.log(process.env.DB_PASS)" > env.txt'],
1506
- ];
1507
- // A variable the command does not set holds nothing the command could show.
1508
- assert.equal(throughCall("Bash", { command: `cd app\necho "K=$FROM_ENV" > .env` }).input.command, `cd app\n${m} > .env`);
1509
- for (const [command, expected] of cases) {
1510
- const { lines } = summaryFor([
1511
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command } }] } },
1512
- {
1513
- type: "user",
1514
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: "MAILER_PHRASE=Zq8vR2mK9xLp" }] },
1515
- toolUseResult: { stdout: "MAILER_PHRASE=Zq8vR2mK9xLp", stderr: "", interrupted: false },
1516
- },
1517
- ]);
1518
- const call = JSON.parse(lines[0]);
1519
- assert.equal(call.message.content[0].input.command, expected);
1520
- assert.equal(lines[1].includes("Zq8vR2mK9xLp"), command === expected, command);
1521
- }
1522
- });
1523
- test("a value written into a dotenv file is learned without the shell operator behind it", () => {
1524
- const value = "Zq8vR2mK9xLp";
1525
- for (const command of [
1526
- `echo K=${value}>.env`,
1527
- `echo K=${value}>>.env`,
1528
- `echo K=${value}|tee .env`,
1529
- `(echo K=${value})>.env`,
1530
- `(echo K=${value}) > .env`,
1531
- `$(echo K=${value}) > .env`,
1532
- `cat .env; echo K=${value}>>.env`,
1533
- ]) {
1534
- const { lines } = summaryFor([
1535
- ...callAndResult("Bash", { command }, "done"),
1536
- { type: "user", message: { role: "user", content: `the key is ${value}` } },
1537
- ]);
1538
- assert.ok(!lines.join("\n").includes(value), command);
1539
- }
1540
- // A dotenv file may hold them, and the value goes whole as well.
1541
- const whole = "aZ9bY8cX7&dW6eV5;fU4";
1542
- const { lines } = summaryFor([
1543
- ...callAndResult("Read", { file_path: ".env" }, `API_KEY=${whole}`),
1544
- { type: "user", message: { role: "user", content: `the key is ${whole}, then aZ9bY8cX7` } },
1545
- ]);
1546
- assert.ok(!lines.join("\n").includes("aZ9bY8cX7"));
1547
- assert.ok(!lines.join("\n").includes("fU4"));
1548
- });
1549
- test("what a command wrote into a dotenv file goes from the rest of the session, however it was written", () => {
1550
- const value = "Zq8vR2mK9xLp";
1551
- for (const command of [
1552
- `F=.env; echo K=${value} > $F`,
1553
- `F=.env; sed -i 's/a/${value}/' $F`,
1554
- `for f in .env .env.local; do echo K=${value} >> $f; done`,
1555
- `exec 3>.env; echo K=${value} >&3`,
1556
- `export X=${value}; envsubst < tpl > .env`,
1557
- `cat > .env <<\\EOF\nK=${value}\nEOF`,
1558
- `ssh host 'tee /app/.env' <<EOF\nK=${value}\nEOF`,
1559
- `echo K=${value} | ssh host "tee -a /app/.env"`,
1560
- `python3 -c "open('.env','w').write('K=${value}')"`,
1561
- `node -e "fs.writeFileSync('.env','K=${value}')"`,
1562
- `dd of=.env <<< K=${value}`,
1563
- `cp /dev/stdin .env <<< K=${value}`,
1564
- // A shell or an interpreter fed a script that writes the file.
1565
- `ssh host <<'REMOTE'\ncat > /app/.env <<'EOF'\nK=${value}\nEOF\nREMOTE`,
1566
- `ssh host bash <<'REMOTE'\necho K=${value} >> /app/.env\nREMOTE`,
1567
- `bash <<'SH'\necho K=${value} > .env\nSH`,
1568
- `bash <<'SH'\necho K=${value} > prod.env\nSH`,
1569
- `bash <<'SH'\nF=.env; echo K=${value} > $F\nSH`,
1570
- `python3 - <<'PY'\nopen('.env','w').write('K=${value}')\nPY`,
1571
- // In-place editors other than sed.
1572
- `ruby -pi -e 'sub("a","${value}")' .env`,
1573
- `ruby -i.bak -pe 'sub("a","${value}")' .env`,
1574
- `awk -i inplace '{print "K=${value}"}' .env`,
1575
- `gawk --include=inplace '{print "K=${value}"}' .env`,
1576
- `perl -p -i -e 's/a/${value}/' .env`,
1577
- `perl -0pi -e 's/a/${value}/' .env`,
1578
- ]) {
1579
- const { lines } = summaryFor([
1580
- ...callAndResult("Bash", { command }, "done"),
1581
- { type: "user", message: { role: "user", content: `the key is ${value}` } },
1582
- ]);
1583
- assert.ok(!lines.join("\n").includes(value), command);
1584
- }
1585
- // Reading it, in the same forms, or writing something else, leaves the command as it was.
1586
- for (const command of [
1587
- `python3 -c "print(open('.env').read())"`,
1588
- `python3 -c "print(open('.env','r').read())"`,
1589
- `python3 -c "print(open('.env', encoding='ascii').read())"`,
1590
- `python3 -c "open('a.txt','w').write('.env')"`,
1591
- `node -e "console.log(fs.readFileSync('.env','utf8'))"`,
1592
- `ruby -e "puts File.read('.env')"`,
1593
- "ruby -ne 'puts $_' .env",
1594
- "awk '{print}' .env",
1595
- "awk -i lib.awk '{print}' .env",
1596
- "dd if=.env of=copy.txt",
1597
- "install -m 600 a.txt .env",
1598
- "F=a.txt; echo K=Zq8vR2mK9xLp > $F; cat .env",
1599
- "for f in a.txt b.txt; do echo K=Zq8vR2mK9xLp > $f; done; cat .env",
1600
- ]) {
1601
- assert.equal(throughCall("Bash", { command }, "plain output").input.command, command);
1602
- }
1603
- });
1604
- test("a redirect or a tee inside a quoted argument writes nothing, unless the command runs that argument", () => {
1605
- for (const command of [
1606
- `echo "add this > .env"`,
1607
- `echo 'then pipe it | tee .env'`,
1608
- `printf 'a > .env\\n'`,
1609
- `git commit -m "stop writing > .env from the hook"`,
1610
- `git commit -m 'stop piping | tee .env'`,
1611
- `grep -n "> .env" deploy.sh`,
1612
- `gh pr comment 1 --body "write > .env first"`,
1613
- ]) {
1614
- assert.equal(throughCall("Bash", { command }, "plain output").input.command, command);
1615
- }
1616
- const m = marker("dotenv");
1617
- const cases = [
1618
- [`echo "K=Zq8vR2mK9xLp > x" > .env`, `${m} > .env`],
1619
- [`echo "K=Zq8vR2mK9xLp" > ".env"`, `${m} > .env`],
1620
- [`echo "K=Zq8vR2mK9xLp" | tee .env`, `${m} > .env`],
1621
- [`sudo sh -c 'echo K=Zq8vR2mK9xLp >> .env'`, `${m} > .env`],
1622
- [`bash -c "echo K=Zq8vR2mK9xLp > .env"`, `${m} > .env`],
1623
- [`ssh host "echo K=Zq8vR2mK9xLp > /app/.env"`, `${m} > /app/.env`],
1624
- ];
1625
- for (const [command, expected] of cases) {
1626
- assert.equal(throughCall("Bash", { command }).input.command, expected);
1627
- }
1628
- });
1629
- test("a commit or pull request's message in a heredoc only says the word, and a script in one still reads the file", () => {
1630
- for (const command of [
1631
- `git commit -m "$(cat <<'EOF'\nStop tracking .env\nEOF\n)"`,
1632
- `git add a && git commit -m "$(cat <<'EOF'\nStop tracking .env\nEOF\n)"`,
1633
- `gh pr create --title x --body "$(cat <<'EOF'\nStop tracking .env\nEOF\n)"`,
1634
- "git commit -F - <<EOF\nStop tracking .env\nEOF",
1635
- ]) {
1636
- const out = throughCall("Bash", { command }, "plain output");
1637
- assert.equal(out.input.command, command);
1638
- assert.equal(out.content, "plain output");
1639
- }
1640
- const m = marker("dotenv");
1641
- for (const [command, expected] of [
1642
- ["python3 - <<'PY'\nprint(open('.env').read())\nPY", `python3 - <<'PY'\n${m}\nPY`],
1643
- ["gh api /x | python3 - <<'PY'\nprint(open('.env').read())\nPY", `gh api /x | python3 - <<'PY'\n${m}\nPY`],
1644
- ["git log | sh <<'EOF'\ncat .env\nEOF", `git log | sh <<'EOF'\n${m}\nEOF`],
1645
- [`git commit -m x; sh <<'EOF'\ncat .env\nEOF`, `git commit -m x; sh <<'EOF'\n${m}\nEOF`],
1646
- ]) {
1647
- const out = throughCall("Bash", { command }, "MAILER_PHRASE=Zq8vR2mK9xLp");
1648
- assert.equal(out.input.command, expected);
1649
- assert.equal(out.content, m, command);
1650
- }
1651
- });
1652
- test("a here-string is no heredoc: the line after it is not its body", () => {
1653
- const read = "tr a b <<< hello\ncat .env";
1654
- assert.equal(throughCall("Bash", { command: read }).input.command, read);
1655
- assert.equal(throughCall("Bash", { command: read }).content, marker("dotenv"));
1656
- assert.equal(throughCall("Bash", { command: "tr a b <<< hello\necho K=Zq8vR2mK9xLp > .env" }).input.command, `tr a b <<< hello\n${marker("dotenv")} > .env`);
1657
- });
1658
- test("a background command's task goes the way of the command", () => {
1659
- const value = "Zq8vR2mK9xLp";
1660
- const command = `echo MAILER_PHRASE=${value} > .env; sleep 100`;
1661
- const started = (id, task, run) => [
1662
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id, name: "Bash", input: { command: run, run_in_background: true } }] } },
1663
- { type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: id, content: `running as ${task}` }] }, toolUseResult: { backgroundTaskId: task } },
1664
- ];
1665
- const followed = (id, name, input, run) => [
1666
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id, name, input }] } },
1667
- { type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: id, content: `stopped ${run}` }] }, toolUseResult: { message: "stopped", command: run, task_id: "any", task_type: "local_bash" } },
1668
- ];
1669
- const { lines } = summaryFor([
1670
- ...started("t1", "bg1", command),
1671
- ...followed("t2", "TaskStop", { task_id: "bg1" }, command),
1672
- ...followed("t3", "TaskOutput", { task_id: "bg1" }, command),
1673
- ...followed("t4", "BashOutput", { bash_id: "bg1" }, command),
1674
- ...followed("t5", "KillShell", { shell_id: "bg1" }, command),
1675
- ...started("t6", "bg2", "npm run dev"),
1676
- ...followed("t7", "TaskStop", { task_id: "bg2" }, "npm run dev"),
1677
- // The file its output lands in is read back as often as the task is asked.
1678
- ...followed("t8", "Bash", { command: "tail -5 /tmp/tasks/bg1.output" }, command),
1679
- ...followed("t9", "Read", { file_path: "/tmp/tasks/bg1.output" }, command),
1680
- ...followed("t10", "Bash", { command: "tail -5 /tmp/tasks/bg2.output" }, "npm run dev"),
1681
- ...followed("t11", "Read", { file_path: "/tmp/tasks/bg2.output" }, "npm run dev"),
1682
- ]);
1683
- const out = lines.map((line) => JSON.parse(line));
1684
- for (const i of [3, 5, 7, 9, 15, 17]) {
1685
- assert.equal(out[i].message.content[0].content, marker("dotenv"));
1686
- assert.equal(out[i].toolUseResult, marker("dotenv"));
1687
- }
1688
- for (const i of [13, 19, 21])
1689
- assert.equal(out[i].message.content[0].content, "stopped npm run dev");
1690
- assert.ok(!lines.join("\n").includes(value));
1691
- });
1692
- test("a file named the other way round is a dotenv file too, and code reading the environment is not", () => {
1693
- const m = marker("dotenv");
1694
- for (const file_path of ["prod.env", "/repo/config/prod.env", "/repo/.x.env", "config/app.env"]) {
1695
- const out = throughCall("Read", { file_path });
1696
- assert.equal(out.content, m, file_path);
1697
- }
1698
- for (const command of ["cat prod.env", "cat config/*.env", "cut -d= -f2 config/app.env", "docker compose --env-file prod.env config", "git show HEAD:.x.env"]) {
1699
- assert.equal(throughCall("Bash", { command }).content, m, command);
1700
- }
1701
- assert.equal(throughCall("Bash", { command: "echo K=Zq8vR2mK9xLp > prod.env" }).input.command, `${m} > prod.env`);
1702
- assert.equal(throughCall("Write", { file_path: "prod.env", content: "K=Zq8vR2mK9xLp" }).input.content, m);
1703
- for (const command of [
1704
- "node -p process.env",
1705
- `node -e "console.log(process.env)"`,
1706
- `node -e "console.log(import.meta.env)"`,
1707
- `node -e "console.log(Object.keys(globalThis.process.env))"`,
1708
- "deno eval 'console.log(Deno.env)'",
1709
- "cat src/foo.env.ts",
1710
- "ls prod.env",
1711
- "rg '.*\\.env' src",
1712
- "python3 - <<'PY'\nprint(cfg.env)\nPY",
1713
- "cat > src/a.ts <<'EOF'\nconst value = cfg.env;\nEOF",
1714
- ]) {
1715
- const out = throughCall("Bash", { command }, "plain output");
1716
- assert.equal(out.input.command, command);
1717
- assert.equal(out.content, "plain output", command);
1718
- }
1719
- });
1720
- test("an in-place edit of a dotenv file loses its script, and stays an edit", () => {
1721
- const m = marker("dotenv");
1722
- const cases = [
1723
- ["sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env", `sed -i ${m} .env`, ".env"],
1724
- ["sed -i.bak 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' config/.env.local", `sed -i ${m} config/.env.local`, "config/.env.local"],
1725
- ["sed -i -e 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env", `sed -i ${m} .env`, ".env"],
1726
- ["perl -pi -e 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env", `sed -i ${m} .env`, ".env"],
1727
- ["cd app && sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env && npm test", `cd app && sed -i ${m} .env && npm test`, "app/.env"],
1728
- // The value sits where the script only names it.
1729
- ['VALUE=Zq8vR2mK9xLp\nsed -i "s/^KEY=.*/KEY=$VALUE/" .env', `sed -i ${m} .env`, ".env"],
1730
- ['VALUE=Zq8vR2mK9xLp; sed -i "s/^KEY=.*/KEY=${VALUE}/" .env', `sed -i ${m} .env`, ".env"],
1731
- // In single quotes `$` is sed's own, the last line.
1732
- ["sed -i '$a KEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1733
- ["sed -i '$ a KEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1734
- ["sed -i '$aKEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1735
- ["sed -i '$iKEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1736
- ["sed -i '$cKEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1737
- ['sed -i "\\$a KEY=Zq8vR2mK9xLp" .env', `sed -i ${m} .env`, ".env"],
1738
- ["sed -i -e '$a KEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1739
- ["sed -i '1iKEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1740
- ["sed -i '/^KEY=/cKEY=Zq8vR2mK9xLp' .env", `sed -i ${m} .env`, ".env"],
1741
- // Named by a variable, or from one.
1742
- ["F=.env; sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' $F", `sed -i ${m} .env`, ".env"],
1743
- [`F=.env; sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' "$F"`, `sed -i ${m} .env`, ".env"],
1744
- [`sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' "$HOME/.env"`, `sed -i ${m} .env`, ".env"],
1745
- [`for f in .env*; do sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' "$f"; done`, `sed -i ${m} .env`, ".env"],
1746
- ["for f in a b; do sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env; done", `for f in a b; sed -i ${m} .env; done`, ".env"],
1747
- // Its script in a heredoc.
1748
- ["sed -i -f - .env <<EOF\ns/^KEY=.*/KEY=Zq8vR2mK9xLp/\nEOF", `sed -i ${m} .env`, ".env"],
1749
- // The other in-place editors.
1750
- ["perl -p -i -e 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env", `sed -i ${m} .env`, ".env"],
1751
- [`ruby -pi -e 'sub("a","Zq8vR2mK9xLp")' .env`, `sed -i ${m} .env`, ".env"],
1752
- [`awk -i inplace '{print "KEY=Zq8vR2mK9xLp"}' .env`, `sed -i ${m} .env`, ".env"],
1753
- ];
1754
- for (const [command, expected, path] of cases) {
1755
- const out = throughCall("Bash", { command, description: "set the key" });
1756
- assert.equal(out.input.command, expected);
1757
- assert.equal(out.content, m, command);
1758
- assert.deepEqual(shellEditPaths(expected, "."), [path]);
1759
- }
1760
- const { lines } = summaryFor([
1761
- ...callAndResult("Bash", { command: "sed -i 's/^KEY=.*/KEY=Zq8vR2mK9xLp/' .env" }, ""),
1762
- { type: "user", message: { role: "user", content: "the key is Zq8vR2mK9xLp" } },
1763
- ]);
1764
- assert.ok(!lines.join("\n").includes("Zq8vR2mK9xLp"), "and the value it set goes from the session");
1765
- for (const [command, value] of [
1766
- ["sed -i 's|^KEY=.*|KEY=Zq8vR2mK9xLp|' .env", "Zq8vR2mK9xLp"],
1767
- ['sed -i "s#^KEY=.*#KEY=Zq8vR2mK9xLp#" .env', "Zq8vR2mK9xLp"],
1768
- ["sed -i 's,^KEY=.*,KEY=Zq8vR2mK9xLp,' .env", "Zq8vR2mK9xLp"],
1769
- ["sed -i 's/^KEY=.*/KEY=Zq8v#R2m|K9xLp/' .env", "Zq8v#R2m|K9xLp"],
1770
- ['VALUE=Zq8vR2mK9xLp; sed -i "s/^KEY=.*/KEY=$VALUE/" .env', "Zq8vR2mK9xLp"],
1771
- ['VALUE=Zq8vR2mK9xLp; echo "KEY=$VALUE" > .env', "Zq8vR2mK9xLp"],
1772
- ["sed -i '$a KEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1773
- ["sed -i '$ a KEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1774
- ["sed -i '$aKEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1775
- ["sed -i '$iKEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1776
- ["sed -i '$cKEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1777
- ["sed -i '1iKEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1778
- ["sed -i '/^KEY=/cKEY=Zq8vR2mK9xLp' .env", "Zq8vR2mK9xLp"],
1779
- ]) {
1780
- const session = summaryFor([
1781
- ...callAndResult("Bash", { command }, ""),
1782
- { type: "user", message: { role: "user", content: `the key is ${value}` } },
1783
- ]);
1784
- assert.ok(!session.lines.join("\n").includes(value), command);
1785
- }
1786
- const kept = "sed -i 's/a/b/' src/env.ts";
1787
- assert.equal(throughCall("Bash", { command: kept }, "plain output").input.command, kept);
1788
- });
1789
- test("an in-place edit of a dotenv file loses its script behind whatever runs it, over any lines, and through a glob", () => {
1790
- const m = marker("dotenv");
1791
- const value = "Hy4tN7wB2cQs";
1792
- const script = `'s/^K=.*/K=${value}/'`;
1793
- const cases = [
1794
- [`sudo -u app sed -i ${script} .env`, `sed -i ${m} .env`],
1795
- [`LC_ALL=C sed -i ${script} .env`, `sed -i ${m} .env`],
1796
- [`time sed -i ${script} .env`, `sed -i ${m} .env`],
1797
- [`/usr/bin/sed -i ${script} .env`, `sed -i ${m} .env`],
1798
- [`env LC_ALL=C timeout 5 sed -i ${script} .env`, `sed -i ${m} .env`],
1799
- [`(sed -i ${script} .env)`, `sed -i ${m} .env)`],
1800
- [`time perl -pi -e ${script} .env`, `sed -i ${m} .env`],
1801
- [`docker exec app sed -i ${script} /app/.env`, `sed -i ${m} /app/.env`],
1802
- [`kubectl exec pod -- sed -i ${script} /app/.env`, `sed -i ${m} /app/.env`],
1803
- // In the quotes of what runs it, among whatever else they hold.
1804
- [`ssh host "sed -i ${script} /srv/.env"`, `sed -i ${m} /srv/.env`],
1805
- [`bash -c "sed -i ${script} .env"`, `sed -i ${m} .env`],
1806
- [`sh -c 'sed -i "s/^K=.*/K=${value}/" .env'`, `sed -i ${m} .env`],
1807
- [`ssh host "cd /srv && sed -i ${script} .env && systemctl restart app"`, `sed -i ${m} .env`],
1808
- // The files come from in front of it.
1809
- [`find . -name .env -exec sed -i ${script} {} +`, `sed -i ${m} .env`],
1810
- [`find . -name '.env*' -exec sed -i ${script} {} \\;`, `sed -i ${m} .env*`],
1811
- [`find . -name '.env*' | xargs sed -i ${script}`, `sed -i ${m} .env*`],
1812
- [`ls .env* | xargs -n1 sed -i ${script}`, `sed -i ${m} .env*`],
1813
- [`grep -rl old --include='*.env' . | xargs sed -i 's/old/${value}/'`, `sed -i ${m} *.env`],
1814
- // Files a glob stands for.
1815
- [`sed -i ${script} .env*`, `sed -i ${m} .env*`],
1816
- [`sed -i ${script} apps/*/.env`, `sed -i ${m} apps/*/.env`],
1817
- // A line the shell joins to the next, and a quoted script over several.
1818
- [`sed -i ${script} \\\n .env`, `sed -i ${m} .env`],
1819
- [`sed -i \\\n ${script} \\\n .env`, `sed -i ${m} .env`],
1820
- [`sed -i \\\n -e ${script} \\\n -e 's/^B=.*/B=2/' \\\n .env`, `sed -i ${m} .env`],
1821
- [`sed -i '/^K=/c\\\nK=${value}' .env`, `sed -i ${m} .env`],
1822
- [`sed -i '$a\\\nK=${value}' .env`, `sed -i ${m} .env`],
1823
- [`sed -i 's/^A=.*/A=1/\ns/^K=.*/K=${value}/' .env`, `sed -i ${m} .env`],
1824
- [`perl -pi -e 's/^A=.*/A=1/;\ns/^K=.*/K=${value}/' .env`, `sed -i ${m} .env`],
1825
- ];
1826
- for (const [command, expected] of cases) {
1827
- const session = summaryFor([
1828
- ...callAndResult("Bash", { command, description: "set the key" }, ""),
1829
- { type: "user", message: { role: "user", content: `the key is ${value}` } },
1830
- ]);
1831
- const out = throughCall("Bash", { command, description: "set the key" });
1832
- assert.equal(out.input.command, expected, command);
1833
- assert.ok(!session.lines.join("\n").includes(value), command);
1834
- assert.equal(redactOne(expected), expected, command);
1835
- }
1836
- // What only speaks of an editor, or runs one on something else.
1837
- for (const kept of [
1838
- "git commit -m \"sed -i 's/a/b/' .env\"",
1839
- "cd app && git commit -m \"sed -i 's/a/b/' .env\"",
1840
- "echo \"sed -i 's/a/b/' .env\"",
1841
- "gh pr create --body \"run sed -i 's/a/b/' .env first\"",
1842
- "docker run --env-file .env app sed -i 's/a/b/' /etc/app.conf",
1843
- "find . -name '*.ts' -exec sed -i 's/process.env/x/' {} +",
1844
- "find . -name '*.ts' | xargs sed -i 's/.env/x/'",
1845
- "time sed -i 's/.env/x/' src/a.ts",
1846
- "time sed -i 's/a/b/' src/*.env.ts",
1847
- "ssh host \"sed -i 's/a/b/' /etc/app.conf\"",
1848
- "sed -n '1p' .env | sed -i 's/a/b/' src/a.ts",
1849
- "time sed 's/a/b/' .env",
1850
- "ruby -Ilib x.rb .env",
1851
- "sed -i 's/a/b/' src/a.ts \\\n src/b.ts",
1852
- ]) {
1853
- assert.equal(throughCall("Bash", { command: kept }, "plain output").input.command, kept);
1854
- }
1855
- assert.deepEqual(shellEditPaths(`sed -i ${m} .env*`, "."), [], "and a glob still names no file to the parse");
1856
- });
1857
- test("a dotenv file is one however a variable builds its path", () => {
1858
- const m = marker("dotenv");
1859
- const value = "Hy4tN7wB2cQs";
1860
- const write = `echo K=${value} > "$ENV_FILE"`;
1861
- const script = `'s/^K=.*/K=${value}/'`;
1862
- const cases = [
1863
- // A value in quotes is one word, and what a substitution turns up only says where it starts.
1864
- [`ENV_FILE="$(git rev-parse --show-toplevel)/.env"\n${write}`, `${m} > .env`],
1865
- [`ENV_FILE=$(git rev-parse --show-toplevel)/.env\n${write}`, `${m} > .env`],
1866
- [`ENV_FILE="$(dirname "$0")/.env"\n${write}`, `${m} > .env`],
1867
- [`ENV_FILE="$(dirname "$(realpath "$0")")/../.env.local"\n${write}`, `${m} > ../.env.local`],
1868
- [`ENV_FILE="/srv/My Project/.env"\n${write}`, `${m} > Project/.env`],
1869
- [`ENV_FILE='/srv/My Project/.env'\n${write}`, `${m} > Project/.env`],
1870
- // One variable from another.
1871
- [`A=.env; B=$A\necho K=${value} > "$B"`, `${m} > .env`],
1872
- [`A=.env\nB="\${A}"\necho K=${value} >> "\${B}"`, `${m} > .env`],
1873
- // A default.
1874
- [`echo K=${value} > "\${ENV_FILE:-.env}"`, `${m} > .env`],
1875
- [`echo K=${value} > \${ENV_FILE-.env}`, `${m} > .env`],
1876
- [`echo K=${value} > "\${ENV_FILE:=.env.local}"`, `${m} > .env.local`],
1877
- [`ENV_FILE="\${ENV_FILE:-.env}"\n${write}`, `${m} > .env`],
1878
- // A name that comes from a variable.
1879
- [`echo K=${value} > "$STAGE.env"`, `${m} > $STAGE.env`],
1880
- [`echo K=${value} > "\${STAGE}.env"`, `${m} > \${STAGE}.env`],
1881
- [`echo K=${value} > config/$STAGE.env`, `${m} > config/$STAGE.env`],
1882
- [`echo K=${value} | tee "$STAGE.env"`, `${m} > $STAGE.env`],
1883
- [`F="$STAGE.env"\necho K=${value} > "$F"`, `${m} > $STAGE.env`],
1884
- // And an edit named any of these ways.
1885
- [`sed -i ${script} "$STAGE.env"`, `sed -i ${m} .env`],
1886
- [`sed -i ${script} "\${STAGE}.env"`, `sed -i ${m} .env`],
1887
- [`sed -i ${script} "\${F:-.env}"`, `sed -i ${m} .env`],
1888
- [`A=.env; B=$A; sed -i ${script} "$B"`, `sed -i ${m} .env`],
1889
- ];
1890
- for (const [command, expected] of cases) {
1891
- const session = summaryFor([
1892
- ...callAndResult("Bash", { command }, ""),
1893
- { type: "user", message: { role: "user", content: `the key is ${value}` } },
1894
- ]);
1895
- assert.equal(throughCall("Bash", { command }, "plain output").input.command, expected, command);
1896
- assert.ok(!session.lines.join("\n").includes(value), command);
1897
- assert.equal(throughCall("Bash", { command: expected }, "plain output").input.command, expected, "and a second pass changes nothing");
1898
- }
1899
- // What a variable builds into another file stays, and so does a quote that only closes.
1900
- for (const kept of [
1901
- `F="$(pwd)/a.txt"; echo K=${value} > "$F"`,
1902
- `F="/srv/My Project/a.txt"; echo K=${value} > "$F"`,
1903
- `A=.env; B=a.txt; echo K=${value} > "$B"`,
1904
- `echo K=${value} > "\${F:-a.txt}"`,
1905
- `echo K=${value} > "$STAGE.envelope"`,
1906
- `echo K=${value} > "$STAGE.environment"`,
1907
- `echo "$config.env is not set" > a.txt`,
1908
- `K="$(cat .env)"; echo "$K" > a.txt`,
1909
- ]) {
1910
- assert.equal(throughCall("Bash", { command: kept }, "plain output").input.command, kept);
1911
- }
1912
- assert.equal(throughCall("Bash", { command: `echo "a=b" > x; F=.env; echo "K=${value}" > $F` }, "plain output").input.command, `${m} > .env`);
1913
- });
1914
- test("cuts what a ! command typed in Claude Code printed from a dotenv file, or wrote into one", () => {
1915
- const turn = (content) => ({ type: "user", message: { role: "user", content } });
1916
- const { lines } = summaryFor([
1917
- turn("<bash-input>cat .env</bash-input>"),
1918
- turn("<bash-stdout>DB_PASS=Zq8vR2mK9xLp</bash-stdout><bash-stderr></bash-stderr>"),
1919
- turn("<bash-input>echo DB_PASS=Zq8vR2mK9xLp | tee .env</bash-input>"),
1920
- turn("<bash-stdout>DB_PASS=Zq8vR2mK9xLp</bash-stdout><bash-stderr></bash-stderr>"),
1921
- turn("<bash-input>ls</bash-input>"),
1922
- turn("<bash-stdout>README.md</bash-stdout><bash-stderr></bash-stderr>"),
1923
- ]);
1924
- const content = lines.map((line) => JSON.parse(line).message.content);
1925
- assert.deepEqual(content, [
1926
- "<bash-input>cat .env</bash-input>",
1927
- marker("dotenv"),
1928
- `<bash-input>${marker("dotenv")} > .env</bash-input>`,
1929
- marker("dotenv"),
1930
- "<bash-input>ls</bash-input>",
1931
- "<bash-stdout>README.md</bash-stdout><bash-stderr></bash-stderr>",
1932
- ]);
1933
- });
1934
- test("cuts the result of a search on a dotenv file, whichever argument names the file", () => {
1935
- const cut = [
1936
- ["Grep", { pattern: "PHRASE", path: ".env", output_mode: "content" }],
1937
- ["Grep", { pattern: "PHRASE", path: "/repo/.env", output_mode: "content" }],
1938
- ["Grep", { pattern: "PHRASE", path: "config/.env.local", output_mode: "content" }],
1939
- ...[".env*", "**/.env*", "{.env,.env.local}", "*.env", "config/.env.*"].map((glob) => ["Grep", { pattern: "PHRASE", glob, output_mode: "content" }]),
1940
- ["mcp__fs__read_file", { path: ".env" }],
1941
- ["mcp__fs__read_multiple_files", { paths: ["README.md", "app/.env.local"] }],
1942
- ["mcp__serena__read_file", { relative_path: ".env" }],
1943
- ];
1944
- for (const [name, input] of cut) {
1945
- const out = throughCall(name, input);
1946
- assert.equal(out.content, marker("dotenv"), JSON.stringify(input));
1947
- assert.equal(out.toolUseResult, marker("dotenv"), JSON.stringify(input));
1948
- assert.deepEqual(out.input, input, "the call itself names the file, not what is in it");
1949
- }
1950
- const kept = [
1951
- ["Grep", { pattern: "\\.env", path: "src", output_mode: "content" }],
1952
- ["Grep", { pattern: "process.env", glob: "*.ts", output_mode: "content" }],
1953
- ["Glob", { pattern: "**/.env*" }],
1954
- ["mcp__fs__read_file", { path: "README.md" }],
1955
- ["Agent", { prompt: "read .env and report", description: "look at .env" }],
1956
- ];
1957
- for (const [name, input] of kept) {
1958
- assert.equal(throughCall(name, input, "plain output").content, "plain output", JSON.stringify(input));
1959
- }
1960
- });
1961
- test("a write through any tool that names a dotenv file loses what it writes", () => {
1962
- const out = throughCall("mcp__fs__write_file", { path: "app/.env", content: "MAILER_PHRASE=Zq8vR2mK9xLp" });
1963
- assert.deepEqual(out.input, { path: "app/.env", content: marker("dotenv") });
1964
- });
1965
- test("a multi-edit on a dotenv file loses every step's strings, and its steps stay edits", () => {
1966
- const edits = [
1967
- { old_string: "MAILER_PHRASE=old1234567890", new_string: "MAILER_PHRASE=Zq8vR2mK9xLp" },
1968
- { old_string: "OTHER=old1234567890", new_string: "OTHER=Zq8vR2mK9xLp", replace_all: true },
1969
- ];
1970
- const record = { type: "assistant", uuid: "a", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "MultiEdit", input: { file_path: "/repo/.env", edits } }] } };
1971
- const { lines } = summaryFor([record]);
1972
- const m = marker("dotenv");
1973
- const call = JSON.parse(lines[0]);
1974
- assert.deepEqual(call.message.content[0].input.edits, [
1975
- { old_string: m, new_string: m },
1976
- { old_string: m, new_string: m, replace_all: true },
1977
- ]);
1978
- assert.equal(parseLines(lines).fileEdits.get("/repo/.env")?.length, 2);
1979
- });
1980
- test("leaves a tool_result alone when the file is not a dotenv file", () => {
1981
- const { lines } = summaryFor([
1982
- {
1983
- type: "assistant",
1984
- message: {
1985
- role: "assistant",
1986
- content: [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/repo/readme.md" } }],
1987
- },
1988
- },
1989
- {
1990
- type: "user",
1991
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: "plain docs" }] },
1992
- },
1993
- ]);
1994
- const result = JSON.parse(lines[1]);
1995
- assert.equal(result.message.content[0]?.content, "plain docs");
1996
- });
1997
- test("applies extra patterns from configuration", () => {
1998
- const out = redactOne("internal ACME-9F3K2 reference", ["ACME-[A-Z0-9]{5}"]);
1999
- assert.equal(out, `internal ${marker("custom")} reference`);
2000
- });
2001
- test("reports an invalid extra pattern instead of throwing", () => {
2002
- const { summary } = redactLines([JSON.stringify({ type: "user" })], { extraPatterns: ["([unclosed"] });
2003
- assert.deepEqual(summary.invalidExtraPatterns, ["([unclosed"]);
2004
- });
2005
- test("redacts a line that is not valid JSON without dropping it", () => {
2006
- const { lines, summary } = redactLines(["not json at all sk-abcdefghij0123456789XYZ"]);
2007
- assert.equal(lines.length, 1);
2008
- assert.equal(lines[0], `not json at all ${marker("openai_key")}`);
2009
- assert.equal(summary.unparsedLines, 1);
2010
- });
2011
- test("a record with U+2028 in a string is read whole, and its key-named secret redacted", async () => {
2012
- // JSON.stringify leaves U+2028 and U+2029 unescaped; node:readline ends a
2013
- // line at both, and the record came apart into two fragments.
2014
- const dir = await mkdtemp(join(tmpdir(), "isy-redact-"));
2015
- const file = join(dir, "session.jsonl");
2016
- const record = { type: "user", toolUseResult: { stdout: "one\u2028two\u2029three", token: "abcdefgh12345678" } };
2017
- await writeFile(file, `${JSON.stringify({ type: "user" })}\r\n${JSON.stringify(record)}\n`);
2018
- const { lines, summary } = await redactTranscriptFile(file);
2019
- assert.equal(lines.length, 2);
2020
- assert.equal(summary.unparsedLines, 0);
2021
- assert.ok(!lines[1].includes("abcdefgh12345678"));
2022
- assert.equal(JSON.parse(lines[1]).toolUseResult.stdout, "one\u2028two\u2029three");
2023
- const session = await parseTranscriptFile(file);
2024
- assert.equal(session.records.length, 2);
2025
- assert.equal(session.skipped.malformedJson, 0);
2026
- await rm(dir, { recursive: true, force: true });
2027
- });
2028
- test("masks email addresses", () => {
2029
- assert.equal(redactOne("Author: Alice <alice.b+isy@mail.example.org>"), `Author: Alice <${marker("email")}>`);
2030
- // Not an address: no domain, or a version where the domain would be.
2031
- assert.equal(redactOne("@nightloom/isy@1.0.1 and @scope/pkg"), "@nightloom/isy@1.0.1 and @scope/pkg");
2032
- });
2033
- test("an attachment record keeps its place in the chain and loses everything but its type", () => {
2034
- // What Claude Code 2.1.285 writes as context for the model: the account's
2035
- // email, its organisation, the system prompt.
2036
- const records = [
2037
- { type: "attachment", uuid: "a1", parentUuid: null, attachment: { type: "session_context", context: { userEmail: "The user's email address is alice@example.org." } }, rendered: [{ content: "alice@example.org" }], renderedRole: "user" },
2038
- { type: "attachment", uuid: "a2", parentUuid: "a1", attachment: { type: "credential_org", organizationUuid: "1b4e28ba-2fa1-11d2-883f-0016d3cca427" } },
2039
- { type: "attachment", uuid: "a3", parentUuid: "a2", attachment: { type: "prompt_snapshot", systemPrompt: "You are Claude Code. # claudeMd ..." } },
2040
- ];
2041
- const { lines } = summaryFor(records);
2042
- const out = lines.map((line) => JSON.parse(line));
2043
- assert.deepEqual(out, [
2044
- { type: "attachment", uuid: "a1", parentUuid: null, attachment: { type: "session_context" } },
2045
- { type: "attachment", uuid: "a2", parentUuid: "a1", attachment: { type: "credential_org" } },
2046
- { type: "attachment", uuid: "a3", parentUuid: "a2", attachment: { type: "prompt_snapshot" } },
2047
- ]);
2048
- });
2049
- test("a recorded request keeps only the fields that name it", () => {
2050
- // What Claude Code 2.1.312 writes beside each API call.
2051
- const records = [
2052
- { type: "api-request-shape", sessionId: "s1", shapeHash: "h1", timestamp: "2026-10-29T20:00:00.000Z", version: "2.1.312", shape: { system: [{ type: "text", text: "You are Claude Code. # claudeMd ..." }], tools: [{ name: "Bash", description: "..." }] } },
2053
- { type: "api-request-blob", sessionId: "s1", hash: "h2", message: { role: "user", content: [{ type: "text", text: "The user's email address is alice@example.org." }] } },
2054
- { type: "api-request", sessionId: "s1", id: "r1", timestamp: "2026-10-29T20:00:01.000Z", version: "2.1.312", shapeHash: "h1", params: { model: "claude-opus-5-5", max_tokens: 64000, safeguards: [{ type: "classifier", classifier_context: "User: alice. Home: /home/alice. Allow: Bash(npm test)" }] }, tail: ["h2"] },
2055
- ];
2056
- const { lines } = summaryFor(records);
2057
- const out = lines.map((line) => JSON.parse(line));
2058
- assert.deepEqual(out, [
2059
- { type: "api-request-shape", sessionId: "s1", shapeHash: "h1", timestamp: "2026-10-29T20:00:00.000Z", version: "2.1.312" },
2060
- { type: "api-request-blob", sessionId: "s1", hash: "h2" },
2061
- { type: "api-request", sessionId: "s1", timestamp: "2026-10-29T20:00:01.000Z", version: "2.1.312", shapeHash: "h1" },
2062
- ]);
2063
- });
2064
- test("a subagent's recorded request still reads as the subagent's", () => {
2065
- const { lines } = summaryFor([
2066
- { type: "api-request", sessionId: "s1", agentId: "a1b2", isSidechain: true, parentToolUseID: "toolu_1", params: { model: "claude-opus-5-5" } },
2067
- ]);
2068
- assert.deepEqual(JSON.parse(lines[0]), { type: "api-request", sessionId: "s1", agentId: "a1b2", isSidechain: true, parentToolUseID: "toolu_1" });
2069
- });
2070
- test("bridge and artifact records go up without the account and organisation they name", () => {
2071
- // What Claude Code 2.1.312 writes for the session's bridge and its artifacts.
2072
- const records = [
2073
- { type: "bridge-session", sessionId: "s1", bridgeSessionId: "b1", lastSequenceNum: 3, ownerAccountUuid: "1b4e28ba-2fa1-11d2-883f-0016d3cca427", ownerOrganizationUuid: "6fa459ea-ee8a-3ca4-894e-db77e160355e" },
2074
- { type: "artifact-autoreact-ledger", sessionId: "s1", v: 1, accountUuid: "1b4e28ba-2fa1-11d2-883f-0016d3cca427", artifacts: {} },
2075
- ];
2076
- const { lines } = summaryFor(records);
2077
- assert.deepEqual(lines.map((line) => JSON.parse(line)), [
2078
- { type: "bridge-session", sessionId: "s1" },
2079
- { type: "artifact-autoreact-ledger", sessionId: "s1" },
2080
- ]);
2081
- });
2082
- test("a turn loses the harness fields beside it, and a dotenv write its second copy", () => {
2083
- // What Claude Code 2.1.31x writes beside a call and beside its result.
2084
- const write = { file_path: "/repo/.env", content: "STRIPE_LIVE=pk_live_totally_real" };
2085
- const records = [
2086
- {
2087
- type: "assistant",
2088
- uuid: "a",
2089
- message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Write", input: write }] },
2090
- wireToolInputs: { t1: write },
2091
- wireIngestContext: { t1: { cwd: "/repo" } },
2092
- serverClassifierRequest: "User: alice. Write /repo/.env",
2093
- },
2094
- {
2095
- type: "user",
2096
- uuid: "b",
2097
- parentUuid: "a",
2098
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: "ok" }] },
2099
- serverClassifierContext: { request: "Write", context: { git_state: { cwd: "/repo", branch: "main" }, live_cwd: "/repo", platform: "linux" } },
2100
- permissionDecision: { decision: "allow", source: "rule", reasonType: "rule" },
2101
- },
2102
- ];
2103
- const { lines } = summaryFor(records);
2104
- const out = lines.map((line) => JSON.parse(line));
2105
- assert.deepEqual(Object.keys(out[0]), ["type", "uuid", "message"]);
2106
- assert.deepEqual(Object.keys(out[1]), ["type", "uuid", "parentUuid", "message"]);
2107
- assert.ok(!lines[0].includes("pk_live_totally_real"));
2108
- });
2109
- test("a dotenv file read through an MCP tool leaves nothing in what the tool returned as data", () => {
2110
- const value = "Zq8vR2mK9xLp";
2111
- const dotenv = `DB_HOST=db.internal\nshortpw=hunter2\nmailer_phrase=correct horse battery\nMAILER_PHRASE=${value}\n`;
2112
- const { lines } = summaryFor([
2113
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "mcp__fs__read_file", input: { path: ".env" } }] } },
2114
- {
2115
- type: "user",
2116
- message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: [{ type: "text", text: "read" }] }] },
2117
- toolUseResult: [{ type: "text", text: "read" }],
2118
- mcpMeta: { structuredContent: { result: dotenv } },
2119
- },
2120
- { type: "user", message: { role: "user", content: `the phrase is ${value}` } },
2121
- ]);
2122
- const out = lines.join("\n");
2123
- for (const held of ["db.internal", "hunter2", "correct horse battery", value])
2124
- assert.ok(!out.includes(held), held);
2125
- assert.deepEqual(Object.keys(JSON.parse(lines[1])).includes("mcpMeta"), false);
2126
- });
2127
- test("a harness message in the user's place keeps its place in the chain and loses its text", () => {
2128
- // What Claude Code writes after `/review`: the developer's command, then the
2129
- // skill's body and a reminder as `isMeta` user records.
2130
- const records = [
2131
- { type: "user", uuid: "u1", parentUuid: null, message: { role: "user", content: "<command-name>/review</command-name>\n<command-args>src/a.ts</command-args>" } },
2132
- { type: "user", uuid: "u2", parentUuid: "u1", isMeta: true, sourceToolUseID: "t1", message: { role: "user", content: [{ type: "text", text: "Base directory for this skill: /home/alice/.claude/skills/review\n# Review\nRead every file..." }] } },
2133
- { type: "user", uuid: "u3", parentUuid: "u2", isMeta: true, message: { role: "user", content: "<system-reminder>The user's email address is alice@example.org.</system-reminder>" } },
2134
- { type: "assistant", uuid: "a1", parentUuid: "u3", message: { role: "assistant", content: [{ type: "text", text: "Reviewing." }] } },
2135
- ];
2136
- const { lines } = summaryFor(records);
2137
- const out = lines.map((line) => JSON.parse(line));
2138
- assert.deepEqual(out[0], records[0]);
2139
- assert.deepEqual(out[1], { ...records[1], message: { role: "user", content: marker("meta") } });
2140
- assert.deepEqual(out[2], { ...records[2], message: { role: "user", content: marker("meta") } });
2141
- assert.deepEqual(out[3], records[3]);
2142
- assert.deepEqual([...parseLines(lines).mainPath], ["a1", "u3", "u2", "u1"]);
2143
- });
2144
- test("the server can re-run the rules over a stored transcript, with no roots of its own", async () => {
2145
- const pkg = JSON.parse(await readFile(new URL("../../package.json", import.meta.url), "utf8"));
2146
- assert.equal(pkg.exports["./redact"], "./dist/redact.js");
2147
- // Uploaded by an older client: its paths already masked, a key it did not know.
2148
- const stored = JSON.stringify({ type: "user", cwd: ".", message: { role: "user", content: `deploy sk_live_${"a1".repeat(12)} from /root/app, ~/x and src/a.ts` } });
2149
- const expected = `deploy ${marker("stripe_key")} from /root/app, ~/x and src/a.ts`;
2150
- for (const options of [undefined, { home: "" }]) {
2151
- const { lines } = redactLines([stored], options);
2152
- assert.equal(JSON.parse(lines[0]).message.content, expected);
2153
- }
2154
- });
2155
- test("keeps output parseable as JSONL and counts what it replaced", () => {
2156
- const { lines, summary } = summaryFor([
2157
- { type: "user", message: { role: "user", content: 'password = "hunter2000"' } },
2158
- { type: "assistant", message: { role: "assistant", content: [{ type: "text", text: `AIza${"x".repeat(35)}` }] } },
2159
- ]);
2160
- for (const line of lines)
2161
- assert.doesNotThrow(() => JSON.parse(line));
2162
- assert.equal(summary.counts.assignment, 1);
2163
- assert.equal(summary.counts.google_key, 1);
2164
- assert.equal(summary.replacements, 2);
2165
- assert.equal(summary.lines, 2);
2166
- });
2167
- test("every builtin rule replaces with the shared marker prefix", () => {
2168
- for (const rule of BUILTIN_RULES) {
2169
- assert.ok(rule.replacement.includes(REDACTION_MARKER), `${rule.type} lacks the marker`);
2170
- assert.ok(rule.pattern.global, `${rule.type} must be a global pattern`);
2171
- }
2172
- });
2173
- test("masks absolute paths in a record, learning the root from the record itself", () => {
2174
- const record = {
2175
- type: "assistant",
2176
- cwd: "/home/alice/work/isy",
2177
- message: {
2178
- role: "assistant",
2179
- content: [
2180
- { type: "tool_use", name: "Edit", input: { file_path: "/home/alice/work/isy/src/a.ts" } },
2181
- { type: "tool_use", name: "Bash", input: { command: "cd /home/alice/work/isy && npm test" } },
2182
- ],
2183
- },
2184
- };
2185
- const { lines, summary } = redactLines([JSON.stringify(record)]);
2186
- const parsed = JSON.parse(lines[0]);
2187
- // The working directory is the one thing every record repeats, so it is the
2188
- // root even when the hook did not say where the session ran.
2189
- assert.equal(parsed.cwd, ".");
2190
- assert.equal(parsed.message.content[0].input.file_path, "src/a.ts");
2191
- assert.equal(parsed.message.content[1].input.command, "cd . && npm test");
2192
- assert.ok((summary.counts.path ?? 0) >= 3);
2193
- });
2194
- test("masks a path carried as a JSON object key", () => {
2195
- // Claude Code keys its per-file state by absolute path. Redaction walked the
2196
- // values only, so the account name stayed in the key of every such map.
2197
- const record = {
2198
- type: "assistant",
2199
- cwd: "/home/alice/work/isy",
2200
- toolUseResult: {
2201
- "/home/alice/work/isy/src/a.ts": { readAt: 1 },
2202
- "/home/alice/.claude/settings.json": { readAt: 2 },
2203
- },
2204
- };
2205
- const { lines } = redactLines([JSON.stringify(record)]);
2206
- const parsed = JSON.parse(lines[0]);
2207
- assert.deepEqual(Object.keys(parsed.toolUseResult), ["src/a.ts", "~/.claude/settings.json"]);
2208
- });
2209
- test("the working directory the caller names wins over the one on the record", () => {
2210
- const record = { type: "user", cwd: "/home/alice/elsewhere", message: { role: "user", content: "/home/alice/work/isy/src/a.ts" } };
2211
- const { lines } = redactLines([JSON.stringify(record)], { cwd: "/home/alice/work/isy" });
2212
- const parsed = JSON.parse(lines[0]);
2213
- assert.equal(parsed.message.content, "src/a.ts");
2214
- });
2215
- test("a stored transcript's relative working directory is no root to learn", () => {
2216
- // Masked on the client: the session started in `src`, and these are other files.
2217
- const content = "cat ./src/a.ts ../src/b.ts /src/c.ts";
2218
- const stored = JSON.stringify({ type: "user", cwd: "src", message: { role: "user", content } });
2219
- const { lines } = redactLines([stored], { home: "" });
2220
- const parsed = JSON.parse(lines[0]);
2221
- assert.equal(parsed.message.content, content);
2222
- assert.equal(parsed.cwd, "src");
2223
- });
2224
- test("a relative name in a path field is read from where the session stood", () => {
2225
- const input = {
2226
- file_path: "src/a.ts",
2227
- path: "../test/b.ts",
2228
- file: "/home/alice/repo/pkg/c.ts",
2229
- notebook_path: "~/notes.ipynb",
2230
- filePath: "https://example.com/d.ts",
2231
- command: "cat src/a.ts",
2232
- };
2233
- const stored = JSON.stringify({ type: "assistant", cwd: "/home/alice/repo/pkg", message: { content: [{ type: "tool_use", input }] } });
2234
- const options = { cwd: "/home/alice/repo", base: "/home/alice/repo/pkg" };
2235
- const { lines } = redactLines([stored], options);
2236
- const parsed = JSON.parse(lines[0]);
2237
- assert.equal(parsed.cwd, "pkg");
2238
- assert.deepEqual(parsed.message.content[0].input, {
2239
- file_path: "pkg/src/a.ts",
2240
- path: "test/b.ts",
2241
- file: "pkg/c.ts",
2242
- notebook_path: "~/notes.ipynb",
2243
- filePath: "https://example.com/d.ts",
2244
- command: "cat src/a.ts",
2245
- });
2246
- // Standing at the root there is nothing to read them from: left as they were typed.
2247
- const atRoot = redactLines([stored], { cwd: "/home/alice/repo" });
2248
- const typed = JSON.parse(atRoot.lines[0]);
2249
- assert.equal(typed.message.content[0].input.file_path, "src/a.ts");
2250
- });
2251
- test("masks a home path in a transcript that never says where it ran", () => {
2252
- const out = redactOne("failed to open /Users/dave/Library/Caches/pnpm/store");
2253
- assert.equal(out, "failed to open ~/Library/Caches/pnpm/store");
2254
- });
2255
- test("a secret and a path in one string do not interfere", () => {
2256
- const out = redactOne("wrote sk-abcdefghij0123456789XYZ to /home/alice/.netrc");
2257
- assert.equal(out, `wrote ${marker("openai_key")} to ~/.netrc`);
2258
- });
2259
- test("leaves ordinary source code untouched", () => {
2260
- const code = "const total = items.reduce((sum, item) => sum + item.price, 0);";
2261
- assert.equal(applyRules(code, BUILTIN_RULES), code);
2262
- });
2263
- const corpus = join(homedir(), ".claude", "projects");
2264
- test("redacts real transcripts without corrupting them", { skip: !existsSync(corpus) }, async () => {
2265
- const projects = await readdir(corpus);
2266
- const files = [];
2267
- for (const project of projects) {
2268
- let entries;
2269
- try {
2270
- entries = await readdir(join(corpus, project));
2271
- }
2272
- catch {
2273
- continue;
2274
- }
2275
- for (const entry of entries) {
2276
- if (entry.endsWith(".jsonl"))
2277
- files.push(join(corpus, project, entry));
2278
- }
2279
- }
2280
- assert.ok(files.length > 0);
2281
- let redactedLines = 0;
2282
- for (const file of files) {
2283
- const { lines, summary } = await redactTranscriptFile(file);
2284
- redactedLines += lines.length;
2285
- assert.equal(summary.invalidExtraPatterns.length, 0);
2286
- // A line that came in broken — a transcript cut off mid-write — goes out
2287
- // broken and is counted; every other one has to come out as JSON.
2288
- const broken = lines.filter((line) => {
2289
- try {
2290
- JSON.parse(line);
2291
- return false;
2292
- }
2293
- catch {
2294
- return true;
2295
- }
2296
- });
2297
- assert.ok(broken.length <= summary.unparsedLines, `${file} produced invalid JSON`);
2298
- }
2299
- assert.ok(redactedLines > 0);
2300
- });
2301
- test("redacted output still parses into the same session structure", { skip: !existsSync(corpus) }, async (t) => {
2302
- const projects = await readdir(corpus);
2303
- // Both parses read one copy: a session still being written grows between two
2304
- // reads of its file, and the counts would differ for no fault of redaction.
2305
- const scratch = await mkdtemp(join(tmpdir(), "isy-structure-"));
2306
- t.after(() => rm(scratch, { recursive: true, force: true }));
2307
- const file = join(scratch, "session.jsonl");
2308
- let checked = 0;
2309
- for (const project of projects) {
2310
- let entries;
2311
- try {
2312
- entries = await readdir(join(corpus, project));
2313
- }
2314
- catch {
2315
- continue;
2316
- }
2317
- for (const entry of entries) {
2318
- if (!entry.endsWith(".jsonl"))
2319
- continue;
2320
- const source = join(corpus, project, entry);
2321
- await copyFile(source, file);
2322
- const original = await parseTranscriptFile(file);
2323
- const { lines } = await redactTranscriptFile(file);
2324
- const redacted = parseLines(lines);
2325
- assert.equal(redacted.records.length, original.records.length, `${source} lost records`);
2326
- assert.equal(redacted.toolUses.length, original.toolUses.length, `${source} lost tool calls`);
2327
- // Edits, not files: a `sed -i` path is resolved from the command's text,
2328
- // which masking rewrites, so two keys for one file can become one.
2329
- const edits = (session) => [...session.fileEdits.values()].reduce((sum, list) => sum + list.length, 0);
2330
- assert.equal(edits(redacted), edits(original), `${source} lost edits`);
2331
- assert.equal(redacted.mainPath.size, original.mainPath.size, `${source} lost tree structure`);
2332
- checked += 1;
2333
- }
2334
- }
2335
- assert.ok(checked > 0);
2336
- });
2337
- test("a command of many statements and a sweep of many values cost what their text is long", () => {
2338
- const timed = (lines) => {
2339
- const started = performance.now();
2340
- const out = redactLines(lines).lines;
2341
- const took = performance.now() - started;
2342
- assert.ok(took < 5000, `took ${Math.round(took)}ms on ${lines[0].slice(0, 60)}…`);
2343
- return out;
2344
- };
2345
- const bash = (command) => JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command } }] } });
2346
- for (const command of [`${";".repeat(1_000_000)} .env`, `for f in ${";".repeat(1_000_000)} > .env`, `${"&&".repeat(500_000)} .env`, `${"|".repeat(1_000_000)} .env`]) {
2347
- assert.equal(timed([bash(command)]).length, 1);
2348
- }
2349
- const sweep = (values, text, times) => {
2350
- const read = callAndResult("Read", { file_path: "/repo/.env" }, values.map((value, i) => `K${i}=${value}`).join("\n")).map((record) => JSON.stringify(record));
2351
- const said = Array.from({ length: times }, () => JSON.stringify({ type: "user", message: { role: "user", content: text } }));
2352
- return timed([...read, ...said]).join("\n");
2353
- };
2354
- const values = Array.from({ length: 500 }, (_, i) => `a${i}b${String(i).padStart(12, "0")}xyz`);
2355
- const out = sweep(values, values.join(" ").repeat(Math.ceil(200_000 / values.join(" ").length)), 100);
2356
- for (const value of values)
2357
- assert.ok(!out.includes(value), value);
2358
- // Values that start alike, in a text that goes on as they do.
2359
- const alike = Array.from({ length: 500 }, (_, i) => `AAAAAAAA${i}${"A".repeat(i % 50)}xyz`);
2360
- sweep(alike, "A".repeat(200_000), 5);
2361
- sweep(alike, "AAAAAAAA1".repeat(22_000), 5);
2362
- });
2363
- test("a sweep cuts every learned value wherever it sits, the longest of those that start together", () => {
2364
- const m = marker("dotenv");
2365
- const short = "Zq8vR2mK";
2366
- const long = "Zq8vR2mK9xLpWq7T";
2367
- const inner = "Hy4tN7wB2cQs";
2368
- const outer = "xxHy4tN7wB2cQsyy";
2369
- const escaped = "ab\\cTp3sV8nJ";
2370
- const wide = "пароль-Zq8vR2mK9xLq";
2371
- const dotenv = [`A_PASSWORD=${short}`, `B_PASSWORD=${long}`, `C_PASSWORD=${inner}`, `D_PASSWORD=${outer}`, `E_PASSWORD=${escaped}`, `F_PASSWORD=${wide}`].join("\n");
2372
- const cases = [
2373
- [short, m],
2374
- [long, m],
2375
- [`${long}${short}${inner}`, `${m}${m}${m}`],
2376
- [`${short}${short}`, `${m}${m}`],
2377
- [`at ${outer} and ${inner}.`, `at ${m} and ${m}.`],
2378
- [`Zq8vR2m ${short.slice(1)} Zq8vR2mK`, `Zq8vR2m ${short.slice(1)} ${m}`],
2379
- [escaped, m],
2380
- [`before ${wide} after`, `before ${m} after`],
2381
- ["nothing learned here", "nothing learned here"],
2382
- ];
2383
- for (const [said, expected] of cases) {
2384
- const { lines } = summaryFor([...callAndResult("Read", { file_path: "/repo/.env" }, dotenv), { type: "user", uuid: "u", message: { role: "user", content: said } }]);
2385
- assert.equal(JSON.parse(lines.at(-1)).message.content, expected);
2386
- }
2387
- });
2388
- test("every rule gets through long runs it has nothing to match in", () => {
2389
- // Shapes a tool's output holds by the hundred kilobytes: a version list, a
2390
- // URL-encoded query, dotted and dashed names. A rule that rescans the rest of
2391
- // the run from every boundary in it takes minutes over one transcript.
2392
- const shapes = ["1.".repeat(50_000), "%2F".repeat(35_000), "a.".repeat(50_000), "ab-".repeat(35_000)];
2393
- for (const text of shapes) {
2394
- for (const rule of BUILTIN_RULES) {
2395
- const started = performance.now();
2396
- applyRules(text, [rule]);
2397
- const took = performance.now() - started;
2398
- assert.ok(took < 1000, `${rule.type} took ${Math.round(took)}ms on ${text.slice(0, 6)}…`);
2399
- }
2400
- }
2401
- });
2402
- test("every rule gets through text shaped almost like what it matches", () => {
2403
- const fill = (unit) => unit.repeat(Math.ceil(200_000 / unit.length));
2404
- const shapes = [
2405
- fill("--password "),
2406
- fill("--password-"),
2407
- `--password${" ".repeat(200_000)}`,
2408
- `--password ${"a".repeat(200_000)}"`,
2409
- fill("a://:"),
2410
- fill("a://"),
2411
- fill("a://:b"),
2412
- `redis://:${"a".repeat(200_000)}`,
2413
- `redis://${"a".repeat(200_000)}`,
2414
- `redis://:${fill("a:")}`,
2415
- fill("curl "),
2416
- fill("curl -u "),
2417
- fill("curl -u a "),
2418
- `curl -u ${"a".repeat(200_000)}`,
2419
- `curl -u ${" ".repeat(200_000)}`,
2420
- fill("curl -ku "),
2421
- fill("curl -fsSLu a:"),
2422
- `curl -${"k".repeat(200_000)}`,
2423
- fill(`curl -${"k".repeat(1_000)} `),
2424
- `curl -u '${fill("a:")}`,
2425
- `curl -u 'a:${fill("b ")}`,
2426
- fill("curl -u 'a:b "),
2427
- fill(`curl -u "a:`),
2428
- `curl -u a:'${fill("b ")}`,
2429
- fill("curl -u a:'b "),
2430
- fill("sshpass -p '"),
2431
- `sshpass -p '${fill("b ")}`,
2432
- fill("sshpass -p 'b "),
2433
- fill("mysql -p'a "),
2434
- fill("curl --user "),
2435
- fill("mysql "),
2436
- fill("mysql -p "),
2437
- fill("sshpass "),
2438
- fill("sshpass -p "),
2439
- fill("docker login "),
2440
- fill("redis-cli -a "),
2441
- fill("bearer "),
2442
- `bearer ${"a".repeat(200_000)}`,
2443
- `bearer ${" ".repeat(200_000)}`,
2444
- fill("bearer a."),
2445
- fill("authorization: "),
2446
- fill("authorization:"),
2447
- fill("authorization: a "),
2448
- `authorization:${" ".repeat(200_000)}`,
2449
- `authorization: a${" ".repeat(200_000)}`,
2450
- `authorization: ${"a".repeat(200_000)}`,
2451
- `authorization: ${"a-".repeat(100_000)}`,
2452
- fill("cookie: "),
2453
- fill('cookie: "'),
2454
- fill("cookie: <"),
2455
- `cookie:${" ".repeat(200_000)}`,
2456
- `cookie: ${"a".repeat(200_000)}`,
2457
- fill("pass"),
2458
- fill("_pass_"),
2459
- fill("aPass"),
2460
- fill("byPass"),
2461
- fill("_pwd"),
2462
- fill("auth-"),
2463
- fill("cookie."),
2464
- fill("a.pwd"),
2465
- fill("a-pwd "),
2466
- fill("pass: "),
2467
- fill("pass: a"),
2468
- fill("pass:"),
2469
- `pass: ${"a".repeat(200_000)}1`,
2470
- `pass:${" ".repeat(200_000)}a1`,
2471
- fill("auth = "),
2472
- fill("auth ="),
2473
- fill("cookie: a"),
2474
- fill("cookie: a="),
2475
- fill("cookie:a"),
2476
- `cookie: ${"a".repeat(200_000)}=`,
2477
- `cookie: a=${"a".repeat(200_000)}`,
2478
- fill("PASSWORD=a{"),
2479
- fill("PASSWORD=a<"),
2480
- fill("PASSWORD=a{ "),
2481
- fill("password='a{"),
2482
- fill('password="a{'),
2483
- fill("password='a' "),
2484
- fill('password="a" '),
2485
- `PASSWORD=${"a{".repeat(100_000)}`,
2486
- `PASSWORD=a${"{".repeat(200_000)}`,
2487
- `PASSWORD=a${"} ".repeat(100_000)}`,
2488
- `password="${"a'".repeat(100_000)}`,
2489
- `password='${'a"'.repeat(100_000)}`,
2490
- `password="a${"{".repeat(200_000)}`,
2491
- fill('"password": "$'),
2492
- fill('password="$A'),
2493
- fill('password="$a '),
2494
- `password="$${"a".repeat(200_000)}`,
2495
- `password="$${"A".repeat(200_000)}`,
2496
- `password="$${"A".repeat(200_000)}1`,
2497
- `password="$${"a ".repeat(100_000)}1`,
2498
- fill("token=tokens["),
2499
- fill("PASSWORD=a$"),
2500
- fill("PASSWORD=a("),
2501
- fill("password=aaaaaaaa("),
2502
- fill("password=aaaaaaaa()"),
2503
- fill("password=a.b("),
2504
- fill("password='"),
2505
- fill("password='$"),
2506
- `PASSWORD=${"a$".repeat(100_000)}`,
2507
- `PASSWORD=${"(".repeat(200_000)}`,
2508
- `PASSWORD=a${"(".repeat(200_000)}`,
2509
- `password=${"a".repeat(200_000)}(`,
2510
- `password=${"a.".repeat(100_000)}`,
2511
- `password=a(${"b".repeat(200_000)}`,
2512
- fill("="),
2513
- fill(":"),
2514
- fill("token= "),
2515
- fill("token: "),
2516
- fill('token"=('),
2517
- fill(`${" ".repeat(1_000)}=`),
2518
- fill(`${"s".repeat(1_000)}token=(`),
2519
- fill(`${"s".repeat(1_000)}Token=(`),
2520
- fill(`token${" ".repeat(1_000)}=(`),
2521
- fill(`token=${" ".repeat(1_000)}(`),
2522
- fill("token1"),
2523
- `-${fill("token1")}`,
2524
- `-${fill("aToken1")} value`,
2525
- fill("aToken1="),
2526
- `${fill("token1")}=`,
2527
- `${fill("aA")}=`,
2528
- `${"A".repeat(200_000)}=`,
2529
- `${"a".repeat(200_000)}A=`,
2530
- fill(`${"a".repeat(1_000)}=`),
2531
- ];
2532
- for (const text of shapes) {
2533
- for (const rule of BUILTIN_RULES) {
2534
- const started = performance.now();
2535
- applyRules(text, [rule]);
2536
- const took = performance.now() - started;
2537
- assert.ok(took < 1000, `${rule.pattern.source.slice(0, 30)} took ${Math.round(took)}ms on ${text.slice(0, 12)}…`);
2538
- }
2539
- }
2540
- const started = performance.now();
2541
- redactLines([JSON.stringify({ [fill("token1")]: "aB3dE6gH9jK2mN5p" })]);
2542
- assert.ok(performance.now() - started < 1000);
2543
- });
2544
- test("what stands in front of a password is walked once, however often a text repeats it", () => {
2545
- for (const unit of ["<", "=", "<<<a", "mongo -u a ", "mongo -u aa ", "mongo -u -p ", "KEY=", "A_KEY=", `./${"a".repeat(64)} `]) {
2546
- const text = unit.repeat(Math.ceil(2_000_000 / unit.length));
2547
- const started = performance.now();
2548
- applyRules(text, BUILTIN_RULES);
2549
- const took = performance.now() - started;
2550
- assert.ok(took < 1000, `${JSON.stringify(unit)} took ${Math.round(took)}ms`);
2551
- }
2552
- });
2553
- test("a walk that stops at the next of its kind leaves the password to that one", () => {
2554
- const value = "aB3dE6gH9jK2mN5p";
2555
- for (const text of [
2556
- `cat <<<<<${value}`,
2557
- `cat <<<a<<<${value}`,
2558
- `A_KEY=B_KEY=${value}`,
2559
- `A_KEY=xKEY=${value}`,
2560
- `A_KEY=x-KEY-${value}`,
2561
- `A_KEY=sk-KEY]${value}`,
2562
- `mongo mongodump -u root -p ${value}`,
2563
- `mongo --name x.mongo -u root -p ${value}`,
2564
- `mongosh -u root -u admin -p ${value}`,
2565
- `mongosh -u 'a;b' -p ${value}`,
2566
- `mongosh -u root -u -p ${value}`,
2567
- `mongosh --username= -p 1 -u -p ${value}`,
2568
- `./a ./b -p ${value}`,
2569
- `./a x=./ -p ${value}`,
2570
- `./${"a".repeat(80)} -p ${value}`,
2571
- ]) {
2572
- assert.ok(!redactOne(text).includes(value), text);
2573
- }
2574
- });
2575
- /** The rules that walk from a client's word to its flag. */
2576
- const WALKING_RULES = BUILTIN_RULES.filter((rule) => rule.pattern.source.includes("curl"));
2577
- test("a walk from a client stops only where the rule could start again", () => {
2578
- const value = "hunter2x";
2579
- const room = (length) => "x".repeat(length);
2580
- for (const text of [
2581
- // Words that run on from the client's, on either side, start nothing.
2582
- `curl x xcurl -u a:${value}`,
2583
- `curl x curlx -u a:${value}`,
2584
- `curl x curl_y -u a:${value}`,
2585
- `curl x _curl -u a:${value}`,
2586
- `curl x CURL -u a:${value}`,
2587
- `curl x xcurl -b k=${value}`,
2588
- `curl x curlx -b k=${value}`,
2589
- `zip x xzip -P ${value}`,
2590
- `zip x zipper -P ${value}`,
2591
- `mysql x xmysql -p${value}`,
2592
- // After `.` or `-` a word is a file's or another tool's to the password flags.
2593
- `curl x a.curl -b k=${value}`,
2594
- `curl x a-curl -b k=${value}`,
2595
- `zip a.zip -P ${value}`,
2596
- `zip -r a-zip -P ${value}`,
2597
- `7z a x.7z -p${value}`,
2598
- `sshpass x a.sshpass -p ${value}`,
2599
- `ssh-keygen -f a.ssh-keygen -N ${value}`,
2600
- // A client that needs a space after it, and clients of more than one word.
2601
- `http x https://y -a a:${value}`,
2602
- `xh x http://y --auth a:${value}`,
2603
- `docker login x docker -p ${value}`,
2604
- `docker login x login -p ${value}`,
2605
- `helm registry login x helm registry -p ${value}`,
2606
- `gh secret set a gh secret -b ${value}`,
2607
- `doctl auth init x doctl auth -t ${value}`,
2608
- `security add-generic-password x security -w ${value}`,
2609
- // A start the walk stops at finds the same from there, in every spelling.
2610
- `curl x curl -u a:${value}`,
2611
- `curl x a.curl -u a:${value}`,
2612
- `curl x /usr/bin/curl -u a:${value}`,
2613
- `curl x curl -u "a:${value} b"`,
2614
- `curl x curl -u a:"${value} b"`,
2615
- `curl x curl -u 'a:${value} b'`,
2616
- `curl x curl -b k=${value}`,
2617
- `curl x\ncurl -u a:${value}`,
2618
- `zip x zip -P ${value}`,
2619
- `zip x unzip -P "${value} b"`,
2620
- `mysql x mariadb -p${value}`,
2621
- `http x xh -a a:${value}`,
2622
- `docker login x podman login -p ${value}`,
2623
- `gh secret set a gh secret set -b ${value}`,
2624
- // With as much room again.
2625
- `curl ${room(199)} -u a:${value}`,
2626
- `zip ${room(199)} -P ${value}`,
2627
- `curl ${room(150)} curl ${room(150)} -u a:${value}`,
2628
- `zip ${room(150)} zip ${room(150)} -P ${value}`,
2629
- // Not a first client's share and password, where a walk stopped at the second.
2630
- `which smbclient && smbclient -U a%${value} //h/s`,
2631
- `apt install smbclient && smbclient -U a%${value} //h/s`,
2632
- ]) {
2633
- assert.ok(!applyRules(text, WALKING_RULES).includes(value), text);
2634
- }
2635
- // Too far from its client a flag stays someone else's.
2636
- for (const text of [`curl ${room(200)} -u a:${value}`, `zip ${room(200)} -P ${value}`, `curl x\n${room(1)} -u a:${value}`, `zip x; -P ${value}`]) {
2637
- assert.equal(applyRules(text, WALKING_RULES), text);
2638
- }
2639
- });
2640
- test("a rule goes over a text once to count and once to cut, however much it cut", () => {
2641
- // Counted and not timed: one pass more is the whole mask again on any text with a password in
2642
- // it, and over all that its markers grew the text by.
2643
- let scans = 0;
2644
- class Counted extends RegExp {
2645
- [Symbol.match](text) {
2646
- scans += 1;
2647
- return super[Symbol.match](text);
2648
- }
2649
- [Symbol.replace](text, replacement) {
2650
- scans += 1;
2651
- return super[Symbol.replace](text, replacement);
2652
- }
2653
- }
2654
- const text = "7z -pa -pb mysql -pa -pb curl -b k=a -b j=b ";
2655
- for (const rule of WALKING_RULES) {
2656
- scans = 0;
2657
- applyRules(text, [{ ...rule, pattern: new Counted(rule.pattern.source, rule.pattern.flags) }]);
2658
- assert.ok(scans <= 2, `${scans} scans`);
2659
- }
2660
- });
2661
- test("the walks from a client's word add up to about the length of the line, however often it says the word", () => {
2662
- const scan = (text) => {
2663
- const started = performance.now();
2664
- applyRules(text, WALKING_RULES);
2665
- return performance.now() - started;
2666
- };
2667
- const line = (unit) => unit.repeat(Math.ceil(2_000_000 / unit.length));
2668
- const plain = line("hello w ");
2669
- for (const unit of ["curl ", "curl -b ", "curl -u ", "a.curl -u ", "zip ", "7z ", "xh ", "mysql "]) {
2670
- const text = line(unit);
2671
- // In turns, so a busy machine slows both; a walk from every word is 15 to 55 times a plain line.
2672
- let quickest = Infinity;
2673
- let quickestPlain = Infinity;
2674
- for (let turn = 0; turn < 5 && !(quickest < 8 * quickestPlain); turn += 1) {
2675
- quickestPlain = Math.min(quickestPlain, scan(plain));
2676
- quickest = Math.min(quickest, scan(text));
2677
- }
2678
- assert.ok(quickest < 8 * quickestPlain, `${unit}: ${Math.round(quickest)}ms, a plain line ${Math.round(quickestPlain)}ms`);
2679
- }
2680
- });
2681
- test("the rules stay small enough for a commit to compile them", () => {
2682
- const size = BUILTIN_RULES.reduce((sum, rule) => sum + rule.pattern.source.length, 0);
2683
- assert.ok(size < 19_950, `${size} characters of pattern`);
2684
- });
2685
- test("a credential's name is looked for from its separator, so a word with none costs one scan", () => {
2686
- const text = "s".repeat(32_000_000);
2687
- for (const rule of BUILTIN_RULES.filter((candidate) => candidate.type === "assignment")) {
2688
- const started = performance.now();
2689
- applyRules(text, [rule]);
2690
- const took = performance.now() - started;
2691
- assert.ok(took < 1000, `${rule.pattern.source.slice(0, 30)} took ${Math.round(took)}ms`);
2692
- }
2693
- });
2694
- test("cutting a write to a dotenv file gets through what looks almost like one", () => {
2695
- const fill = (unit) => unit.repeat(Math.ceil(200_000 / unit.length));
2696
- const shapes = [
2697
- `${fill("$a")} > .env`,
2698
- `${fill("a=")} $a > .env`,
2699
- `${Array.from({ length: 30_000 }, (_, i) => `$a${i}`).join("")} > .env`,
2700
- `${Array.from({ length: 30_000 }, (_, i) => `a${i}=`).join("")} $b > .env`,
2701
- `echo ${fill('\\"')} > .env`,
2702
- `echo "${fill('\\"')} > .env`,
2703
- `echo ${fill("'")} > .env`,
2704
- `echo ${fill('"')} > .env`,
2705
- `echo ${fill('"\\')} > .env`,
2706
- `echo ${fill('> "')} .env`,
2707
- `echo ${fill("> a'")} .env`,
2708
- `echo ${fill(">")} .env`,
2709
- `echo >${" ".repeat(200_000)}.env`,
2710
- `echo ${fill("> ")}.env`,
2711
- `tee ${fill("a ")} .env`,
2712
- `${fill("<<<")} .env`,
2713
- `${fill("<<")} .env`,
2714
- `${fill("<<a ")}\n.env`,
2715
- `<<${" ".repeat(200_000)}.env`,
2716
- `${fill("if ")}.env`,
2717
- `${fill("( ")}.env`,
2718
- `${fill("! ")}.env`,
2719
- `${" ".repeat(200_000)}.env`,
2720
- `cat ${fill("a:")}.env`,
2721
- `cat ${fill(":")}`,
2722
- `cat ${fill(".env ")}`,
2723
- `cat ${fill("a.env ")}`,
2724
- `cat ${"a".repeat(200_000)}.env`,
2725
- `sed -i ${fill("a ")}.env`,
2726
- `sed -i ${fill("s/a/b/ ")}.env`,
2727
- `perl -pi -e ${fill("' ")}.env`,
2728
- `${fill("$(")}.env`,
2729
- `$(${"a".repeat(200_000)}.env`,
2730
- `${fill("$(a ")}.env`,
2731
- `${fill("| ")}.env`,
2732
- `|${" ".repeat(200_000)}.env`,
2733
- `${fill("|xarg")}.env`,
2734
- `echo ${fill(">|")} .env`,
2735
- `sed -i ${fill("s|")} .env`,
2736
- `sed -i ${fill("-s/")} .env`,
2737
- `sed -i ${fill("$a")} .env`,
2738
- `a=b; sed -i ${fill("$a")} .env`,
2739
- `sed -i '${fill("$ ")}' .env`,
2740
- `sed -i '${fill("/a/ ")}' .env`,
2741
- `sed -i '/${"a".repeat(200_000)}' .env`,
2742
- `sed -i '${"1".repeat(200_000)}' .env`,
2743
- `sed -i '${fill("1 ")}' .env`,
2744
- `sed -i '$${" ".repeat(200_000)}' .env`,
2745
- `cat > .env <<EOF\n${fill("$a")}\nEOF`,
2746
- `cat > .env <<EOF\n${fill("a=$a")}\nEOF`,
2747
- `cat > .env <<EOF\n${fill("$a=\n")}EOF`,
2748
- `tail ${"a".repeat(200_000)}`,
2749
- `tail ${"a".repeat(200_000)}.outpu .env`,
2750
- `tail ${fill("a.outpu ")}`,
2751
- `tail ${fill("a.output.")}`,
2752
- `tail ${fill("-")}.output`,
2753
- // Each statement here asks what the whole command assigns.
2754
- fill('echo "$X" > .env\n'),
2755
- fill("sed -i s/$X/b/ .env\n"),
2756
- fill('a=b echo "$X" > .env\n'),
2757
- `${fill("F=.env;")} echo $F > $F`,
2758
- fill("F=.env; echo K=v > $F\n"),
2759
- `for f in ${fill(".env ")}; do echo $f > $f; done`,
2760
- fill("for f in .env; "),
2761
- `${fill("for ")}.env`,
2762
- `for${" ".repeat(200_000)}.env`,
2763
- `for f in${" ".repeat(200_000)}.env`,
2764
- fill("exec 3>.env;"),
2765
- `python3 -c "${fill("open('.env',")}"`,
2766
- `python3 -c "${fill("open(")}.env"`,
2767
- `python3 -c "${fill('open(\\"')}.env"`,
2768
- `python3 -c "${fill("open('.env', mode=")}"`,
2769
- `python3 -c "${fill("open('.env', mode='")}"`,
2770
- `python3 -c "${fill("open('a','")}.env"`,
2771
- `python3 -c "open(\\"${"a".repeat(200_000)}.env"`,
2772
- `${fill("Path('")}.env`,
2773
- `${fill("Path('a')")}.env`,
2774
- `sed -i $${"a".repeat(200_000)} .env`,
2775
- `sed -i ${fill("${a")} .env`,
2776
- `sed -i ${fill("$")} .env`,
2777
- fill("for a in .env "),
2778
- `open(${" ".repeat(200_000)}.env`,
2779
- `open('.env'${" ".repeat(200_000)}`,
2780
- `python3 -c "open('${"a".repeat(200_000)}.env','w')"`,
2781
- `${fill("writeFileSync('")}.env`,
2782
- fill("Path('.env')"),
2783
- `Path('.env')${" ".repeat(200_000)}`,
2784
- fill("dd of=.env "),
2785
- `dd of=${"a".repeat(200_000)}.env`,
2786
- `cp ${fill("a ")}/dev/stdin .env`,
2787
- `${fill("cp /dev/stdin\n")}.env`,
2788
- `${fill("cp a\n")}.env`,
2789
- `awk -i${" ".repeat(200_000)}inplace .env`,
2790
- `ruby ${fill("-a ")}.env`,
2791
- `bash <<SH\n${fill("echo K=v > .env\n")}SH`,
2792
- `echo ${fill("K=v;")} > .env`,
2793
- `echo ${fill("K=a>")}.env`,
2794
- `echo K=${fill("a;")}>.env`,
2795
- `echo K=${"a".repeat(200_000)}>.env`,
2796
- `${fill("K=aZ9bY8cX7;\n")}>.env`,
2797
- `${fill("tee '")} .env`,
2798
- // Each heredoc here looks for where it ends.
2799
- `${fill("cat <<a\na\n")}.env`,
2800
- `${fill("cat > .env <<a\na\n")}`,
2801
- `cat <<a\n${"\n".repeat(200_000)}a\n.env`,
2802
- ];
2803
- for (const command of shapes) {
2804
- const started = performance.now();
2805
- const { lines } = redactLines([JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command } }] } })]);
2806
- const took = performance.now() - started;
2807
- assert.equal(lines.length, 1);
2808
- assert.ok(took < 5000, `took ${Math.round(took)}ms on ${command.slice(0, 12)}…`);
2809
- }
2810
- });
2811
- test("learning a dotenv file's values gets through what looks almost like one", () => {
2812
- const fill = (unit) => unit.repeat(Math.ceil(200_000 / unit.length));
2813
- const shapes = [
2814
- fill("A="),
2815
- fill('A="'),
2816
- fill("A='"),
2817
- fill(`A="B='`),
2818
- `A=${" ".repeat(200_000)}`,
2819
- `A${" ".repeat(200_000)}`,
2820
- `A="${"b".repeat(200_000)}`,
2821
- fill("a b "),
2822
- fill("="),
2823
- fill("a="),
2824
- fill("\\n"),
2825
- fill("A=;"),
2826
- `A=${fill(";")}`,
2827
- `A=${fill("a-")}!`,
2828
- `A=${fill("a_b")}`,
2829
- `A=${fill("a/")}!`,
2830
- `A=${fill("a.")} `,
2831
- `${"a".repeat(200_000)}=`,
2832
- `${"a".repeat(100_000)}${"=".repeat(100_000)}b`,
2833
- fill("KEY=Zq8vR2mK9xLp\n"),
2834
- Array.from({ length: 9_000 }, (_, i) => `KEY=Zq8vR2mK9xLp${i}\n`).join(""),
2835
- ];
2836
- for (const text of shapes) {
2837
- const started = performance.now();
2838
- const { lines } = redactLines([
2839
- JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: ".env" } }] } }),
2840
- JSON.stringify({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: text }] } }),
2841
- JSON.stringify({ type: "user", message: { role: "user", content: text } }),
2842
- ]);
2843
- const took = performance.now() - started;
2844
- assert.equal(lines.length, 3);
2845
- assert.ok(took < 5000, `took ${Math.round(took)}ms on ${text.slice(0, 12)}…`);
2846
- }
2847
- });
2848
- test("an email or a URL password is still caught where the run it sits in starts with punctuation", () => {
2849
- const out = applyRules("mail -alice@example.com; db=-x.postgres://u:hunter22@db.local/app", BUILTIN_RULES);
2850
- assert.ok(!out.includes("alice@example.com"));
2851
- assert.ok(!out.includes("hunter22"));
2852
- });
2853
- test("a backslash outside quotes opens no quote, so a dotenv write behind one is still cut", () => {
2854
- const value = "Zq8vR2mK9xLp4T";
2855
- const m = marker("dotenv");
2856
- const cases = [
2857
- [`echo APP_KEY=\\"${value}\\" > .env`, `${m} > .env`],
2858
- [`echo APP_KEY=\\"${value}\\" >> .env.local`, `${m} > .env.local`],
2859
- [`echo APP_KEY=\\"${value}\\" | tee .env`, `${m} > .env`],
2860
- [`echo don\\'t; echo "APP_KEY=${value}" > .env`, `echo don\\'t; ${m} > .env`],
2861
- [`echo "C:\\\\"; echo "APP_KEY=${value}" > .env`, `echo "C:\\\\"; ${m} > .env`],
2862
- ];
2863
- for (const [command, expected] of cases) {
2864
- const { lines } = summaryFor([
2865
- { type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command } }] } },
2866
- { type: "user", message: { role: "user", content: `the key is ${value}` } },
2867
- ]);
2868
- const [call, later] = lines.map((line) => JSON.parse(line));
2869
- assert.equal(call.message.content[0].input.command, expected);
2870
- assert.equal(later.message.content, `the key is ${m}`);
2871
- }
2872
- // Escaped, it is no redirect at all; in quotes, it is what a message says.
2873
- for (const command of [`echo \\> .env`, `git commit -m "x > .env"`, `git commit -m "don't > .env"`]) {
2874
- assert.equal(throughCall("Bash", { command }, "plain output").input.command, command);
2875
- }
2876
- });
2877
- test("a variable naming a dotenv file, used all over a command, costs no more than the command is long", () => {
2878
- for (const command of [
2879
- `F=${"a".repeat(1000)}.env; echo ${"$F ".repeat(66_000)}`,
2880
- `F=${"a".repeat(8000)}.env; echo ${"$F ".repeat(24_000)}`,
2881
- `F=${"a".repeat(100_000)}.env;${"$F;".repeat(33_000)}`,
2882
- `F=${"a".repeat(100_000)}.env; echo ${"$F ".repeat(33_000)}`,
2883
- `F=${"a".repeat(250)}.env;${"$F;".repeat(66_000)}`,
2884
- ]) {
2885
- const started = performance.now();
2886
- const { lines } = redactLines([JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command } }] } })]);
2887
- const took = performance.now() - started;
2888
- assert.equal(lines.length, 1);
2889
- assert.ok(took < 5000, `took ${Math.round(took)}ms on ${command.slice(0, 12)}…`);
2890
- }
2891
- assert.equal(throughCall("Bash", { command: "F=.env; echo K=Zq8vR2mK9xLp > $F" }, "plain output").input.command, `${marker("dotenv")} > .env`);
2892
- });
2893
- test("a run of millions of characters costs the text it sits in, not the session", () => {
2894
- const size = 8_000_000;
2895
- for (const text of [
2896
- `Authorization: ${"a".repeat(size)}`,
2897
- `authorization=${"a".repeat(size)}`,
2898
- `Authorization: Bearer ${"QUJD+/==".repeat(size / 8)}`,
2899
- `Cookie: ${"a=b; ".repeat(size / 5)}`,
2900
- `password=${"a".repeat(size)}`,
2901
- `sk-${"a".repeat(size)}`,
2902
- `/home/x/${"a/".repeat(size / 2)}`,
2903
- ]) {
2904
- const { lines, summary } = redactLines([
2905
- JSON.stringify({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: text }] } }),
2906
- `not json: ${text}`,
2907
- ]);
2908
- const content = JSON.parse(lines[0]).message.content[0].content;
2909
- assert.equal(content, marker("oversized"), text.slice(0, 24));
2910
- assert.equal(lines[1], marker("oversized"), text.slice(0, 24));
2911
- assert.equal(summary.counts.oversized, 2);
2912
- }
2913
- });
2914
- test("a dotenv read's result is learned from and cut however deep it nests", () => {
2915
- const depth = 200_000;
2916
- const { lines } = redactLines([
2917
- JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: "/p/.env" } }] } }),
2918
- `{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":${"[".repeat(depth)}"MAILER_PHRASE=Zq8vR2mK9xLp"${"]".repeat(depth)}}]}}`,
2919
- JSON.stringify({ type: "user", message: { role: "user", content: "the phrase is Zq8vR2mK9xLp" } }),
2920
- ]);
2921
- assert.equal(JSON.parse(lines[1]).message.content[0].content, marker("dotenv"));
2922
- assert.equal(JSON.parse(lines[2]).message.content, `the phrase is ${marker("dotenv")}`);
2923
- });
2924
- test("a line too dear to parse is cut whole, and so is what comes past the session's limit", () => {
2925
- const blocks = (count) => JSON.stringify({ type: "user", x: Array(count).fill({}) });
2926
- const one = redactLines([blocks(5_333_334), JSON.stringify({ type: "user", uuid: "after" })], { home: "/home/u" });
2927
- assert.equal(one.lines[0], marker("oversized"));
2928
- assert.equal(JSON.parse(one.lines[1]).uuid, "after", "a line of its own does not take the rest with it");
2929
- const many = redactLines(Array.from({ length: 5 }, () => blocks(1_500_000)), { home: "/home/u" });
2930
- assert.deepEqual(many.lines.map((line) => line === marker("oversized")), [false, false, false, true, true]);
2931
- assert.equal(many.summary.counts.oversized, 2);
2932
- });
2933
- test("redaction may add so much to a session in all, and what comes past that goes whole", () => {
2934
- // Each `7z -pa ` is 24 characters more, against a limit of 16 MiB.
2935
- const grows = (length) => "7z -pa ".repeat(Math.ceil(length / 7));
2936
- const said = (content) => JSON.stringify({ type: "user", message: { role: "user", content } });
2937
- const content = (line) => JSON.parse(line).message.content;
2938
- const large = grows(5_000_000);
2939
- // Whatever line the text stands in, the rest of the session stays.
2940
- for (const [line, cut] of [
2941
- [said(large), said(marker("oversized"))],
2942
- [large, marker("oversized")],
2943
- [JSON.stringify(large), JSON.stringify(marker("oversized"))],
2944
- ]) {
2945
- const one = redactLines([line, said("after")], { home: "/home/u" });
2946
- assert.deepEqual(one.lines, [cut, said("after")]);
2947
- assert.equal(one.summary.counts.oversized, 1);
2948
- }
2949
- // Added up over the lines of a session, and over the texts of a line.
2950
- const lines = redactLines(Array.from({ length: 5 }, () => said(grows(1_000_000))), { home: "/home/u" });
2951
- assert.deepEqual(lines.lines.map((line) => content(line) === marker("oversized")), [false, false, false, false, true]);
2952
- assert.ok(!lines.lines[0].includes("-pa "));
2953
- const texts = redactLines([said(Array.from({ length: 5 }, () => grows(1_000_000)))], { home: "/home/u" });
2954
- assert.deepEqual(content(texts.lines[0]).map((text) => text === marker("oversized")), [false, false, false, false, true]);
2955
- // What the rules take off makes room, as only what is kept costs anything.
2956
- const shrinks = said(`password=${"a".repeat(400_000)}`);
2957
- const room = redactLines([shrinks, shrinks, shrinks, said(large)], { home: "/home/u" });
2958
- assert.equal(String(content(room.lines[3])).slice(0, 8), "7z -p[IS");
2959
- // Found out ahead of the cut, whose result is what the heap runs out on.
2960
- const replace = String.prototype.replace;
2961
- let longest = 0;
2962
- String.prototype.replace = function (...args) {
2963
- longest = Math.max(longest, this.length);
2964
- return replace.apply(this, args);
2965
- };
2966
- try {
2967
- redactLines([said(large)], { home: "/home/u" });
2968
- }
2969
- finally {
2970
- String.prototype.replace = replace;
2971
- }
2972
- assert.ok(longest < large.length, `a replace on ${longest} characters`);
2973
- // Too short to be worth a marker: the marker itself is what grows, which the line pays for.
2974
- const shorts = said(Array.from({ length: 1_500_000 }, () => "7z -pa "));
2975
- const short = redactLines([shorts, said("after")], { home: "/home/u" });
2976
- assert.deepEqual(short.lines, [marker("oversized"), said("after")]);
2977
- });
2978
- function dotenvThenSaid(value, said) {
2979
- return redactLines([
2980
- JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: ".env" } }] } }),
2981
- JSON.stringify({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: `TOKEN=${value}` }] } }),
2982
- JSON.stringify({ type: "user", message: { role: "user", content: said } }),
2983
- ]).lines;
2984
- }
2985
- test("a long learned value is looked for in time proportional to the text", () => {
2986
- const started = performance.now();
2987
- dotenvThenSaid(`${"a".repeat(30_000)}1`, "a".repeat(60_000));
2988
- const took = performance.now() - started;
2989
- assert.ok(took < 2000, `took ${Math.round(took)}ms`);
2990
- });
2991
- test("text too dear to look through for a learned value goes whole, so that none of it stays", () => {
2992
- const value = `${"a".repeat(30_000)}1`;
2993
- const said = `${"a".repeat(60_000)}${value} the rest`;
2994
- const lines = dotenvThenSaid(value, said);
2995
- assert.ok(!lines.join("\n").includes(value));
2996
- assert.ok(!lines.join("\n").includes("the rest"));
2997
- assert.ok(lines[2].includes("oversized"), lines[2].slice(0, 200));
2998
- });
2999
- test("a learned value said many times goes each time, however long it is", () => {
3000
- const value = `Zq8vR2mK9xLp${"Q7".repeat(5_000)}`;
3001
- const lines = dotenvThenSaid(value, `${value} and ${value} and ${value} and that is all`);
3002
- assert.ok(!lines.join("\n").includes(value));
3003
- assert.ok(lines[2].includes("and that is all"));
3004
- assert.equal(lines[2].split("dotenv").length - 1, 3);
3005
- });
3006
- test("a value that shares a stretch with the text around it does not cost that text", () => {
3007
- const value = `${"0".repeat(24)}Zq8vR2mK9xLp`;
3008
- const lines = dotenvThenSaid(value, `${"0".repeat(100)} and ${value} and the rest`);
3009
- assert.ok(!lines.join("\n").includes(value));
3010
- assert.ok(lines[2].includes("0".repeat(100)));
3011
- assert.ok(lines[2].includes("and the rest"));
3012
- });
3013
- test("a command of many dashes in front of a dotenv file is read in time proportional to it", () => {
3014
- const command = `sed${" -/sed".repeat(20_000)} .env`;
3015
- const started = performance.now();
3016
- redactLines([JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command } }] } })]);
3017
- const took = performance.now() - started;
3018
- assert.ok(took < 2000, `took ${Math.round(took)}ms`);
3019
- const kept = (edit) => redactLines([JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Bash", input: { command: edit } }] } })]).lines[0];
3020
- assert.ok(!kept(`sed -E -n -u -i 's/K=.*/K=Zq8vR2mK9xLp/' .env`).includes("Zq8vR2mK9xLp"));
3021
- assert.ok(!kept(`perl -p -i -e 's/K=.*/K=Zq8vR2mK9xLp/' .env`).includes("Zq8vR2mK9xLp"));
3022
- });
3023
- test("past what a session may cut of learned values, the text that holds one goes whole", () => {
3024
- const value = "a1a1a1a1";
3025
- const read = JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Read", input: { file_path: ".env" } }] } });
3026
- const shown = JSON.stringify({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: `TOKEN=${value}` }] } });
3027
- const said = (text) => JSON.stringify({ type: "user", message: { role: "user", content: text } });
3028
- const { lines, summary } = redactLines([read, shown, said(value.repeat(600_000)), said(`${value} and the rest`.repeat(400_001)), said(`${value} once`), said("nothing of it")]);
3029
- assert.ok(!lines.join("\n").includes(value));
3030
- assert.equal(summary.counts.dotenv, 1_000_000);
3031
- assert.equal(summary.counts.oversized, 2);
3032
- assert.equal(lines[2].split("dotenv").length - 1, 600_000);
3033
- assert.equal(JSON.parse(lines[3]).message.content, marker("oversized"));
3034
- assert.equal(JSON.parse(lines[4]).message.content, marker("oversized"));
3035
- assert.equal(JSON.parse(lines[5]).message.content, "nothing of it");
3036
- });
3037
- test("a command that names a dotenv file goes whole past what a session may look through", () => {
3038
- const value = "Zq8vR2mK9xLp";
3039
- const bash = (id, command) => JSON.stringify({ type: "assistant", message: { role: "assistant", content: [{ type: "tool_use", id, name: "Bash", input: { command } }] } });
3040
- const printed = JSON.stringify({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: `TOKEN=${value}` }] } });
3041
- const { lines, summary } = redactLines([
3042
- bash("t1", `F=.env; echo TOKEN=${value} > $F; ${"$a;".repeat(1_500_000)}`),
3043
- printed,
3044
- bash("t2", `echo TOKEN=${value} > .env`),
3045
- ]);
3046
- const command = (line) => JSON.parse(line).message.content[0].input.command;
3047
- assert.equal(command(lines[0]), marker("oversized"));
3048
- assert.equal(summary.counts.oversized, 1);
3049
- assert.ok(!lines[1].includes(value));
3050
- assert.equal(command(lines[2]), `${marker("dotenv")} > .env`, "one that fits still goes as it does");
3051
- assert.ok(!lines.join("\n").includes(value));
3052
- });
3053
- //# sourceMappingURL=redact.test.js.map