0

我在通过 GET 请求包含其他文件的页面中有此代码:

$page = strtolower($_GET['page']);

if(!$page or !$allow[$page] or $page == 'home') {
    header("Location: home.php");
}

where$allow是一个硬编码数组,其中包含允许的字符串列表,这些字符串是要包含的有效文件。我是否遗漏了一些明显的允许一些代码注入的东西,或者这个检查是否足够好?

4

5 回答 5

3

只要register_globals不允许 $allow 被覆盖,它就不会受到攻击。

不过它会发出通知,而且我个人不会不区分大小写,所以我会这样做:

if (empty($_GET['page']) || empty($allow[$_GET['page']]) || ($_GET['page'] == 'home'))
{
    // Technically a header location should be a complete URL - http://...
    header("Location: home.php");
    exit();
}
于 2009-11-17T12:07:09.087 回答
2

似乎还可以。只需在 header() 之后添加一个 exit() 语句,以确保即使 header() 失败,您的脚本也会终止。

于 2009-11-17T12:07:08.930 回答
0

这已经足够了

 $allow = array('home', 'another_one', 'blah');
 $page = isset($_GET['page']) ? $_GET['page'] : 'home';
 if(in_array($page, $allow, true))
     include "$page.php";

我不确定你为什么在那里使用“标题”

于 2009-11-17T12:07:12.660 回答
0

"good enough" is a very wide brush. Can you expand a little? Also, if you know $_GET['page'] should be restricted to a small set of values (say, integers, or identifiers containing only lowercase letters, et cetera) then it never hurts to validate the data with a regex.

于 2009-11-17T12:07:15.193 回答
0

it might be better to do a regex on the $page firstly to ensure it meets the sanitised criteria of what can be passed in before checking further, if the contents of $page is not conforming, then do not process further...

于 2009-11-17T12:07:19.357 回答