Completed
Push — work-fleets ( 867546...331178 )
by SuperNova.WS
06:17
created

V2PropertyContainer::__set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 5
b 0
f 1
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 9
cp 0
crap 12
rs 9.6666
1
<?php
2
3
use \Common\ContainerMagic;
4
use \Common\IPropertyContainer;
5
6
class V2PropertyContainer extends ContainerMagic implements IPropertyContainer {
7
8
  /**
9
   * Property descriptions
10
   *
11
   * @var array[]
12
   */
13
  protected $properties = array();
14
15
  /**
16
   * Array of accessors - getters/setters/etc
17
   *
18
   * Getter is a callable like
19
   *    function () use ($that) {}
20
   *  or Pimple-like (P_CONTAINER_GETTER_PIMPLE)
21
   *    function ($this) {}
22
   *
23
   * Setter is a callable like
24
   *    function ($value) use ($that) {}
25
   *
26
   * Importer is a callable like
27
   *    function (&$row) use ($this) {}
28
   *
29
   * Exporter is a callable like
30
   *    function (&$row) use ($this) {}
31
   *
32
   * @var callable[][]
33
   */
34
  protected $accessors;
35
36
  public function setProperties($properties) {
37
    $this->properties = $properties;
38
  }
39
40
  // TODO - batch assign
41
  public function assignAccessor($varName, $type, $callable) {
42
    if (empty($callable)) {
43
      return;
44
    }
45
46
    if (is_callable($callable)) {
47
      $this->accessors[$type][$varName] = $callable;
48
    } else {
49
      throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope');
50
    }
51
  }
52
53
  public function __set($name, $value) {
54
    if(is_callable($value)) {
55
      $this->accessors[$name][P_CONTAINER_GETTER_PIMPLE] = $value;
56
    } elseif (is_callable($this->accessors[$name][P_CONTAINER_SETTER])) {
57
      call_user_func($this->accessors[$name][P_CONTAINER_SETTER], $value);
0 ignored issues
show
Security Code Execution introduced by
$this->accessors[$name][P_CONTAINER_SETTER] can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_POST
    in includes/general.php on line 258
  2. sys_get_param() returns tainted data, and $value is assigned
    in includes/general.php on line 266
  3. sys_get_param_id() returns tainted data, and BuddyContainer::$buddy_id is assigned
    in includes/classes/Buddy/BuddyContainer.php on line 81
  4. Tainted property BuddyContainer::$buddy_id is read
    in includes/classes/Buddy/BuddyModel.php on line 237
  5. $cBuddy->buddy_id is passed to V2PropertyContainer::__set()
    in includes/classes/Buddy/BuddyModel.php on line -1
  6. V2PropertyContainer::$accessors is assigned
    in includes/classes/V2PropertyContainer.php on line 55
  7. Tainted property V2PropertyContainer::$accessors is read
    in includes/classes/V2PropertyContainer.php on line 57

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
58
    } else {
59
      $this->values[$name] = $value;
60
    }
61
  }
62
63
  public function __get($name) {
64
    if (is_callable($this->accessors[$name][P_CONTAINER_GETTER_PIMPLE])) {
65
      return call_user_func($this->accessors[$name][P_CONTAINER_GETTER_PIMPLE], $this);
0 ignored issues
show
Security Code Execution introduced by
$this->accessors[$name][...ONTAINER_GETTER_PIMPLE] can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_POST
    in includes/general.php on line 258
  2. sys_get_param() returns tainted data, and $value is assigned
    in includes/general.php on line 266
  3. sys_get_param_id() returns tainted data, and BuddyContainer::$buddy_id is assigned
    in includes/classes/Buddy/BuddyContainer.php on line 81
  4. Tainted property BuddyContainer::$buddy_id is read
    in includes/classes/Buddy/BuddyModel.php on line 237
  5. $cBuddy->buddy_id is passed to V2PropertyContainer::__set()
    in includes/classes/Buddy/BuddyModel.php on line -1
  6. V2PropertyContainer::$accessors is assigned
    in includes/classes/V2PropertyContainer.php on line 55
  7. Tainted property V2PropertyContainer::$accessors is read
    in includes/classes/V2PropertyContainer.php on line 65

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
66
    } elseif (is_callable($this->accessors[$name][P_CONTAINER_GETTER])) {
67
      return call_user_func($this->accessors[$name][P_CONTAINER_GETTER]);
0 ignored issues
show
Security Code Execution introduced by
$this->accessors[$name][P_CONTAINER_GETTER] can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_POST
    in includes/general.php on line 258
  2. sys_get_param() returns tainted data, and $value is assigned
    in includes/general.php on line 266
  3. sys_get_param_id() returns tainted data, and BuddyContainer::$buddy_id is assigned
    in includes/classes/Buddy/BuddyContainer.php on line 81
  4. Tainted property BuddyContainer::$buddy_id is read
    in includes/classes/Buddy/BuddyModel.php on line 237
  5. $cBuddy->buddy_id is passed to V2PropertyContainer::__set()
    in includes/classes/Buddy/BuddyModel.php on line -1
  6. V2PropertyContainer::$accessors is assigned
    in includes/classes/V2PropertyContainer.php on line 55
  7. Tainted property V2PropertyContainer::$accessors is read
    in includes/classes/V2PropertyContainer.php on line 67

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
68
    } else {
69
      return $this->values[$name];
70
    }
71
  }
72
73
  public function __isset($name) {
74
    // TODO - or here already can isset($this->name) ????
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
    $value = $this->$name;
76
    return isset($value);
77
  }
78
79
  public function clearProperties() {
80
    foreach ($this->properties as $propertyName => $propertyData) {
81
      unset($this->values[$propertyName]);
82
    }
83
  }
84
85
}
86