I ran my web application (built with the Play Framework using Java) through Veracode and it has returned the warning:
CWE-80: Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS)
for my jQuery function .html()
.
I am updating a <table>
element with HTML from a REST call.
Here is the JavaScript:
function getPTPComments() {
var finalURL = restURL + "/restGetCommentsTable";
var keyToSend = $("#ptpKey").val();
var htmlTable = "";
// Clear the comments table...
$("#displayCommentsOnTab").empty();
$.ajax({
type: "POST",
url: finalURL,
data: keyToSend,
dataType: "text",
contentType: "text/plain",
success: function (resp) {
htmlTable = resp;
},
error: function (req, status, err) {
console.log("tabCommentsTab - something went wrong");
console.log("tabCommentsTab - req: " + req);
console.log("tabCommentsTab - status: " + status);
console.log("tabCommentsTab - err: " + err);
}
});
// Replace table html with htmlTable...
$("#displayCommentsOnTab").html(htmlTable); // WARNING HERE
}
I found a few posts on this, but not sure how to change my code to remove this warning:
Avoid Veracode CWE-80: Improper Neutralization of Script-Related HTML in jquery htm() method
https://www.codeproject.com/Questions/1062655/Improper-Neutralization-of-Script-Related-HTML-Tag
If you have any ideas on how to update my code, I look forward to your response.