1 +import { describe, expect, it }
from "vitest";
2 +import { LeaksEngine }
from "../src/engine.ts";
4 +const secret
= "TOKEN_123";
5 +const opaque
= "TOKEN_999 __LEAK_TOKEN_abcdef123456__";
8 + return new LeaksEngine({
9 + settings: { regex: [{ pattern:
"TOKEN_[0-9]+", label:
"TOKEN" }] },
13 +const fixtures
: Array<[
string, (text
: string)
=> unknown]>
= [
15 + "OpenAI Responses and Codex",
19 + prompt_cache_key: opaque,
20 + previous_response_id: opaque,
21 + metadata: { text: opaque },
22 + text: { format: { name: opaque } },
24 + { role:
"user", content: [{ type:
"input_text", text }] },
29 + content: [{ type:
"output_text", text, annotations: [opaque] }],
34 + encrypted_content: opaque,
35 + summary: [{ type:
"summary_text", text }],
38 + type:
"function_call",
42 + arguments:
JSON.
stringify({ thinkingSignature: text }),
45 + type:
"function_call_output",
48 + { type:
"input_text", text },
49 + { type:
"input_image", image_url: opaque },
52 + { type:
"custom_tool_call", name: opaque, input: text },
53 + { type:
"custom_tool_call_output", call_id: opaque, output: text },
54 + { type:
"compaction", encrypted_content: opaque },
55 + { type:
"future_protocol", text: opaque },
63 + headers: { authorization: opaque },
64 + system: [{ type:
"text", text, cache_control: { type: opaque } }],
69 + { type:
"text", text },
70 + { type:
"thinking", thinking: text },
71 + { type:
"thinking", thinking: opaque, signature: opaque },
72 + { type:
"redacted_thinking", data: opaque },
77 + input: { encrypted_content: text },
80 + type:
"tool_result",
81 + tool_use_id: opaque,
82 + content: [{ type:
"text", text }],
84 + { type:
"image", source: { type:
"base64", data: opaque } },
91 + "Chat Completions and Mistral",
95 + { role:
"system", content: text },
99 + { type:
"text", text },
100 + { type:
"image_url", image_url: { url: opaque } },
107 + reasoning_content: text,
108 + reasoning_details: [
109 + { type:
"reasoning.text", text: opaque, signature: opaque },
110 + { type:
"reasoning.text", text },
111 + { type:
"reasoning.summary", summary: text },
112 + { type:
"reasoning.encrypted", data: opaque },
120 + arguments:
JSON.
stringify({ value: text }),
125 + { role:
"tool", tool_call_id: opaque, content: text },
130 + "Gemini and Vertex",
134 + systemInstruction: text,
135 + cachedContent: opaque,
136 + httpOptions: { headers: { authorization: opaque } },
143 + { thought:
true, text: opaque, thoughtSignature: opaque },
148 + args: { signature: text },
150 + thoughtSignature: opaque,
153 + functionResponse: {
156 + response: { output: text },
157 + parts: [{ inlineData: { data: opaque, mimeType: opaque } }],
160 + { inlineData: { data: opaque, mimeType: opaque } },
167 + "Bedrock Converse",
170 + system: [{ text }, { cachePoint: { type: opaque } }],
176 + { reasoningContent: { reasoningText: { text } } },
178 + reasoningContent: {
179 + reasoningText: { text: opaque, signature: opaque },
183 + reasoningContent: { redactedContent:
new Uint8Array([
1,
2,
3]) },
189 + input: { signature: text },
198 + { json: { value: text } },
199 + { image: { source: { bytes: opaque } } },
209 + "Pi message gateway",
212 + options: { sessionId: opaque },
214 + systemPrompt: text,
216 + { role:
"user", content: text },
221 + responseId: opaque,
223 + { type:
"text", text, textSignature: opaque },
224 + { type:
"thinking", thinking: text },
225 + { type:
"thinking", thinking: opaque, thinkingSignature: opaque },
230 + arguments: { [text]: text },
231 + thoughtSignature: opaque,
234 + usage: { diagnostic: opaque },
237 + role:
"toolResult",
239 + toolCallId: opaque,
241 + { type:
"text", text },
242 + { type:
"image", data: opaque },
244 + details: { value: text },
252 +function schema(text
: string) {
256 + required: [opaque],
267 + $defs: { [opaque]: { title: text, const: text } },
270 + if: { description: text },
272 + then: { description: text },
273 + else: { description: text },
276 + additionalProperties: { description: text },
280 +const toolFixtures
: Array<[
string, (text
: string)
=> unknown]>
= [
289 + parameters:
schema(text),
303 + parameters:
schema(text),
316 + input_schema:
schema(text),
317 + cache_control: { type: opaque },
328 + functionDeclarations: [
332 + parametersJsonSchema:
schema(text),
349 + inputSchema: { json:
schema(text) },
357 + "deferred Responses tools",
361 + type:
"tool_search_output",
364 + { name: opaque, description: text, parameters:
schema(text) },
372 +describe(
"leaks plaintext surfaces", ()
=> {
373 + it.
each([
...fixtures,
...toolFixtures])(
374 + "redacts only plaintext in %s",
375 + (_name, fixture)
=> {
376 + const runtime
= engine();
377 + const placeholder
= runtime.
redactText(secret).
text;
378 + const payload
= fixture(secret);
379 + const result
= runtime.
redactDeep(payload,
"provider",
"provider");
380 + expect(result.
value).
toEqual(
fixture(placeholder));
381 + expect(result.count).
toBeGreaterThan(
0);
382 + expect(runtime.
restoreDeep(result.
value,
"provider").
value).
toEqual(
385 + expect(payload).
toEqual(
fixture(secret));
389 + it(
"leaves unknown payloads untouched and warns without a raw recursive fallback", ()
=> {
390 + const runtime
= engine();
392 + customEnvelope: { text: secret },
393 + headers: { authorization: secret },
395 + expect(runtime.
redactDeep(payload,
"provider",
"provider").
value).
toEqual(
398 + runtime.
redactDeep(payload,
"provider",
"provider");
402 + .warnings.
filter((warning)
=> warning.
code === "surface.provider"),
404 + expect(runtime.
stats().mappingCount).
toBe(
0);
407 + it(
"does not scan signatures, binary fields, or metadata for unresolved placeholders", ()
=> {
408 + const runtime
= engine();
413 + { type:
"thinking", thinking: opaque, thinkingSignature: opaque },
414 + { type:
"text", text:
"clean", textSignature: opaque },
415 + { type:
"image", data: opaque },
418 + expect(runtime.
hydrateToolInput(message,
"message")).
toEqual({
422 + expect(runtime.
redactDeep([message],
"context",
"context").count).
toBe(
0);
423 + expect(runtime.
stats().mappingCount).
toBe(
0);
426 + it.
each([
"google-generative-ai",
"google-vertex"])(
427 + "preserves signed text in %s context before serialization",
429 + const runtime
= engine();
434 + { type:
"text", text: opaque, textSignature: opaque },
435 + { type:
"text", text: secret },
438 + const result
= runtime.
redactDeep([message],
"context",
"context");
439 + expect(result.
value[
0].
content[
0]).
toEqual(message.
content[
0]);
440 + expect(result.
value[
0].
content[
1].
text).not.
toContain(secret);
442 + runtime.
hydrateToolInput(result.
value[
0],
"message").issue,
447 + it(
"still scans arbitrary tool data including protocol-looking names and values", ()
=> {
448 + const runtime
= engine();
451 + encrypted_content: secret,
452 + thinkingSignature: secret,
453 + nested: { [secret]: secret },
455 + const result
= runtime.
redactDeep(data);
456 + expect(
JSON.
stringify(result.
value)).not.
toContain(secret);
457 + expect(runtime.
hydrateToolInput(result.
value)).
toEqual({
462 + runtime.
hydrateToolInput({ encrypted_content: opaque }).issue,