編碼智能體讀取的檔案與網頁可藏有不可見 Unicode 指令
Invisible instructions: hidden Unicode in the files and pages a coding agent reads
不可見 Unicode 字元可把指令藏在編碼智能體讀取的檔案、網頁與任務內容中,令模型讀到人類在編輯器或 diff 中看不到的文字。文章介紹雙向控制符、零寬字元及 Unicode tag 字元,並提供 TypeScript 掃描器及可在 CI 使用的 ripgrep 命令。文章建議清理或轉義模型輸入中的相關字元,對程式碼庫中的發現則讓檢查失敗並交由人工確認,亦建議移除智能體輸出中的 tag 字元。
A reviewer reads a rules file that says "follow HTML5 best practices" and approves it. The model reading the same file sees a second sentence telling it to add a script tag to every page and not to mention it. Nothing about the file looks wrong in an editor, a diff view or a pull request. The second sentence is written in characters that render as nothing.
This is not a model bug you can wait out. It is a gap between what people see and what software reads, and coding agents sit right in it: they read issues, READMEs, rules files, web pages and source code written by strangers, and they write code that people review by eye. This post covers the character classes involved, the public research on each, and the checks worth adding.
Three families of characters
Bidirectional controls. Unicode lets left-to-right and right-to-left text share a line, using invisible controls such as RIGHT-TO-LEFT OVERRIDE (U+202E) and the isolates U+2066 to U+2069. In 2021 the Trojan Source research showed these can make source code display in a different order from how a compiler reads it, so a comment appears to close where it does not, or a string appears to end early. It was published as CVE-2021-42574, and the same work described homoglyph attacks (lookalike letters from other scripts) as CVE-2021-42694.
Zero-width characters. ZERO WIDTH SPACE (U+200B), ZERO WIDTH NON-JOINER and JOINER (U+200C, U+200D), WORD JOINER (U+2060) and the zero-width no-break space (U+FEFF) take up no space. They can split a word so a keyword filter misses it, or carry data in the pattern of where they appear.
Unicode tag characters. The Tags block, U+E0000 to U+E007F, mirrors ASCII: there is an invisible version of every printable letter, digit and symbol. Unicode's emoji standard notes that a tag-unaware implementation displays any sequence of them as invisible. In January 2024 Riley Goodside showed a chat model following instructions written entirely in tag characters, and Johann Rehberger described the technique as "ASCII smuggling", including the reverse direction: a model can be prompted to emit tag characters, hiding data in its own output. AWS's security blog has a write-up on defending against it.
Tag characters are the sharpest of the three for agents. Bidi tricks need a compiler or parser to disagree with a renderer; tag characters need only a model that reads text a person cannot see.
Where they reach a coding agent
-
Agent instruction files. In March 2025 Pillar Security published the "Rules File Backdoor": hidden Unicode inside Cursor and GitHub Copilot rules files that steered the assistants into adding code, while the hidden characters stayed invisible in the GitHub pull request review used to accept the files. The same applies to
AGENTS.md,CLAUDE.md,.cursorrulesand.github/copilot-instructions.md. - Issues, pull request comments and commit messages. Anything a task pulls in from a tracker is written by whoever can file an issue.
- Fetched pages and package READMEs. A web fetch or a dependency's documentation lands in context as text.
- Source code the agent copies or writes. A snippet from the web can carry bidi controls into a commit. A model can also emit these characters itself.
- The agent's own output. Tag characters in a generated link, message or file can carry data out past a person who sees nothing unusual.
A scanner you can put in front of the model
The scanner below walks text by code point, reports each hidden character with its line and column, decodes tag characters so a reviewer can read what they say, and strips them on request:
// Code points that render as nothing, or that reorder the text around them.
const HIDDEN: Array<[number, number, string]> = [
[0x200b, 0x200f, 'zero-width character or direction mark'],
[0x202a, 0x202e, 'bidirectional embedding or override'],
[0x2060, 0x2064, 'word joiner or invisible operator'],
[0x2066, 0x2069, 'bidirectional isolate'],
[0xfeff, 0xfeff, 'zero-width no-break space'],
[0xe0000, 0xe007f, 'Unicode tag character'],
];
export interface Finding {
line: number;
column: number;
codePoint: string; // e.g. "U+202E"
kind: string;
}
function kindOf(cp: number): string | undefined {
for (const [lo, hi, kind] of HIDDEN) if (cp >= lo && cp <= hi) return kind;
return undefined;
}
export function findHidden(text: string): Finding[] {
const findings: Finding[] = [];
let line = 1;
let column = 1;
for (const ch of text) {
// for...of walks code points, so tag characters (outside the BMP) arrive whole
const cp = ch.codePointAt(0)!;
const kind = kindOf(cp);
if (kind) {
findings.push({ line, column, codePoint: `U+${cp.toString(16).toUpperCase().padStart(4, '0')}`, kind });
}
if (ch === '\n') {
line++;
column = 1;
} else {
column++;
}
}
return findings;
}
// Tag characters mirror ASCII: U+E0041 is a hidden "A". Decoding them shows a reviewer
// what the hidden text says. Show it to people; never feed it to the model.
export function decodeTags(text: string): string {
let out = '';
for (const ch of text) {
const cp = ch.codePointAt(0)!;
if (cp >= 0xe0020 && cp <= 0xe007e) out += String.fromCodePoint(cp - 0xe0000);
}
return out;
}
export function stripHidden(text: string): string {
let out = '';
for (const ch of text) if (!kindOf(ch.codePointAt(0)!)) out += ch;
return out;
}
Iterating with for...of matters: tag characters sit outside the Basic Multilingual Plane, so they are two UTF-16 units each, and indexing a string by charCodeAt would see only surrogate halves.
A one-liner for CI
For files in a repository, ripgrep can find the same ranges:
rg -n '[\x{200B}-\x{200F}\x{202A}-\x{202E}\x{2060}-\x{2064}\x{2066}-\x{2069}\x{FEFF}\x{E0000}-\x{E007F}]' .
rg exits with status 0 when it finds a match and 1 when it finds none, so a CI step can fail on a match. Run it at least over agent instruction files and over every pull request an agent opened. GitHub already shows a warning on files that contain bidirectional text, per its October 2021 changelog, but that warning covers bidi controls only, and a warning is not a failed check.
What to do with a finding
Stripping everything is tempting and occasionally wrong. These characters have real uses:
- Right-to-left languages. Arabic and Hebrew text legitimately uses direction marks and isolates, in documentation and in string literals.
- Emoji. ZERO WIDTH JOINER builds many emoji sequences, and some flag emoji, such as the flags of England, Scotland and Wales, are built from tag characters. AWS's post notes that stripping tags turns those flags into plain black flags.
- Byte order marks. A U+FEFF at the very start of a file is a byte order mark, common in files from Windows tools.
A workable policy splits by where the text is going:
- Into a model's context (issues, pages, rules files, tool results): strip tag characters always, and strip or escape bidi and zero-width characters. Log the finding with the decoded text so a person can see what was attempted. The model gets clean text; it never needs the hidden part.
- Into a repository (an agent's patch): do not strip silently, because that changes the code. Fail the check, show the reviewer each finding with its code point, and require a person to accept it. In source code, a legitimate bidi character can be written as an escape sequence instead.
- Out of the agent (messages, links, files it writes for people): strip tag characters from output, which closes the smuggling direction.
Treat the scanner as one layer. Homoglyphs, lookalike letters from other scripts, are visible characters and pass straight through it; catching those needs confusable detection such as Unicode's UTS #39 data, or a policy that identifiers are ASCII.
Where Sandscale fits
Scanning reduces how often hidden instructions reach a model. It does not make a successful one harmless. That is the job of where the agent's code runs: in a Sandscale sandbox, each run gets its own Firecracker microVM, the network mode defaults to none, and DLP checks prompts and MCP tool calls for credentials. If an invisible instruction does get followed, it is followed inside a box with nothing to reach. Our post on prompt injection to code execution maps the rest of the layers.
Originally published on the Sandscale blog.
來源:DEV Community · dev.to