php - 根据用户输入安全地调用函数

标签 php security

我正在尝试创建一个 AJAX 脚本,它将采用两个 GET 变量、类和方法,并将它们映射到我们设计的方法(类似于 CodeIgniter 对 ajax 的作用,我很确定)。由于我依靠用户输入来确定要执行的类和方法,因此我担心黑客可能会通过某种方式利用该技术为他们谋取利益。

代码:

//Grab and clean (just in case, why not) the class and method variables from GET
$class = urlencode(trim($_GET['c']));
$method = urlencode(trim($_GET['m']));

//Ensure the passed function is callable
if(method_exists($class, $method)){
    $class::$method();
}

在使用此技术时,我应该注意哪些缺点或安全注意事项?

最佳答案

检查是否允许用户调用方法:

// methods that user can call:
$user_methods = array("method1", "method2", "method3", );

//Ensure the passed function is callable
if(method_exists($class, $method) and in_array($method, $user_methods){
    $class::$method();
}

否则您将无法控制用户可以做什么。

关于php - 根据用户输入安全地调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414372/

相关文章:

php - 如何使用 ./configure 设置变量

php - 使用 hmac 在 Elixir 和 PHP 中生成签名

php - 创建搜索功能

php - 在 php 上显示 utf8_general_ci 数据(非英语)

security - 我可以禁止随服务器请求自动发送 cookie 吗?

javascript - 通过脚本更改浏览器设置

phpMyAdmin: "URI too large"

python - 安全删除内存中的密码(Python)

security - 为什么使用具有 CBC 模式的非随机 IV 会存在漏洞?

security - AES256 CBC + HMAC SHA256 确保 secret 性*和*身份验证?