在我之前的文章call function dynamically, passing arguments from variable Sven指出我的代碼易受本地文件包含的影響。我做了一些修改以防止LFI。這是足夠的還是應該擔心?Ajax處理 - 安全問題
if ($_SERVER['HTTP_X_REQUESTED_WITH'] !== "XMLHttpRequest")
{
echo "Error";
exit();
}
$req = explode("_",$_POST['req']);
/*
User input should always be escaped
using preg_quote before being used in a regexp pattern.
Thanks bwoebi
*/
$className = preg_quote($req[0]) . "Controller" ;
$methodName = $req[1];
$args= isset($_POST["data"]) ? $_POST['data'] : array();
$file = "application/controllers/" . $className . ".php" ;
if (!file_exists($file) || preg_match("/^[a-z]$/", strtolower($className)))
exit();
require_once $file;
$controller = new $className;
$result = call_user_func_array(array($controller, $methodName),$args);
echo json_encode($result);
另一個問題可能是用戶可以從該文件夾調用任何控制器文件的公用方法。 但據我所知,更多的框架正在使用它們的路由domain.xy/controller/method/par模式,它具有相同的風險。 (儘管在我的控制器中,我儘可能多地使用服務器端驗證)
我正在考慮將一些身份驗證放入ajax處理程序/路由器文件中。
// PSEUDO CODE
$user = new User();
// maybe bad practice to store the id session after authentication. Any comment on this?
$userGroup =$user->getUserGroupById($_SESSION["user"]);
$security = new Security();
$whiteList = $security->getWhiteList($userGroup);
//$whiteList is an array with the list of controllers the user may access
if (!in_array(className, $whiteList))
exit();
歡迎任何評論,最佳實踐例子!