alexander-turner/agent-input-sanitizer
GitHub: alexander-turner/agent-input-sanitizer
该库在不可信文本进入 LLM 之前净化其中的提示注入攻击向量,覆盖不可见字符、隐藏 HTML 指令和数据外泄 URL 检测三个层面。
Stars: 0 | Forks: 0
# `agent-input-sanitizer`
在任何模型(无论是在 agent、RAG 还是工具调用 pipeline 中)看到不可信文本之前对其进行净化。与具体 Provider 无关:它净化的是字节,而不是 prompt。
它做三件事,分为三个入口点,以便繁重的 HTML 依赖项保持可选:
1. **不可见字符 + ANSI 剥离**(`./invisible`,零运行时依赖项) — 移除零宽空格、bidi 控制字符、变体选择符、标签字符、ANSI/SGR 转义序列以及其他用于走私指令的隐藏代码点。在书写系统(阿拉伯语、印度语系、emoji)需要的地方保留 ZWNJ/ZWJ。
2. **隐藏 HTML 拼接**(`./html`) — 移除隐藏在注释、`display:none`、屏幕外、白底白字、`hidden`/`aria-hidden` 等中的指令。保留一个占位符;其他所有字节原样保留。
3. **Exfil URL 检测**(`./html`,仅检测) — 报告旨在将数据泄漏到跨域的 URL(位于 query/path 中的 payload、嵌入式凭证、`data:`/`javascript:` 目标、跨域重定向)。仅作报告,从不修改。
有关每种攻击向量的详细信息,请参阅 [THREAT-MODEL.md](./THREAT-MODEL.md)。
```
npm install agent-input-sanitizer
```
## 用法
### 便捷函数
```
import { sanitize } from "agent-input-sanitizer";
// Layer 1 only (invisible chars + ANSI), no heavy deps:
const { cleaned, found, warnings } = await sanitize(untrustedText);
// Opt into the HTML layers for web/HTML ingress:
const result = await sanitize(fetchedPageSource, { html: true });
// result.cleaned — hidden HTML spliced out, placeholders left in place
// result.found — stable category codes neutralized (e.g. ["cf-format", "hidden-html"])
// result.warnings — human-facing notices (long-run alerts, exfil reasons, …)
```
`sanitize` 永远不会抛出异常,也永远不会静默丢弃内容:对文本的任何更改都会伴随至少一个 `warnings` 条目。`{ html: true }` 路径会延迟加载 HTML 依赖项,因此仅调用 Layer-1 的用户永远不会为此付出性能代价。
### 零依赖的不可见字符核心
```
import {
stripInvisible,
stripInvisibleWithReport,
} from "agent-input-sanitizer/invisible";
stripInvisible(text); // -> cleaned string
const { cleaned, found } = stripInvisibleWithReport(text);
// found names exactly the category codes removed, e.g. ["variation-selectors"]
```
### HTML 层
较重——它会引入 unified/remark/rehype 依赖图(约 200 毫秒的模块加载时间)。仅在需要时才直接导入它。
```
import {
sanitizeHtml,
detectExfil,
checkExfilUrl,
} from "agent-input-sanitizer/html";
const cleaned = sanitizeHtml(pageSource); // null when nothing to strip/report
const threats = detectExfil(pageSource); // null or [{ isImage, reason, target }]
const reason = checkExfilUrl(oneUrl); // null or a string reason
```
标签:DLL 劫持, DNS 反向解析, Naabu, 人工智能, 内容安全, 大语言模型, 数据可视化, 数据清洗, 暗色界面, 用户模式Hook绕过, 自定义脚本