BiDi controls: stored order versus displayed order

Bidirectional text combines scripts written left-to-right and right-to-left. The Unicode Bidirectional Algorithm computes a display order without rearranging stored code points. Controls can clarify direction, so their presence alone does not prove an attack. Risk appears when rendering hides the actual sequence in an identifier, filename or source-code review.
Controls to recognise
LRM U+200E and RLM U+200F are invisible direction marks. U+202A–U+202E cover embedding and override controls; U+2066–U+2069 cover isolates and their terminator. ALM U+061C serves Arabic context. Searching only U+202A–U+202E misses legitimate and potentially surprising controls in the other groups.
A reproducible example
A stored sequence containing abc, RLO U+202E, DEF and PDF U+202C has eight code points. Part of it can render in reverse while the array remains unchanged. A code-point list or escaped representation is more reliable than eyesight. Do not infer a file extension, command or domain from rendered order alone.
When directional help is legitimate
Mixed Arabic–Latin or Hebrew–Latin prose may need marks to place punctuation and numbers predictably. On the web, semantic markup such as dir and bdi is often easier to audit. HTML cannot be placed into an email subject, filename or plain-text field, however, so each format needs its own supported mechanism.
When to intervene
An ASCII-only identifier should reject unexpected direction controls according to its validator. For source code, enable editor warnings, inspect diffs with controls visible and run tests. Bulk deletion inside string literals can alter user text, translations or fixtures. It is not a substitute for review of program behaviour.
A safe review sequence
- Keep an unchanged copy and isolate the smallest sample.
- List code points in stored order.
- Match each control to its terminator and surrounding script.
- Consult the field or markup contract.
- Change a copy and test it in both LTR and RTL containers.
Afterward, inspect brackets, punctuation, phone numbers and mixed-script references. If the user’s language is RTL, deleting every control may reduce readability.
JavaScript example
const sample = "abc\u202EDEF\u202C";
[...sample].length; // 8
/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/u.test(sample); // trueThe expression detects common controls but cannot decide whether each one is allowed. Use language tooling and code review for software; use browser testing and an informed reader for natural-language content.
Make review interfaces explicit
For sensitive values, show rendered text, an escaped representation and stored code points together. Add cases using LRI, RLI and PDI as well as older RLO/PDF controls. A validator should name the control and position instead of silently removing it. That lets a reviewer distinguish correct mixed-direction content from a sequence that violates an identifier policy.
Sources and specifications
- Unicode Standard Annex #9: Bidirectional Algorithm
- W3C: Unicode controls for bidi text
- MDN: the bdi element