我已经为我的Web应用程序编写了一个基本的“安全检查程序”。我需要一眼就能看到用户提交的代码是否包含恶意内容。
这是我现在运行的代码的截图:http://cl.ly/677a6dc40034f096697f
以下是我对这三段代码使用的PHP代码:
<!-- The View -->
<h2>Security analysis</h2>
<?php echo securitycheck($html, $css, $js); ?>
-
// The controller
function securitycheck($html, $css, $js)
{
// The code is the html, css, and js, appended together. We're scanning it all.
$code = $html." ".$css." ".$js;
// $insecure is our array of naughty things to search for.
$insecure = array(
/* HTML Elements */
'applet',
'basefont',
'base',
'behavior',
'bgsound',
'blink',
'embed',
'expression',
'frameset',
'frame',
'ilayer',
'iframe',
'isindex',
'javascript',
'layer',
'link',
'meta',
'object',
'plaintext',
'style',
'script',
'xml',
'xss',
/* Javascript Elements */
'alert',
'cmd',
'passthru',
'eval',
'exec',
'expression',
'system',
'fopen',
'fromcharcode',
'fsockopen',
'file',
'file_get_contents',
'readfile',
'unlink',
/* Misc Elements */
'vbscript:',
'<?',
'<?php',
'?>'
);
$found = "";
$output = "<p><strong>Potentially insecure items found:</strong> ";
foreach($insecure as $item)
{
if (($pos = strpos($code, $item)) !== FALSE)
{
$found .= "$item, ";
}
}
if ($found == "")
{
$output .= "None.<br/>";
}
else
{
$output .= "<span class=\"alert\">".substr($found, 0, -2)."</span>"."</p><br/>"; // cuts trailing comma and space from $found
}
return $output;
}
最后,这里是返回输出(HTML格式)的截图:http://cl.ly/f246dc419fb499dd6bd7
看到截图了吗?有几个问题。尾随的空格和逗号没有被去掉(我使用了substr()
来做这件事),而且正如你从第一个截图中可以看到的那样,它报告了两个alert
,但实际上只运行了一个。
我做错了什么?
谢谢!
杰克
编辑:正如Fosco友好地指出的那样,我的数组中重复列出了alert
(呃!)。我已经修复了这个问题,但是留下的尾随逗号问题仍然存在。我知道这是一个较小的问题,但我发誓它不应该存在...