A Trojan Source attack hides malicious logic in plain sight by using Unicode bidirectional override characters. The code looks correct to a human reviewer but compiles differently. Nothing in the file is encrypted and no character is hidden; the trick is that the same bytes are displayed in a different order from the one the compiler reads.
How the trick works
Text is stored in logical order: the bytes are read left-to-right by the compiler. But the visual renderer follows Unicode bidirectional rules. A character like U+202E Right-to-Left Override can force part of the line to display in reverse order while the compiler still reads the original bytes. The line is not obfuscated in any encoding sense; it is ordinary text whose on-screen order differs from its logical order.
The characters behind the attack
The controls that make this possible are ordinary characters that exist for legitimate bidirectional text. They fall into a few groups. The original embedding and override characters wrap a run of text and change how it is ordered: U+202A Left-to-Right Embedding, U+202B Right-to-Left Embedding, U+202D Left-to-Right Override, U+202E Right-to-Left Override, and U+202C Pop Directional Formatting. Later versions of Unicode added isolation controls, U+2066 through U+2069, which confine reordering to a nested range. Directional marks such as U+200E and U+200F set a direction without wrapping a run at all.
None of these characters is malicious on its own. They exist because editors must be able to display a right-to-left phrase inside a left-to-right sentence. The risk is not the character itself but where it shows up — inside code where no one wrote it on purpose.
Why compilers and renderers disagree
Source code is stored and consumed in logical order. A compiler reads the byte sequence as it is, following the grammar of the language, and it never applies bidirectional reordering. The visual reordering happens later, in the text renderer that draws the file on screen. Code review, in turn, happens on exactly that rendered view.
The gap between the two is what makes the attack work: the reviewer reads the displayed order, and the compiler reads the logical order, and on a line rigged with a directional control those two orders no longer match.
Why it matters for code review
A reviewer might see:
if (isAdmin) { /* safe logic */ }
while the compiler sees:
if (isAdmin) { /* evil logic */ }
The difference is invisible unless you inspect the raw bytes.
What a Trojan Source attack looks like
Attacks of this kind share a rough shape. A region of a line is enclosed between a directional control and its matching terminator, so the display order inside that region reverses. Characters that look like a comment marker or a closing string delimiter appear to sit in one place while, logically, they sit somewhere else. Nothing is deleted and nothing is invented: the characters are the same, only read in a different order than they are shown.
Because the technique relies on ordering rather than on a hidden payload, the whole difference is confined to logic that the compiler still interprets literally, one character at a time. That is what makes it convincing on screen and dangerous in the build.
What this tool flags
The scanner marks every bidirectional control character with a high-severity finding and shows the surrounding context. It does not block your build, but it gives you a concrete reason to inspect the line manually.
Not all BiDi is malicious
Right-to-left text is essential for Arabic, Hebrew, Persian, and other languages. The risk appears when BiDi controls show up unexpectedly inside code, URLs, or identifiers. That is what the scanner is designed to catch.
How to spot it and reduce the risk
- Scan the source, not the rendered page. A character scanner flags directional controls and shows the logical context around each one, so you can see what the line actually says.
- Add a check to your pipeline. A lint rule or a continuous-integration step can fail the build when directional controls appear in source files, or force them to be reviewed explicitly before merging.
- Read unfamiliar code where invisibles are visible. Use an editor that renders invisible characters, or convert the file to a form that escapes them before reading it.
- Keep the controls out of identifiers, string literals, and URLs. Unless the content genuinely requires them, there is rarely a reason for a bidirectional control to be in code.
What a scan can and cannot tell you
The scanner identifies the control characters and shows the surrounding context, which is enough to know that a line deserves a manual look. It does not tell you what the author intended, it does not rewrite your file, and it does not judge whether the code is exploitable. A flagged character is a reason to inspect, not a verdict.
BiDi controls are not the only Unicode trick that can make two strings look identical while having different bytes. See Unicode Normalization in Plain English for how the same letter can be encoded in more than one way.