PHPSandbox is a PHP 5.3.2+ class designed to implement tightly-controlled and highly configurable code sandboxes in pure PHP code. It utilizes the PHPParser and FunctionParser libraries to parse the sandboxed code for violations of the sandbox configuration and to disassemble closures passed to the sandbox.
The PHPSandbox Toolkit is used to demonstrate the use of the PHPSandbox class, its various configuration options, creation of custom templates and for easy experimentation. The JSON templates it generates can also be imported directly into a PHPSandbox instance to allow for easy sharing of common configurations.
The PHPSandbox project is licensed under the open source BSD License. Click the "PHPSandbox License" tab below or see the LICENSE file that came with your distribution for more information.
Click the "Using PHPSandbox" tab below to continue.
If you have downloaded PHPSandbox through composer, then the only thing you need to do to include all required PHPSandbox classes is:
require_once('vendor/autoload.php');
If you have a PSR-0 compliant autoloader, simply put the PHPSandbox folder under /src in your library folder and it will handle loading all PHPSandbox files for you. Otherwise, you must include all PHPSandbox files and its dependencies yourself.
You have two methods of instantiating a PHPSandbox instance: the new keyword, or the static method PHPSandbox::create();
New Keyword$sandbox = new \PHPSandbox\PHPSandbox();Static Method
$sandbox = \PHPSandbox\PHPSandbox::create();
You can pass the following array arguments to either method to setup the sandbox:
$sandbox = new \PHPSandbox\PHPSandbox($options,
$functions,
$variables,
$constants,
$namespaces,
$aliases,
$superglobals,
$magic_constants,
$classes,
$interfaces,
$traits);
$sandbox = \PHPSandbox\PHPSandbox::create($options,
$functions,
$variables,
$constants,
$namespaces,
$aliases,
$superglobals,
$magic_constants,
$classes,
$interfaces,
$traits);
Click the "Configuring PHPSandbox" tab below to continue.
You have four main areas of configuration available in PHPSandbox: Options, Whitelists, Blacklists and Definitions.
Options are configured in the PHPSandbox class by accessing the flag's public instance property (e.g. $sandbox->allow_functions = true; ) or through the set_option() and set_options() instance methods.
Example:$sandbox->set_option('allow_functions', false);
By default, an exception is thrown if any function, variable, global, superglobal, constant, magic constant, namespace, alias (aka use), class, interface, or type is executed in sandboxed code without being either defined or whitelisted, or if it has been blacklisted.
Keywords other than eval, die, exit, include, require, include_once, require_once, and __halt_compiler, are allowed even if they are not whitelisted as long as they are not in the blacklist and a keyword whitelist hasn't been defined.
Operators and primitives are allowed even if they are not whitelisted as long as they are not in the blacklist and a whitelist hasn't been defined.
Whitelists override their blacklist counterpart, therefore if a whitelist of functions has been defined and also a blacklist of functions, the blacklist will have no effect. Furthermore, definitions override both whitelists and blacklists, so that a defined function will always be allowed even if that function is not defined in a function whitelist or is in a function blacklist.
Whitelists are configured in the PHPSandbox class through the whitelist(), whitelist_func(), whitelist_var(), etc. instance methods.
Detailed explanations of the whitelist instance methods can be found in the API documentation.
Blacklists are configured in the PHPSandbox class through the blacklist(), blacklist_func(), blacklist_var(), etc. instance methods.
Detailed explanations of the blacklist instance methods can be found in the API documentation.
Definitions are configured in the PHPSandbox class through the define(), define_func(), define_var(), etc. instance methods. They are fully trusted, and override any respective whitelist or blacklist.
Detailed explanations of the definition instance methods can be found in the API documentation.
Custom validators are configured in the PHPSandbox class through the set_validator(), set_func_validator(), set_var_validator(), etc. instance methods. They are passed the name of the element and the PHPSandbox instance as arguments, and override any respective whitelist or blacklist.
Detailed explanations of the custom validator instance methods can be found in the API documentation.
Click the "Using PHPSandbox Toolkit" tab to continue.
WARNING: The PHPSandbox toolkit is NOT DESIGNED to be used on a publicly accessible server! It is for local testing in a private environment only! Use of The PHPSandbox toolkit on a public-facing server will likely lead to the rooting of your server!
The far right of the toolkit screen is the toolbar. Links to the API documentation and this help dialog can be found here. This is also where sandbox templates, stored in the /toolkit/templates/ folder, can be selected and automatically loaded in the toolkit environment to test, or templates can be imported from your local filesystem.
More importantly, this is where the various sandbox configuration options can be selected, whitelists and blacklists can be enabled, and definitions for functions, superglobals, etc. can be defined. Click the "Configuring PHPSandbox Toolkit" tab below for more information.
Finally, the error level for the code to be executed can be set, and clicking "Run Code In Sandbox" will execute the toolkit environment and output the response.
The top left of the toolkit screen is the code editor, which can be set to four possible modes via the Editor Mode menu above the code editor. These modes are:
When "Run Code In Sandbox" is clicked, any execution output is sent to this box, including error messages, uncaught exceptions and echoed output. Any values returned by the sandbox via the return keyword will also be shown as a var_dump() output in a line underneath the echoed output.
Finally, a line will be shown at the end of the output showing the amount of milliseconds spent preparing and executing the sandbox as well as the total time spent.
Click the "Using Templates" tab below to continue.
In the toolbar on the right side of the screen is a set of tabs that allow you to define all four areas of configuration for your sandbox.
This tab shows a list of checkboxes correlating with the configuration options available in the PHPSandbox class. When a checkbox is checked its corresponding flag will be enabled, when unchecked its corresponding flag will be disabled.
This tab shows a dropdown menu of the type and a text input of the name of the item to add. As you type the name of the item to add an alert tooltip will show if the name you are typing is invalid for the selected type. Click the "+" button next to the text input to add the item to your whitelist or blacklist. You can then click on an item to remove it from the whitelist or blacklist.
NOTE: Whitelists will override their blacklist counterparts! (e.g. if you define a whitelisted function then the function blacklist will have no effect.)
This tab shows a dropdown menu of the definition type. Click the "Define" button next to the dropdown menu to open the selected definition editor.
NOTE: Definitions are fully trusted, and override their whitelist and blacklist counterparts!
Each of the definition editors open in their own window, and will show you a preview of what their result will be in your sandboxed code.
In this editor you will be able to specify the name of your defined function, a checkbox that indicates whether to pass the PHPSandbox instance as the first argument, a text field in which you can describe the arguments of your function, and a code editor in which to write your defined function code. If this code is malformed the execution of your sandbox will fail.
In this editor you will be able to specify the name of your defined variable, the variable scalar value type, and the variable value.
In this editor you will be able to specify the name of your defined superglobal, the superglobal key to set, the superglobal scalar value type, and the superglobal value.
NOTE: You are able to define a callable function as the value of superglobal when configuring the PHPSandbox class from PHP through the define_superglobal() instance method. It will be passed the PHPSandbox instance as the first argument, and is fully trusted.
In this editor you will be able to specify the name of your defined constant, the constant scalar value type, and the constant value.
In this editor you will be able to specify the name of your defined magic constant, the magic constant scalar value type, and the magic constant value.
NOTE: You are able to define a callable function as the value of magic constant when configuring the PHPSandbox class from PHP through the define_magic_const() instance method. It will be passed the PHPSandbox instance as the first argument, and is fully trusted.
In this editor you will be able to specify the name of your defined namespace.
In this editor you will be able to specify the name of your defined namespace to use, and the alias to use. If alias is left blank the namespace will not use an alias.
In this editor you will be able to specify the name of the class to redefine, and the class to redefine it to use.
In this editor you will be able to specify the name of the interface to redefine, and the interface to redefine it to use.
In this editor you will be able to specify the name of the trait to redefine, and the trait to redefine it to use.
Click the "Using PHPSandbox Templates" tab below to continue.
The PHPSandbox toolkit comes with a special template feature that allows for you to open and save templates in a JSON format that the PHPSandbox class can easily import. This makes the sharing of sandboxed configurations incredibly easy as well as making it simple to extend existing templates for your own uses.
To load a template, simply select a template from the dropdown list of available templates in the /toolkit/templates/ folder, or click "Import A Template" to load one from your local filesystem. Once a template is selected the toolkit environment will automatically load all the template options, whitelists, blacklists, definitions, and all setup, trusted and sandboxed code in their respective modes.
If you wish to save your current sandbox configuration, simply click the "Save As New Template" button. You will be prompted for a name for your new template. Once you have named your template, you will be alerted to the success or failure of the operation, and your template will be stored in the /toolkit/templates/ folder. Once your template is saved it automatically becomes available in the dropdown templates menu for you to choose from. If you check the "Download Template?" checkbox you will be prompted to download your template and save it in a location of your choosing instead of saving it to the /toolkit/templates/ folder.
If you wish to import a JSON template into a PHPSandbox instance within PHP, simply call the import() instance method and either pass the JSON string or JSON array to the method. You may also specify a bitmask of import options, by passing it as the second argument to the method.
Example:$sandbox->import(file_get_contents('template.json'));
These class constants are provided to simplify setting the bitmask option to pass to the import() method.
$sandbox->import(file_get_contents('template.json'),
PHPSandbox\PHPSandbox::IMPORT_OPTIONS);
Click the "PHPSandbox License" tab below to continue.
Copyright (c) 2013-2014 by Elijah Horton (fieryprophet [at] yahoo.com)
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.
© Copyright 2013-2014 Elijah Horton — fieryprophet.com