Skip to content

Commit

Permalink
Fix #4600 - set attributes in a bunch of places
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Nov 18, 2020
1 parent ddbfbb2 commit 236292f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 27 deletions.
61 changes: 49 additions & 12 deletions src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,48 @@ public static function analyze(
new PhpParser\Node\Name(['get_class']),
[
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Variable(substr($type->typeof, 1))
new PhpParser\Node\Expr\Variable(
substr($type->typeof, 1),
$stmt->cond->getAttributes()
),
false,
false,
$stmt->cond->getAttributes()
),
]
],
$stmt->cond->getAttributes()
);
} elseif ($type instanceof Type\Atomic\TDependentGetType) {
$type_statements[] = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name(['gettype']),
[
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Variable(substr($type->typeof, 1))
new PhpParser\Node\Expr\Variable(
substr($type->typeof, 1),
$stmt->cond->getAttributes()
),
false,
false,
$stmt->cond->getAttributes()
),
]
],
$stmt->cond->getAttributes()
);
} elseif ($type instanceof Type\Atomic\TDependentGetDebugType) {
$type_statements[] = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name(['get_debug_type']),
[
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Variable(substr($type->typeof, 1))
new PhpParser\Node\Expr\Variable(
substr($type->typeof, 1),
$stmt->cond->getAttributes()
),
false,
false,
$stmt->cond->getAttributes()
),
]
],
$stmt->cond->getAttributes()
);
} else {
$type_statements = null;
Expand Down Expand Up @@ -226,7 +247,8 @@ public static function analyze(
} else {
$case_if_stmt = new PhpParser\Node\Stmt\If_(
$switch_scope->leftover_case_equality_expr,
['stmts' => $case_stmts]
['stmts' => $case_stmts],
$case->getAttributes()
);

$switch_scope->leftover_statements = [$case_if_stmt];
Expand Down Expand Up @@ -652,12 +674,19 @@ private static function simplifyCaseEqualityExpression(
new PhpParser\Node\Name\FullyQualified(['in_array']),
[
new PhpParser\Node\Arg(
$var
$var,
false,
false,
$var->getAttributes()
),
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Array_(
$nested_or_options
)
$nested_or_options,
$case_equality_expr->getAttributes()
),
false,
false,
$case_equality_expr->getAttributes()
),
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\ConstFetch(
Expand Down Expand Up @@ -686,7 +715,10 @@ private static function getOptionsFromNestedOr(
&& $case_equality_expr->left->name === $var->name
) {
$in_array_values[] = new PhpParser\Node\Expr\ArrayItem(
$case_equality_expr->right
$case_equality_expr->right,
null,
false,
$case_equality_expr->right->getAttributes()
);

return $in_array_values;
Expand All @@ -703,7 +735,12 @@ private static function getOptionsFromNestedOr(
return null;
}

$in_array_values[] = new PhpParser\Node\Expr\ArrayItem($case_equality_expr->right->right);
$in_array_values[] = new PhpParser\Node\Expr\ArrayItem(
$case_equality_expr->right->right,
null,
false,
$case_equality_expr->right->right->getAttributes()
);

return self::getOptionsFromNestedOr(
$case_equality_expr->left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,16 @@ public static function analyze(
$array_keys[$item_key_value] = true;
}

if ($statements_analyzer->data_flow_graph
&& ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
if (($data_flow_graph = $statements_analyzer->data_flow_graph)
&& ($data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|| !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues()))
) {
if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
if ($item_value_type->parent_nodes) {
if ($item_value_type->parent_nodes
&& !($item_value_type->isSingle()
&& $item_value_type->hasLiteralValue()
&& $data_flow_graph instanceof \Psalm\Internal\Codebase\TaintFlowGraph)
) {
$var_location = new CodeLocation($statements_analyzer->getSource(), $item);

$new_parent_node = \Psalm\Internal\DataFlow\DataFlowNode::getForAssignment(
Expand All @@ -228,10 +232,10 @@ public static function analyze(
$var_location
);

$statements_analyzer->data_flow_graph->addNode($new_parent_node);
$data_flow_graph->addNode($new_parent_node);

foreach ($item_value_type->parent_nodes as $parent_node) {
$statements_analyzer->data_flow_graph->addPath(
$data_flow_graph->addPath(
$parent_node,
$new_parent_node,
'array-assignment'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,11 @@ private static function processTaintedness(
return $input_type;
}

// literal data can’t be tainted
if ($input_type->isSingle() && $input_type->hasLiteralValue()) {
return $input_type;
}

if ($function_param->type && $function_param->type->isString() && !$input_type->isString()) {
$cast_type = CastAnalyzer::castStringAttempt(
$statements_analyzer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ public static function handleMagicMethod(
* @return PhpParser\Node\Expr\ArrayItem
*/
function (PhpParser\Node\Arg $arg): PhpParser\Node\Expr\ArrayItem {
return new PhpParser\Node\Expr\ArrayItem($arg->value);
return new PhpParser\Node\Expr\ArrayItem(
$arg->value,
null,
false,
$arg->getAttributes()
);
},
$stmt->args
);
Expand All @@ -162,13 +167,23 @@ function (PhpParser\Node\Arg $arg): PhpParser\Node\Expr\ArrayItem {
$statements_analyzer->node_data = clone $statements_analyzer->node_data;

return new AtomicCallContext(
new MethodIdentifier(
$fq_class_name,
'__call'
),
new MethodIdentifier($fq_class_name, '__call'),
[
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\String_($method_name_lc)),
new PhpParser\Node\Arg(new PhpParser\Node\Expr\Array_($array_values)),
new PhpParser\Node\Arg(
new PhpParser\Node\Scalar\String_($method_name_lc),
false,
false,
$stmt->getAttributes()
),
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Array_(
$array_values,
$stmt->getAttributes()
),
false,
false,
$stmt->getAttributes()
),
],
$old_node_data
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,29 @@ private static function handleNamedCall(

$array_values = array_map(
function (PhpParser\Node\Arg $arg): PhpParser\Node\Expr\ArrayItem {
return new PhpParser\Node\Expr\ArrayItem($arg->value);
return new PhpParser\Node\Expr\ArrayItem(
$arg->value,
null,
false,
$arg->getAttributes()
);
},
$args
);

$args = [
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\String_((string) $method_id)),
new PhpParser\Node\Arg(new PhpParser\Node\Expr\Array_($array_values)),
new PhpParser\Node\Arg(
new PhpParser\Node\Scalar\String_((string) $method_id, $stmt_name->getAttributes()),
false,
false,
$stmt_name->getAttributes()
),
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Array_($array_values, $stmt->getAttributes()),
false,
false,
$stmt->getAttributes()
),
];

$method_id = new MethodIdentifier(
Expand Down

0 comments on commit 236292f

Please sign in to comment.