r/programming • u/New_Pay3669 • 16m ago
A 2-byte BOM can beat your charset=utf-8 header and bypass your HTML sanitizer
https://agentack.appI host HTML written by AI agents, so I scan uploads and block things like <meta http-equiv="refresh">. The scanner reads UTF-8, and the server sends charset=utf-8.
The catch: in browsers, a byte-order mark beats the HTTP header. A file starting with FF FE is decoded as UTF-16, so my scanner sees junk while the browser sees a working redirect. They're reading two different documents.
Fix: refuse any UTF-16/32 BOM, require valid UTF-8, and add a strict CSP sandbox as a safety net.
Lesson: a sanitizer is only safe if it decodes input exactly the way the browser does.
Anyone hit other cases where the browser and server-side checks disagree?
5
u/viva1831 8m ago
Tbh a sataniser should block apparent garbage too. You can't satanise trash!