Issues (5)

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/Concerns/MapsIdentity.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 /** @noinspection PhpUndefinedMethodInspection */
2
3
namespace Sprocketbox\Eloquent\Identity\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
use Illuminate\Support\Facades\Date;
9
use Laravel\Nova\Fields\BelongsToMany;
10
use LogicException;
11
use Sprocketbox\Eloquent\Identity\Facades\Identity;
12
use Sprocketbox\Eloquent\Identity\ModelIdentity;
13
use Sprocketbox\Eloquent\Identity\Query\Builder;
14
15
/**
16
 * MapsIdentity
17
 *
18
 * This trait provides identity map functionality for Eloquent models.
19
 *
20
 * @package Sprocketbox\Eloquent\Identity\Concerns
21
 */
22
trait MapsIdentity
23
{
24
    /**
25
     * Boots the trait.
26
     */
27
    public static function bootMapsIdentity(): void
28
    {
29
        // Add a deleted event so the identity is removed from the map
30
        static::deleted(fn(Model $model) => Identity::removeIdentity($model->getModelIdentity()));
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting ',' or ')'
Loading history...
31
        // Add a created event so newly created models are stored
32
        static::created(fn(Model $model) => Identity::storeIdentity($model->getModelIdentity(), $model));
33
    }
34
35
    /**
36
     * Override the default newFromBuilder method to use the identity map.
37
     *
38
     * @see          \Illuminate\Database\Eloquent\Model::newFromBuilder()
39
     *
40
     * @param array $attributes
41
     * @param null  $connection
42
     *
43
     * @return \Illuminate\Database\Eloquent\Model
44
     * @noinspection PhpIncompatibleReturnTypeInspection
45
     */
46
    public function newFromBuilder($attributes = [], $connection = null): Model
47
    {
48
        $attributes = (array) $attributes;
49
        $key        = $attributes[$this->getKeyName()] ?? null;
50
        $identity   = $model = null;
51
52
        if ($key !== null) {
53
            $identity = $this->getModelIdentity($key, $connection);
54
55
            if (Identity::hasIdentity($identity)) {
56
                $model = Identity::getIdentity($identity);
57
                /** @noinspection NullPointerExceptionInspection */
58
                $this->updateModelAttributes($model, $attributes);
59
60
                return $model;
61
            }
62
        }
63
64
        $model = parent::newFromBuilder($attributes, $connection);
65
66
        if ($identity !== null) {
67
            Identity::storeIdentity($model->getModelIdentity(), $model);
68
        }
69
70
        return $model;
71
    }
72
73
    /**
74
     * Get the model identity.
75
     *
76
     * @param null        $id
77
     * @param string|null $connection
78
     *
79
     * @return \Sprocketbox\Eloquent\Identity\ModelIdentity
80
     */
81
    public function getModelIdentity($id = null, ?string $connection = null): ModelIdentity
82
    {
83
        $connection = $connection ?? $this->getConnectionName() ?? static::getConnectionResolver()->getDefaultConnection();
84
85
        return new ModelIdentity(static::class, $id ?? $this->getKey(), $connection);
86
    }
87
88
    /**
89
     * Create a new Eloquent query builder for the model.
90
     *
91
     * @param \Illuminate\Database\Query\Builder $query
92
     *
93
     * @return \Illuminate\Database\Eloquent\Builder|\Sprocketbox\Eloquent\Identity\Query\Builder|static
94
     */
95
    public function newEloquentBuilder($query)
96
    {
97
        return new Builder($query);
98
    }
99
100
    /**
101
     * Change the original attributes to match the new attributes, and re-add the dirty records.
102
     *
103
     * @param \Illuminate\Database\Eloquent\Model $model
104
     * @param array                               $attributes
105
     */
106
    protected function updateModelAttributes(Model $model, array $attributes = []): void
107
    {
108
        if (! $this->areAttributesMoreRecent($model, $attributes)) {
109
            return;
110
        }
111
112
        $dirtyAttributes = $model->getDirty();
113
        $model->setRawAttributes($attributes, true);
114
        $model->setRawAttributes(array_merge($model->getAttributes(), $dirtyAttributes), false);
115
    }
116
117
    /**
118
     * Check if the provided attributes are newer.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Model $model
121
     * @param array                               $attributes
122
     *
123
     * @return bool
124
     */
125
    protected function areAttributesMoreRecent(Model $model, array $attributes): bool
126
    {
127
        if (! $this->usesTimestamps()) {
128
            return true;
129
        }
130
131
        $updatedAt = $attributes[$this->getUpdatedAtColumn()];
132
133
        if ($updatedAt !== null) {
134
            $format = $this->getDateFormat();
135
136
            if (is_numeric($updatedAt)) {
137
                $updatedAt = Date::createFromTimestamp($updatedAt);
138
            } else if (Date::hasFormat($updatedAt, $format)) {
139
                $updatedAt = Date::createFromFormat($format, $updatedAt);
140
            }
141
142
            return $model->getAttribute($this->getUpdatedAtColumn())->isBefore($updatedAt);
143
        }
144
145
        return true;
146
    }
147
148
    /**
149
     * Get a relationship value from a method.
150
     *
151
     * @param string $method
152
     *
153
     * @return mixed
154
     *
155
     * @throws \LogicException
156
     */
157
    protected function getRelationshipFromMethod($method)
158
    {
159
        $relation = $this->$method();
160
161
        if (! $relation instanceof Relation) {
162
            if (is_null($relation)) {
163
                throw new LogicException(sprintf(
164
                    '%s::%s must return a relationship instance, but "null" was returned. Was the "return" keyword used?', static::class, $method
165
                ));
166
            }
167
168
            throw new LogicException(sprintf(
169
                '%s::%s must return a relationship instance.', static::class, $method
170
            ));
171
        }
172
173
        return tap($this->getRelationshipResults($relation), function ($results) use ($method) {
174
            $this->setRelation($method, $results);
175
        });
176
    }
177
178
    protected function getRelationshipResults(Relation $relation)
179
    {
180
        if ($relation instanceof BelongsToMany) {
181
            return $relation->getResults();
182
        }
183
184
        if ($relation instanceof BelongsTo) {
185
            $related = $relation->getRelated();
186
187
            if (method_exists($related, 'getModelIdentity')) {
188
                $identity = $related->getModelIdentity($this->getAttribute($relation->getForeignKeyName()));
189
190
                if (Identity::hasIdentity($identity)) {
191
                    return Identity::getIdentity($identity);
192
                }
193
            }
194
        }
195
196
        return $relation->getResults();
197
    }
198
}