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/Http/Resources/JsonResource.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\Http\Resources;
6
7
use Exception;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\Resources\Json\JsonResource as BaseResource;
10
use Longman\LaravelLodash\Eloquent\UuidAsPrimaryContract;
11
use Longman\LaravelLodash\Http\Resources\Response\ResourceResponse;
12
use Longman\LaravelLodash\Support\Arr;
13
use ReflectionClass;
14
15
use function array_keys;
16
use function array_merge;
17
use function array_merge_recursive;
18
use function is_null;
19
use function method_exists;
20
use function ucfirst;
21
22
abstract class JsonResource extends BaseResource
23
{
24
    use TransformsData;
25
26
    protected array $includedRelations = [];
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
27
    protected string $dataWrapper = 'data';
28
    /**
29
     * Default resource type, if resource is not Model
30
     */
31
    protected string $resourceType = 'object';
32
33 3
    public function getTransformed(): array
34
    {
35 3
        if ($this->resource instanceof TransformableContract) {
36 3
            return $this->transformToApi($this->resource);
37
        }
38
39
        return [];
40
    }
41
42 6
    public function toArray($request): array
43
    {
44 6
        $data = $this->getResourceData();
45
46 6
        $relationsData = $this->getRelationsData();
47
48 6
        if (! empty($relationsData)) {
49
            $data['relationships'] = $relationsData;
50
        }
51
52 6
        if (! empty($this->getDataWrapper())) {
53 2
            return [$this->getDataWrapper() => $data];
54
        }
55
56 4
        return $data;
57
    }
58
59 6
    public function getDataWrapper(): string
60
    {
61 6
        return $this->dataWrapper;
62
    }
63
64 5
    public function setDataWrapper(string $dataWrapper): void
65
    {
66 5
        $this->dataWrapper = $dataWrapper;
67 5
    }
68
69
    public function withRelations(array $relations): self
70
    {
71
        $this->includedRelations = array_merge($this->includedRelations, $relations);
72
73
        return $this;
74
    }
75
76 3
    public function toResponse($request): JsonResponse
77
    {
78 3
        return (new ResourceResponse($this))->toResponse($request);
79
    }
80
81 2
    public function withResourceType(string $resourceType): self
82
    {
83 2
        $this->resourceType = $resourceType;
84
85 2
        return $this;
86
    }
87
88
    public function appendAdditional(array $data): self
89
    {
90
        $this->additional = array_merge_recursive($this->additional, $data);
91
92
        return $this;
93
    }
94
95 6
    protected function getResourceData(): array
96
    {
97 6
        if ($this->resource instanceof TransformableContract) {
98
            $data = [
99 3
                'id'         => $this->getResourceId(),
100 3
                'type'       => $this->getResourceType(),
101 3
                'attributes' => $this->getTransformed(),
102
            ];
103 3
        } elseif (is_null($this->resource)) {
104
            $data = [
105
                'id'         => null,
106
                'type'       => null,
107
                'attributes' => [],
108
            ];
109
        } else {
110
            $data = [
111 3
                'id'         => $this->getResourceId(),
112 3
                'type'       => $this->getResourceType(),
113 3
                'attributes' => $this->getTransformed(),
114
            ];
115
        }
116
117 6
        return $data;
118
    }
119
120 6
    protected function getRelationsData(): array
121
    {
122 6
        if (is_null($this->resource)) {
123
            return [];
124
        }
125
126 6
        $relations = [];
127
128 6
        $relationsChain = Arr::undot($this->includedRelations);
129 6
        foreach ($relationsChain as $currentRelation => $remainingRelationChain) {
130
            $methodName = 'include' . ucfirst($currentRelation);
131
132
            if (! method_exists($this, $methodName)) {
133
                throw new Exception('Relation method does not exist: ' . $methodName);
134
            }
135
136
            $remainingRelations = [];
137
            if (! empty($remainingRelationChain)) {
138
                $remainingRelations = array_keys(Arr::dot($remainingRelationChain));
139
            }
140
141
            /** @var \Longman\LaravelLodash\Http\Resources\JsonResource $resource */
142
            $resource = $this->$methodName();
143
            if (is_null($resource)) {
144
                continue;
145
            }
146
147
            $resource->withRelations($remainingRelations);
148
149
            $relations[$currentRelation] = $resource;
150
        }
151
152 6
        return $relations;
153
    }
154
155 6
    protected function getResourceType(): string
156
    {
157 6
        if (! $this->resource instanceof TransformableContract) {
158 3
            return $this->resourceType;
159
        }
160
161 3
        $reflection = new ReflectionClass($this->resource->getModel());
162
163 3
        return $reflection->getShortName();
164
    }
165
166 6
    protected function getResourceId(): ?string
167
    {
168 6
        if ($this->resource instanceof UuidAsPrimaryContract) {
169
            return $this->resource->getUidString();
170
        }
171
172 6
        if ($this->resource instanceof TransformableContract) {
173 3
            return (string) $this->resource->getKey();
174
        }
175
176 3
        return null;
177
    }
178
}
179