Skip to Content
ZeroLeaksDocs
Shield SDK

Output scanning

Find credentials, personal data, exfiltration links, and canary tokens in model output and redact them. scanOutputText, detectSecrets, detectPII, detectExfiltration, and canaries.

An agent following injected instructions usually reveals the attack in its output: a credential from a tool result, a customer's card number, or a markdown image whose URL sends the conversation to an attacker's server. These functions find and redact such content in model output. The provider wrappers run them on every response by default.

sanitize() detects and redacts text leaked from your system prompt.

scanOutputText()

function scanOutputText(text: string, options?: ScanOutputOptions): ScanOutputResult

Runs the detectors below and returns { findings, redacted, blocked }. blocked is true when any finding is high or critical.

OptionDefaultDescription
secretsontrue, false, or SecretsOptions
piiofftrue, false, or PIIOptions
exfiltrationontrue, false, or ExfiltrationOptions
injectionofftrue (every category), false, or InjectionOptions
canarynoneA canary token, or a list of them, to look for
redactionText"[REDACTED]"A string, or a function of the finding that returns one, such as redactionLabel for [REDACTED:email]
redactMinSeverity"low"Only redact findings at or above this severity

Each finding is { type, kind, start, end, severity, confidence, preview }. type is secret, pii, exfiltration, injection, or canary; kind names the detector, such as aws_access_key_id or markdown_image; start and end are offsets in the text. preview is safe to log: it never holds the full value (sk-proj-…9f2a, j***@example.com).

import { scanOutputText } from "@zeroleaks/shield";

const { findings, redacted, blocked } = scanOutputText(modelOutput, {
  pii: true,
  exfiltration: { allowedDomains: ["docs.example.com", ".cdn.example.com"] },
});
if (blocked) {
  log.warn("output findings", findings.map((f) => `${f.kind} ${f.preview}`));
}
return redacted;

detectSecrets()

Finds credentials in text: over 100 kinds, including AWS, Google Cloud, and Azure keys; OpenAI, Anthropic, and a dozen other AI provider keys; GitHub, GitLab, npm, and PyPI tokens; Stripe, Slack, Discord, Twilio, SendGrid, and Shopify secrets; private key blocks; JWTs (a Supabase service_role token is critical); database URLs with passwords; and password = "…"-style assignments with high entropy. SECRET_KINDS lists them all.

Output often contains example keys, so placeholders (sk-..., YOUR_API_KEY, xxxx), well-known examples, and anything inside code spans or fenced code blocks are reported at low severity. Stripe test keys are low as well.

OptionDescription
kindsOnly report these kinds
excludeNever report these kinds
minConfidenceDrop findings below this confidence

detectPII()

Finds personal data. On by default in detectPII(): email, phone, credit_card (with a Luhn check), us_ssn, and iban (with its checksum). Opt in to ip_address (public addresses only), uk_nino, and us_passport. Example values, such as example.com addresses, test card numbers, and 555 phone numbers, are reported at low severity.

detectExfiltration()

Finds content that sends data out when it is rendered or clicked: markdown images and links (inline and reference-style), HTML images, frames, scripts, stylesheets, media, forms, meta refresh, inline event handlers, CSS url() and @import, javascript: links, and bare URLs whose query or path carries data.

OptionDefaultDescription
allowedDomains[]Hosts output may load from or link to: an exact host, or a suffix such as ".example.com"
blockAllImagestrueFlag every image from a host not on the allowlist, even without data in the URL. Images load when rendered, so the request alone is a beacon.
flagLinks"suspicious""suspicious" flags links that carry data or disguise their target; "all" also flags other non-allowlisted links at low severity; "none" ignores links

An auto-loading resource to a host not on the allowlist with data in the URL is critical; one without data is high; a clickable link carrying data is medium.

detectInjection()

Finds injection payloads in output that a downstream system renders or executes, the class OWASP calls improper output handling (LLM05). This includes model text or tool-call arguments used as a script, a SQL query, a shell command, a rendered template, a spreadsheet cell, or a file path. These are standard security test vectors; use detect() for prompt-injection phrases.

Every category is off by default, because a coding assistant legitimately writes SQL, shell, and HTML. Turn on the ones that match where the output goes:

import { scanOutputText } from "@zeroleaks/shield";

// One category, or several:
scanOutputText(output, { injection: { sql: true, shell: true } });
// Everything:
scanOutputText(output, { injection: true });
// Standalone:
import { detectInjection } from "@zeroleaks/shield";
detectInjection(output, { html: true });
OptionCatches
html<script>, event-handler attributes (onerror=), javascript: and vbscript: URLs, data:text/html, srcdoc, CSS expression(; <iframe>/<object>/<embed> at medium (html_injection)
sqlUNION SELECT, a quote breaking out into a stacked DROP/DELETE/UPDATE, OR 1=1 and OR 'a'='a tautologies, and admin'-- comment terminators
shellcommand substitution of a real command ($(curl …)), curl … | sh, chaining into a destructive command (&& rm -rf), reverse shells (/dev/tcp/…, nc -e), and fork bombs
templateserver-side template injection: {{ }} reaching __class__, config, self, request, or os.; ${ } expression language (${T(...)}, Runtime); <% %> tags running code; and {{7*7}} probes
csva cell starting with = + - @ that calls out: =HYPERLINK(, =IMPORTXML(, =WEBSERVICE(, or a cmd|/msexcel| DDE link
paths../ (raw, encoded, or ..;/) reaching a sensitive file such as /etc/passwd, win.ini, .ssh/, or .aws/credentials, and percent-encoded traversal on its own
minConfidenceDrop findings below this confidence

Severity is high for shapes that execute (script tags, event handlers, command substitution, stacked destructive SQL, formula callouts, traversal to a sensitive file) and medium for weaker signals (iframes, an admin'-- comment, {{7*7}}, a bare encoded ../).

Code blocks

A coding assistant shows SQL, shell, and HTML on purpose, so a match inside a fenced (```) or inline (`) code block is reported at low severity rather than dropped. The finding is still there for a reviewer, but low severity does not set blocked and, with the default redactMinSeverity, a wrapper reads it without blocking. The same payload outside a code block keeps its real severity. This is the same rule the exfiltration detector uses. It does not cover indented (four-space) code blocks, which are treated as prose.

Turning it on in a wrapper

The provider wrappers pass their output option directly to scanOutputText, so injection scanning is enabled the same way in each wrapper:

import { shieldOpenAI } from "@zeroleaks/shield";

const client = shieldOpenAI(openai, {
  output: { injection: { html: true, sql: true, shell: true } },
  blockOnOutputFindings: true, // throw OutputBlockedError on a high finding
});

The MCP client wrapper (shieldMcpClient) checks tool-call arguments with scanArguments, which is the same ScanOutputOptions, so scanArguments: { injection: { shell: true, sql: true } } blocks a callTool whose arguments carry a shell or SQL payload.

When both injection.html and exfiltration are on, an event-handler attribute such as onerror= is reported by both detectors (one as injection/xss, one as exfiltration/html_event_handler). Redaction merges the overlapping spans, so the redacted output is unaffected. The findings list includes both detections.

False-positive rates

Measured with every category on over benign corpora (only non-attack rows), reporting the share of documents with any finding, and with a medium-or-high finding:

CorpusnAny findingMedium or high
OpenAssistant prompts1,5950.00%0.00%
Wikipedia documents1,0000.00%0.00%
No Robots instructions5000.00%0.00%
UltraChat (code-heavy)18,0000.11%0.03%

On the code-heavy set the few remaining hits are almost all real <script> or <iframe> in HTML tutorials written without code fences; the SQL, shell, and template hits there sit inside fences and are reported at low severity. Per kind on that set: xss 0.08% any (0.03% medium or high), html_injection, sql, shell, and template each 0.01% any, csv_formula and path_traversal 0.00%.

What is not covered

Prompt injection and jailbreak phrasing (use detect()), NoSQL and LDAP injection, indented code blocks, fully custom template engines, and CSV formulas that only compute (=SUM(...)) rather than call out. Generic $(...) substitution and bare ../ relative paths are deliberately not flagged, because ordinary scripts and imports use them constantly.

Canary tokens

A canary is a random token you place in the system prompt. It has no reason to appear in output, so if it does, the prompt leaked, even if the model translated or paraphrased the rest.

import { createCanary, findCanary, harden } from "@zeroleaks/shield";

const canary = createCanary(); // e.g. "ZL-CANARY-7f3a9c2e41b0d6a8"
const system = harden(basePrompt, { canary });

const hits = findCanary(modelOutput, canary);
if (hits.length > 0) {
  // The system prompt leaked.
}

createCanary() uses Web Crypto (globalThis.crypto.getRandomValues), available in Node.js 19+, Bun, Deno, Cloudflare Workers, and browsers; on Node.js 18, start Node with --experimental-global-webcrypto or pass your own random token.

findCanary() finds the token verbatim, with changed case or inserted spaces, zero-width characters, or dropped hyphens, reversed, and inside base64, hex, or URL encoding. With the provider wrappers, pass canary: true and they create one, plant it, and treat any appearance in output as a leak.

Redaction helpers

redactFindings(text, findings, { redactionText }) merges overlapping findings and replaces each with the redaction text. redactionLabel gives [REDACTED:<kind>].

Performance

All detectors together take about 0.1ms on a 2KB response. They avoid patterns that can backtrack catastrophically, so crafted input can't stall them: each finishes a 1MB input in a fraction of a second, or a few seconds when nearly all of it is findings, such as 50,000 card numbers.

On this page