Issues (118)

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/Lodash/Eloquent/ManyToManyPreload.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 Longman\LaravelLodash\Eloquent;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
10
use function array_unshift;
11
use function explode;
12
use function str_replace;
13
14
/**
15
 * @method $this limitPerGroupViaSubQuery(int $limit = 10)
16
 * @method $this limitPerGroupViaUnion(int $limit = 10, array $pivotColumns = [])
17
 */
18
trait ManyToManyPreload
19
{
20
    /**
21
     * Get the table associated with the model.
22
     *
23
     * @return string
24
     */
25
    abstract public function getTable();
26
27
    /**
28
     * Get the database connection for the model.
29
     *
30
     * @return \Illuminate\Database\Connection
31
     */
32
    abstract public function getConnection();
33
34
    /**
35
     * Get a new query builder that doesn't have any global scopes.
36
     *
37
     * @return \Illuminate\Database\Eloquent\Builder|static
38
     */
39
    abstract public function newQueryWithoutScopes();
40
41
    public function scopeLimitPerGroupViaSubQuery(Builder $query, int $limit = 10): Model
42
    {
43
        $table = $this->getTable();
44
        $queryKeyColumn = $query->getQuery()->wheres[0]['column'];
45
        $join = $query->getQuery()->joins;
46
        $newQuery = $this->newQueryWithoutScopes();
47
        $connection = $this->getConnection();
48
49
        // Initialize MySQL variables inline
50
        $newQuery->from($connection->raw('(select @num:=0, @group:=0) as `vars`, ' . $this->quoteColumn($table)));
51
52
        // If no columns already selected, let's select *
53
        if (! $query->getQuery()->columns) {
54
            $newQuery->select($table . '.*');
55
        }
56
57
        // Make sure column aliases are unique
58
        $groupAlias = $table . '_grp';
59
        $numAlias = $table . '_rn';
60
61
        // Apply mysql variables
62
        $newQuery->addSelect($connection->raw(
63
            "@num := if(@group = {$this->quoteColumn($queryKeyColumn)}, @num+1, 1) as `{$numAlias}`, @group := {$this->quoteColumn($queryKeyColumn)} as `{$groupAlias}`",
64
        ));
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
65
66
        // Make sure first order clause is the group order
67
        $newQuery->getQuery()->orders = (array) $query->getQuery()->orders;
68
        array_unshift($newQuery->getQuery()->orders, [
69
            'column'    => $queryKeyColumn,
70
            'direction' => 'asc',
71
        ]);
72
73
        if ($join) {
74
            $leftKey = explode('.', $queryKeyColumn)[1];
75
            $leftKeyColumn = "`{$table}`.`{$leftKey}`";
76
            $newQuery->addSelect($queryKeyColumn);
77
            $newQuery->mergeBindings($query->getQuery());
78
            $newQuery->getQuery()->joins = (array) $query->getQuery()->joins;
79
            $query->whereRaw("{$leftKeyColumn} = {$this->quoteColumn($queryKeyColumn)}");
80
        }
81
82
        $query->from($connection->raw("({$newQuery->toSql()}) as `{$table}`"))
83
            ->where($numAlias, '<=', $limit);
84
85
        return $this;
86
    }
87
88
    public function scopeLimitPerGroupViaUnion(Builder $query, int $limit = 10, array $pivotColumns = []): Model
89
    {
90
        $table = $this->getTable();
91
        $queryKeyColumn = $query->getQuery()->wheres[0]['column'];
92
        $joins = $query->getQuery()->joins;
93
        $connection = $this->getConnection();
94
95
        $queryKeyValues = $query->getQuery()->wheres[0]['values'];
96
        $pivotTable = explode('.', $queryKeyColumn)[0];
97
98
        $joinLeftColumn = $joins[0]->wheres[0]['first'];
99
        $joinRightColumn = $joins[0]->wheres[0]['second'];
100
        $joinOperator = $joins[0]->wheres[0]['operator'];
101
102
        // Remove extra wheres
103
        $wheres = $query->getQuery()->wheres;
104
        $bindings = $query->getQuery()->bindings;
105
        foreach ($wheres as $key => $where) {
106
            if (! isset($where['column']) || $where['column'] !== $queryKeyColumn) {
107
                continue;
108
            }
109
110
            //$count = count($where['values']);
111
            unset($wheres[$key]);
112
        }
113
        $groups = $query->getQuery()->groups;
114
        $orders = $query->getQuery()->orders;
115
116
        foreach ($queryKeyValues as $value) {
117
            if (! isset($unionQuery1)) {
118
                $unionQuery1 = $connection->table($pivotTable)
119
                    ->select([$table . '.*'])
120
                    ->join($table, $joinLeftColumn, $joinOperator, $joinRightColumn)
121
                    ->where($queryKeyColumn, '=', $value)
122
                    ->limit($limit);
123
                if (! empty($groups)) {
124
                    foreach ($groups as $group) {
125
                        $unionQuery1->groupBy($group);
126
                    }
127
                }
128
129
                if (! empty($orders)) {
130
                    foreach ($orders as $order) {
131
                        $unionQuery1->orderBy($order['column'], $order['direction']);
132
                    }
133
                }
134
135
                // Merge wheres
136
                $unionQuery1->mergeWheres($wheres, $bindings);
137
            } else {
138
                $select = [
139
                    $table . '.*',
140
                ];
141
142
                foreach ($pivotColumns as $pivotColumn) {
143
                    $select[] = $pivotTable . '.' . $pivotColumn . ' as pivot_' . $pivotColumn;
144
                }
145
146
                $unionQuery2 = $connection->table($pivotTable)
147
                    ->select($select)
148
                    ->join($table, $joinLeftColumn, $joinOperator, $joinRightColumn)
149
                    ->where($queryKeyColumn, '=', $value)
150
                    ->limit($limit);
151
                if (! empty($groups)) {
152
                    foreach ($groups as $group) {
153
                        $unionQuery2->groupBy($group);
154
                    }
155
                }
156
157
                if (! empty($orders)) {
158
                    foreach ($orders as $order) {
159
                        $unionQuery2->orderBy($order['column'], $order['direction']);
160
                    }
161
                }
162
163
                // Merge wheres
164
                $unionQuery2->mergeWheres($wheres, $bindings);
165
166
                $unionQuery1->unionAll($unionQuery2);
167
            }
168
        }
169
170
        if (! isset($unionQuery1)) {
171
            throw new InvalidArgumentException('Union query does not found');
172
        }
173
174
        $query->setQuery($unionQuery1);
175
176
        return $this;
177
    }
178
179
    private function quoteColumn(string $column): string
180
    {
181
182
        return '`' . str_replace('.', '`.`', $column) . '`';
183
    }
184
}
185