Morgan Cheng
Morgan Cheng

Reputation: 76028

Does all browser engines treat "\<" as "<" and "\>" as ">"?

The main point to make "<" to "\<" and ">" to ">" is to make avoid below inline script:

<script>
   var foo = "</script><script>alert('bug');</script><script>"; // the value of foo is generated from server
</script>

The string value of foo is generated from server side. So, we plan to change "<" to "\<" and ">" to ">". (I know there is argument that ">" should be escaped to "& gt;", but it is not in consideration in this case.)

So, the expected result is:

<script>
   var foo = "\</script\>\<script\>alert('bug');\</script\>\<script\>"; // the value of foo is generated from server
</script>

For IE7/8 and Firefox, the HTML rendering engine will not treat \<script\> in javascript string as <script> tag, and JavaScript engine still take it as string "<script>". However, I'm not sure whether all browsers treat ">" and "\<" this way. Is this kind of standard for all browsers?

Upvotes: 1

Views: 139

Answers (1)

Justin Niessner
Justin Niessner

Reputation: 245449

No, your best bet is to use &gt; and &lt; for the greater than and less than signs respectively.

So you'd want something like this:

var foo = "&lt;/script&gt;&lt;script&gt;alert('bug');&lt;/script&gt;&lt;script&gt;";

Upvotes: 5

Related Questions