Issues (3099)

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.

Constraints/PasswordRestrictionsValidatorTest.php (10 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 Kunstmaan\AdminBundle\Tests\Validator\Constraints;
4
5
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictions;
6
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictionsValidator;
7
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
8
9
/**
10
 * Class PasswordRestrictionsValidatorTest
11
 *
12
 * Unit test for the password restrictions validator, will test with different sets of parameters
13
 * for the validator is possible.
14
 */
15
class PasswordRestrictionsValidatorTest extends ConstraintValidatorTestCase
16
{
17
    const PARAMETER_MIN_LENGTH = 8;
18
    const PARAMETER_MAX_LENGTH = 16;
19
    const PARAMETER_MIN_DIGITS = 3;
20
    const PARAMETER_MIN_UPPERCASE = 2;
21
    const PARAMETER_MIN_SPECIAL_CHARACTERS = 1;
22
23
    /**
24
     * @return PasswordRestrictionsValidator
25
     */
26
    protected function createValidator()
27
    {
28
        return new PasswordRestrictionsValidator(self::PARAMETER_MIN_DIGITS, self::PARAMETER_MIN_UPPERCASE, self::PARAMETER_MIN_SPECIAL_CHARACTERS, self::PARAMETER_MIN_LENGTH, self::PARAMETER_MAX_LENGTH);
29
    }
30
31
    /**
32
     * Set a validator with a limited number of parameters to overrule the default one from createValidator.
33
     *
34
     * @param int $minDigits
35
     * @param int $minUppercase
36
     * @param int $minSpecialCharacters
37
     * @param int $minLength
38
     * @param int $maxLength
39
     */
40
    protected function setValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength)
41
    {
42
        $this->validator = new PasswordRestrictionsValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength);
43
        $this->validator->initialize($this->context);
44
    }
45
46
    /**
47
     * @param string      $password
48
     * @param string|null $message
49
     * @param array       $parameters
50
     * @param null        $code
51
     *
52
     * @dataProvider dataPasswordsWithAllParameters
53
     */
54
    public function testPasswordWithAllParametersSet($password, $message = null, array $parameters = [], $code = null)
55
    {
56
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
57
    }
58
59
    /**
60
     * @param string      $password
61
     * @param string|null $message
62
     * @param array       $parameters
63
     * @param null        $code
64
     *
65
     * @dataProvider dataPasswordsToShort
66
     */
67
    public function testPasswordToShortOnlySet($password, $message = null, array $parameters = [], $code = null)
68
    {
69
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, null);
70
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
71
    }
72
73
    /**
74
     * @param string      $password
75
     * @param string|null $message
76
     * @param array       $parameters
77
     * @param null        $code
78
     *
79
     * @dataProvider dataPasswordsToLong
80
     */
81
    public function testPasswordToLongOnlySet($password, $message = null, array $parameters = [], $code = null)
82
    {
83
        $this->setValidator(null, null, null, null, self::PARAMETER_MAX_LENGTH);
84
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
85
    }
86
87
    /**
88
     * @param string      $password
89
     * @param string|null $message
90
     * @param array       $parameters
91
     * @param null        $code
92
     *
93
     * @dataProvider dataPasswordsMinimumDigits
94
     */
95
    public function testPasswordMinimumDigitsOnlySet($password, $message = null, array $parameters = [], $code = null)
96
    {
97
        $this->setValidator(self::PARAMETER_MIN_DIGITS, null, null, null, null);
98
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
99
    }
100
101
    /**
102
     * @param string      $password
103
     * @param string|null $message
104
     * @param array       $parameters
105
     * @param null        $code
106
     *
107
     * @dataProvider dataPasswordsMinimumUppercase
108
     */
109
    public function testPasswordMinimumUppercaseOnlySet($password, $message = null, array $parameters = [], $code = null)
110
    {
111
        $this->setValidator(null, self::PARAMETER_MIN_UPPERCASE, null, null, null);
112
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
113
    }
114
115
    /**
116
     * @param string      $password
117
     * @param string|null $message
118
     * @param array       $parameters
119
     * @param null        $code
120
     *
121
     * @dataProvider dataPasswordsMinimumSpecialCharacters
122
     */
123
    public function testPasswordMinimumSpecialCharactersOnlySet($password, $message = null, array $parameters = [], $code = null)
124
    {
125
        $this->setValidator(null, null, self::PARAMETER_MIN_SPECIAL_CHARACTERS, null, null);
126
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
127
    }
128
129
    /**
130
     * @param string      $password
131
     * @param string|null $message
132
     * @param array       $parameters
133
     * @param null        $code
134
     *
135
     * @dataProvider dataPasswordsLengthRange
136
     */
137
    public function testPasswordLengthRangeSet($password, $message = null, array $parameters = [], $code = null)
138
    {
139
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, self::PARAMETER_MAX_LENGTH);
140
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
141
    }
142
143
    /**
144
     * Uses the set validator combined with data to assert.
145
     *
146
     * @param string      $password
147
     * @param string|null $message
148
     * @param array       $parameters
149
     * @param null        $code
150
     */
151
    private function buildAndTestPasswordRestrictions($password, $message = null, array $parameters = [], $code = null)
152
    {
153
        $constraint = new PasswordRestrictions();
154
155
        $this->validator->validate($password, $constraint);
156
157
        if ($message && $code) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $message of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
158
            $this->buildViolation($message)
159
                ->setCode($code)
160
                ->setParameters($parameters)
161
                ->assertRaised();
162
        } else {
163
            $this->assertNoViolation();
164
        }
165
    }
166
167
    /**
168
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
169
     */
170
    public function dataPasswordsWithAllParameters()
171
    {
172
        return [
173
            ['ABcdef789!'],
174
            ['AB123!q', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
175
            ['AB123!q541kkjhghvhb451', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
176
            ['abCDEFG*', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
177
            ['ab123efg!', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
178
            ['AB123efg', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
179
        ];
180
    }
181
182
    /**
183
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
184
     */
185
    public function dataPasswordsToShort()
186
    {
187
        return [
188
            ['password'],
189
            ['ABC?123', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
190
            ['admin', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
191
        ];
192
    }
193
194
    /**
195
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
196
     */
197
    public function dataPasswordsToLong()
198
    {
199
        return [
200
            ['correct!'],
201
            ['thispasswordistolong', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
202
        ];
203
    }
204
205
    /**
206
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
207
     */
208
    public function dataPasswordsMinimumDigits()
209
    {
210
        return [
211
            ['withdigits123'],
212
            ['nodigits', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
213
        ];
214
    }
215
216
    /**
217
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
218
     */
219 View Code Duplication
    public function dataPasswordsMinimumUppercase()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221
        return [
222
            ['PassworD'],
223
            ['password', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
224
        ];
225
    }
226
227
    /**
228
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
229
     */
230 View Code Duplication
    public function dataPasswordsMinimumSpecialCharacters()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232
        return [
233
            ['password!'],
234
            ['password', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
235
        ];
236
    }
237
238
    /**
239
     * Combine the data of the too long and too short test for an extra length
240
     * range test.
241
     *
242
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
243
     */
244
    public function dataPasswordsLengthRange()
245
    {
246
        return $this->dataPasswordsToLong() + $this->dataPasswordsToShort();
247
    }
248
}
249