Issues (50)

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/QueryBuilder.php (3 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 Spatie\QueryBuilder;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Http\Request;
7
use Spatie\QueryBuilder\Concerns\AddsFieldsToQuery;
8
use Spatie\QueryBuilder\Concerns\AddsIncludesToQuery;
9
use Spatie\QueryBuilder\Concerns\AppendsAttributesToResults;
10
use Spatie\QueryBuilder\Concerns\FiltersQuery;
11
use Spatie\QueryBuilder\Concerns\SortsQuery;
12
13
class QueryBuilder extends Builder
14
{
15
    use FiltersQuery,
16
        SortsQuery,
17
        AddsIncludesToQuery,
18
        AddsFieldsToQuery,
19
        AppendsAttributesToResults;
20
21
    /** @var \Spatie\QueryBuilder\QueryBuilderRequest */
22
    protected $request;
23
24
    /**
25
     * QueryBuilder constructor.
26
     *
27
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
28
     * @param null|\Illuminate\Http\Request $request
29
     */
30
    public function __construct($builder, ?Request $request = null)
31
    {
32
        parent::__construct(clone $builder->getQuery());
33
34
        $this->initializeFromBuilder($builder);
0 ignored issues
show
It seems like $builder defined by parameter $builder on line 30 can also be of type object<Illuminate\Databa...ent\Relations\Relation>; however, Spatie\QueryBuilder\Quer...initializeFromBuilder() does only seem to accept object<Illuminate\Database\Eloquent\Builder>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
35
36
        $this->request = $request
37
            ? QueryBuilderRequest::fromRequest($request)
38
            : app(QueryBuilderRequest::class);
39
    }
40
41
    /**
42
     * Create a new QueryBuilder for a request and model.
43
     *
44
     * @param string|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $baseQuery Model class or base query builder
45
     * @param \Illuminate\Http\Request $request
46
     *
47
     * @return \Spatie\QueryBuilder\QueryBuilder
48
     */
49
    public static function for($baseQuery, ?Request $request = null): self
50
    {
51
        if (is_string($baseQuery)) {
52
            /** @var Builder $baseQuery */
53
            $baseQuery = $baseQuery::query();
54
        }
55
56
        return new static($baseQuery, $request ?? app(Request::class));
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function get($columns = ['*'])
63
    {
64
        $results = parent::get($columns);
65
66
        if ($this->request->appends()->isNotEmpty()) {
67
            $results = $this->addAppendsToResults($results);
0 ignored issues
show
It seems like $results can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Spatie\QueryBuilder\Conc...::addAppendsToResults() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
        }
69
70
        return $results;
71
    }
72
73
    /**
74
     * Add the model, scopes, eager loaded relationships, local macro's and onDelete callback
75
     * from the $builder to this query builder.
76
     *
77
     * @param \Illuminate\Database\Eloquent\Builder $builder
78
     */
79
    protected function initializeFromBuilder(Builder $builder)
80
    {
81
        $this
82
            ->setModel($builder->getModel())
0 ignored issues
show
It seems like $builder->getModel() targeting Illuminate\Database\Eloquent\Builder::getModel() can also be of type object<Illuminate\Database\Eloquent\Builder>; however, Illuminate\Database\Eloquent\Builder::setModel() does only seem to accept object<Illuminate\Database\Eloquent\Model>, 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...
83
            ->setEagerLoads($builder->getEagerLoads());
84
85
        $builder->macro('getProtected', function (Builder $builder, string $property) {
86
            return $builder->{$property};
87
        });
88
89
        $this->scopes = $builder->getProtected('scopes');
90
91
        $this->localMacros = $builder->getProtected('localMacros');
92
93
        $this->onDelete = $builder->getProtected('onDelete');
94
    }
95
}
96