Bidirectional text — usually shortened to BiDi — is text that mixes two reading directions on the same line. This page explains what BiDi is, the invisible characters that steer it, and how those characters affect everything from display and search to code review and security.

What is BiDi / bidirectional text?

Most scripts run in a single direction. English and most other Latin-based languages are written left to right (LTR). Arabic, Hebrew, Persian, and several others are written right to left (RTL). BiDi is what happens when both directions appear in the same piece of text — an English sentence that quotes an Arabic phrase, or an Arabic paragraph that quotes a URL or a code identifier.

Computers handle this with the Unicode Bidirectional Algorithm, which assigns each character an implicit direction and then works out the order in which those characters are displayed. The text itself is stored in one logical order — the order in which it was typed — while the visual order on screen is rearranged so that each run stays readable. In short, logical order and display order are two different things, and BiDi is the bridge between them.

The characters that steer direction

Alongside the letters themselves, Unicode defines a small set of invisible characters that let text set or isolate direction explicitly:

  • U+200E Left-to-Right Mark (LRM) and U+200F Right-to-Left Mark (RLM) — invisible markers that fix the direction of an ambiguous character, such as punctuation sitting between two runs.
  • U+061C Arabic Letter Mark — a directional marker used in Arabic context.
  • U+202A–U+202E — the embedding and override controls (LRE, RLE, PDF, LRO, RLO) that force a span of text to a chosen directional level.
  • U+2066–U+2069 — the newer isolate controls (LRI, RLI, FSI, PDI) that contain a directional run without letting it leak into the surrounding text.

None of these characters takes up visible space. That is exactly what makes them useful for correct rendering — and equally useful for hiding things.

How direction affects display

Because the renderer is allowed to reorder characters for readability, the same sequence of code points can be shown in more than one way. A line can read one thing on screen while carrying its characters in a different order underneath. In ordinary bilingual prose this is desirable: it keeps the text natural for both scripts. But it also means that what you see is not always what the bytes say, and that gap is the property that attacks such as Trojan Source exploit.

Where BiDi text shows up in practice

  • Reading and editing: mixed-direction lines need isolation marks to look right; without them, punctuation appears to jump to the wrong end of a sentence.
  • Search and comparison: an invisible mark inside a string can make two apparently identical values differ, so exact-match search and deduplication can quietly miss.
  • Copy-paste and data entry: directional controls travel with the text when it is copied, and can end up sitting silently inside a form field, an email address, or a filename.
  • Interfaces and localization: an RTL interface that also shows LTR strings needs correct BiDi handling, or labels and numbers display in the wrong order.
  • Code and security: an override inside a comment, string, or identifier can hide logic from a reviewer — the mechanism behind Trojan Source.

Why a bidirectional text detector is useful

Because the directional controls are invisible, neither an editor nor a preview will point them out. A bidirectional text detector shows where they are: it reports each directional or hidden character with its position and code point, so you can tell deliberate, correct BiDi markup apart from a stray control that has no business being there. The output is not a judgement — it is a clear map of what is actually inside the text.

Related reading

Directional controls are the raw material for Trojan Source attacks. To see how they are used to hide code, read Trojan Source Scanner and the companion page Trojan Source: When Text Order Lies. For the encoding angle, see Unicode Normalization in Plain English.

Scan Your Text With Our AI Watermark Detector

Frequently Asked Questions

What are BiDi control characters?

BiDi control characters are invisible Unicode code points that tell a renderer which direction a run of text should read. They exist so right-to-left scripts such as Arabic and Hebrew can share a line with left-to-right text.

What do RLO, LRO, and PDF actually do?

RLO (U+202E) forces everything after it into a right-to-left visual order, LRO (U+202D) forces a left-to-right order, and PDF (U+202C) ends the run by restoring the previous direction. The older LRE (U+202A) and RLE (U+202B) start an embedding without overriding character direction.

When is BiDi normal and when is it abuse?

Normal use is inside bilingual text, where the controls make mixed Arabic, Hebrew, and Latin content display correctly. Abuse is a directional control in a place that has no legitimate need, such as an identifier, a string literal, a URL, or a file path, and context is what separates the two.

Why can display order differ from logical order?

Display order can differ because the text is stored in logical order while the renderer rearranges runs for readability. An override or embedding changes only the visual output, so the bytes and the picture on screen can disagree.

How do you detect BiDi characters?

You detect them by scanning the raw text for the specific directional code points and reading each match in context. A normal editor preview hides them, so you need either an editor that renders control characters or a browser-based scanner.

What should you do after a finding?

Compare the visual order against the logical order before deciding anything. If the control is not needed for legitimate bilingual content, remove it; if it is genuinely needed, keep it and leave a note explaining why for the next reviewer.

Related Tools