Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ValidatorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public function leaveNode(Node $node){
if($this->sandbox->isDefinedClass($class)){
$node->type = new Node\Name($this->sandbox->getDefinedClass($class));
}
if ($this->sandbox->isWhitelistedInterface($class))
$this->sandbox->checkInterface($class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS :s

else
$this->sandbox->checkType($class);
return $node;
} else if($node instanceof Node\Expr\New_){
if(!$this->sandbox->allow_objects){
Expand Down Expand Up @@ -358,6 +362,10 @@ public function leaveNode(Node $node){
} else {
$this->sandbox->validationError("Sandboxed code attempted use invalid namespace or alias!", Error::DEFINE_ALIAS_ERROR, $node);
}
if ($this->sandbox->isWhitelistedInterface($use->alias))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS :s

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what does "CS :s" mean?
Also, I think I still have some flawed logic in this, I will work on this again and update the PR later.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Coding style": if ($this->sandbox->isWhitelistedInterface($use->alias)) { ;)

$this->sandbox->checkInterface($use->alias);
else
$this->sandbox->checkType($use->alias);
}
return false;
} else if($node instanceof Node\Expr\ShellExec){
Expand Down
20 changes: 20 additions & 0 deletions tests/DefaultConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,24 @@ public function testWhitelistMagicConstants(){
$this->sandbox->whitelistMagicConst('DIR');
$this->assertEquals(str_replace('tests', 'src', __DIR__), $this->sandbox->execute(function(){ return __DIR__; }));
}

/**
* Test whether sandbox disallows non-whitelisted classes in use statements
*/
public function testDisallowsTypeInUse(){
$this->expectException('PHPSandbox\Error');
$this->sandbox->allow_aliases = true;
$this->sandbox->execute('use TestClass;');
}

/**
* Test whether sandbox disallows non-whitelisted classes in parameter type hints
*/
public function testDisallowsTypeInParam(){
$this->expectException('PHPSandbox\Error');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sur, but use statement and Error::class ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test I'm expecting the use statement to fail even though use statements are allowed because the class I'm using is not whitelisted.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean:
in top of file:
use PHPSandbox\Error;

And line 448:
$this->expectException(Error::class);

$this->sandbox->allow_functions = true;
$this->sandbox->execute(function() {
function testTypeInParam(TestClass $param) {};
});
}
}