Escaping at the write site fixes the reader that interprets markup and corrupts the reader that prints text verbatim
Escaping is applied once, at the point a value is written to storage, but decoding is a property of each reader independently. A codebase with two renderers over one field, one that interprets markup and one that prints text verbatim, has exactly one of them correct for any escaping decision. Adding escaping at the write site fixes the interpreting reader and corrupts the verbatim one, which now shows the escape sequences.
Resurfaces when
- I am about to add escaping or sanitising to a value before it is stored
- two screens or services read the same stored text field
- one reader renders markup and another prints plain text
- a field name appears in more places than the helper that encodes it
- user-typed text starts showing entity codes on one screen after a fix on another
- mirroring another client's escaping at the write site
The lesson’s retrieval contract, weighted three times heavier than its body. A lesson that states when it applies doesn’t need a semantic search to find it.
The lesson
Escaping at the write site is a decision about every reader of that field, not only the one that motivated it. Run as a check: text containing < and & stored escaped displayed correctly in a reader that decodes and as a < b & c in a reader that prints verbatim.
The helper appears only where encoding is applied, so searching for it finds the writers. The readers show up only when you search for the field.
The failure that taught it
A release branch added an escaping helper at two write sites that wrapped user-typed text before it became part of a stored log description. The motivation was sound: the receiving screen rendered those descriptions through a markup renderer, so unescaped user text was wrong there. A second screen read the same stored field and had been built deliberately without a markup renderer, with a comment explaining why. It now displayed the escape sequences.
How to apply it
- When you add or change escaping at a write site, list every reader of that field before calling it done. Grep the field name, not the helper.
- Two readers means a decision, not a fix: either store raw and escape on output per reader, or give every reader a decode step.
- Trace each reader's path and look for the absence of a decode step.
- Test with text containing markup characters through each reader.
Carries a runnable check
It names a grep-able signature — the shape this failure takes on sight. Prose doesn’t prevent recurrence; executable checks do. This one runs today, against every edit, as signature-scan.
Where this claim comes from
- Lesson
Escaping at the write site fixes the reader that interprets markup and corrupts the reader that prints text verbatim
- Distillationmechanism stated
Written from the mechanism, not the incident — which is what lets it transfer to code sharing nothing with the original.
- Scar1 occurrence
One recorded failure — weaker evidence, and ranked accordingly rather than presented as settled.
- Evidencenone recorded
No source records recorded — hand-written and migrated lessons predate the pipeline that captures them.
What happened when it was used
- 4
- retrieved
- 2
- acted on
- 0
- held up
- 0
- did not hold up
Applied 2 times. Outcomes move the ranking both ways, which is what makes this improve rather than just grow.