4

We have a hybrid webforms/asp.net application which does a lot of partial-page updates from javascript using jquery.

The typical (unsafe) pattern in our application's javascript is to respond to a user request to re-write part of the page with something like this:

$.ajax({
        type: "GET",
        url: urlVariableHere,
        success: function (data) {
            $("#elementIdHere").html(data); 
        },
        error: function (XMLHttpRequest, ajaxOptions, ex) {
            errorHandlerFunction(XMLHttpRequest);  
        }    

"urlVariableHere" points to an MVC Controller method that returns a rendered MVC view. In other words, the Controller method returns a blob of raw HTML.

This pattern is unsafe because of the call to JQuery's html() method, which is vulnerable to a cross-site scripting attack. We now need this application to pass a Veracode static analysis, and this unsafe pattern is repeated several hundred times.

Hooman pointed out in his answer that if we are calling a Controller method which renders a View which does not use the Html.Raw method we are safe from an XSS attack. The problem is, we need to pass a Veracode static scan, and for internal reasons we cannot mark these flaws as "mitigated." For internal reasons the application must pass a static scan with zero mitigations.

What is the best (i.e. most time-economical) way to make this application safe, and still keep the ability to do partial-page updates from javascript? Right now I only see three alternatives, all of them huge efforts:

  1. Change every partial-page postback to a full-page postback.
  2. Change every ajax call to fetch JSON instead of HTML, and then safely create DOM elements from the JSON using safe methods like document.createElement(), element.setAttribute(), element.appendChild() and etc.
  3. Re-write the application to use a javascript framework (Angular, Vue) or library (React).

Am I missing an easier solution?

15
  • I am facing the same problems now with Veracode static scan. Which solution did you choose to implement? Any advice? Commented May 9, 2019 at 13:38
  • @Lion200 afraid I've not decided on one yet. Commented May 9, 2019 at 17:18
  • 1
    One alternative you didn't consider yet is to approach the Veracode support team and explain the fix you have developed. Maybe you will find a solution with them to suppress the false alert. You asked the question 4 years ago, and started a bounty recently - is there still no solution available? Commented Mar 9, 2023 at 7:16
  • 1
    Is there some reason you can't just update your jquery to use vanilla innerHTML? The innerHTML method by default won't evaluate or execute anything in a <script> tag like jquery html() does. Commented Mar 9, 2023 at 20:46
  • 1
    @TomRegan we ended up using the library dompurify. Commented Feb 14, 2024 at 6:26

1 Answer 1

0

As far as I know, XSS is a problem when you are getting some input from the user, it is not clear to me why you should not trust the response from your own controller? What you are doing is very typical and I have seen countless number of tutorials teaching it (like c-sharpcorner, aspsnippets or dotnetthoughts, etc).

Also, ASP.NET MVC View Engine encodes HTML by default. I am not sure how you render your Partial View, but unless you are using @Html.Raw you will potentially double encode the result.

But if you want to encode the HTML result, you can escape your HTML string, see this answer

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

3 Comments

Thanks Hooman, but I'm afraid you missed the point. I trust the response from my own Controller. The issue is that the call to jQuery.html() fails a Veracode static scan, and for internal reasons we cannot mark these flaws as "mitigated." I need to render html, so I cannot use the jQuery text() method.
@TomRegan, I have updated my answer. Would be nice if you could share what sort of error/advice you are getting from Veracode...
every use of html() is flagged as a medium flaw by Veracode. The particulars are not relevant to this question. I'm looking for advice on the easiest way to refactor. I know how the asp.net view engine works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.