Issues (24)

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/Resources/ResourceFactory.php (1 issue)

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
namespace Flugg\Responder\Resources;
4
5
use Flugg\Responder\Contracts\Resources\ResourceFactory as ResourceFactoryContract;
6
use Flugg\Responder\Contracts\Resources\ResourceKeyResolver as ResourceKeyResolverContract;
7
use Flugg\Responder\Contracts\Transformers\TransformerResolver;
8
use Illuminate\Support\Arr;
9
use League\Fractal\Resource\Collection as CollectionResource;
10
use League\Fractal\Resource\Item as ItemResource;
11
use League\Fractal\Resource\NullResource;
12
use League\Fractal\Resource\Primitive;
13
use League\Fractal\Resource\ResourceInterface;
14
use Traversable;
15
16
/**
17
 * This class is responsible for making Fractal resources from a variety of data types.
18
 *
19
 * @package flugger/laravel-responder
20
 * @author  Alexander TømmerÃ¥s <[email protected]>
21
 * @license The MIT License
22
 */
23
class ResourceFactory implements ResourceFactoryContract
24
{
25
    /**
26
     * A service class, used to normalize data.
27
     *
28
     * @var \Flugg\Responder\Resources\DataNormalizer
29
     */
30
    protected $normalizer;
31
32
    /**
33
     * A resolver class, used to resolve resource keys.
34
     *
35
     * @var \Flugg\Responder\Contracts\Transformers\TransformerResolver
36
     */
37
    protected $transformerResolver;
38
39
    /**
40
     * A resolver class, used to resolve resource keys.
41
     *
42
     * @var \Flugg\Responder\Contracts\Resources\ResourceKeyResolver
43
     */
44
    protected $resourceKeyResolver;
45
46
    /**
47
     * Construct the factory class.
48
     *
49
     * @param \Flugg\Responder\Resources\DataNormalizer                   $normalizer
50
     * @param \Flugg\Responder\Contracts\Transformers\TransformerResolver $transformerResolver
51
     * @param \Flugg\Responder\Contracts\Resources\ResourceKeyResolver    $resourceKeyResolver
52
     */
53 64
    public function __construct(DataNormalizer $normalizer, TransformerResolver $transformerResolver, ResourceKeyResolverContract $resourceKeyResolver)
54
    {
55 64
        $this->normalizer = $normalizer;
56 64
        $this->transformerResolver = $transformerResolver;
57 64
        $this->resourceKeyResolver = $resourceKeyResolver;
58 64
    }
59
60
    /**
61
     * Make resource from the given data.
62
     *
63
     * @param  mixed                                                          $data
64
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer
65
     * @param  string|null                                                    $resourceKey
66
     * @return \League\Fractal\Resource\ResourceInterface
67
     */
68 64
    public function make($data = null, $transformer = null, string $resourceKey = null): ResourceInterface
69
    {
70 64
        if ($data instanceof ResourceInterface) {
71 1
            return $this->makeFromResource($data, $transformer, $resourceKey);
72 63
        } elseif (is_null($data = $this->normalizer->normalize($data))) {
73 15
            return $this->instatiateResource($data, null, $resourceKey);
74
        }
75
76 51
        $transformer = $this->resolveTransformer($data, $transformer);
77 51
        $resourceKey = $this->resolveResourceKey($data, $resourceKey);
78
79 51
        return $this->instatiateResource($data, $transformer, $resourceKey);
80
    }
81
82
    /**
83
     * Make resource from the given resource.
84
     *
85
     * @param  \League\Fractal\Resource\ResourceInterface                     $resource
86
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer
87
     * @param  string|null                                                    $resourceKey
88
     * @return \League\Fractal\Resource\ResourceInterface
89
     */
90 1
    public function makeFromResource(ResourceInterface $resource, $transformer = null, string $resourceKey = null): ResourceInterface
91
    {
92 1
        $transformer = $this->resolveTransformer($resource->getData(), $transformer ?: $resource->getTransformer());
93 1
        $resourceKey = $this->resolveResourceKey($resource->getData(), $resourceKey ?: $resource->getResourceKey());
94
95 1
        return $resource->setTransformer($transformer)->setResourceKey($resourceKey);
96
    }
97
98
    /**
99
     * Instatiate a new resource instance.
100
     *
101
     * @param  mixed                                                   $data
102
     * @param  \Flugg\Responder\Transformers\Transformer|callable|null $transformer
103
     * @param  string|null                                             $resourceKey
104
     * @return \League\Fractal\Resource\ResourceInterface
105
     */
106 63
    protected function instatiateResource($data, $transformer = null, string $resourceKey = null): ResourceInterface
107
    {
108 63
        if (is_null($data)) {
109 15
            return new NullResource(null, null, $resourceKey);
110 51
        } elseif ($this->shouldCreateCollection($data)) {
111 25
            return new CollectionResource($data, $transformer, $resourceKey);
112 43
        } elseif (is_scalar($data)) {
113 1
            return new Primitive($data, $transformer, $resourceKey);
114
        }
115
116 42
        return new ItemResource($data, $transformer, $resourceKey);
117
    }
118
119
    /**
120
     * Indicates if the data belongs to a collection resource.
121
     *
122
     * @param  mixed $data
123
     * @return bool
124
     */
125 51
    protected function shouldCreateCollection($data): bool
126
    {
127 51
        if (is_array($data)) {
128 10
            return ! Arr::isAssoc($data) && ! is_scalar(Arr::first($data));
0 ignored issues
show
$data is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
        }
130
131 42
        return $data instanceof Traversable;
132
    }
133
134
    /**
135
     * Resolve a transformer.
136
     *
137
     * @param  mixed                                                          $data
138
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer
139
     * @return \Flugg\Responder\Transformers\Transformer|callable
140
     */
141 52
    protected function resolveTransformer($data, $transformer)
142
    {
143 52
        if (isset($transformer)) {
144 27
            return $this->transformerResolver->resolve($transformer);
145
        }
146
147 33
        return $this->transformerResolver->resolveFromData($data);
148
    }
149
150
    /**
151
     * Resolve a resource key.
152
     *
153
     * @param  mixed       $data
154
     * @param  string|null $resourceKey
155
     * @return null|string
156
     */
157 52
    protected function resolveResourceKey($data, string $resourceKey = null)
158
    {
159 52
        return isset($resourceKey) ? $resourceKey : $this->resourceKeyResolver->resolve($data);
160
    }
161
}