Skip to content

Commit

Permalink
Merge pull request #6784 from weirdan/forbid-exit-and-die
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Oct 31, 2021
2 parents 6fa2da0 + ac098a5 commit 7f14d09
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 199 deletions.
33 changes: 30 additions & 3 deletions src/Psalm/Internal/Analyzer/Statements/Expression/ExitAnalyzer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression;

use PhpParser;
use PhpParser\Node\Expr\Exit_;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
Expand All @@ -10,6 +10,7 @@
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\TaintFlowGraph;
use Psalm\Internal\DataFlow\TaintSink;
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\ImpureFunctionCall;
use Psalm\IssueBuffer;
use Psalm\Storage\FunctionLikeParameter;
Expand All @@ -21,11 +22,37 @@ class ExitAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\Exit_ $stmt,
Exit_ $stmt,
Context $context
) : bool {
$expr_type = null;

$config = $statements_analyzer->getProjectAnalyzer()->getConfig();

$forbidden = null;

if (isset($config->forbidden_functions['exit'])
&& $stmt->getAttribute('kind') === Exit_::KIND_EXIT
) {
$forbidden = 'exit';
} elseif (isset($config->forbidden_functions['die'])
&& $stmt->getAttribute('kind') === Exit_::KIND_DIE
) {
$forbidden = 'die';
}

if ($forbidden) {
if (IssueBuffer::accepts(
new ForbiddenCode(
'You have forbidden the use of ' . $forbidden,
new CodeLocation($statements_analyzer, $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}

if ($stmt->expr) {
$context->inside_call = true;

Expand Down Expand Up @@ -91,7 +118,7 @@ public static function analyze(
&& !$context->collect_initializations
) {
if ($context->mutation_free || $context->external_mutation_free) {
$function_name = $stmt->getAttribute('kind') === PhpParser\Node\Expr\Exit_::KIND_DIE ? 'die' : 'exit';
$function_name = $stmt->getAttribute('kind') === Exit_::KIND_DIE ? 'die' : 'exit';

if (IssueBuffer::accepts(
new ImpureFunctionCall(
Expand Down
196 changes: 0 additions & 196 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,202 +929,6 @@ public static function staticLeave() : void {
$this->analyzeFile($file_path, new Context());
}

public function testAllowedEchoFunction(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm></psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
echo "hello";'
);

$this->analyzeFile($file_path, new Context());
}

public function testForbiddenEchoFunctionViaFunctions(): void
{
$this->expectExceptionMessage('ForbiddenCode');
$this->expectException(\Psalm\Exception\CodeException::class);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm>
<forbiddenFunctions>
<function name="echo" />
</forbiddenFunctions>
</psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
echo "hello";'
);

$this->analyzeFile($file_path, new Context());
}

public function testForbiddenEchoFunctionViaFlag(): void
{
$this->expectExceptionMessage('ForbiddenEcho');
$this->expectException(\Psalm\Exception\CodeException::class);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm forbidEcho="true"></psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
echo "hello";'
);

$this->analyzeFile($file_path, new Context());
}

public function testAllowedPrintFunction(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm></psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
print "hello";'
);

$this->analyzeFile($file_path, new Context());
}

public function testForbiddenPrintFunction(): void
{
$this->expectExceptionMessage('ForbiddenCode');
$this->expectException(\Psalm\Exception\CodeException::class);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm>
<forbiddenFunctions>
<function name="print" />
</forbiddenFunctions>
</psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
print "hello";'
);

$this->analyzeFile($file_path, new Context());
}

public function testAllowedVarExportFunction(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm></psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
$a = [1, 2, 3];
var_export($a);'
);

$this->analyzeFile($file_path, new Context());
}

public function testForbiddenVarExportFunction(): void
{
$this->expectExceptionMessage('ForbiddenCode');
$this->expectException(\Psalm\Exception\CodeException::class);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm>
<forbiddenFunctions>
<function name="var_export" />
</forbiddenFunctions>
</psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
$a = [1, 2, 3];
var_export($a);'
);

$this->analyzeFile($file_path, new Context());
}

public function testForbiddenEmptyFunction(): void
{
$this->expectExceptionMessage('ForbiddenCode');
$this->expectException(\Psalm\Exception\CodeException::class);
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
'<?xml version="1.0"?>
<psalm>
<forbiddenFunctions>
<function name="empty" />
</forbiddenFunctions>
</psalm>'
)
);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
empty(false);'
);

$this->analyzeFile($file_path, new Context());
}

public function testValidThrowInvalidCatch(): void
{
$this->expectExceptionMessage('InvalidCatch');
Expand Down

0 comments on commit 7f14d09

Please sign in to comment.