The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { describe, expect, test } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Redacted from "effect/Redacted"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import { |
| 6 | collectRedactedSecrets, |
| 7 | redactionMarker, |
| 8 | redactJson, |
| 9 | redactStream, |
| 10 | redactText, |
| 11 | } from "@/Util/Redaction.ts"; |
| 12 | |
| 13 | const tags = ["unit", "local"]; |
| 14 | |
| 15 | describe("collectRedactedSecrets", () => { |
| 16 | test( |
| 17 | "collects strings inside redacted values and containers", |
| 18 | () => { |
| 19 | expect(collectRedactedSecrets({ token: Redacted.make("s3cr3t") })).toEqual(["s3cr3t"]); |
| 20 | expect(collectRedactedSecrets(Redacted.make({ token: "s3cr3t" }))).toEqual(["s3cr3t"]); |
| 21 | expect( |
| 22 | collectRedactedSecrets(Redacted.make(["s3cr3t", { nested: "another-secret" }])), |
| 23 | ).toEqual(["s3cr3t", "another-secret"]); |
| 24 | expect( |
| 25 | collectRedactedSecrets({ public: "ordinary-text", token: Redacted.make("s3cr3t") }), |
| 26 | ).toEqual(["s3cr3t"]); |
| 27 | expect( |
| 28 | collectRedactedSecrets( |
| 29 | Redacted.make({ |
| 30 | token: Redacted.make("s3cr3t"), |
| 31 | nested: { password: "another-secret" }, |
| 32 | }), |
| 33 | ), |
| 34 | ).toEqual(["s3cr3t", "another-secret"]); |
| 35 | expect(collectRedactedSecrets(Redacted.make({ pin: "ab" }))).toEqual(["ab"]); |
| 36 | expect(collectRedactedSecrets(Redacted.make(123456))).toEqual(["123456"]); |
| 37 | }, |
| 38 | { tags }, |
| 39 | ); |
| 40 | |
| 41 | test( |
| 42 | "parses a redacted JSON string so its inner values are secrets too", |
| 43 | () => { |
| 44 | expect(collectRedactedSecrets(Redacted.make('{"pin":"s3cr3t"}'))).toEqual([ |
| 45 | '{"pin":"s3cr3t"}', |
| 46 | "s3cr3t", |
| 47 | ]); |
| 48 | }, |
| 49 | { tags }, |
| 50 | ); |
| 51 | |
| 52 | test( |
| 53 | "skips empty strings, plain numbers and non-plain objects, and de-duplicates", |
| 54 | () => { |
| 55 | expect(collectRedactedSecrets(Redacted.make(""))).toEqual([]); |
| 56 | expect(collectRedactedSecrets({ port: 8080, token: Redacted.make("s3cr3t") })).toEqual([ |
| 57 | "s3cr3t", |
| 58 | ]); |
| 59 | expect(collectRedactedSecrets(Redacted.make(new Date(0)))).toEqual([]); |
| 60 | expect(collectRedactedSecrets([Redacted.make("s3cr3t"), Redacted.make("s3cr3t")])).toEqual([ |
| 61 | "s3cr3t", |
| 62 | ]); |
| 63 | }, |
| 64 | { tags }, |
| 65 | ); |
| 66 | |
| 67 | test( |
| 68 | "parses a redacted JSON array and keeps a string that only looks like JSON", |
| 69 | () => { |
| 70 | expect(collectRedactedSecrets(Redacted.make('["a1b2","c3d4"]'))).toEqual([ |
| 71 | '["a1b2","c3d4"]', |
| 72 | "a1b2", |
| 73 | "c3d4", |
| 74 | ]); |
| 75 | expect(collectRedactedSecrets(Redacted.make("{not json"))).toEqual(["{not json"]); |
| 76 | }, |
| 77 | { tags }, |
| 78 | ); |
| 79 | }); |
| 80 | |
| 81 | describe("redactText", () => { |
| 82 | test( |
| 83 | "replaces the raw, base64 and JSON-escaped forms of a secret", |
| 84 | () => { |
| 85 | const secret = 'pa"ss'; |
| 86 | const base64 = Buffer.from(secret).toString("base64"); |
| 87 | expect(redactText(`raw ${secret} b64 ${base64} json pa\\"ss`, [secret])).toBe( |
| 88 | "raw <redacted> b64 <redacted> json <redacted>", |
| 89 | ); |
| 90 | }, |
| 91 | { tags }, |
| 92 | ); |
| 93 | |
| 94 | test( |
| 95 | "matches long secrets anywhere and short secrets only as whole tokens", |
| 96 | () => { |
| 97 | expect(redactText("prefixs3cr3tsuffix", ["s3cr3t"])).toBe("prefix<redacted>suffix"); |
| 98 | expect(redactText("code 14293 says 42", ["42"])).toBe("code 14293 says <redacted>"); |
| 99 | }, |
| 100 | { tags }, |
| 101 | ); |
| 102 | |
| 103 | test( |
| 104 | "replaces the longest secret first when one is a prefix of another", |
| 105 | () => { |
| 106 | expect(redactText("value abcdefghij", ["abcd", "abcdefghij"])).toBe("value <redacted>"); |
| 107 | }, |
| 108 | { tags }, |
| 109 | ); |
| 110 | |
| 111 | test( |
| 112 | "uses the caller's marker", |
| 113 | () => { |
| 114 | expect(redactText("token s3cr3t", ["s3cr3t"], "[REDACTED]")).toBe("token [REDACTED]"); |
| 115 | }, |
| 116 | { tags }, |
| 117 | ); |
| 118 | |
| 119 | test( |
| 120 | "matches a short secret literally, not as a regex", |
| 121 | () => { |
| 122 | expect(redactText("1x2 and 1.2", ["1.2"])).toBe("1x2 and <redacted>"); |
| 123 | }, |
| 124 | { tags }, |
| 125 | ); |
| 126 | |
| 127 | test( |
| 128 | "matches a form anywhere when any secret producing it is long", |
| 129 | () => { |
| 130 | // "NDI=" is both a long secret and the base64 form of the short "42". |
| 131 | expect(redactText("xNDI=x", ["NDI=", "42"])).toBe("x<redacted>x"); |
| 132 | }, |
| 133 | { tags }, |
| 134 | ); |
| 135 | |
| 136 | test( |
| 137 | "returns the text unchanged without secrets", |
| 138 | () => { |
| 139 | expect(redactText("s3cr3t", [])).toBe("s3cr3t"); |
| 140 | }, |
| 141 | { tags }, |
| 142 | ); |
| 143 | }); |
| 144 | |
| 145 | describe("redactJson", () => { |
| 146 | test( |
| 147 | "redacts JSON leaf by leaf and falls back to text", |
| 148 | () => { |
| 149 | const body = JSON.stringify({ |
| 150 | n: 14293, |
| 151 | token: "42", |
| 152 | data: Buffer.from("42").toString("base64"), |
| 153 | message: 'code 14293 says "42"', |
| 154 | }); |
| 155 | expect(JSON.parse(redactJson(body, ["42"]))).toEqual({ |
| 156 | n: 14293, |
| 157 | token: "<redacted>", |
| 158 | data: "<redacted>", |
| 159 | message: 'code 14293 says "<redacted>"', |
| 160 | }); |
| 161 | expect(JSON.parse(redactJson('{"message":"abcdefghij"}', ["abcd", "abcdefghij"]))).toEqual({ |
| 162 | message: "<redacted>", |
| 163 | }); |
| 164 | expect(redactJson("rendered a v1/s3cr3t name", ["s3cr3t"])).toBe( |
| 165 | "rendered a v1/<redacted> name", |
| 166 | ); |
| 167 | }, |
| 168 | { tags }, |
| 169 | ); |
| 170 | |
| 171 | test( |
| 172 | "redacts a number leaf equal to a secret and keeps other leaves", |
| 173 | () => { |
| 174 | expect(JSON.parse(redactJson('{"n":42,"list":["42",null,true,7]}', ["42"]))).toEqual({ |
| 175 | n: "<redacted>", |
| 176 | list: ["<redacted>", null, true, 7], |
| 177 | }); |
| 178 | }, |
| 179 | { tags }, |
| 180 | ); |
| 181 | |
| 182 | test( |
| 183 | "returns the text unchanged without secrets", |
| 184 | () => { |
| 185 | expect(redactJson('{ "a": 1 }', [])).toBe('{ "a": 1 }'); |
| 186 | }, |
| 187 | { tags }, |
| 188 | ); |
| 189 | }); |
| 190 | |
| 191 | describe("redactStream", () => { |
| 192 | test.effect( |
| 193 | "redacts a secret split across chunks, including its base64 form", |
| 194 | () => |
| 195 | Effect.gen(function* () { |
| 196 | const base64 = Buffer.from("s3cr3t").toString("base64"); |
| 197 | const chunks = ["one s3c", "r3t two ", base64.slice(0, 3), base64.slice(3), " end"]; |
| 198 | const out = yield* Stream.fromIterable(chunks).pipe( |
| 199 | (stream) => redactStream(stream, ["s3cr3t"]), |
| 200 | Stream.mkString, |
| 201 | ); |
| 202 | expect(out).toBe("one <redacted> two <redacted> end"); |
| 203 | }), |
| 204 | { tags }, |
| 205 | ); |
| 206 | |
| 207 | test.effect( |
| 208 | "output does not depend on chunking when one secret overlaps another", |
| 209 | () => |
| 210 | Effect.gen(function* () { |
| 211 | const cases = [ |
| 212 | // A shorter secret is a prefix of a longer one (#2399). |
| 213 | { |
| 214 | secrets: ["hunter2", "hunter2-admin-token"], |
| 215 | text: "token=hunter2-admin-token\n", |
| 216 | expected: "token=<redacted>\n", |
| 217 | }, |
| 218 | { secrets: ["aa\\\\", 'aa\\\\"'], text: 'aa\\\\"', expected: "<redacted>" }, |
| 219 | // A shorter secret sits inside a longer one that starts earlier. |
| 220 | { |
| 221 | secrets: ["mid-secret", "outer-mid-secret-tail"], |
| 222 | text: "x outer-mid-secret-tail y", |
| 223 | expected: "x <redacted> y", |
| 224 | }, |
| 225 | ]; |
| 226 | for (const { secrets, text, expected } of cases) { |
| 227 | const whole = yield* Stream.make(text).pipe( |
| 228 | (stream) => redactStream(stream, secrets), |
| 229 | Stream.mkString, |
| 230 | ); |
| 231 | expect(whole).toBe(expected); |
| 232 | for (let split = 1; split < text.length; split++) { |
| 233 | const out = yield* Stream.make(text.slice(0, split), text.slice(split)).pipe( |
| 234 | (stream) => redactStream(stream, secrets), |
| 235 | Stream.mkString, |
| 236 | ); |
| 237 | expect(out).toBe(expected); |
| 238 | } |
| 239 | } |
| 240 | }), |
| 241 | { tags }, |
| 242 | ); |
| 243 | |
| 244 | const streamed = ( |
| 245 | chunks: ReadonlyArray<string>, |
| 246 | secrets: ReadonlyArray<string>, |
| 247 | marker?: string, |
| 248 | ) => |
| 249 | Stream.fromIterable(chunks).pipe( |
| 250 | (stream) => redactStream(stream, secrets, marker), |
| 251 | Stream.mkString, |
| 252 | ); |
| 253 | |
| 254 | test.effect( |
| 255 | "flushes a held-back match when the stream ends", |
| 256 | () => |
| 257 | Effect.gen(function* () { |
| 258 | const secrets = ["hunter2", "hunter2-admin-token"]; |
| 259 | expect(yield* streamed(["token=hunter2"], secrets)).toBe("token=<redacted>"); |
| 260 | expect(yield* streamed(["hunter2-adm", "in"], secrets)).toBe("<redacted>-admin"); |
| 261 | expect(yield* streamed(["key=s3c"], ["s3cr3t"])).toBe("key=s3c"); |
| 262 | }), |
| 263 | { tags }, |
| 264 | ); |
| 265 | |
| 266 | test.effect( |
| 267 | "redacts several matches in one chunk and a JSON-escaped form split across chunks", |
| 268 | () => |
| 269 | Effect.gen(function* () { |
| 270 | expect(yield* streamed(["s3cr3t and s3cr3t"], ["s3cr3t"])).toBe( |
| 271 | "<redacted> and <redacted>", |
| 272 | ); |
| 273 | expect(yield* streamed(["msg pa\\", '"ss end'], ['pa"ss'])).toBe("msg <redacted> end"); |
| 274 | }), |
| 275 | { tags }, |
| 276 | ); |
| 277 | |
| 278 | test.effect( |
| 279 | "matches short secrets anywhere, unlike redactText", |
| 280 | () => |
| 281 | Effect.gen(function* () { |
| 282 | expect(yield* streamed(["code 14293"], ["42"])).toBe("code 1<redacted>93"); |
| 283 | }), |
| 284 | { tags }, |
| 285 | ); |
| 286 | |
| 287 | test.effect( |
| 288 | "uses the caller's marker and passes chunks through without secrets", |
| 289 | () => |
| 290 | Effect.gen(function* () { |
| 291 | expect(yield* streamed(["token s3cr3t"], ["s3cr3t"], "[REDACTED]")).toBe( |
| 292 | "token [REDACTED]", |
| 293 | ); |
| 294 | const chunks = yield* Stream.fromIterable(["a", "b"]).pipe( |
| 295 | (stream) => redactStream(stream, []), |
| 296 | Stream.runCollect, |
| 297 | ); |
| 298 | expect([...chunks]).toEqual(["a", "b"]); |
| 299 | }), |
| 300 | { tags }, |
| 301 | ); |
| 302 | }); |
| 303 | |
| 304 | describe("redactionMarker", () => { |
| 305 | test( |
| 306 | "keeps the preferred marker unless a secret appears inside it", |
| 307 | () => { |
| 308 | expect(redactionMarker(["s3cr3t"], "[REDACTED]")).toBe("[REDACTED]"); |
| 309 | const marker = redactionMarker(["[REDACTED]"], "[REDACTED]"); |
| 310 | expect(marker).not.toBe("[REDACTED]"); |
| 311 | expect("[REDACTED]".includes(marker)).toBe(false); |
| 312 | }, |
| 313 | { tags }, |
| 314 | ); |
| 315 | |
| 316 | test( |
| 317 | "falls back to a supplementary private-use character, then to an empty marker", |
| 318 | () => { |
| 319 | const range = (from: number, to: number) => { |
| 320 | let out = ""; |
| 321 | for (let codePoint = from; codePoint <= to; codePoint++) { |
| 322 | out += String.fromCodePoint(codePoint); |
| 323 | } |
| 324 | return out; |
| 325 | }; |
| 326 | const basic = range(0xe000, 0xf8ff); |
| 327 | expect(redactionMarker(["redacted", basic])).toBe(String.fromCodePoint(0xf0000)); |
| 328 | expect(redactionMarker(["redacted", basic, range(0xf0000, 0xffffd)])).toBe(""); |
| 329 | }, |
| 330 | { tags }, |
| 331 | ); |
| 332 | }); |