Menu

HTML Entity Escaping: 6 Mistakes That Cause Bugs and XSS
📰

HTML Entity Escaping: 6 Mistakes That Cause Bugs and XSS

DEV Community: html·Sanjaya Sahu·9 days ago
#XSOW4BYT
#devto2#devto#dev#escapes#entities
Reading 0:00
15s threshold

Pasting text into HTML looks trivial until a stray < or & breaks your page or opens an XSS hole. Here are the entity and escaping mistakes I see most often.

1. Escaping in the wrong context

HTML text, HTML attributes, URLs, JavaScript strings and CSS each need different escaping. Escaping for HTML body does not make a value safe inside onclick="..." or a <script> block.

2. Double escaping

If your template engine already escapes, escaping again gives you &amp;amp; on screen. Escape once, at output time, not when storing data.

3. Forgetting quotes in attributes

& , < and > are not enough inside attributes. You must also escape " (and ' if you use single quotes), or a value like " onmouseover="alert(1) escapes the attribute.

4. Named vs numeric entities

&copy; , &#169; and &#xA9; render the same. Named entities are readable; numeric ones are universal. Always include the trailing semicolon.

5.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.