PHP strpos() 返回奇怪的结果

标签 php security loops

我已经为我的网络应用编写了一个基本的“安全检查程序”。我需要看一眼用户提交的代码是否包含恶意内容。

这是我现在正在运行的代码的屏幕截图: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,正如你从第一个中看到的那样屏幕截图,只有一个通过此运行。

我做错了什么?

谢谢!

jack

编辑正如 Fosco 友善地指出的那样,alert 在我的数组中列出了两次(doh!)。我已经解决了这个问题,但是留下尾随逗号的问题仍然存在。我知道这是一个较小的问题,但我发誓它仍然不应该存在......

最佳答案

一眼看去,您的代码应该会提供您想要的输出。我不确定出了什么问题。

与其将 $found 构建为字符串,我建议将其构建为数组,然后使用 implode() 获取字符串:

  • $found = ""; 替换为 $found = array();
  • 替换$found .= "$item, ";$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
}

用这个:

if (!count($found))
{
    $output .= "None.<br/>";
}
else
{
    $output .= "<span class=\"alert\">".implode(', ',$found)."</span>"."</p><br/>";  // cuts trailing comma and space from $found
}

关于PHP strpos() 返回奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477228/

相关文章:

php - 渴望加载深层嵌套关系?

javascript - Jquery 更改数组 ID

docker - 当您将服务帐户分配给 Cloud Run 服务时,究竟会发生什么?

c# - 遍历单词列表并搜索文件夹以查找包含该单词的文件

java - 在java中搜索没有循环的arraylist

javascript - 日期差异并使用 JS 插入同一数组

PHP问卷计算设计

PHP - 在函数范围内引用时的封装

python - 如何使用 Python 将文件隐藏在图像中?

java - 在android上解码加密信息