EXERCISE 2 — Correct encoding of "> =============================================================== THE RAW VALUE: "> (a) HTML BODY context ->

HERE

Apply HTML ENTITY encoding (encode < > & ; here " also commonly encoded): " stays " (or ") -- harmless in body > -> > < -> < Encoded output: "><img src=x onerror=alert(1)> Rendered HTML:

"><img src=x onerror=alert(1)>

WHAT THE BROWSER DISPLAYS (as literal text): "> WHY IT DOESN'T EXECUTE: the < and > are now < / > ENTITIES, so the parser does NOT see a start tag. No element is created, so there is no onerror to fire. The string is shown as inert text -- data, not markup. (b) QUOTED HTML ATTRIBUTE context -> Apply ATTRIBUTE encoding (must also encode the quote that delimits the value; encode " and < > too): " -> " > -> > < -> < Encoded output: "><img src=x onerror=alert(1)> Rendered HTML: WHAT THE BROWSER DOES: the " is treated as a literal quote CHARACTER INSIDE the value, NOT as the delimiter that ends the attribute. So the value never closes early; the attacker can't break out of value="..." to add a new tag or an onX handler. The whole thing stays the (harmless) value of the input. WHY IT DOESN'T EXECUTE: the breakout depended on a real " ending the attribute followed by > closing the tag. Encoding " -> " and > -> > removes both, so no breakout, no , no onerror. KEY POINTS: - In BOTH contexts the dangerous characters (", >, <) were converted to entities so the parser reads them as DATA. The difference is that the attribute case CRUCIALLY also encodes the delimiting quote -- that's the extra character the attribute context makes dangerous. - The displayed text is the original string in both cases (the user sees exactly what they typed), but nothing is parsed as a tag or handler. - Note: this works because the value is QUOTED. An unquoted attribute (value=HERE) would still be exploitable via whitespace even with these encodings -- always quote (Chapter 3).