AJAX响应返回HTML响应(XSS VeraCode)


function viewAcc() {
    var errorMsg = "";
    var result = true;
    $(".errorView").hide();
    var accNum = document.getElementById('custAccNum').value;
    var accType = document.getElementById('custAccType').value;
    $("#overlayPopup").show();
    $.ajax({
        url : '<attribute:resourceURL/>',
        data : {
            "custNo" : accNum ,
            "custType" : accType 
        },
        success : function(data) {
            if (data == 'CUS_ACC') {
                window.location = "/cust/account/c";
            } else {
                $("#overlayPopup").hide();
                //display warning
                $(".errorView").show();
                $(".errorView").html(data); // <--- XSS line
                e.preventDefault();
            }
        },
        cache : false,
        dataType : 'text',
        error : function(error, textStatus, errorThrown) {
            alert('error in ajax call: ' + textStatus);
            console.log('error in ajax call: ' + textStatus);
            window.location = "/cust/account/c/lookup";
        },
        timeout : ajaxTimeOutMilliSeconds
    });
}

所以VeraCode指出我在$(".errorView").html(data);上有问题我该如何解决?如果我只是将其发布到文字上,它将像HTML一样在客户端上显示吗?

您可以简单地使用.text()而不是.html()。如果您没有来自服务器的任何标记,那么这是一个完全可行的替代方案,因为.text()将阻止被解释为HTML

的内容

//doing sc+ript is only needed here because Stack Snippets otherwise throws an error.
var msg = "This is <b>a message</b> with <script>console.log('some code')</sc"+"ript>";
$("#msgHtml").html(msg);
$("#msgText").text(msg);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Message via .html():</h3>
<div id="msgHtml"></div>
<h3>Message via .text():</h3>
<div id="msgText"></div>

不要盲目信任的工具,这些工具声称您容易受到XSS的影响。

如果data的值不值得信赖,您才有XSS的风险。由于它来自您自己的服务器,因此您应该已经对XSS问题进行了消毒数据,然后再将其发送到AJAX请求的响应中。

最新更新