Issues (21)

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/Helpers/DataIncludedHelper.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
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/25/15
6
 * Time: 5:15 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Api\JsonApi\Helpers;
12
13
use NilPortugues\Api\JsonApi\JsonApiTransformer;
14
use NilPortugues\Serializer\Serializer;
15
16
class DataIncludedHelper
17
{
18
    /**
19
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
20
     * @param array                               $array
21
     * @param array                               $data
22
     * @param null                                $parentType
23
     */
24
    public static function setResponseDataIncluded(array &$mappings, array $array, array &$data, $parentType = null)
25
    {
26
        $parentType = (null === $parentType) ? $array[Serializer::CLASS_IDENTIFIER_KEY] : $parentType;
27
28
        foreach (self::removeTypeAndId($mappings, $array) as $value) {
29
            if (\is_array($value)) {
30
                if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $value)) {
31
                    $attributes = [];
32
                    $relationships = [];
33
                    $type = $value[Serializer::CLASS_IDENTIFIER_KEY];
34
35
                    self::addToRelationshipsArray($mappings, $data, $value, $type, $relationships, $attributes);
36
                    self::addToIncludedArray($mappings, $data, $attributes, $value);
37
                    continue;
38
                }
39
40
                if (\is_array($value)) {
41
                    foreach ($value as $inArrayValue) {
42
                        if (\is_array($inArrayValue)) {
43
44
                            //Remove those resources that do not to appear in the getIncludedResources array.
45
                            foreach ($inArrayValue as $position => $includableValue) {
46
                                if ($mappings[$parentType]->isFilteringIncludedResources()
47
                                    && false === in_array(
48
                                        $includableValue[Serializer::CLASS_IDENTIFIER_KEY],
49
                                        $mappings[$parentType]->getIncludedResources(),
50
                                        true
51
                                    )
52
                                ) {
53
                                    unset($inArrayValue[$position]);
54
                                }
55
                            }
56
57
                            self::setResponseDataIncluded($mappings, $inArrayValue, $data, $parentType);
58
                        }
59
                    }
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
67
     * @param array                               $copy
68
     *
69
     * @return array
70
     */
71
    protected static function removeTypeAndId(array &$mappings, array $copy)
72
    {
73
        if (!empty($copy[Serializer::CLASS_IDENTIFIER_KEY])) {
74
            $type = $copy[Serializer::CLASS_IDENTIFIER_KEY];
75
76
            if (\is_scalar($type)) {
77
                foreach ($mappings[$type]->getIdProperties() as $propertyName) {
78
                    unset($copy[$propertyName]);
79
                }
80
                unset($copy[Serializer::CLASS_IDENTIFIER_KEY]);
81
            }
82
        }
83
84
        return $copy;
85
    }
86
87
    /**
88
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
89
     * @param array                               $data
90
     * @param array                               $value
91
     * @param string                              $type
92
     * @param array                               $relationships
93
     * @param array                               $attributes
94
     */
95
    protected static function addToRelationshipsArray(
96
        array &$mappings,
97
        array &$data,
98
        array &$value,
99
        $type,
100
        array &$relationships,
101
        array &$attributes
102
    ) {
103
        foreach ($value as $propertyName => $attribute) {
104
            if (PropertyHelper::isAttributeProperty($mappings, $propertyName, $type)) {
105
                $propertyName = DataAttributesHelper::transformToValidMemberName($propertyName);
106
107
                if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $attribute)) {
108
                    self::setResponseDataIncluded($mappings, $value, $data);
109
110
                    $relationships[$propertyName] = \array_merge(
111
                        DataLinksHelper::setResponseDataLinks($mappings, $attribute),
112
                        [
113
                            JsonApiTransformer::DATA_KEY => [
114
                                $propertyName => PropertyHelper::setResponseDataTypeAndId(
115
                                        $mappings,
116
                                        $attribute
117
                                    ),
118
                            ],
119
                        ],
120
                        $mappings[$type]->getRelationships()
121
                    );
122
123
                    continue;
124
                }
125
                $attributes[$propertyName] = $attribute;
126
            }
127
        }
128
    }
129
130
    /**
131
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
132
     * @param array                               $data
133
     * @param array                               $attributes
134
     * @param array                               $value
135
     */
136
    protected static function addToIncludedArray(array &$mappings, array &$data, array &$attributes, array &$value)
137
    {
138
        if (\count($attributes) > 0) {
139
            $includedData = PropertyHelper::setResponseDataTypeAndId($mappings, $value);
140
141
            if (self::hasIdKey($includedData)) {
142
                $arrayData = \array_merge(
143
                    [
144
                        JsonApiTransformer::TYPE_KEY => $includedData[JsonApiTransformer::TYPE_KEY],
145
                        JsonApiTransformer::ID_KEY => $includedData[JsonApiTransformer::ID_KEY],
146
                        JsonApiTransformer::ATTRIBUTES_KEY => $attributes,
147
                        JsonApiTransformer::RELATIONSHIPS_KEY => [],
148
                    ],
149
                    DataLinksHelper::setResponseDataLinks($mappings, $value)
150
                );
151
152
                $relationshipData = [];
153
                self::addRelationshipsToIncludedResources(
154
                    $mappings,
155
                    $relationshipData,
156
                    $value,
157
                    $value[Serializer::CLASS_IDENTIFIER_KEY],
158
                    $attributes
0 ignored issues
show
The call to DataIncludedHelper::addR...psToIncludedResources() has too many arguments starting with $attributes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
159
                );
160
161
                if ($relationshipData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationshipData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
162
                    $arrayData[JsonApiTransformer::RELATIONSHIPS_KEY] = \array_merge(
163
                        $arrayData[JsonApiTransformer::RELATIONSHIPS_KEY],
164
                        $relationshipData
165
                    );
166
                }
167
168
                $data[JsonApiTransformer::INCLUDED_KEY][] = \array_filter($arrayData);
169
            }
170
        }
171
172
        if (!empty($data[JsonApiTransformer::INCLUDED_KEY])) {
173
            $data[JsonApiTransformer::INCLUDED_KEY] = \array_values(
174
                \array_unique($data[JsonApiTransformer::INCLUDED_KEY], SORT_REGULAR)
175
            );
176
        }
177
    }
178
179
    /**
180
     * @param array  $mappings
181
     * @param array  $data
182
     * @param array  $value
183
     * @param string $type
184
     */
185 View Code Duplication
    protected static function addRelationshipsToIncludedResources(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
        array &$mappings,
187
        array &$data,
188
        array &$value,
189
        $type
190
    ) {
191
        foreach ($value as $propertyName => $attribute) {
192
            if (PropertyHelper::isAttributeProperty($mappings, $propertyName, $type)) {
193
                $propertyName = DataAttributesHelper::transformToValidMemberName($propertyName);
194
195
                if (\is_array($attribute) && \array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $attribute)) {
196
                    $data[$propertyName][JsonApiTransformer::DATA_KEY] = PropertyHelper::setResponseDataTypeAndId(
197
                        $mappings,
198
                        $attribute
199
                    );
200
201
                    continue;
202
                }
203
            }
204
        }
205
    }
206
207
    /**
208
     * @param array $includedData
209
     *
210
     * @return bool
211
     */
212
    protected static function hasIdKey(array &$includedData)
213
    {
214
        return \array_key_exists(JsonApiTransformer::ID_KEY, $includedData)
215
        && !empty($includedData[JsonApiTransformer::ID_KEY]);
216
    }
217
}
218