Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - XSS - HTML encoding - Character entity reference vs. Numeric entity reference

We've been looking for ways to HTML encode our JSP pages to counter XSS.

The OWASP site shows How_to_perform_HTML_entity_encoding_in_Java

The article talks about entity encoding the "Big 5" i.e.

  21          {"#39", new Integer(39)}, // ' - apostrophe
  22          {"quot", new Integer(34)}, // " - double-quote
  23          {"amp", new Integer(38)}, // & - ampersand
  24          {"lt", new Integer(60)}, // < - less-than
  25          {"gt", new Integer(62)}, // > - greater-than

i.e.

<script>

is encoded as

  &lt;script&gt;

but the Java code sample included in the article uses numeric reference encoding i.e.

<script></script>

is encoded as

 &#60;script&#62;&#60;&#47;script&#62;

Is there a reason for using character references over entity references? Which is best and why?

like image 994
rbrayb Avatar asked Feb 20 '26 04:02

rbrayb


1 Answers

They're the same as far as protecting yourself from XSS is concerned. The only real practical differences are readability and size.

like image 89
Laurence Gonsalves Avatar answered Feb 22 '26 18:02

Laurence Gonsalves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!