Skip to content

Commit

Permalink
Rename TaintGraph to ControlFlowGraph because it’s about to do more
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Sep 21, 2020
1 parent 6334c50 commit 56cddd1
Show file tree
Hide file tree
Showing 44 changed files with 269 additions and 274 deletions.
16 changes: 8 additions & 8 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ class Codebase
public $populator;

/**
* @var ?Internal\Codebase\TaintGraph
* @var ?Internal\Codebase\ControlFlowGraph
*/
public $taint_graph = null;
public $control_flow_graph = null;

/**
* @var bool
Expand Down Expand Up @@ -1598,19 +1598,19 @@ public function addTaintSource(
array $taints = \Psalm\Type\TaintKindGroup::ALL_INPUT,
?CodeLocation $code_location = null
) : void {
if (!$this->taint_graph) {
if (!$this->control_flow_graph) {
return;
}

$source = new \Psalm\Internal\Taint\Source(
$source = new \Psalm\Internal\ControlFlow\TaintSource(
$taint_id,
$taint_id,
$code_location,
null,
$taints
);

$this->taint_graph->addSource($source);
$this->control_flow_graph->addSource($source);

$expr_type->parent_nodes = [
$source,
Expand All @@ -1627,18 +1627,18 @@ public function addTaintSink(
array $taints = \Psalm\Type\TaintKindGroup::ALL_INPUT,
?CodeLocation $code_location = null
) : void {
if (!$this->taint_graph) {
if (!$this->control_flow_graph) {
return;
}

$sink = new \Psalm\Internal\Taint\Sink(
$sink = new \Psalm\Internal\ControlFlow\TaintSink(
$taint_id,
$taint_id,
$code_location,
null,
$taints
);

$this->taint_graph->addSink($sink);
$this->control_flow_graph->addSink($sink);
}
}
14 changes: 7 additions & 7 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
use function array_search;
use function array_keys;
use function end;
use Psalm\Internal\Taint\TaintNode;
use Psalm\Internal\ControlFlow\ControlFlowNode;
use Psalm\Storage\FunctionStorage;

/**
Expand Down Expand Up @@ -851,23 +851,23 @@ function (FunctionLikeParameter $p): bool {
}
}

if ($codebase->taint_graph
if ($codebase->control_flow_graph
&& $this->function instanceof ClassMethod
&& $cased_method_id
&& $storage->specialize_call
&& isset($context->vars_in_scope['$this'])
&& $context->vars_in_scope['$this']->parent_nodes
) {
$method_source = TaintNode::getForMethodReturn(
$method_source = ControlFlowNode::getForMethodReturn(
(string) $method_id,
$cased_method_id,
$storage->location
);

$codebase->taint_graph->addTaintNode($method_source);
$codebase->control_flow_graph->addNode($method_source);

foreach ($context->vars_in_scope['$this']->parent_nodes as $parent_node) {
$codebase->taint_graph->addPath(
$codebase->control_flow_graph->addPath(
$parent_node,
$method_source,
'$this'
Expand Down Expand Up @@ -1191,8 +1191,8 @@ private function processParams(
]);
}

if ($cased_method_id && $codebase->taint_graph) {
$type_source = TaintNode::getForMethodArgument(
if ($cased_method_id && $codebase->control_flow_graph) {
$type_source = ControlFlowNode::getForMethodArgument(
$cased_method_id,
$cased_method_id,
$offset,
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/IssueData.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class IssueData
public $link;

/**
* @var ?list<TaintNodeData|array{label: string, entry_path_type: string}>
* @var ?list<ControlFlowNodeData|array{label: string, entry_path_type: string}>
*/
public $taint_trace;

Expand All @@ -122,7 +122,7 @@ class IssueData
private $dupe_key;

/**
* @param ?list<TaintNodeData|array{label: string, entry_path_type: string}> $taint_trace
* @param ?list<ControlFlowNodeData|array{label: string, entry_path_type: string}> $taint_trace
*/
public function __construct(
string $severity,
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
use function substr_count;
use function array_map;
use function end;
use Psalm\Internal\Codebase\TaintGraph;
use Psalm\Internal\Codebase\ControlFlowGraph;
use function ini_get;
use function in_array;

Expand Down Expand Up @@ -665,7 +665,7 @@ public function consolidateAnalyzedData(): void

public function trackTaintedInputs(): void
{
$this->codebase->taint_graph = new TaintGraph();
$this->codebase->control_flow_graph = new ControlFlowGraph();
}

public function trackUnusedSuppressions(): void
Expand Down
10 changes: 5 additions & 5 deletions src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Psalm\Internal\Analyzer\Statements\Expression\Call\ArgumentAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CastAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Taint\Sink;
use Psalm\Internal\ControlFlow\TaintSink;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\ForbiddenCode;
Expand Down Expand Up @@ -36,7 +36,7 @@ public static function analyze(

$expr_type = $statements_analyzer->node_data->getType($expr);

if ($statements_analyzer->taint_graph && $expr_type) {
if ($statements_analyzer->control_flow_graph && $expr_type) {
$expr_type = CastAnalyzer::castStringAttempt(
$statements_analyzer,
$context,
Expand All @@ -46,10 +46,10 @@ public static function analyze(
);
}

if ($statements_analyzer->taint_graph) {
if ($statements_analyzer->control_flow_graph) {
$call_location = new CodeLocation($statements_analyzer->getSource(), $stmt);

$echo_param_sink = Sink::getForMethodArgument(
$echo_param_sink = TaintSink::getForMethodArgument(
'echo',
'echo',
(int) $i,
Expand All @@ -63,7 +63,7 @@ public static function analyze(
Type\TaintKind::SYSTEM_SECRET
];

$statements_analyzer->taint_graph->addSink($echo_param_sink);
$statements_analyzer->control_flow_graph->addSink($echo_param_sink);
}

if ($expr_type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,23 @@ public static function analyze(
$array_keys[$item_key_value] = true;
}

if ($statements_analyzer->taint_graph
if ($statements_analyzer->control_flow_graph
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
) {
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
if ($item_value_type->parent_nodes) {
$var_location = new CodeLocation($statements_analyzer->getSource(), $item);

$new_parent_node = \Psalm\Internal\Taint\TaintNode::getForAssignment(
$new_parent_node = \Psalm\Internal\ControlFlow\ControlFlowNode::getForAssignment(
'array'
. ($item_key_value !== null ? '[\'' . $item_key_value . '\']' : ''),
$var_location
);

$statements_analyzer->taint_graph->addTaintNode($new_parent_node);
$statements_analyzer->control_flow_graph->addNode($new_parent_node);

foreach ($item_value_type->parent_nodes as $parent_node) {
$statements_analyzer->taint_graph->addPath(
$statements_analyzer->control_flow_graph->addPath(
$parent_node,
$new_parent_node,
'array-assignment'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public static function updateArrayType(
$context->possibly_assigned_var_ids[$array_var_id] = true;
}

if ($statements_analyzer->taint_graph) {
if ($statements_analyzer->control_flow_graph) {
self::taintArrayAssignment(
$statements_analyzer,
$child_stmt->var,
Expand Down Expand Up @@ -773,30 +773,30 @@ private static function taintArrayAssignment(
?string $array_var_id,
array $key_values
) : void {
if ($statements_analyzer->taint_graph
if ($statements_analyzer->control_flow_graph
&& $child_stmt_type->parent_nodes
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
) {
$var_location = new \Psalm\CodeLocation($statements_analyzer->getSource(), $stmt);

$new_parent_node = \Psalm\Internal\Taint\TaintNode::getForAssignment(
$new_parent_node = \Psalm\Internal\ControlFlow\ControlFlowNode::getForAssignment(
$array_var_id ?: 'array-assignment',
$var_location
);

$statements_analyzer->taint_graph->addTaintNode($new_parent_node);
$statements_analyzer->control_flow_graph->addNode($new_parent_node);

foreach ($child_stmt_type->parent_nodes as $parent_node) {
if ($key_values) {
foreach ($key_values as $key_value) {
$statements_analyzer->taint_graph->addPath(
$statements_analyzer->control_flow_graph->addPath(
$parent_node,
$new_parent_node,
'array-assignment-\'' . $key_value->value . '\''
);
}
} else {
$statements_analyzer->taint_graph->addPath(
$statements_analyzer->control_flow_graph->addPath(
$parent_node,
$new_parent_node,
'array-assignment'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
use function count;
use function in_array;
use function strtolower;
use Psalm\Internal\Taint\TaintNode;
use Psalm\Internal\ControlFlow\ControlFlowNode;

/**
* @internal
Expand Down Expand Up @@ -524,7 +524,7 @@ public static function analyze(
}
}

if ($statements_analyzer->taint_graph && !$context->collect_initializations) {
if ($statements_analyzer->control_flow_graph && !$context->collect_initializations) {
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);

self::taintProperty(
Expand Down Expand Up @@ -1147,11 +1147,11 @@ private static function taintProperty(
Type\Union $assignment_value_type,
Context $context
) : void {
if (!$statements_analyzer->taint_graph) {
if (!$statements_analyzer->control_flow_graph) {
return;
}

$taint_graph = $statements_analyzer->taint_graph;
$control_flow_graph = $statements_analyzer->control_flow_graph;

$var_location = new CodeLocation($statements_analyzer->getSource(), $stmt->var);
$property_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
Expand All @@ -1175,21 +1175,21 @@ private static function taintProperty(
return;
}

$var_node = TaintNode::getForAssignment(
$var_node = ControlFlowNode::getForAssignment(
$var_id,
$var_location
);

$taint_graph->addTaintNode($var_node);
$control_flow_graph->addNode($var_node);

$property_node = TaintNode::getForAssignment(
$property_node = ControlFlowNode::getForAssignment(
$var_property_id ?: $var_id . '->$property',
$property_location
);

$taint_graph->addTaintNode($property_node);
$control_flow_graph->addNode($property_node);

$taint_graph->addPath(
$control_flow_graph->addPath(
$property_node,
$var_node,
'property-assignment'
Expand All @@ -1198,15 +1198,15 @@ private static function taintProperty(

if ($assignment_value_type->parent_nodes) {
foreach ($assignment_value_type->parent_nodes as $parent_node) {
$taint_graph->addPath($parent_node, $property_node, '=');
$control_flow_graph->addPath($parent_node, $property_node, '=');
}
}

$stmt_var_type = clone $context->vars_in_scope[$var_id];

if ($context->vars_in_scope[$var_id]->parent_nodes) {
foreach ($context->vars_in_scope[$var_id]->parent_nodes as $parent_node) {
$taint_graph->addPath($parent_node, $var_node, '=');
$control_flow_graph->addPath($parent_node, $var_node, '=');
}
}

Expand All @@ -1223,29 +1223,29 @@ private static function taintProperty(

$code_location = new CodeLocation($statements_analyzer->getSource(), $stmt);

$localized_property_node = new TaintNode(
$localized_property_node = new ControlFlowNode(
$property_id . '-' . $code_location->file_name . ':' . $code_location->raw_file_start,
$property_id,
$code_location,
null
);

$taint_graph->addTaintNode($localized_property_node);
$control_flow_graph->addNode($localized_property_node);

$property_node = new TaintNode(
$property_node = new ControlFlowNode(
$property_id,
$property_id,
null,
null
);

$taint_graph->addTaintNode($property_node);
$control_flow_graph->addNode($property_node);

$taint_graph->addPath($localized_property_node, $property_node, 'property-assignment');
$control_flow_graph->addPath($localized_property_node, $property_node, 'property-assignment');

if ($assignment_value_type->parent_nodes) {
foreach ($assignment_value_type->parent_nodes as $parent_node) {
$taint_graph->addPath($parent_node, $localized_property_node, '=');
$control_flow_graph->addPath($parent_node, $localized_property_node, '=');
}
}
}
Expand Down
Loading

0 comments on commit 56cddd1

Please sign in to comment.