Issues (28)

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/Raw/PdoClient.php (2 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 Hgraca\MicroDbal\Raw;
4
5
use Exception;
6
use Hgraca\Helper\ArrayHelper;
7
use Hgraca\MicroDbal\Raw\Exception\BindingException;
8
use Hgraca\MicroDbal\Raw\Exception\ExecutionException;
9
use Hgraca\MicroDbal\Raw\Exception\TypeResolutionException;
10
use Hgraca\MicroDbal\RawClientInterface;
11
use PDO;
12
use PDOStatement;
13
14
final class PdoClient implements RawClientInterface
15
{
16
    /** @var PDO */
17
    private $pdo;
18
19 17
    public function __construct(PDO $pdo)
20
    {
21 17
        $this->pdo = $pdo;
22 17
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
23 17
    }
24
25 13
    public function executeQuery(string $sql, array $bindingsList = []): array
26
    {
27 13
        return $this->execute($sql, $bindingsList)->fetchAll(PDO::FETCH_ASSOC);
28
    }
29
30 9
    public function executeCommand(string $sql, array $bindingsList)
31
    {
32 9
        if (!ArrayHelper::isTwoDimensional($bindingsList)) {
33 8
            $bindingsList = [$bindingsList];
34
        }
35
36
        try {
37 9
            $this->pdo->beginTransaction();
38 9
            foreach ($bindingsList as $bindings) {
39 9
                $preparedStatement = $this->execute($sql, $bindings, $preparedStatement ?? null);
40
            }
41 6
            $this->pdo->commit();
42 3
        } catch (Exception $e) {
43 3
            $this->pdo->rollBack();
44 3
            throw $e;
45
        }
46 6
    }
47
48
    /**
49
     * @throws ExecutionException
50
     */
51 17
    private function execute(string $sql, array $bindings = [], PDOStatement $preparedStatement = null): PDOStatement
52
    {
53 17
        $preparedStatement = $preparedStatement ?? $this->pdo->prepare($sql);
54
55 17
        $this->bindParameterList($preparedStatement, $bindings);
56
57 14
        if (!$preparedStatement->execute()) {
58 2
            throw new ExecutionException(
59 2
                "Could not execute query: '$sql'"
60 2
                . ' Error code: ' . $preparedStatement->errorCode()
61 2
                . ' Error Info: ' . json_encode($preparedStatement->errorInfo())
62
            );
63
        }
64
65 12
        return $preparedStatement;
66
    }
67
68 16
    private function bindParameterList(PDOStatement $stmt, array $parameterList)
69
    {
70 16
        foreach ($parameterList as $name => $value) {
71 12
            $this->bindParameter($stmt, $name, $value);
72
        }
73 13
    }
74
75
    /**
76
     * @throws BindingException
77
     */
78 12
    private function bindParameter(PDOStatement $stmt, string $name, $value)
79
    {
80 12
        $pdoType = $this->resolvePdoType($value);
81 11
        $bound = $stmt->bindValue(
82
            $name,
83 11
            $pdoType === PDO::PARAM_STR ? strval($value) : $value,
84
            $pdoType
85
        );
86
87 11
        if (false === $bound) {
88 2
            throw new BindingException(
89 2
                'Could not bind value: ' . json_encode(['name' => $name, 'value' => $value, 'type' => $pdoType])
90
            );
91
        }
92 9
    }
93
94
    /**
95
     * @param mixed $value
96
     *
97
     * @throws TypeResolutionException
98
     */
99 12
    private function resolvePdoType($value): int
100
    {
101 12
        $type = gettype($value);
102
        switch ($type) {
103 12
            case 'boolean':
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
104 5
                $pdoType = PDO::PARAM_BOOL; // 5
105 5
                break;
106 8
            case 'string':
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
107 5
            case 'double': // float
108 7
                $pdoType = PDO::PARAM_STR; // 2
109 7
                break;
110 5
            case 'integer':
111 1
                $pdoType = PDO::PARAM_INT; // 1
112 1
                break;
113 5
            case 'NULL':
114 4
                $pdoType = PDO::PARAM_NULL; // 0
115 4
                break;
116 1
            case 'object':
117 1
                $class = get_class($value);
118 1
                throw new TypeResolutionException("Invalid type '$class' for query filter.");
119
            default:
120
                throw new TypeResolutionException("Invalid type '$type' for query filter.");
121
        }
122
123 11
        return $pdoType;
124
    }
125
}
126