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/Javascript/ValidatorHandler.php (9 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\Javascript;
4
5
use Proengsoft\JsValidation\Support\DelegatedValidator;
6
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
7
8
class ValidatorHandler
9
{
10
    use UseDelegatedValidatorTrait;
11
12
    /**
13
     * Rule used to disable validations.
14
     *
15
     * @const string
16
     */
17
    const JSVALIDATION_DISABLE = 'NoJsValidation';
18
19
    /**
20
     * @var RuleParser
21
     */
22
    protected $rules;
23
    /**
24
     * @var MessageParser
25
     */
26
    protected $messages;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $remote = true;
32
33
    /**
34
     * Create a new JsValidation instance.
35
     *
36
     * @param RuleParser $rules
37
     * @param MessageParser $messages
38
     */
39 120
    public function __construct(RuleParser $rules, MessageParser $messages)
40
    {
41 120
        $this->rules = $rules;
42 120
        $this->messages = $messages;
43 120
        $this->validator = $rules->getDelegatedValidator();
44 120
    }
45
46
    /**
47
     * Sets delegated Validator instance.
48
     *
49
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
50
     * @return void
51
     */
52 48
    public function setDelegatedValidator(DelegatedValidator $validator)
53
    {
54 48
        $this->validator = $validator;
55 48
        $this->rules->setDelegatedValidator($validator);
56 48
        $this->messages->setDelegatedValidator($validator);
57 48
    }
58
59
    /**
60
     * Enable or disables remote validations.
61
     *
62
     * @param bool $enabled
63
     * @return void
64
     */
65 12
    public function setRemote($enabled)
66
    {
67 12
        $this->remote = $enabled;
68 12
    }
69
70
    /**
71
     * Generate Javascript Validations.
72
     *
73
     * @return array
74
     */
75 48
    protected function generateJavascriptValidations()
76
    {
77 48
        $jsValidations = [];
78
79 48
        foreach ($this->validator->getRules() as $attribute => $rules) {
80 48
            if (! $this->jsValidationEnabled($attribute)) {
81 12
                continue;
82
            }
83
84 36
            $newRules = $this->jsConvertRules($attribute, $rules, $this->remote);
85 36
            $jsValidations = array_merge($jsValidations, $newRules);
86
        }
87
88 48
        return $jsValidations;
89
    }
90
91
    /**
92
     * Make Laravel Validations compatible with JQuery Validation Plugin.
93
     *
94
     * @param $attribute
95
     * @param $rules
96
     * @param bool $includeRemote
97
     * @return array
98
     */
99 36
    protected function jsConvertRules($attribute, $rules, $includeRemote)
100
    {
101 36
        $jsRules = [];
102 36
        foreach ($rules as $rawRule) {
103 36
            [$rule, $parameters] = $this->validator->parseRule($rawRule);
0 ignored issues
show
The variable $rule does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $parameters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
104 36
            [$jsAttribute, $jsRule, $jsParams] = $this->rules->getRule($attribute, $rule, $parameters, $rawRule);
0 ignored issues
show
The variable $jsAttribute does not exist. Did you mean $attribute?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $jsRule does not exist. Did you mean $jsRules?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $jsParams does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
105 36
            if ($this->isValidatable($jsRule, $includeRemote)) {
0 ignored issues
show
The variable $jsRule does not exist. Did you mean $jsRules?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
106 24
                $jsRules[$jsAttribute][$jsRule][] = [
0 ignored issues
show
The variable $jsAttribute does not exist. Did you mean $attribute?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $jsRule does not exist. Did you mean $jsRules?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
107 24
                    $rule,
108 27
                    $jsParams,
109
                    $this->messages->getMessage($attribute, $rule, $parameters),
110
                    $this->validator->isImplicit($rule),
111
                    $jsAttribute,
0 ignored issues
show
The variable $jsAttribute does not exist. Did you mean $attribute?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
112
                ];
113 36
            }
114
        }
115
116
        return $jsRules;
117
    }
118
119
    /**
120
     * Check if rule should be validated with javascript.
121
     *
122
     * @param $jsRule
123 36
     * @param bool $includeRemote
124
     * @return bool
125 36
     */
126
    protected function isValidatable($jsRule, $includeRemote)
127
    {
128
        return $jsRule && ($includeRemote || $jsRule !== RuleParser::REMOTE_RULE);
129
    }
130
131
    /**
132
     * Check if JS Validation is disabled for attribute.
133
     *
134 48
     * @param $attribute
135
     * @return bool
136 48
     */
137
    public function jsValidationEnabled($attribute)
138
    {
139
        return ! $this->validator->hasRule($attribute, self::JSVALIDATION_DISABLE);
140
    }
141
142
    /**
143
     * Returns view data to render javascript.
144 48
     *
145
     * @return array
146 48
     */
147 48
    public function validationData()
148
    {
149
        $jsMessages = [];
150 48
        $jsValidations = $this->generateJavascriptValidations();
151 48
152
        return [
153
            'rules' => $jsValidations,
154
            'messages' => $jsMessages,
155
        ];
156
    }
157
158
    /**
159
     * Validate Conditional Validations using Ajax in specified fields.
160
     *
161
     * @param string $attribute
162 12
     * @param string|array $rules
163
     * @return void
164
     */
165
    public function sometimes($attribute, $rules = [])
166 12
    {
167 12
        $callback = function () {
168 12
            return true;
169 12
        };
170
        $this->validator->sometimes($attribute, $rules, $callback);
171
        $this->rules->addConditionalRules($attribute, (array) $rules);
172
    }
173
}
174