axiles / php-sandbox
一个可以在沙箱环境中运行PHP代码的PHP库。Corveda PHPSandbox的分支。
v2.0.1
2016-05-20 20:23 UTC
Requires
- php: >=5.4
- jeremeamia/functionparser: *
- nikic/php-parser: 2.*
Requires (Dev)
- phpunit/phpunit: 4.8.*
- symfony/yaml: ~2.1
Replaces
README
从 Cordeva PHPSandbox 分支。
一个全功能的PHP 5.4+沙箱类,利用PHP-Parser来防止沙箱中的代码运行不安全代码。
它还利用FunctionParser来反汇编传递给沙箱的可调用对象,这样PHP可调用对象也可以在沙箱中运行,而无需首先将它们转换为字符串。
手册: https://manual.phpsandbox.org
在线API文档: https://docs.phpsandbox.org
特性
- 细粒度的白名单和黑名单,并配置了合理的默认值。
- 包括动态演示系统,允许对自定义沙箱配置进行本地测试
- 可以重新定义内部PHP和其他函数,以提高沙箱使用的安全性。
- 可以重新定义超全局变量和魔术常量,以便将您的自定义值暴露给沙箱代码。
- 可以覆盖get_defined_*和get_declared_*函数,以便只向沙箱代码显示允许的功能、类等。
- 可以选择性地允许和禁止函数创建、类声明、常量定义、关键词等。
- 可以在设置和拆除沙箱时添加和附加可信代码,并自动将它们定义的类、函数、变量等白名单。
- 可以检索生成的沙箱代码以供以后使用。
- 可以通过execute方法直接将参数传递给沙箱代码,以便将选择的外部变量暴露给沙箱。
- 可以访问解析、准备和生成的代码AST,以便进行进一步分析或序列化。
- 可以定义自定义验证函数,以细粒度控制沙箱的每个元素。
- 可以指定自定义错误处理器来拦截PHP错误,并以自定义逻辑处理它们。
- 可以指定自定义异常处理器来拦截抛出的异常,并以自定义逻辑处理它们。
- 可以指定验证错误处理器来拦截抛出的验证错误,并以自定义逻辑处理它们。
- 可以拦截回调并验证它们是否与函数白名单和黑名单相匹配,即使它们作为字符串调用也是如此。
示例用法
function test($string){
return 'Hello ' . $string;
}
$sandbox = new PHPSandbox\PHPSandbox;
$sandbox->whitelistFunc('test');
$result = $sandbox->execute(function(){
return test('world');
});
var_dump($result); //Hello world
自定义验证示例
function custom_func(){
echo 'I am valid!';
}
$sandbox = new PHPSandbox\PHPSandbox;
//this will mark any function valid that begins with "custom_"
$sandbox->setFuncValidator(function($function_name, PHPSandbox\PHPSandbox $sandbox){
return (substr($function_name, 0, 7) == 'custom_'); //return true if function is valid, false otherwise
});
$sandbox->execute(function(){
custom_func();
});
//echoes "I am valid!"
自定义验证错误处理器示例
$sandbox = new PHPSandbox\PHPSandbox;
//this will intercept parser validation errors and quietly exit, otherwise it will throw the validation error
$sandbox->setValidationErrorHandler(function(PHPSandbox\Error $error, PHPSandbox\PHPSandbox $sandbox){
if($error->getCode() == PHPSandbox\Error::PARSER_ERROR){ //PARSER_ERROR == 1
exit;
}
throw $error;
});
$sandbox->execute('<?php i am malformed PHP code; ?>');
//does nothing
禁用验证示例
$sandbox = new PHPSandbox\PHPSandbox;
//this will disable function validation
$sandbox->setOption('validate_functions', false); // or $sandbox->validate_functions = false;
$sandbox->execute('<?php echo system("ping google.com"); ?>');
//Pinging google.com. . .
要求
- PHP 5.4+
- PHP-Parser
- FunctionParser(如果您希望使用闭包)
- PHP应使用--enable-tokenizer选项编译(通常是这样)
安装
要使用composer安装,只需将以下内容添加到项目根目录的composer.json文件中
{
"require": {
"axiles/php-sandbox": "2.*"
}
}
然后运行composer install --dry-run以检查任何潜在问题,然后运行composer install进行安装。
许可证
Copyright (c) 2013-2016 by Corveda, LLC.
Some rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.