I'm making and setting AntiXssEncoder
for my web project which uses mvc3 and .net4, and I've done these steps:
- add
AxtiXSSLibrary
reference - create
AntiXSSEncoder
derived fromHttpEncoder
- override the
HtmlEncode
method:output.Write(Encoder.HtmlEncode(value));
- set
encoderType
inweb.config
to<httpRuntime encoderType="AntiXSSEncoder, MyDll"/>
But I still can see the alert popup by xss input (XssTest<script>alert("test");</script>
).
So I tested and found out the default encoder doesn't encode values which are assigned to ViewBag
. My test code is below and it shows one isn't encoded.
//In controller code
[ValidateInput(false)]
public ActionResult AntiXss(string TextArea)
{
ViewBag.DisplayInput = TextArea;
ViewBag.DisplayEncodedInput = Encoder.HtmlEncode(TextArea);
return View();
}
//In view code
@using (Html.BeginForm("AntiXss", "Test"))
{
@Html.TextArea("TextArea")
<p/>
@Html.Raw(ViewBag.DisplayInput)
<p/>
@Html.Raw(ViewBag.DisplayEncodedInput)
//@ViewBag.DisplayInput
<p/>
<input type="submit" value="ok"/>
}
//In redered page code
<textarea id="TextArea" name="TextArea"> XssTest<script>alert("Test");</script></textarea> <p/>
XssTest<script>alert("Test");</script> <p/>
XssTest<script>alert("Test");</script> <p/>
Can any one give me an answer or clue?