我昨天发布了一个问题,我打算今天再回来,但是我写了一些 JavaScript 作为针对 XSS 的第一道防线。但是,当在我的实时服务器上测试它时,我发现了一些无效输入,因为 javascript 捕获了 php 部分。我的表单使用 post 而 php 不在我的表单项中(我没有输入)。这可能是拾起表格 Action 之类的吗?我很困惑,任何想法
这是我的代码,它是在提交按钮上触发的。
function validateForBadNess(){
var theShit = new Array("*","^", "$", "(",")","{", "}","[", "]","\\", "|", "'","/","?",",","=",">","gt","lt", "<","script","`","´","php");
var tagName = new Array();
tagName[0] = "input";
tagName[1] = "select";
tagName[2] = "textbox";
tagName[3] = "textarea";
for (ms=0;ms<tagName.length;ms++){
// loop through the elements of the form
var formItems = document.getElementsByTagName(tagName[ms]);
for (var xs=0;xs<formItems.length;xs++){
var thisString = formItems[xs].value;
// loop through bad array
for (zs in theShit){
//alert(thisString + " " + thisString.indexOf(theShit[zs]))
if(thisString.indexOf(theShit[zs]) >= 0){
alert("Sorry but the following character: " + theShit[zs] + " is not permitted. Please omit it from your input.\nIf this is part of your password please contact us to heave your password reset.")
return false;
}
}
// loop for formitems
}
// tagName toop
}
// original condition
}
最佳答案
就防御攻击而言,您所做的是完全不必要和无用的。任何基于 JavaScript 的“保护”都会在几秒钟内被绕过,合法用户将不会高兴无法使用例如 $
符号。始终假设任何传入数据都可能已被篡改。
在服务器端输出数据时需要小心。对任何传入的文本数据使用 htmlspecialchars()
。如果您有传入的 HTML,您需要“清理”,请按照 Sarfraz 的建议使用 HTML 净化器。
相关SO阅读:
关于javascript - 我的表格会被黑客入侵吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2666923/