18

Sometimes, server side will generate strings to be embedded in inline JavaScript code. For example, if "UserName" should be generated by ASP.NET. Then it looks like.

<script>
   var username = "<%UserName%>";
</script>

This is not safe, because a user can have his/her name to be

</script><script>alert('bug')</script></script>

It is XSS vulnerability.

So, basically, the code should be:

<script>
   var username = "<% JavascriptEncode(UserName)%>";
</script>

What JavascriptEncode does is to add charater "\" before "/" and "'" and """. So, the output html is like. var username = "</script>alert(\'bug\')</script></script>";

Browser will not interpret "</script>" as end of script block. So, XSS in avoided.

However, there are still "<" and ">" there. It is suggested to escape these two characters as well. First of all, I don't believe it is a good idea to change "<" to "&lt;" and ">" to "&gt;" here. And, I'm not sure changing "<" to "\<" and ">" to "\>" is recognizable to all browsers. It seems it is not necessary to do further encoding for "<" and ">".

Is there any suggestion on this?

Thanks.

3 Answers 3

16

The problem has different answers depending on what markup language you are using.

If you are using HTML, then you must not represent them with entities as script elements are marked as containing CDATA.

If you are using XHTML, then you may represent them as CDATA with explicit CDATA markers, or you may represent them with entities.

If you are using XHTML, but serving it as text/html, then you need to write something which conforms to the rules of XHTML but still works with a text/html parser. This generally means using explicit CDATA markers and commenting them out in JavaScript.

<script type="text/javascript">
// <![CDATA[
  …
// ]]>
</script>

A while ago, I wrote a bit about the hows and whys of this.

Sign up to request clarification or add additional context in comments.

3 Comments

But still the > in ]]> inside the CDATA block must be repalced by &gt;. So foo[bar[0]]>1234 must be replaced with foo[bar[0]]&lt;1234 or foo[bar[0]] > 1234. Otherwise the CDATA block would be closed prematurely.
Since CDATA renders & characters as meaning "&" and not "Start of entity" - that wouldn't work. If you need to represent the string "]]>" inside CDATA then I'm pretty sure you are suck and should be using entities to start with (outside a CDATA block)
Or just add a space: foo[bar[0]] > 1234 - or if its a part of a string: 'foo[bar[0]]'+'>1234' - or just contain all your scripts in external .js files.
14

No, you should not escape < and > using HTML entities inside <script> in HTML.

  • Use JavaScript string escaping rules (replace \ with \\ and " with \")
  • and replace all occurances of </ with <\/, to prevent escaping out of the <script> element.

In XHTML it's more complicated.

  • If you send XHTML as XML (the way that's incompatible with IE) and don't use CDATA block, then you need to escape entities, in addition to JavaScript string escaping.
  • If you send XHTML as XML and use CDATA block, then don't escape entities, but replace ]]> with ]]]]><![CDATA[> to prevent escaping out of it (in addition to JavaScript string escaping).
  • If you send XHTML as text/html (what 99% of people does) then you have to use XML CDATA block, XML CDATA escaping and HTML escaping all at once.

1 Comment

+1. Minor nitpick: not all occurrences of </ need to be escaped (although that’s probably the easiest solution). Only the full </script string followed by any space character, >, or / will end the respective opening tag.
2

The cheap and easy way:

<script type="text/javascript">
    var username = "<%= Encode(UserName) %>";
</script>

where the encoding scheme in Encode is to translate each character of input into the associated \xABCD representation compatible with JavaScript.

Another cheap and easy way:

<script type="text/javascript">
    var username = decodeBase64("<%= EncodeBase64(UserName) %>");
</script>

if you are dealing only with ASCII.

Of course, pst hit the nail on the head with the strict way of doing it.

1 Comment

+1 Also, this solution makes your output source code look h4x0r!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.