Issues (5)

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.

src/Rule.php (1 issue)

Labels
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
declare(strict_types = 1);
4
5
namespace hanneskod\clean;
6
7
final class Rule implements ValidatorInterface
8
{
9
    private ?string $default;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
10
    private ?string $errorMsg;
11
12
    /**
13
     * @var array<callable> Pre match filters
14
     */
15
    private array $pre;
16
17
    /**
18
     * @var array<callable> Post match filters
19
     */
20
    private array $post;
21
22
    /**
23
     * @var array<callable>
24
     */
25
    private array $matchers;
26
27
    /**
28
     * @param array<callable> $pre
29
     * @param array<callable> $matchers
30
     * @param array<callable> $post
31
     */
32
    public function __construct(
33
        array $pre = [],
34
        array $matchers = [],
35
        array $post = [],
36
        ?string $default = null,
37
        ?string $errorMsg = null
38
    ) {
39
        $this->pre = $pre;
40
        $this->post = $post;
41
        $this->matchers = $matchers;
42
        $this->default = $default;
43
        $this->errorMsg = $errorMsg;
44
    }
45
46
    /**
47
     * Create a new Rule with one or more pre match filters
48
     *
49
     * A filter should take a raw value and return the filtered value.
50
     */
51
    public function pre(callable ...$pre): Rule
52
    {
53
        return new static([...$this->pre, ...$pre], $this->matchers, $this->post, $this->default, $this->errorMsg);
54
    }
55
56
    /**
57
     * Create a new Rule with one or more matchers
58
     *
59
     * A matcher should take a raw value and return true if value is a match
60
     * and false if it is not.
61
     */
62
    public function match(callable ...$match): Rule
63
    {
64
        return new static($this->pre, [...$this->matchers, ...$match], $this->post, $this->default, $this->errorMsg);
65
    }
66
67
    /**
68
     * Create a new Rule with a regular expression matcher
69
     *
70
     * $regexp should be a regular expression consumable by preg_match().
71
     */
72
    public function regexp(string $regexp): Rule
73
    {
74
        return $this->match('is_string', function (string $value) use ($regexp): bool {
75
            return (bool)preg_match($regexp, $value);
76
        });
77
    }
78
79
    /**
80
     * Create a new Rule with one or more post match filters
81
     *
82
     * A filter should take a raw value and return the filtered value.
83
     */
84
    public function post(callable ...$post): Rule
85
    {
86
        return new static($this->pre, $this->matchers, [...$this->post, ...$post], $this->default, $this->errorMsg);
87
    }
88
89
    /**
90
     * Create a new Rule with default value
91
     */
92
    public function def(string $default): Rule
93
    {
94
        return new static($this->pre, $this->matchers, $this->post, $default, $this->errorMsg);
95
    }
96
97
    /**
98
     * Create a new Rule with exception message
99
     *
100
     * %s is replaced with parent exception message
101
     */
102
    public function msg(string $errorMsg): Rule
103
    {
104
        return new static($this->pre, $this->matchers, $this->post, $this->default, $errorMsg);
105
    }
106
107
    public function applyTo($data): ResultInterface
108
    {
109
        try {
110
            if (is_null($data)) {
111
                if (!isset($this->default)) {
112
                    throw new Exception('value missing');
113
                }
114
115
                $data = $this->default;
116
            }
117
118
            foreach ($this->pre as $filter) {
119
                $data = $filter($data);
120
            }
121
122
            foreach ($this->matchers as $matcherId => $matcher) {
123
                if (!$matcher($data)) {
124
                    throw new Exception("matcher #$matcherId failed");
125
                }
126
            }
127
128
            foreach ($this->post as $filter) {
129
                $data = $filter($data);
130
            }
131
132
            return new Valid($data);
133
        } catch (\Throwable $e) {
134
            if (isset($this->errorMsg)) {
135
                $e = new Exception(sprintf($this->errorMsg, $e->getMessage()), 0, $e);
136
            }
137
138
            return new Invalid($e);
139
        }
140
    }
141
142
    public function validate($data)
143
    {
144
        return $this->applyTo($data)->getValidData();
145
    }
146
}
147