Issues (20)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Validator/Items.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest\Validator;
19
20
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface;
21
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation;
22
use Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException;
23
24
/**
25
 * Checks that each element of an array validates against its corresponding
26
 * validator in a collection.
27
 * <a href="http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf">Here</a>
28
 * you can see examples of use for this validator.
29
 *
30
 * @author mcustiel
31
 */
32
class Items extends AbstractIterableValidator
33
{
34
    const ITEMS_INDEX = 'items';
35
    const ADDITIONAL_ITEMS_INDEX = 'additionalItems';
36
37
    /**
38
     * @var bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface
39
     */
40
    private $additionalItems = true;
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @see \Mcustiel\SimpleRequest\Validator\AbstractIterableValidator::setSpecification()
46
     */
47 90
    public function setSpecification($specification = null)
48
    {
49 90
        $this->checkSpecificationIsArray($specification);
50
51 89
        $this->initItems($specification);
52 87
        $this->initAdditionalItems($specification);
53 86
    }
54
55 87
    private function initAdditionalItems($specification)
56
    {
57 87
        if (isset($specification[self::ADDITIONAL_ITEMS_INDEX])) {
58 86
            $this->setAdditionalItems($specification[self::ADDITIONAL_ITEMS_INDEX]);
59 85
        }
60 86
    }
61
62 89
    private function initItems($specification)
63
    {
64 89
        if (isset($specification[self::ITEMS_INDEX])) {
65 88
            $this->setItems($specification[self::ITEMS_INDEX]);
66 86
        }
67 87
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate()
73
     */
74 88
    public function validate($value)
75
    {
76 88
        if (!is_array($value)) {
77 1
            return false;
78
        }
79
80
        // From json-schema definition: if "items" is not present, or its value is an object,
81
        // validation of the instance always succeeds, regardless of the value of "additionalItems";
82 87
        if (empty($this->items)) {
83
            return true;
84
        }
85
86 87
        return $this->executeItemsValidation($value);
87
    }
88
89 87
    private function executeItemsValidation($value)
90
    {
91 87
        if ($this->items instanceof ValidatorInterface) {
92 2
            return $this->validateArray($value, $this->items);
93
        }
94 85
        $valid = $this->validateTuple($value);
95
        // From json-schema definition: if the value of "additionalItems" is boolean value false and
96
        // the value of "items" is an array, the instance is valid if its size is less than, or
97
        // equal to, the size of "items".
98 85
        if ($this->additionalItems === false) {
99 83
            return $valid && (count($value) <= count($this->items));
100
        }
101
102
        // From json-schema definition: if the value of "additionalItems" is
103
        // boolean value true or an object, validation of the instance always succeeds;
104 2
        return $this->validateRest($value);
105
    }
106
107
    /**
108
     * Validates each element against its corresponding validator. Then,
109
     * if additionalItems is a validator, checks the rest again those
110
     * validators.
111
     *
112
     * @param array $list
113
     *
114
     * @return bool
115
     */
116 2
    private function validateRest(array $list)
117
    {
118 2
        $count = count($this->items);
119 2
        return $this->additionalItems === true ||
120 2
            $this->validateArray(
121 2
                array_slice($list, $count, count($list) - $count),
122 2
                $this->additionalItems
0 ignored issues
show
It seems like $this->additionalItems can also be of type boolean; however, Mcustiel\SimpleRequest\V...\Items::validateArray() does only seem to accept object<Mcustiel\SimpleRe...ces\ValidatorInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123 2
            );
124
    }
125
126
    /**
127
     * Validates each one of the elements of the array against
128
     * its corresponding specified validator.
129
     *
130
     * @param array $tuple
131
     *
132
     * @return bool
133
     */
134 85
    private function validateTuple(array $tuple)
135
    {
136 85
        $keys = array_keys($tuple);
137 85
        $count = count($this->items);
138 85
        for ($index = 0; $index < $count; $index++) {
139
            // In the specification is not clear what to do when instance size
140
            // is less than items size. I chose to pass null and if null passes
141
            // the validation, it returns true.
142 85
            $value = isset($keys[$index]) ? $tuple[$keys[$index]] : null;
143 85
            if (!$this->items[$index]->validate($value)) {
144 6
                return false;
145
            }
146 85
        }
147
148 79
        return true;
149
    }
150
151
    /**
152
     * Checks and sets the specified items values.
153
     *
154
     * @param array|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification
155
     */
156 88
    private function setItems($specification)
157
    {
158 88
        if ($specification instanceof ValidatorAnnotation) {
159 2
            $this->items = $this->createValidatorInstanceFromAnnotation(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createValidatorIn...otation($specification) of type object<Mcustiel\SimpleRe...ces\ValidatorInterface> is incompatible with the declared type array<integer,object<Mcu...es\ValidatorInterface>> of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
160
                $specification
161 2
            );
162 2
        } else {
163 86
            $this->checkSpecificationIsArray($specification);
164 85
            $this->items = $this->convertAnnotationsToValidators($specification);
0 ignored issues
show
It seems like $specification defined by parameter $specification on line 156 can also be of type object<Mcustiel\SimpleRe...ces\ValidatorInterface>; however, Mcustiel\SimpleRequest\V...notationsToValidators() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
165
        }
166 86
    }
167
168 85
    private function convertAnnotationsToValidators(array $specification)
169
    {
170 85
        $items = [];
171 85
        foreach ($specification as $index => $validator) {
172 85
            $items[$index] = $this->checkIfAnnotationAndReturnObject($validator);
173 84
        }
174 84
        return $items;
175
    }
176
177
    /**
178
     * Checks and sets the specified additionalItems.
179
     *
180
     * @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification
181
     */
182 86
    private function setAdditionalItems($specification)
183
    {
184 86
        if (is_bool($specification)) {
185 83
            $this->additionalItems = $specification;
186 86
        } elseif ($specification instanceof ValidatorAnnotation) {
187 2
            $this->additionalItems = $this->createValidatorInstanceFromAnnotation(
188
                $specification
189 2
            );
190 2
        } else {
191 1
            throw new UnspecifiedValidatorException(
192
                'The validator is being initialized without a valid validator Annotation'
193 1
            );
194
        }
195 85
    }
196
197
    /**
198
     * Validates an array against a specific validator.
199
     *
200
     * @param array                                                 $array
201
     * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator
202
     */
203 4
    private function validateArray(array $array, ValidatorInterface $validator)
204
    {
205 4
        foreach ($array as $item) {
206 4
            if (!$validator->validate($item)) {
207 1
                return false;
208
            }
209 3
        }
210
211 3
        return true;
212
    }
213
}
214