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

时间:2012-07-10 13:28:24

标签: 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();
}

使用这种技术时,我应该注意哪些缺点或安全警告?

5 个答案:

答案 0 :(得分:14)

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

// 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();
}

否则你将无法控制用户将能够做什么。

答案 1 :(得分:6)

<?php
class AjaxCallableFunction
{
    public static $callable_from_ajax = TRUE;
}

$class = $_POST['class'];
$method = $_POST['method'];

if ( class_exists( $class ) && isset( $class::$callable_from_ajax ) && $class::$callable_from_ajax ) {
    call_user_func( $class, $method );
}

结合其他一些答案以获得最佳效果。需要PHP 5.3.0或更高版本。你甚至可以实现一个接口

<?php
interface AjaxCallable {}

class MyClass implements AjaxCallable 
{
    // Your code here
}

$class = $_POST['class'];
$method = $_POST['method'];

if ( class_exists( $class ) && in_array( 'AjaxCallable', class_implements( $class ) ) ) {
    call_user_func( $class, $method );
}

这种方法遵循OOP原则,非常冗长(易于维护),并且不要求您维护一个可以调用哪些类的数组,哪些不能调用。

答案 2 :(得分:4)

考虑到你没有传递任何论据,现在这是相对安全的。但我会在你的IF中添加一个有效类列表,如:

//Ensure the passed function is callable
if(method_exists($class, $method)){
    if(in_array($class, array('controller1', 'controller2'))){
        $class::$method();
    }
}

这样一来,黑客就不能用这种方式在框架中调用任何可能的类,而只能调用你允许的类。

答案 3 :(得分:2)

在这种情况下,你必须处理Reflection

这是你需要的例子。

<?php
class Apple {
    public function firstMethod() { }
    final protected function secondMethod() { }
    private static function thirdMethod() { }
}

$class = new ReflectionClass('Apple');
$methods = $class->getMethods();
var_dump($methods);
?>

使用ReflectionMethods:invoke执行方法可能是这样的:

  <?php
class HelloWorld {

    public function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod->invoke(new HelloWorld(), 'Mike');
?>

最后我们可以:

 $class = urlencode(trim($_GET['c']));
  $method = urlencode(trim($_GET['m']));

  $allowed_methods = array("insert", "update", "delete");

  if(method_exists($class, $method) and in_array($method, $allowed_methods){
    $reflectionMethod = new ReflectionMethod($class, $method);
    $reflectionMethod->invoke(new $class, 'First Argument');
   }

答案 4 :(得分:1)

urlencode()让我有点担心。虽然它可能是安全的,但我会更严格地消毒。我只允许字母,数字和下划线。你不应该真的需要任何其他字符的类或方法名称。我不认为我见过任何人。

我在所有项目中都使用了很多的东西:

function very_safe_string( $string )
{
    return preg_replace("/[^A-Za-z0-9_]/" , '' , $string);
}

正如其他海报所提到的那样,你应该明确地允许某种类型的白名单(至少对于类来说,因为我确定不是每个类都需要从ajax访问)。以及检查class_exists()和method_exists()。

如果任何这些检查失败,我还建议使用某种类型的电子邮件警报系统。我确定你想知道是否有人试图hax0r j00。

相关问题