在我之前的动态调用函数中,从变量Sven 传递参数指出我的代码容易受到本地文件包含的攻击。我做了一些修改以防止 LFI。够了还是我应该担心?
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();
欢迎任何意见,最佳实践示例!