Issues (4)

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.

Constraints/CsvValidator.php (1 issue)

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 Toa\Component\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\Constraints\FileValidator;
7
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
8
use Toa\Component\Validator\Provider\CsvProviderInterface;
9
10
/**
11
 * Class CsvValidator
12
 *
13
 * @author Enrico Thies <[email protected]>
14
 */
15
class CsvValidator extends FileValidator
16
{
17
    /** @var ProviderInterface */
18
    protected $provider;
19
20
    /**
21
     * @param CsvProviderInterface $provider
22
     */
23
    public function __construct(CsvProviderInterface $provider)
24
    {
25
        $this->provider = $provider;
0 ignored issues
show
Documentation Bug introduced by
It seems like $provider of type object<Toa\Component\Val...r\CsvProviderInterface> is incompatible with the declared type object<Toa\Component\Val...ints\ProviderInterface> of property $provider.

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...
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function validate($value, Constraint $constraint)
32
    {
33
        $violations = count($this->context->getViolations());
34
35
        parent::validate($value, $constraint);
36
37
        $failed = count($this->context->getViolations()) !== $violations;
38
39
        if ($failed || null === $value || '' === $value) {
40
            return;
41
        }
42
43
        $path = $value instanceof \SplFileInfo ? $value->getPathname() : (string) $value;
44
45
        $this->validateDelimiter($constraint);
46
        $this->validateEnclosure($constraint);
47
        $this->validateEscape($constraint);
48
49
        $config = array(
50
            'delimiter' => $constraint->delimiter,
51
            'enclosure' => $constraint->enclosure,
52
            'escape'    => $constraint->escape,
53
        );
54
55
        $rows = $this->provider->countRows($path, $config);
56
57
        if ($this->validateMaxRows($rows, $constraint)) {
58
            return;
59
        }
60
61
        if ($this->validateMinRows($rows, $constraint)) {
62
            return;
63
        }
64
65
        $columnsSizes = $this->provider->collectColumnSizes($path, $config);
66
67
        if ($this->validateEmptyColumns($columnsSizes, $constraint)) {
68
            return;
69
        }
70
71
        if ($this->validateMaxColumns($columnsSizes, $constraint)) {
72
            return;
73
        }
74
75
        if ($this->validateMinColumns($columnsSizes, $constraint)) {
76
            return;
77
        }
78
    }
79
80
    /**
81
     * @param Constraint $constraint
82
     *
83
     * @throws ConstraintDefinitionException
84
     */
85
    protected function validateDelimiter(Constraint $constraint)
86
    {
87
        if (strlen($constraint->delimiter) > 2) {
88
            throw new ConstraintDefinitionException(sprintf('"%s" is not a valid delimiter', $constraint->delimiter));
89
        }
90
    }
91
92
    /**
93
     * @param Constraint $constraint
94
     *
95
     * @throws ConstraintDefinitionException
96
     */
97
    protected function validateEnclosure(Constraint $constraint)
98
    {
99
        if (strlen($constraint->enclosure) > 2) {
100
            throw new ConstraintDefinitionException(sprintf('"%s" is not a valid enclosure', $constraint->enclosure));
101
        }
102
    }
103
104
    /**
105
     * @param Constraint $constraint
106
     *
107
     * @throws ConstraintDefinitionException
108
     */
109
    protected function validateEscape(Constraint $constraint)
110
    {
111
        if (strlen($constraint->escape) > 2) {
112
            throw new ConstraintDefinitionException(sprintf('"%s" is not a valid escape', $constraint->escape));
113
        }
114
    }
115
116
    /**
117
     * @param array      $columnsSizes
118
     * @param Constraint $constraint
119
     *
120
     * @throws ConstraintDefinitionException
121
     * @return boolean
122
     */
123
    protected function validateEmptyColumns($columnsSizes, Constraint $constraint)
124
    {
125
        if (!$constraint->ignoreEmptyColumns && isset($columnsSizes[0])) {
126
            $this->context->addViolation(
127
                $constraint->emptyColumnsMessage,
128
                array(
129
                    '{{ occurrences }}' => implode(',', $columnsSizes[0])
130
                )
131
            );
132
133
            return true;
134
        }
135
    }
136
137
    /**
138
     * @param array      $columnsSizes
139
     * @param Constraint $constraint
140
     *
141
     * @throws ConstraintDefinitionException
142
     * @return boolean
143
     */
144
    protected function validateMaxColumns($columnsSizes, Constraint $constraint)
145
    {
146
        if (null !== $constraint->maxColumns) {
147
            if (!ctype_digit((string) $constraint->maxColumns)) {
148
                throw new ConstraintDefinitionException(
149
                    sprintf(
150
                        '"%s" is not a valid column size',
151
                        $constraint->maxColumns
152
                    )
153
                );
154
            }
155
156
            foreach ($columnsSizes as $size => $occurrences) {
157
                if ($size > $constraint->maxColumns) {
158
                    $this->context->addViolation(
159
                        $constraint->maxColumnsMessage,
160
                        array(
161
                            '{{ max_columns }}' => $constraint->maxColumns,
162
                            '{{ occurrences }}' => implode(',', $occurrences)
163
                        )
164
                    );
165
166
                    return true;
167
                }
168
            }
169
        }
170
    }
171
172
    /**
173
     * @param array      $columnsSizes
174
     * @param Constraint $constraint
175
     *
176
     * @throws ConstraintDefinitionException
177
     * @return boolean
178
     */
179
    protected function validateMinColumns($columnsSizes, Constraint $constraint)
180
    {
181
        if (null !== $constraint->minColumns) {
182
            if (!ctype_digit((string) $constraint->minColumns)) {
183
                throw new ConstraintDefinitionException(
184
                    sprintf(
185
                        '"%s" is not a valid column size',
186
                        $constraint->minColumns
187
                    )
188
                );
189
            }
190
191
            foreach ($columnsSizes as $size => $occurrences) {
192
                if (0 !== $size && $size < $constraint->minColumns) {
193
                    $this->context->addViolation(
194
                        $constraint->minColumnsMessage,
195
                        array(
196
                            '{{ min_columns }}' => $constraint->minColumns,
197
                            '{{ occurrences }}' => implode(',', $occurrences)
198
                        )
199
                    );
200
201
                    return true;
202
                }
203
            }
204
        }
205
    }
206
207
    /**
208
     * @param integer    $rows
209
     * @param Constraint $constraint
210
     *
211
     * @throws ConstraintDefinitionException
212
     * @return boolean
213
     */
214
    protected function validateMaxRows($rows, Constraint $constraint)
215
    {
216
        if (null !== $constraint->maxRows) {
217
            if (!ctype_digit((string) $constraint->maxRows)) {
218
                throw new ConstraintDefinitionException(
219
                    sprintf(
220
                        '"%s" is not a valid row size',
221
                        $constraint->maxRows
222
                    )
223
                );
224
            }
225
226
            if ($rows > $constraint->maxRows) {
227
                $this->context->addViolation(
228
                    $constraint->maxRowsMessage,
229
                    array(
230
                        '{{ max_rows }}' => $constraint->maxRows,
231
                    )
232
                );
233
234
                return true;
235
            }
236
        }
237
    }
238
239
    /**
240
     * @param integer    $rows
241
     * @param Constraint $constraint
242
     *
243
     * @throws ConstraintDefinitionException
244
     * @return boolean
245
     */
246
    protected function validateminRows($rows, Constraint $constraint)
247
    {
248
        if (null !== $constraint->minRows) {
249
            if (!ctype_digit((string) $constraint->minRows)) {
250
                throw new ConstraintDefinitionException(
251
                    sprintf(
252
                        '"%s" is not a valid row size',
253
                        $constraint->minRows
254
                    )
255
                );
256
            }
257
258
            if ($rows < $constraint->minRows) {
259
                $this->context->addViolation(
260
                    $constraint->minRowsMessage,
261
                    array(
262
                        '{{ min_rows }}' => $constraint->minRows,
263
                    )
264
                );
265
266
                return true;
267
            }
268
        }
269
    }
270
}
271