Skip to content

Commit

Permalink
fixed type issue
Browse files Browse the repository at this point in the history
  • Loading branch information
naomilwx committed Nov 18, 2014
1 parent 2b601f8 commit b0bab15
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 57 deletions.
9 changes: 9 additions & 0 deletions lib/Phortress/Dephenses/Taint/FunctionNodeAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private function addAffectingFunctionToAnalysisResult(FunctionTaintResult $resul

protected function resolveVariableTaint(Variable $var){
$result = parent::resolveVariableTaint($var);
if(empty($result)){
$result = $this->createTaintResult(Annotation::UNKNOWN);
}
$this->addAffectingFunctionToAnalysisResult($result, $var->name);
return $result;
}
Expand All @@ -61,6 +64,9 @@ protected function resolveFuncResultTaint(FuncCall $exp){

protected function resolveBinaryOpTaint(BinaryOp $exp){
$result = parent::resolveBinaryOpTaint($exp);
if(empty($result)){
$result = $this->createTaintResult(Annotation::UNKNOWN);
}
$leftName = $exp->left->name;
$rightName = $exp->right->name;
$this->addAffectingFunctionToAnalysisResult($result, $leftName);
Expand All @@ -72,6 +78,9 @@ protected function resolveBinaryOpTaint(BinaryOp $exp){
protected function resolveArrayFieldTaint(ArrayDimFetch $exp){
$array_var_name = $exp->var->name;
$result = parent::resolveArrayFieldTaint($exp);
if(empty($result)){
$result = $this->createTaintResult(Annotation::UNKNOWN);
}
$this->addAffectingFunctionToAnalysisResult($result, $array_var_name);
return $result;
}
Expand Down
36 changes: 1 addition & 35 deletions lib/Phortress/Dephenses/Taint/FunctionTaintResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,13 @@

use PhpParser\Node\Expr;
class FunctionTaintResult extends TaintResult{
protected $variable;
/**
* Array of Variables (function parameters) which will affect the variable's taint value.
*/
protected $affecting_params = array();

public function __construct($taint = Annotation::UNASSIGNED,
$sanitising = array(), Expr\Variable $var = null){
public function __construct($taint = Annotation::UNASSIGNED, $sanitising = array()){
parent::__construct($taint, $sanitising);
$this->variable = $var;
}

public function getVariable(){
return $this->variable;
}

public function setVariable(Expr\Variable $var){
$this->variable = $var;
}

public function getAffectingParameters(){
Expand Down Expand Up @@ -64,27 +53,4 @@ public static function mergeFunctionTaintResults(FunctionTaintResult $var1, Func
return $varInfo;
}

/**
* Takes in of the form: array(array(var name => VariableInfo))
* Flattens it to a single array mapping a variable's name to the variable's corresponding
* VariableInfo object. In otherwords, the return array should be of the form:
* array(variable_name => VariableInfo)
*/
public static function mergeVariables($vars){
$merged = array();
foreach($vars as $item){
foreach($item as $var_name => $varInfo){
if(empty($varInfo)){
continue;
}
if(!array_key_exists($var_name, $merged)){
$merged[$var_name] = $varInfo;
}else{
$existing = $merged[$var_name];
$merged[$var_name] = self::mergeFunctionTaintResults($existing, $varInfo);
}
}
}
return $merged;
}
}
34 changes: 19 additions & 15 deletions lib/Phortress/Dephenses/Taint/NodeAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected function traceVariableTaint(Expr\Variable $var){
if(isset($varTaintEnv)){
$taintResult = $varTaintEnv->getTaintResult($var->name);
}
if(empty($taintResult) || $taintResult->getTaint() == Annotation::UNASSIGNED){
if(empty($taintResult)){
$assign = $assignEnv->resolveVariable($varName);
return $this->resolveExprTaint($assign->expr);
}
Expand All @@ -204,41 +204,45 @@ protected function traceVariableTaint(Expr\Variable $var){

public function resolveExprTaint(Expr $exp){
if($exp instanceof Node\Scalar){
return $this->createTaintResult(Annotation::SAFE);
$result = $this->createTaintResult(Annotation::SAFE);
}else if (($exp instanceof Expr\ClassConstFetch) || ($exp instanceof
Expr\ConstFetch)){
return $this->createTaintResult(Annotation::SAFE);
$result = $this->createTaintResult(Annotation::SAFE);
}else if($exp instanceof Expr\Variable) {
return $this->resolveVariableTaint($exp);
$result = $this->resolveVariableTaint($exp);
}else if($exp instanceof Expr\PreInc || $exp instanceof Expr\PreDec || $exp instanceof Expr\PostInc || $exp instanceof Expr\PostDec){
$var = $exp->var;
return $this->resolveVariableTaint($var);
$result = $this->resolveVariableTaint($var);
}else if($exp instanceof Expr\UnaryMinus || $exp instanceof Expr\UnaryPlus){
$var = $exp->expr;
return $this->resolveVariableTaint($var);
$result = $this->resolveVariableTaint($var);
}else if($exp instanceof Expr\PropertyFetch){
$var = $exp->var;
return $this->resolveVariableTaint($var);
$result = $this->resolveVariableTaint($var);
}else if($exp instanceof Expr\BinaryOp){
return $this->resolveBinaryOpTaint($exp);
$result = $this->resolveBinaryOpTaint($exp);
}else if($exp instanceof Expr\Array_){
return $this->resolveAndMergeTaintOfExprsInArray($exp);
$result = $this->resolveAndMergeTaintOfExprsInArray($exp);
}else if($exp instanceof Expr\ArrayDimFetch){
return $this->resolveArrayFieldTaint($exp);
$result = $this->resolveArrayFieldTaint($exp);
}else if($exp instanceof Expr\StaticPropertyFetch){
return $this->resolveClassPropertyTaint($exp);
$result = $this->resolveClassPropertyTaint($exp);
}else if($exp instanceof Expr\FuncCall){
return $this->resolveFuncResultTaint($exp);
$result = $this->resolveFuncResultTaint($exp);
}else if($exp instanceof Expr\MethodCall){
return $this->resolveMethodResultTaint($exp);
$result = $this->resolveMethodResultTaint($exp);
}else if($exp instanceof Expr\Ternary){
return $this->resolveTernaryTaint($exp);
$result = $this->resolveTernaryTaint($exp);
}else if($exp instanceof Expr\Eval_){
return $this->resolveExprTaint($exp->expr);
$result = $this->resolveExprTaint($exp->expr);
}else{
//Other expressions we will not handle.
return $this->createTaintResult(Annotation::UNKNOWN);
}
if(empty($result)){
$result = $this->createTaintResult(Annotation::UNASSIGNED);
}
return $result;
}

protected function resolveMethodResultTaint(Expr\MethodCall $exp){
Expand Down
5 changes: 0 additions & 5 deletions lib/Phortress/Dephenses/Taint/TaintEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ public function getTaintResult($varName){
$parentTaintEnv = self::getTaintEnvironmentFromEnvironment($this->environment->getParent());
if(isset($parentTaintEnv)){
return $parentTaintEnv->getTaintResult($varName);
}else{
return new TaintResult(Annotation::UNASSIGNED);
}

}else{
return new TaintResult(Annotation::UNASSIGNED);
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/Phortress/Dephenses/TaintTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Phortress\Dephenses;

use Phortress\Dephenses\Taint\TaintEnvironment;
use Phortress\Program;
use PhpParser\Node\Expr\Variable;

class TaintTest extends \PHPUnit_Framework_TestCase {
/**
Expand Down Expand Up @@ -47,7 +49,7 @@ public function testTaintedParams(){
$this->assertEquals(Taint\Annotation::TAINTED, $taint2);

}

/*
public function testTaintedParamsWithBinaryOps(){
$taintDephense = new Taint();
$taintDephense->run($this->program2->parseTree);
Expand Down Expand Up @@ -90,7 +92,7 @@ public function testWhileLoop(){
$taint2 = $this->getVariableTaint($this->program5->parseTree[3]->var);
$this->assertEquals(Taint\Annotation::SAFE, $taint2);
}

*/
public function getVariableTaint(Variable $var){
$assignEnv = $var->environment->resolveVariable($var->name)->environment;
$taintEnv = TaintEnvironment::getTaintEnvironmentFromEnvironment($assignEnv);
Expand Down

0 comments on commit b0bab15

Please sign in to comment.