Issues (21)

Security Analysis    not enabled

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/Remote/Validator.php (2 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
namespace Proengsoft\JsValidation\Remote;
4
5
use Illuminate\Http\Exceptions\HttpResponseException;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Support\Arr;
8
use Illuminate\Validation\ValidationException;
9
use Illuminate\Validation\ValidationRuleParser;
10
use Illuminate\Validation\Validator as BaseValidator;
11
use Proengsoft\JsValidation\Support\AccessProtectedTrait;
12
use Proengsoft\JsValidation\Support\RuleListTrait;
13
14
class Validator
15
{
16
    use AccessProtectedTrait;
17
    use RuleListTrait;
18
19
    /**
20
     * Validator extension name.
21
     */
22
    const EXTENSION_NAME = 'jsvalidation';
23
24
    /**
25
     * @var \Illuminate\Validation\Validator
26
     */
27
    protected $validator;
28
29
    /**
30
     * Whether to escape validation messages.
31
     *
32
     * @var bool
33
     */
34
    protected $escape;
35
36
    /**
37
     * RemoteValidator constructor.
38
     *
39
     * @param \Illuminate\Validation\Validator $validator
40
     * @param bool $escape
41
     */
42 96
    public function __construct(BaseValidator $validator, $escape = false)
43
    {
44 96
        $this->validator = $validator;
45 96
        $this->escape = $escape;
46 96
    }
47
48
    /**
49
     * Validate request.
50
     *
51
     * @param $field
52
     * @param $parameters
53
     * @return void
54
     *
55
     * @throws \Illuminate\Validation\ValidationException
56
     */
57 96
    public function validate($field, $parameters = [])
58
    {
59 96
        $attribute = $this->parseAttributeName($field);
60 96
        $validationParams = $this->parseParameters($parameters);
61 96
        $validationResult = $this->validateJsRemoteRequest($attribute, $validationParams);
62
63 96
        $this->throwValidationException($validationResult, $this->validator);
64
    }
65
66
    /**
67
     * Throw the failed validation exception.
68
     *
69
     * @param mixed $result
70
     * @param \Illuminate\Validation\Validator  $validator
71
     * @return void
72
     *
73
     * @throws \Illuminate\Validation\ValidationException|\Illuminate\Http\Exceptions\HttpResponseException
74
     */
75 96
    protected function throwValidationException($result, $validator)
76
    {
77 96
        $response = new JsonResponse($result, 200);
78
79 96
        if ($result !== true && class_exists(ValidationException::class)) {
80 48
            throw new ValidationException($validator, $response);
81
        }
82
83 48
        throw new HttpResponseException($response);
84
    }
85
86
    /**
87
     *  Parse Validation input request data.
88
     *
89
     * @param $data
90
     * @return array
91
     */
92 96
    protected function parseAttributeName($data)
93
    {
94 96
        parse_str($data, $attrParts);
95 96
        $attrParts = is_null($attrParts) ? [] : $attrParts;
96 96
        $newAttr = array_keys(Arr::dot($attrParts));
0 ignored issues
show
$attrParts is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
98 96
        return array_pop($newAttr);
99
    }
100
101
    /**
102
     *  Parse Validation parameters.
103
     *
104
     * @param $parameters
105
     * @return array
106
     */
107 96
    protected function parseParameters($parameters)
108
    {
109 96
        $newParams = ['validate_all' => false];
110 96
        if (isset($parameters[0])) {
111 84
            $newParams['validate_all'] = ($parameters[0] === 'true') ? true : false;
112
        }
113
114 96
        return $newParams;
115
    }
116
117
    /**
118
     * Validate remote Javascript Validations.
119
     *
120
     * @param $attribute
121
     * @param array $parameters
122
     * @return array|bool
123
     */
124 96
    protected function validateJsRemoteRequest($attribute, $parameters)
125
    {
126 96
        $this->setRemoteValidation($attribute, $parameters['validate_all']);
127
128 96
        $validator = $this->validator;
129 96
        if ($validator->passes()) {
130 48
            return true;
131
        }
132
133 48
        $messages = $validator->messages()->get($attribute);
134
135 48
        if ($this->escape) {
136 12
            foreach ($messages as $key => $value) {
137 12
                $messages[$key] = e($value);
138
            }
139
        }
140
141 48
        return $messages;
142
    }
143
144
    /**
145
     * Sets data for validate remote rules.
146
     *
147
     * @param $attribute
148
     * @param bool $validateAll
149
     * @return void
150
     */
151 96
    protected function setRemoteValidation($attribute, $validateAll = false)
152
    {
153 96
        $validator = $this->validator;
154 96
        $rules = $validator->getRules();
155 96
        $rules = isset($rules[$attribute]) ? $rules[$attribute] : [];
156 96
        if (in_array('no_js_validation', $rules)) {
157 12
            $validator->setRules([$attribute => []]);
158
159 12
            return;
160
        }
161 84
        if (! $validateAll) {
162 72
            $rules = $this->purgeNonRemoteRules($rules, $validator);
163
        }
164 84
        $validator->setRules([$attribute => $rules]);
165 84
    }
166
167
    /**
168
     * Remove rules that should not be validated remotely.
169
     *
170
     * @param $rules
171
     * @param BaseValidator $validator
172
     * @return mixed
173
     */
174 72
    protected function purgeNonRemoteRules($rules, $validator)
175
    {
176 72
        $protectedValidator = $this->createProtectedCaller($validator);
0 ignored issues
show
$protectedValidator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
177
178 72
        foreach ($rules as $i => $rule) {
179 72
            $parsedRule = ValidationRuleParser::parse($rule);
180 72
            if (! $this->isRemoteRule($parsedRule[0])) {
181 54
                unset($rules[$i]);
182
            }
183
        }
184
185 72
        return $rules;
186
    }
187
}
188