Issues (13)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/Pagerfanta/ExtendedPdoAdapter.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule\Pagerfanta;
6
7
use Aura\Sql\ExtendedPdoInterface;
8
use Pagerfanta\Adapter\AdapterInterface;
9
10
use function assert;
11
use function count;
12
use function is_int;
13
use function preg_match;
14
use function preg_replace;
15
use function preg_split;
16
use function strpos;
17
use function strtolower;
18
use function trim;
19
20
use const PHP_EOL;
21
22
/**
23
 * @template T
24
 * @implements AdapterInterface<T>
25
 */
26
class ExtendedPdoAdapter implements AdapterInterface
27
{
28
    private ExtendedPdoInterface $pdo;
29
    private string $sql;
30
31
    /** @var array<mixed> */
32
    private array $params;
33
    private FetcherInterface $fetcher;
34
35
    /**
36
     * @param array<mixed> $params
37
     */
38
    public function __construct(ExtendedPdoInterface $pdo, string $sql, array $params, ?FetcherInterface $fetcher = null)
39
    {
40
        $this->pdo = $pdo;
41
        $this->sql = $sql;
42
        $this->params = $params;
43
        $this->fetcher = $fetcher ?? new FetchAssoc($pdo);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @SuppressWarnings(PHPMD.GotoStatement)
50
     */
51
    public function getNbResults(): int
52
    {
53
        // be smart and try to guess the total number of records
54
        $countQuery = $this->rewriteCountQuery($this->sql);
55
        if (! $countQuery) {
56
            // GROUP BY => fetch the whole result set and count the rows returned
57
            $result = $this->pdo->perform($this->sql, $this->params)->fetchAll();
58
            $count = ! $result ? 0 : count($result);
59
            goto ret;
60
        }
61
62
        if ($this->params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
            $count = $this->pdo->fetchValue($countQuery, $this->params);
64
            goto ret;
65
        }
66
67
        $count = $this->pdo->fetchValue($countQuery);
68
        ret:
69
        /** @var string $count */
70
        $nbResult = ! $count ? 0 : (int) $count;
71
        assert(is_int($nbResult));
72
        assert($nbResult >= 0);
73
74
        return $nbResult;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     *
80
     * @param int $offset
81
     * @param int $length
82
     *
83
     * @return array<mixed>
84
     */
85
    public function getSlice(int $offset, int $length): iterable
86
    {
87
        $sql = $this->sql . $this->getLimitClause($offset, $length);
88
        $result = ($this->fetcher)($sql, $this->params);
89
90
        return ! $result ? [] : $result;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getLimitClause(int $offset, int $length): string
97
    {
98
        if ($offset && $length) {
99
            return PHP_EOL . "LIMIT {$length} OFFSET {$offset}";
100
        }
101
102
        if ($length) {
103
            return PHP_EOL . "LIMIT {$length}";
104
        }
105
106
        return '';
107
    }
108
109
    /**
110
     * Return count query
111
     *
112
     * @param string $query
113
     *
114
     * @return string
115
     *
116
     * @see https://github.com/pear/Pager/blob/master/examples/Pager_Wrapper.php
117
     * Taken from pear/pager and modified.
118
     * tested at https://github.com/pear/Pager/blob/80c0e31c8b94f913cfbdeccbe83b63822f42a2f8/tests/pager_wrapper_test.php#L19
119
     * @codeCoverageIgnore
120
     */
121
    public function rewriteCountQuery($query)
122
    {
123
        if (is_int(strpos(strtolower($query), 'union'))) {
0 ignored issues
show
The condition is_int(strpos(strtolower($query), 'union')) is always true.
Loading history...
124
            return '';
125
        }
126
127
        if (preg_match('/^\s*SELECT\s+\bDISTINCT\b/is', $query) || preg_match('/\s+GROUP\s+BY\s+/is', $query)) {
128
            return '';
129
        }
130
131
        $openParenthesis = '(?:\()';
132
        $closeParenthesis = '(?:\))';
133
        $subQueryInSelect = $openParenthesis . '.*\bFROM\b.*' . $closeParenthesis;
134
        $pattern = '/(?:.*' . $subQueryInSelect . '.*)\bFROM\b\s+/Uims';
135
        if (preg_match($pattern, $query)) {
136
            return '';
137
        }
138
139
        $subQueryWithLimitOrder = $openParenthesis . '.*\b(LIMIT|ORDER)\b.*' . $closeParenthesis;
140
        $pattern = '/.*\bFROM\b.*(?:.*' . $subQueryWithLimitOrder . '.*).*/Uims';
141
        if (preg_match($pattern, $query)) {
142
            return '';
143
        }
144
145
        $queryCount = preg_replace('/(?:.*)\bFROM\b\s+/Uims', 'SELECT COUNT(*) FROM ', $query, 1);
146
        /** @var array<int> $split */
147
        $split = preg_split('/\s+ORDER\s+BY\s+/is', (string) $queryCount);
148
        [$queryCount] = $split;
149
        /** @var array<int> $split2 */
150
        $split2 = preg_split('/\bLIMIT\b/is', (string) $queryCount);
151
        [$queryCount2] = $split2;
152
153
        return trim((string) $queryCount2);
154
    }
155
}
156