【发布时间】:2011-02-09 15:35:15
【问题描述】:
我昨天发布了一个问题,我打算回到今天,但是我编写了一些 JavaScript 作为预防 XSS 的第一道防线。但是,当在我的实时服务器上进行测试时,我会捕获一些无效输入,因为 javascript 会捕获 php 部分。我的表单使用 post 并且 php 不在我的表单项中(我没有输入它)。这可能是拿起表单动作还是什么?我很困惑,有什么想法
这是我的代码,它是在提交按钮上触发的。
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
}
【问题讨论】:
-
其他人已经为您指出了正确的方向,但我只想提一下,如果您打算这样做,那么您这样做的方式非常混乱且不必要。你的大部分代码都可以用一个相对简单的正则表达式代替,不需要复杂的数组设置等。
-
"抱歉,不允许使用以下字符:" + theShit[zs] + "。请尝试使用另一个字符进行破解,如果您想收到一些建议,请大声说 'KEVIN MITNICK'。" xD
标签: javascript php