Issues (7)

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.php (1 issue)

Labels
Severity

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
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Validate;
22
23
/**
24
 * An immutable set of rules for a set of fields that can validate data.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Validator
30
{
31
    /**
32
     * @var array<string,\Caridea\Validate\Rule\Set> Associative array of field name to rule set
33
     */
34
    protected $ruleset;
35
36
    /**
37
     * Creates a new validator.
38
     *
39
     * @param array<string,\Caridea\Validate\Rule\Set> $ruleset Associative array of field name to rule set
40
     */
41 1
    public function __construct(array $ruleset)
42
    {
43 1
        $this->ruleset = $ruleset;
44 1
    }
45
46
    /**
47
     * Gets a field from the values.
48
     *
49
     * This can be overridden to access by other means (e.g. object properties,
50
     * getter methods).
51
     *
52
     * @param mixed $values The values
53
     * @param string $field The field to access
54
     * @return mixed The accessed value
55
     */
56 1
    protected function access($values, string $field)
57
    {
58 1
        return isset($values[$field]) ? $values[$field] : null;
59
    }
60
61
    /**
62
     * Iterates over the ruleset and collects any error codes.
63
     *
64
     * @param object|array $values An object or associative array to validate
65
     * @return array Associative array of field name to error
66
     * @throws \InvalidArgumentException if `$values` is null
67
     */
68 2
    protected function iterate($values)
69
    {
70 2
        if (!is_object($values) && !is_array($values)) {
71 1
            throw new \InvalidArgumentException("Unable to validate provided object");
72
        }
73 1
        $errors = [];
74 1
        foreach ($this->ruleset as $field => $rules) {
75 1
            $value = $this->access($values, $field);
76 1
            $error = $rules->apply($value, $values);
0 ignored issues
show
It seems like $values defined by parameter $values on line 68 can also be of type object; however, Caridea\Validate\Rule\Set::apply() 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...
77 1
            if ($error !== null) {
78 1
                $errors[$field] = count($error) > 1 || count(array_filter(array_keys($error), 'is_string')) > 0 ?
79 1
                    $error : current($error);
80
            }
81
        }
82 1
        return $errors;
83
    }
84
85
    /**
86
     * Validates the provided value, returning a result object.
87
     *
88
     * @param object|array $values An object or associative array to validate
89
     * @return \Caridea\Validate\Result The validation results
90
     * @throws \InvalidArgumentException if `$values` is null
91
     */
92 1
    public function validate($values): Result
93
    {
94 1
        return new Result($this->iterate($values));
95
    }
96
97
    /**
98
     * Validates the provided value, throwing an exception upon failure.
99
     *
100
     * @param object|array $values An object or associative array to validate
101
     * @throws \Caridea\Validate\Exception\Invalid if validation fails
102
     * @throws \InvalidArgumentException if `$values` is null
103
     */
104 2
    public function assert($values)
105
    {
106 2
        $errors = $this->iterate($values);
107 2
        if (!empty($errors)) {
108 1
            throw new Exception\Invalid($errors);
109
        }
110 1
    }
111
}
112