4

In light of posts such as these:

JSON unparseable cruft: Why so serious?

Why do people put code like "throw 1; <dont be evil>" and "for(;;);" in front of json responses?

Why does Google prepend while(1); to their JSON responses?

I would like to follow the advice laid out in the following answer: How should web app developers defend against JSON hijacking?

Is there an easy way to add an unparsable cruft to JSON responses built using System.Web.Mvc.JsonResult? The security.se post suggests that I use </* at the beginning of the response.

Community
  • 1
  • 1
SilverlightFox
  • 28,804
  • 10
  • 63
  • 132

1 Answers1

3

You could write a custom action result to perform this:

public class SafeJsonResult: JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.Write("</*");
        base.ExecuteResult(context);
    }
}

and then use it instead of the default one:

public ActionResult Index()
{
    return new SafeJsonResult
    {
        Data = new { Foo = "bar" },
        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
    };
}
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876