Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3
4import { FeedbackInput, FeedbackReport } from "./FeedbackReport.ts";
5import { scrubFeedback, scrubFeedbackText } from "./scrubFeedback.ts";
6
7const decodeInput = Schema.decodeUnknownSync(FeedbackInput);
8const decodeReport = Schema.decodeUnknownSync(FeedbackReport);
9
10describe("scrubFeedback", () => {
11 it.each([
12 [
13 "known values, longest first",
14 "Reading /home/alice/acme as alice on alice-laptop",
15 ["/home/alice", "/home/alice/acme", "alice", "alice-laptop"],
16 "Reading [redacted] as [redacted] on [redacted]",
17 ],
18 [
19 "known values only as whole words",
20 "t3_feedback failed for t3 and Bob, not bobcat",
21 ["t3", "Bob", "x"],
22 "t3_feedback failed for t3 and [redacted], not bobcat",
23 ],
24 [
25 "URLs with credentials before emails inside them",
26 "clone https://user:hunter2@git.acme.dev/app.git, mail ops@acme.dev",
27 [],
28 "clone [redacted] mail [redacted]",
29 ],
30 [
31 "key prefixes and JWTs",
32 "sk-ant-api03-abcdefghijkl ghp_abcdefghijklmnop eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjMifQ.c2ln",
33 [],
34 "[redacted] [redacted] [redacted]",
35 ],
36 [
37 "long random runs, not long identifiers or paths",
38 "aB3dE5fG7hI9jK1lM3nO5pQ7rS9tU1vW t3_environment_preferences_update apps/server/src/persistence/Migrations/088",
39 [],
40 "[redacted] t3_environment_preferences_update apps/server/src/persistence/Migrations/088",
41 ],
42 [
43 "whole absolute paths",
44 "/home/x/acme\n/tmp/a/b.ts\n~/acme/app\nfile:///srv/acme/x.ts\nC:\\Users\\x\\acme\\a.ts",
45 [],
46 "[redacted]\n[redacted]\n[redacted]\n[redacted]\n[redacted]",
47 ],
48 [
49 "a known value that is a URL scheme",
50 "Opened file:///home/alice/private.txt",
51 ["file"],
52 "Opened [redacted]",
53 ],
54 [
55 "the tail of a known path",
56 "Opened /home/alice/Develop/acme\nThen /Users/al/My Project/src/a.ts",
57 ["/home/alice", "/Users/al/My Project"],
58 "Opened [redacted]\nThen [redacted]",
59 ],
60 [
61 "nothing that only looks like a path",
62 "See https://github.com/org/repo for t3 thread send and t3_thread_read: 3/10 and/or a / b /mcp /compact /secret.env",
63 [],
64 "See https://github.com/org/repo for t3 thread send and t3_thread_read: 3/10 and/or a / b /mcp /compact /secret.env",
65 ],
66 [
67 "quoted paths with spaces",
68 `Read "/home/alice/My Project/secret.txt", 'C:\\Users\\My Name\\a.ts' and \`~/My Notes\` for '/compact'`,
69 [],
70 `Read "[redacted]", '[redacted]' and \`[redacted]\` for '/compact'`,
71 ],
72 [
73 "unquoted paths with spaces",
74 "Opened /home/alice/My Project/secret.txt then C:\\Users\\My Name\\a.ts today",
75 [],
76 "Opened [redacted]",
77 ],
78 [
79 "a quoted path to its own closing quote",
80 `Read "/home/alice/O'Brien/secret.txt" and \`/srv/it's here/a\``,
81 [],
82 'Read "[redacted]" and `[redacted]`',
83 ],
84 [
85 "unquoted folders of several words, to the last separator on the line",
86 "Read /home/alice/My Secret Project/secret.txt and C:\\Users\\My Big Folder\\a.ts. Then and/or stays",
87 [],
88 "Read [redacted]",
89 ],
90 [
91 "a file name with a space at the end of a path",
92 "Read /home/alice/Secret File.txt\nRead ~/Secret File.txt\nRead file:///srv/Secret File.txt\nRead C:\\Users\\Secret File.txt\nRead /home/alice/Secret File.txt",
93 ["/home/alice"],
94 "Read [redacted]\nRead [redacted]\nRead [redacted]\nRead [redacted]\nRead [redacted]",
95 ],
96 [
97 "slash commands and spaced slashes, which a first segment with a space would swallow",
98 "Ran /mcp /compact, then /goal, a / b / c",
99 [],
100 "Ran /mcp /compact, then /goal, a / b / c",
101 ],
102 [
103 "a folder name holding a sentence break",
104 "Read /home/alice/Secret. Project/secret.txt",
105 [],
106 "Read [redacted]",
107 ],
108 [
109 "only the lines that hold a path",
110 "Saw /home/a/b today\nand/or https://github.com/org/repo stays\nthen ~/x/y. Done",
111 [],
112 "Saw [redacted]\nand/or https://github.com/org/repo stays\nthen [redacted]",
113 ],
114 [
115 "secrets before the known values inside them",
116 "Mail alice@privatecompany.com with sk-proj-abcdefghij1234567890 from git@github.com:acme/app.git",
117 ["alice", "proj", "git@github.com:acme/app.git"],
118 "Mail [redacted] with [redacted] from [redacted]",
119 ],
120 [
121 "a random run with a known value inside it",
122 "Key AbCd1234EfGh5678-proj-QrSt9012UvWx3456 leaked",
123 ["proj"],
124 "Key [redacted] leaked",
125 ],
126 [
127 "a run whose only digits are a known value",
128 "Key AbCdEfGhIjKlMnOp-1234-QrStUvWxYzAbCdEf leaked",
129 ["1234"],
130 "Key [redacted] leaked",
131 ],
132 [
133 "a random run right before a path",
134 "AbCd1234EfGh5678-QrSt9012UvWx3456=C:\\Users\\private\\file.txt\nAbCd1234EfGh5678-QrSt9012UvWx3456=file:///home/private/file.txt",
135 [],
136 "[redacted]\n[redacted]",
137 ],
138 [
139 "UNC paths",
140 `Opened \\\\fileserver\\finance\\payroll.xlsx\nand "\\\\fs\\My Share\\a.txt" then`,
141 [],
142 'Opened [redacted]\nand "[redacted]" then',
143 ],
144 [
145 "a two-letter username such as t3",
146 "t3 thread send failed for t3",
147 ["t3"],
148 "t3 thread send failed for t3",
149 ],
150 ["a 1,200-character adversarial path", `/a/b${" x/".repeat(398)}`, [], "[redacted]"],
151 ])("scrubs %s", (_, text, sensitive, expected) => {
152 expect(scrubFeedback(text, sensitive)).toBe(expected);
153 });
154
155 it("keeps a valid input a valid report when redaction lengthens it", () => {
156 const input = decodeInput({
157 kind: "bug",
158 subject: "",
159 expected: "",
160 actual: `${"😀".repeat(100)}${" bob".repeat(15)}`,
161 workaround: "",
162 });
163 const actual = scrubFeedback(input.actual, ["bob"]);
164 const report = {
165 ...input,
166 actual,
167 id: "r",
168 threadId: null,
169 createdAt: "",
170 source: "mcp",
171 trace: null,
172 };
173 expect(decodeReport({ ...report, sent: true }).actual).toBe(actual);
174 expect(Array.from(actual)).toHaveLength(265);
175 });
176
177 it("scrubs before capping at 300 characters", () => {
178 expect(scrubFeedback(`${"a ".repeat(140)}ops@acme.dev`, [])).toBe(
179 `${"a ".repeat(140)}[redacted]`,
180 );
181 expect(Array.from(scrubFeedback("😀".repeat(400), []))).toHaveLength(300);
182 expect(scrubFeedback(`${"a@".repeat(20_000)}`, [])).toBe("[redacted]");
183 });
184
185 it("redacts a token the scan window cuts, whatever it holds past the window", () => {
186 const url = `https://alice:${"a".repeat(5_000)}@example.com`;
187 expect(scrubFeedback(`see ${url} now`, [], 1_000)).toBe("[redacted]");
188 expect(scrubFeedback(`kept\nsee ${url} now`, [], 1_000)).toBe("kept\n[redacted]");
189 expect(scrubFeedback(`${"b\n".repeat(2_000)}tail`, [], 1_000)).toBe("b\n".repeat(500));
190 });
191
192 it("redacts the whole line the scan window cuts, so no match across the cut survives", () => {
193 expect(
194 scrubFeedback(`sk-${"a".repeat(3_986)} Project Aurora tail`, ["Project Aurora"], 1_000),
195 ).toBe("[redacted]");
196 expect(
197 scrubFeedback(`see "/My Documents/${"a ".repeat(2_500)}secret.txt" now`, [], 1_000),
198 ).toBe("[redacted]");
199 // A known value cannot span lines: each of its lines is a known value of its own.
200 expect(scrubFeedback("Project\nAurora Labs, Aurora Labs", ["Project\nAurora Labs"])).toBe(
201 "[redacted]\n[redacted], [redacted]",
202 );
203 expect(scrubFeedbackText("Q1\nX", ["Q1\nX"], 1_000).text).toBe("[redacted]\n[redacted]");
204 expect(scrubFeedbackText("Aurora\nX", ["Aurora\nX"], 1_000).text).toBe(
205 "[redacted]\n[redacted]",
206 );
207 // A standalone value under three characters still redacts nothing.
208 expect(scrubFeedback("Q1 and X", ["Q1", "X"])).toBe("Q1 and X");
209 const reply = Array.from({ length: 100 }, (_, index) => `Line ${index} of the reply.`).join(
210 "\n",
211 );
212 expect(scrubFeedbackText(reply, [], 1_000)).toEqual({
213 text: Array.from(reply).slice(0, 1_000).join(""),
214 cut: true,
215 });
216 });
217
218 it("reports a cut when redaction pushes the text past the cap", () => {
219 expect(scrubFeedbackText("Bob ".repeat(249), ["Bob"], 1_000).cut).toBe(true);
220 expect(scrubFeedbackText("Bob ".repeat(10), ["Bob"], 1_000)).toEqual({
221 text: "[redacted] ".repeat(10),
222 cut: false,
223 });
224 });
225});