ArticleslgStudy

computer science

Trojan Source

Trojan Source is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Trojan Source rather than just read about it. In short: Trojan Source is a software vulnerability that abuses Unicode's bidirectional characters to display source code differently than the actual execution of the source code. The exploit utilizes how writing scripts of different reading directions are displayed and encoded on computers.

Key takeaways

  • Trojan Source belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Trojan Source to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Trojan Source from memory before moving on to harder problems.

Reference excerpt

Trojan Source is a software vulnerability that abuses Unicode's bidirectional characters to display source code differently than the actual execution of the source code. The exploit utilizes how writing scripts of different reading directions are displayed and encoded on computers. It was discovered by Nicholas Boucher and Ross Anderson at Cambridge University in late 2021.

Background

Unicode is an encoding standard for representing text, symbols, and glyphs. Unicode is the most dominant encoding on computers, used in over 98% of websites as of September 2023. It supports many languages, and because of this, it must support different methods of writing text. This requires support for both left-to-right languages, such as English and Russian, and right-to-left languages, such as Hebrew and Arabic. Since Unicode aims to enable using more than one writing system, it must be able to mix scripts with different display orders and resolve conflicting orders. As a solution, Unicode contains characters called bidirectional characters (Bidi) that describe how text is displayed and represented. These characters can be abused to change how text is interpreted without changing it visually, as the characters are often invisible.

Methodology In the exploit, bidirectional characters are abused to visually reorder text in source code so that later execution occurs in a different order. Bidirectional characters can be inserted in areas of source code where string literals are allowed. This often applies to documentation, variables, or comments.

In the above example, the RLI mark (right-to-left isolate) forces the following text to be interpreted differently than it is displayed: the triple-quote is first (ending the string), followed by a semicolon (starting a new line), and finally with the premature return (returning None and ignoring any code below it). The new line terminates the RLI mark, preventing it from flowing into the below code. Because of the Bidi character, some source code editors and IDEs rearrange the code for display without any visual indication that the code has been rearranged, so a human code reviewer would not normally detect them. However, when the code is inserted into a compiler, the compiler may ignore the Bidi character and process the characters in a different order than visually displayed. When the compiler is finished, it could potentially execute code that visually appeared to be non-executable. Formatting marks can be combined multiple times to create complex attacks.

Impact and mitigation Programming languages that support Unicode strings and follow Unicode's Bidi algorithm are vulnerable to the exploit. This includes languages such as Java, Go, C, C++, C#, Python, and JavaScript. While the attack is not strictly an error, many compilers, interpreters, and websites added warnings or mitigations for the exploit. Both GNU GCC and LLVM received requests to deal with the exploit. Marek Polacek submitted a patch to GCC shortly after the exploit was published that implemented a warning for potentially unsafe directional characters; this functionality was merged for GCC 12 under the -Wbidi-chars flag. LLVM also merged similar patches. Rust fixed the exploit in 1.56.1, rejecting code that includes the characters by default. The developers of Rust found no vulnerable packages prior to the fix. Many source code editors and IDEs now make these potentially unsafe characters more visible. Visual Studio Code now renders control characters by default. Notepad++ and vim already made these characters more visible, as noted in the research paper. Red Hat issued an advisory on their website, labeling the exploit as "moderate". GitHub released a warning on their blog, as well as updating the website to show a dialog box when Bidi characters are detected in a repository's code.

References

External links https://trojansource.codes/ site by the discoverers, Nicholas Boucher and Ross Anderson Proof of concept code Trojan Source full research paper NIST National Vulnerability Database & CVE Common Vulnerabilities and Exposures CVE-2021-42574 - NIST & CVE (BIDI exploit) CVE-2021-42694 - NIST & CVE (homoglyph attack) UAX 9 from the Unicode Consortium about bidirectional characters and formatting Unicode UTR 36 from the Unicode Consortium, which describes the vulnerability in Unicode CERT/CC vulnerability report

Worked examples

Example 1 — a first encounter with Trojan Source

Start with the simplest possible case. Write down what Trojan Source claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Trojan Source before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Trojan Source ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Trojan Source

In research
Trojan Source appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Trojan Source in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Trojan Source is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2021 in computing, Injection exploits, Software bugs, so understanding it makes those chapters shorter.
In everyday life
Look for Trojan Source outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Trojan Source” →

How to study Trojan Source in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Trojan Source means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Trojan Source out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Trojan Source in simple terms?

Trojan Source is a software vulnerability that abuses Unicode's bidirectional characters to display source code differently than the actual execution of the source code. The exploit utilizes how writing scripts of different reading directions are displayed and encoded on computers.

Why does Trojan Source matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Trojan Source?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Trojan Source.

Tags

  • 2021 in computing
  • Injection exploits
  • Software bugs

Keep exploring