I've been reading a blog post and I saw something which says JS Encoding Vulnerability. In the sample code below, if the user enters something like this :
\x3cscript\x3e%20alert(\x27pwnd\x27)%20\x3c/script\x3e, it says JS will render it as HTML which looks odd to me. I tried it and he was right.
@{
ViewBag.Title = "Home Page";
}
<h2 id="welcome-message">Welcome to our website</h2>
@if(!string.IsNullOrWhiteSpace(ViewBag.UserName)) {
<script type="text/javascript">
$(function () {
//ViewBag.Username value comes from Controller.
var message = 'Welcome, @ViewBag.UserName!';
$("#welcome-message").html(message).hide().show('slow');
});
</script>
}
My question is, why Javascript decodes already encoded string automatically? Or it does have something with jQuery's html() function which does that? The OP says use Ajax.EncodeJavascriptString() method in order to solve this problem. But why will I need to encode already encoded string? I checked jQuery's website and it doesn't mention anything like that for html() method.
If you like to see the whole blog post, please visit this address http://weblogs.asp.net/jgalloway/archive/2011/04/28/preventing-javascript-encoding-xss-attacks-in-asp-net-mvc.aspx
.html()to inject plain text.@ViewBag.UserNamewith the plain, unescaped string?