URL Injection 的问题

由于我不喜欢各个 framework 的 URL Rewrite 方案(单点入口)、利用 PHP 自行编写针对 $_SERVER['REQUEST_URI'] 的解析,所以目前我做的项目中,采用的是硬写 Rewrite Rule 的方案,例如这一条 Nginx 中的 rewrite rule:

rewrite /topic/1$ /topic.php?action=view&model=topic&id=1 last;

但问题在于,我们可以通过 /topic/1?action=delete 这般的方式来改变参数。所以对 URL Injecttion 的清理是必须的,我采用的方法是重新写一遍 $_GET 的内容:

function rebuild_url_args ()
{
    $rs = array();
    foreach (explode('&', $_SERVER['QUERY_STRING']) as $str)
    {
        $str = explode('=', $str, 2);
        if (!isset($rs[$str[0]]))
        {
            $rs[$str[0]] = isset($str[1]) ? $str[1] : '';
        }
    }
    $_GET = $rs;
}

这样问题便解决了。

Comments (0)

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter



 

About