PHP API 结构安全性增强 - 我能做什么?



我正在用PHP为我的应用程序重写我的API,并且正在使用请求结构

http://example.com/controller/function

我的 API 将request_uri分解为:

array(
0 => "controller",
1 => "function"
);

并通过以下代码执行请求的函数。

//Build the file name
$fileName = ('../controllers/' . $endpoint[0] . '.php');
//Look for the file that is being requested
if(!file_exists($fileName)){
echo json_encode( StatusCodes["no_endpoint"] );
die();
} else {
include_once($fileName);
}
//Does the function exist?
if(!function_exists($endpoint[1])){
echo json_encode( StatusCodes["no_function"] );
die();
}
//Import the API settings to get all the keys necessary
include_once('../includes/ApiSettings.php');
//Include the common functions. Done here so the user can't bypass function_exists checks
include_once('../includes/CommonFunctions.php');
//Finally, execute the requested function
echo json_encode($endpoint[1]());

正如我的一位同事向我指出的那样,通过echo json_encode($endpoint[1]());执行可能会导致人们离开 Webroot 目录,或者回显系统信息,例如,如果他们到达端点http://example.com/controller/phpinfo。这是一个主要的安全问题。

我应该使用什么方法,或者如何防止人们进行远程代码执行并导致各种问题。

创建一个可以调用的函数数组,并将其用作白名单。检查它,而不是检查存在的每个函数。

最新更新