Issues (7)

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/Rules/Delimited.php (3 issues)

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
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Validator;
8
use Illuminate\Support\Str;
9
10
class Delimited implements Rule
11
{
12
    /** @var string|array|\Illuminate\Contracts\Validation\Rule */
13
    protected $rule;
14
15
    protected $minimum = null;
16
17
    protected $maximum = null;
18
19
    protected $allowDuplicates = false;
20
21
    protected $message = '';
22
23
    protected $separatedBy = ',';
24
25
    /** @var bool */
26
    protected $trimItems = true;
27
28
    /** @var string */
29
    protected $validationMessageWord = 'item';
30 66
31
    public function __construct($rule)
32 66
    {
33 66
        $this->rule = $rule;
34
    }
35 12
36
    public function min(int $minimum)
37 12
    {
38
        $this->minimum = $minimum;
39 12
40
        return $this;
41
    }
42 6
43
    public function max(int $maximum)
44 6
    {
45
        $this->maximum = $maximum;
46 6
47
        return $this;
48
    }
49 6
50
    public function allowDuplicates(bool $allowed = true)
51 6
    {
52
        $this->allowDuplicates = $allowed;
53 6
54
        return $this;
55
    }
56 6
57
    public function separatedBy(string $separator)
58 6
    {
59
        $this->separatedBy = $separator;
60 6
61
        return $this;
62
    }
63 6
64
    public function doNotTrimItems()
65 6
    {
66
        $this->trimItems = false;
67 6
68
        return true;
69
    }
70
71
    public function validationMessageWord(string $word)
72
    {
73
        $this->validationMessageWord = $word;
74
75
        return $this;
76
    }
77 66
78
    public function passes($attribute, $value)
79 66
    {
80 60
        if ($this->trimItems) {
81
            $value = trim($value);
82
        }
83 66
84
        $items = collect(explode($this->separatedBy, $value))
85 66
            ->filter(function ($item) {
86 66
                return strlen((string) $item) > 0;
87
            });
88 66
89 12
        if (! is_null($this->minimum)) {
90 6
            if ($items->count() < $this->minimum) {
91 6
                $this->message = __('validationRules::messages.delimited.min', [
0 ignored issues
show
Documentation Bug introduced by
It seems like __('validationRules::mes...ord, $items->count()))) can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
92 6
                    'min' => $this->minimum,
93 6
                    'actual' => $items->count(),
94
                    'item' => Str::plural($this->validationMessageWord, $items->count()),
95
                ]);
96 6
97
                return false;
98
            }
99
        }
100 66
101 6
        if (! is_null($this->maximum)) {
102 6
            if ($items->count() > $this->maximum) {
103 6
                $this->message = __('validationRules::messages.delimited.max', [
0 ignored issues
show
Documentation Bug introduced by
It seems like __('validationRules::mes...ord, $items->count()))) can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
104 6
                    'max' => $this->maximum,
105 6
                    'actual' => $items->count(),
106
                    'item' => Str::plural($this->validationMessageWord, $items->count()),
107
                ]);
108 6
109
                return false;
110
            }
111
        }
112 66
113
        if ($this->trimItems) {
114 60
            $items = $items->map(function (string $item) {
115 60
                return trim($item);
116
            });
117
        }
118 66
119 66
        foreach ($items as $item) {
120
            [$isValid, $validationMessage] = $this->validate($attribute, $item);
121 66
122 36
            if (! $isValid) {
123
                $this->message = $validationMessage;
124 36
125
                return false;
126
            }
127
        }
128 66
129 60
        if (! $this->allowDuplicates) {
130 6
            if ($items->unique()->count() !== $items->count()) {
131
                $this->message = __('validationRules::messages.delimited.unique');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('validationRules::messages.delimited.unique') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
132 6
133
                return false;
134
            }
135
        }
136 60
137
        return true;
138
    }
139
140
    public function message()
141
    {
142
        return $this->message;
143
    }
144 66
145
    protected function validate(string $attribute, string $item): array
146 66
    {
147
        $attribute = Arr::last(explode('.', $attribute));
148 66
149
        $validator = Validator::make([$attribute => $item], [$attribute => $this->rule]);
150
151 66
        return [
152 66
            $validator->passes(),
153
            $validator->getMessageBag()->first($attribute),
154
        ];
155
    }
156
}
157