Issues (24)

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/Model/Adapter/PDO/Statement.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 BenTools\SimpleDBAL\Model\Adapter\PDO;
4
5
use BenTools\SimpleDBAL\Contract\AdapterInterface;
6
use BenTools\SimpleDBAL\Contract\StatementInterface;
7
use BenTools\SimpleDBAL\Contract\ResultInterface;
8
use BenTools\SimpleDBAL\Model\StatementTrait;
9
use PDO;
10
use PDOStatement;
11
12
class Statement implements StatementInterface
13
{
14
    use StatementTrait;
15
16
    /**
17
     * @var PDOAdapter
18
     */
19
    private $connection;
20
21
    /**
22
     * @var PDOStatement
23
     */
24
    private $stmt;
25
26
    /**
27
     * PDOStatement constructor.
28
     * @param PDOAdapter $connection
29
     * @param PDOStatement $statement
30
     * @param array $values
31
     */
32
    public function __construct(PDOAdapter $connection, PDOStatement $statement, array $values = null)
33
    {
34
        $this->connection = $connection;
35
        $this->stmt       = $statement;
36
        $this->values     = $values;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    final public function getConnection(): AdapterInterface
43
    {
44
        return $this->connection;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function getQueryString(): string
51
    {
52
        return $this->stmt->queryString;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     * @return PDOStatement
58
     */
59
    public function getWrappedStatement()
60
    {
61
        return $this->stmt;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function bind(): void
68
    {
69
        if ($this->hasValues()) {
70
            if (0 === array_keys($this->getValues())[0]) {
71
                $this->bindNumericParameters();
72
            } else {
73
                $this->bindNamedParameters();
74
            }
75
        }
76
    }
77
78
    /**
79
     * Bind :named parameters
80
     */
81
    protected function bindNamedParameters(): void
82
    {
83
        foreach ($this->getValues() as $key => $value) {
84
            $value = $this->toScalar($value);
85
            $this->getWrappedStatement()->bindValue(sprintf(':%s', $key), $value, $this->getPdoType($value));
86
        }
87
    }
88
89
    /**
90
     * Bind ? parameters
91
     */
92
    protected function bindNumericParameters(): void
93
    {
94
        $values = $this->getValues();
95
        $cnt    = count($values);
96
        for ($index0 = 0, $index1 = 1; $index0 < $cnt; $index0++, $index1++) {
97
            $value = $this->toScalar($values[$index0]);
98
            $this->getWrappedStatement()->bindValue($index1, $value, $this->getPdoType($value));
99
        }
100
    }
101
102
    /**
103
     * Attempt to convert non-scalar values.
104
     *
105
     * @param $value
106
     * @return string
107
     */
108
    protected function toScalar($value)
109
    {
110
        if (is_scalar($value) || null === $value) {
111
            return $value;
112
        } else {
113
            if (is_object($value)) {
114
                if (is_callable([$value, '__toString'])) {
115
                    return (string) $value;
116
                } elseif ($value instanceof \DateTimeInterface) {
117
                    return $value->format('Y-m-d H:i:s');
118
                } else {
119
                    throw new \InvalidArgumentException(sprintf("Cast of class %s is impossible", get_class($value)));
120
                }
121
            } else {
122
                throw new \InvalidArgumentException(sprintf("Cast of type %s is impossible", gettype($value)));
123
            }
124
        }
125
    }
126
127
    /**
128
     * @param $var
129
     * @return int
130
     */
131
    protected function getPdoType($value)
132
    {
133
        if (!is_scalar($value) && null !== $value) {
134
            throw new \InvalidArgumentException("Can only cast scalar variables.");
135
        }
136
        switch (strtolower(gettype($value))) :
137
            case 'integer':
138
                return PDO::PARAM_INT;
139
            case 'boolean':
140
                return PDO::PARAM_BOOL;
141
            case 'NULL':
142
                return PDO::PARAM_NULL;
143
            case 'double':
144
            case 'string':
145
            default:
146
                return PDO::PARAM_STR;
147
        endswitch;
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function preview(): string
154
    {
155
        if (!$this->hasValues()) {
156
            return $this->stmt->queryString;
157
        }
158
159
        $escape = function ($value) {
160
            if (null === $value) {
161
                return 'NULL';
162
            }
163
            $value = $this->toScalar($value);
164
            $type = gettype($value);
165
            switch ($type) {
166
                case 'boolean':
167
                    return (int) $value;
168
                case 'double':
169
                case 'integer':
170
                    return $value;
171
                default:
172
                    return (string) "'" . addslashes($value) . "'";
173
            }
174
        };
175
176
        $keywords = [];
177
        $preview  = $this->stmt->queryString;
178
179
        # Case of question mark placeholders
180
        if ($this->hasAnonymousPlaceholders()) {
181
            foreach ($this->values as $value) {
182
                $preview = preg_replace("/([\?])/", $escape($value), $preview, 1);
183
            }
184
        } # Case of named placeholders
185
        else {
186
            foreach ($this->values as $key => $value) {
187
                if (!in_array($key, $keywords, true)) {
188
                    $keywords[] = $key;
189
                }
190
            }
191
            foreach ($keywords as $keyword) {
192
                $pattern = "/(\:\b" . $keyword . "\b)/i";
193
                $preview = preg_replace($pattern, $escape($this->values[$keyword]), $preview);
194
            }
195
        }
196
        return $preview;
197
    }
198
199
    /**
200
     * @inheritDoc
201
     */
202
    public function createResult(): ResultInterface
203
    {
204
        return new Result($this->getConnection()->getWrappedConnection(), $this->getWrappedStatement());
0 ignored issues
show
It seems like $this->getConnection()->getWrappedConnection() targeting BenTools\SimpleDBAL\Cont...:getWrappedConnection() can also be of type object<mysqli>; however, BenTools\SimpleDBAL\Mode...O\Result::__construct() does only seem to accept object<PDO>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
205
    }
206
207
    /**
208
     * @inheritDoc
209
     */
210
    public function __toString(): string
211
    {
212
        return $this->getQueryString();
213
    }
214
}
215