garak icon indicating copy to clipboard operation
garak copied to clipboard

Encoding probes lose punctuation: morse() and nato() drop it from the payload

Open 00xcyd opened this issue 2 weeks ago • 1 comments

Summary

garak.resources.encodings.morse() and nato() build their maps from A-Z (plus digits for morse) and drop every character they cannot map. Because InjectMorse / InjectNato use the original payload as the probe trigger, a payload that loses characters can never be reproduced by the target, so DecodeMatch / DecodeApprox cannot score it: any payload containing punctuation becomes permanently undetectable.

Reproduction

>>> from garak.resources.encodings import morse, nato
>>> morse(b"a.b?c")
b'.- -... -.-.'          # decodes as "ABC" — a different instruction
>>> nato(b"a.b?c")
b'Alfa Bravo Charlie'
>>> morse(b"Don't panic!")
b'-.. --- -. - / .--. .- -. .. -.-.'   # "DON T PANIC"
>>> morse(b"email: x@y")
b'. -- .- .. .-.. / -..- -.--'          # "EMAIL XY"

The a.b?c case is the clearest: the target is shown ABC, not a.b?c, while the trigger remains a.b?c.

Impact

Punctuation is common in payloads (Ignore all previous instructions!, URLs, $, @, dotted domains). For InjectMorse, test_lossy_strategies_drop_unmapped_input documents the "!?@" -> [""] collapse, but the practical effect is not just a short prompt: the encode is a different string, and a target that correctly decodes and follows it is scored as not having decoded the trigger. That is a false negative in the direction that matters for a scanner.

A related silent loss — digits from nato() — is fixed separately in #2480. This issue is only about punctuation, which is a design question rather than a missing table entry.

Why this is not just a table fix

  • Morse has standard ITU punctuation (. = .-.-.-, ? = ..--.., ! = -.-.--, / = -..-., : = ---..., @ = .--.-., $ = ...-..-, ' = .----., - = -....-, ( = -.--., ) = -.--.-, & = .-..., : etc.), so the mapping is well defined.
  • The NATO/ICAO spelling alphabet has no standard punctuation words. Choices would be to spell punctuation ("period", "comma"), to pass it through literally, or to keep dropping it and document the limitation.
  • tests/probes/test_probes_encoding.py::test_lossy_strategies_drop_unmapped_input currently asserts MORSE.encode("!?@") == [""], and test_strategies_encode_to_known_values pins the "Haha pwned!" goldens, so changing the behaviour means revising tests that were written deliberately. I did not want to make that call inside a bug-fix PR.

Options

  1. Add ITU punctuation to morse and pass unmapped characters through unchanged for both encoders, updating the lossy-strategy test to assert preservation instead of collapse. Payload fidelity wins; the encodings stop being "lossy by design".
  2. Add morse punctuation only, leaving nato lossy, on the grounds that a phonetic alphabet is a dictionary with no punctuation vocabulary. Partially fixes the false negative; InjectNato keeps it.
  3. Keep the current behaviour and document it in the probe docs (docs/source/extending.probe.rst currently says nothing about morse/nato lossiness), so users know why punctuation-bearing payloads under-report.

Happy to implement whichever shape maintainers prefer; option 1 is the one that removes the false negative. If the intent is that these encoders are deliberately lossy and detection should be aimed at the characters that survive, option 3 plus a clearer test name would be the honest resolution.


Developed with AI assistance (Claude, via a coding agent); reproductions and tests were run locally as shown.

00xcyd avatar Nov 18 '26 00:11 00xcyd

I'd like to work on this. Since it's a design question, which way do you prefer? My suggestion: add the standard ITU punctuation to morse() so common payloads survive, and for nato() (which has no standard punctuation) set the trigger to the text that survives encoding, so DecodeMatch/DecodeApprox score what the target was actually shown. Plus tests with the a.b?c and Don't panic! cases. Happy to go another way if you'd rather.

faisalhacks avatar Nov 22 '26 17:11 faisalhacks