Corveda / PHPSandbox

A PHP-based sandboxing library with a full suite of configuration and validation options.
https://phpsandbox.org
Other
220 stars 46 forks source link

Not working again #35

Closed Jobians closed 10 months ago

Jobians commented 10 months ago

Please I don't know why this not working

<?php

require_once 'vendor/autoload.php';

use PHPSandbox\PHPSandbox;

function test($string){
    return 'Hello ' . $string;
}

class Server {
    public function sendMessage($message) {
        echo "Sending message: " . $message;
    }
}
$Server = new Server();

$sandbox = new PHPSandbox();
$sandbox->whitelistClass("Server");
$sandbox->whitelistVar("Server");
$sandbox->defineVars(['Server']);

$result = $sandbox->execute('<?php 
echo $Server->sendMessage("ping google.com");
?>');

var_dump($result);

Fatal error: Uncaught PHPSandbox\Error: Cannot define unnamed variable! in /data/data/com.termux/files/home/php/bota/vendor/corveda/php-sandbox/src/PHPSandbox.php:7195 Stack trace:

0 /data/data/com.termux/files/home/php/bota/vendor/corveda/php-sandbox/src/PHPSandbox.php(2774): PHPSandbox\PHPSandbox->validationError('Cannot define u...', 203, NULL, '')

1 /data/data/com.termux/files/home/php/bota/vendor/corveda/php-sandbox/src/PHPSandbox.php(2792): PHPSandbox\PHPSandbox->defineVar(0, 'Server')

2 /data/data/com.termux/files/home/php/bota/test.php(21): PHPSandbox\PHPSandbox->defineVars(Array)

3 {main}

thrown in /data/data/com.termux/files/home/php/bota/vendor/corveda/php-sandbox/src/PHPSandbox.php on line 7195

Jobians commented 10 months ago

Fixed

Changed

$sandbox->defineVars(['Server']);

To

$sandbox->defineVars(['Server' => $Server]);