Skip to content

Instantly share code, notes, and snippets.

@raeq
Last active September 3, 2026 11:09
Show Gist options
  • Select an option

  • Save raeq/94fcb04d6fbbb0254d56b5c04ffd426c to your computer and use it in GitHub Desktop.

Select an option

Save raeq/94fcb04d6fbbb0254d56b5c04ffd426c to your computer and use it in GitHub Desktop.
disarm: #934 shipped detection of the deletion class; no surface applies the erase, so 0 of 57 surface/form pairs recover and the smuggled character reaches the consumer
#!/usr/bin/env python3
"""#739's detection half shipped in #934; the recovery half did not (revised 2026-09-03).
Boucher et al.'s deletion class hides a character from a human reader by
following it with an erasing control. disarm now *detects* every form of it and
*names* the semantics — "erases the preceding 'a'" — and no surface applies
them, so the smuggled character reaches whatever reads the output.
pip install disarm==0.15.0
python deletion_recovery.py
"""
from __future__ import annotations
import disarm
BS, DEL, CR = "\x08", "\x7f", "\r"
def renders_as_paypal(control: str) -> str:
"""The paper's §VI-A construction: one ASCII character, then the erase."""
if control == CR:
return "ZZZZZZ" + CR + "paypal"
return "".join(ch + "X" + control for ch in "paypal")
def apply_erase(text: str) -> str:
"""What a terminal does. Nine lines, no renderer, no table."""
out: list[str] = []
for ch in text:
if ch in (BS, DEL):
if out:
out.pop()
elif ch == CR:
out.clear()
else:
out.append(ch)
return "".join(out)
def surfaces() -> dict[str, object]:
out: dict[str, object] = {}
for name in sorted(getattr(disarm, "PRESETS", ())):
fn = getattr(disarm, name, None)
if callable(fn):
out[name] = fn
for profile in disarm.list_profiles():
out[f"profile:{profile}"] = disarm.get_pipeline(profile)
return out
def main() -> int:
print(f"disarm {disarm.__version__}\n")
print("Detection — what #934 shipped:")
for label, control in (("BS", BS), ("DEL", DEL), ("CR", CR)):
probe = renders_as_paypal(control)
report = disarm.inspect_anomalies(probe)
print(
f" {label:<4} anomalous={report.anomalous!s:<6} kinds={report.kinds}\n"
f" reason: {report.reason}"
)
print("\nRecovery — what did not:")
print(f" {'surface':<32}{'BS':<10}{'DEL':<10}{'CR':<10}")
print(" " + "-" * 60)
recovered = 0
for name, fn in sorted(surfaces().items()):
cells = []
for control in (BS, DEL, CR):
out = fn(renders_as_paypal(control))
ok = out == fn("paypal")
recovered += ok
cells.append("OK" if ok else "no")
print(f" {name:<32}{cells[0]:<10}{cells[1]:<10}{cells[2]:<10}")
print(f"\n surfaces recovering any form: {recovered} of {len(surfaces()) * 3}")
print("\nWhat the output actually carries:")
probe = renders_as_paypal(BS)
print(f" a human sees {apply_erase(probe)!r}")
print(f" canonicalize give {disarm.canonicalize(probe)!r}")
print(f" applying the erase{apply_erase(probe)!r} <- nine lines above")
print("\nHow this relates to the bidi case (revised 2026-09-03; see the issue's Revision):")
print(" Both classes hide the code points behind a rendering, and for both the")
print(" rendering is the clean string. The difference is cost: display order")
print(" needs UAX #9 and destroys real RTL text (#740); BS/DEL have no")
print(" legitimate use in text, and CR has CRLF, which is why CR stays flagged.")
print(" apply_erase above clears the line on CR, which is wrong for CRLF; the")
print(" cell-aware cursor model in bad_characters_by_class.py is the reference.")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment