r/programming • • 16m ago

A 2-byte BOM can beat your charset=utf-8 header and bypass your HTML sanitizer

https://agentack.app

I 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?

0 Upvotes

2 comments sorted by

5

u/viva1831 8m ago

Tbh a sataniser should block apparent garbage too. You can't satanise trash!

0

u/New_Pay3669 1m ago

Yeah, agreed. Now anything that isn't valid UTF-8 just gets rejected. The sneaky part is that it only looked like garbage to my scanner, not to the browser.