-
Notifications
You must be signed in to change notification settings - Fork 47
Type checks in use statements and parameter type hints #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| else | ||
| $this->sandbox->checkType($class); | ||
| return $node; | ||
| } else if($node instanceof Node\Expr\New_){ | ||
| if(!$this->sandbox->allow_objects){ | ||
|
|
@@ -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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CS :s There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, what does "CS :s" mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sur, but use statement and Error::class ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean: And line 448: |
||
| $this->sandbox->allow_functions = true; | ||
| $this->sandbox->execute(function() { | ||
| function testTypeInParam(TestClass $param) {}; | ||
| }); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CS :s