Issues (35)

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/Normalizer.php (4 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 As3\Modlr\Api\JsonApiOrg;
4
5
use As3\Modlr\Api\AbstractNormalizer;
6
use As3\Modlr\Api\NormalizerException;
7
use As3\Modlr\Metadata\EntityMetadata;
8
use As3\Modlr\Rest\RestPayload;
9
10
/**
11
 * Normalizes REST payloads into standard arrays based on the JSON API spec.
12
 *
13
 * @author Jacob Bare <[email protected]>
14
 */
15
final class Normalizer extends AbstractNormalizer
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 View Code Duplication
    protected function extractId(array $rawPayload)
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...
21
    {
22
        if (isset($rawPayload['data']['id']) && !empty($rawPayload['data']['id'])) {
23
            return $rawPayload['data']['id'];
24
        }
25
        return null;
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 View Code Duplication
    protected function extractType(array $rawPayload)
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...
32
    {
33
        if (isset($rawPayload['data']['type']) && !empty($rawPayload['data']['type'])) {
34
            return $rawPayload['data']['type'];
35
        }
36
        throw NormalizerException::badRequest('The "type" member was missing from the payload. All payloads must contain a type.');
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function extractProperties(array $rawPayload, EntityMetadata $metadata)
43
    {
44
        $data = $rawPayload['data'];
45
        $attributes = $this->extractAttributes($data, $metadata);
46
        $relationships = $this->extractRelationships($data, $metadata);
47
        return array_merge($attributes, $relationships);
48
    }
49
50
    /**
51
     * Extracts the model's attributes, per JSON API spec.
52
     *
53
     * @param   array           $data
54
     * @param   EntityMetadata  $metadata
55
     * @return  array
56
     */
57
    protected function extractAttributes(array $data, EntityMetadata $metadata)
58
    {
59
        $flattened = [];
60
        if (!isset($data['attributes']) || !is_array($data['attributes'])) {
61
            return $data['attributes'] = [];
62
        }
63
64
        $keyMap = array_flip(array_keys($data['attributes']));
65
        foreach ($metadata->getProperties() as $key => $propMeta) {
66
            if (!isset($keyMap[$key])) {
67
                continue;
68
            }
69
            if (true === $metadata->hasAttribute($key) || true === $metadata->hasEmbed($key)) {
70
                $flattened[$key] = $data['attributes'][$key];
71
            }
72
        }
73
        return $flattened;
74
    }
75
76
    /**
77
     * Extracts the model's relationships, per JSON API spec.
78
     *
79
     * @param   array           $data
80
     * @param   EntityMetadata  $metadata
81
     * @return  array
82
     */
83
    protected function extractRelationships(array $data, EntityMetadata $metadata)
84
    {
85
        $flattened = [];
86
        if (!isset($data['relationships']) || !is_array($data['relationships'])) {
87
            return $flattened;
88
        }
89
90
        foreach ($metadata->getRelationships() as $key => $relMeta) {
91
            if (!isset($data['relationships'][$key])) {
92
                continue;
93
            }
94
            $rel = $data['relationships'][$key];
95
            // Need to use array_key_exists over isset because 'null' is valid. Creating a key map for each relationship is too costly since every relationship needs the data check.
96
            if (false === array_key_exists('data', $rel)) {
97
                throw NormalizerException::badRequest(sprintf('The "data" member was missing from the payload for relationship "%s"', $key));
98
            }
99
100
            if (empty($rel['data'])) {
101
                $flattened[$key] = $relMeta->isOne() ? null : [];
102
                continue;
103
            }
104
105
            if (!is_array($rel['data'])) {
106
                throw NormalizerException::badRequest(sprintf('The "data" member is not valid in the payload for relationship "%s"', $key));
107
            }
108
109 View Code Duplication
            if (true === $relMeta->isOne() && true === $this->isSequentialArray($rel['data'])) {
0 ignored issues
show
This code seems to be duplicated across 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...
110
                throw NormalizerException::badRequest(sprintf('The data payload for relationship "%s" is malformed. Data types of "one" must be an associative array, sequential found.', $key));
111
            }
112 View Code Duplication
            if (true === $relMeta->isMany() && false === $this->isSequentialArray($rel['data'])) {
0 ignored issues
show
This code seems to be duplicated across 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...
113
                throw NormalizerException::badRequest(sprintf('The data payload for relationship "%s" is malformed. Data types of "many" must be a sequential array, associative found.', $key));
114
            }
115
            $flattened[$key] = $rel['data'];
116
        }
117
        return $flattened;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    protected function getRawPayload(RestPayload $payload)
124
    {
125
        $rawPayload = @json_decode($payload->getData(), true);
126
        if (!is_array($rawPayload)) {
127
            throw NormalizerException::badRequest('Unable to parse. Is the JSON valid?');
128
        }
129
        return $rawPayload;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135
    protected function validateRawPayload(array $rawPayload)
136
    {
137
        if (!isset($rawPayload['data']) || !is_array($rawPayload['data'])) {
138
            throw NormalizerException::badRequest('No "data" member was found in the payload. All payloads must be keyed with "data."');
139
        }
140
141
        if (empty($rawPayload['data'])) {
142
            return $rawPayload;
143
        }
144
        if (true === $this->isSequentialArray($rawPayload['data'])) {
145
            throw NormalizerException::badRequest('Normalizing multiple records is currently not supported.');
146
        }
147
        return $rawPayload;
148
    }
149
150
    /**
151
     * Determines if an array is sequential.
152
     *
153
     * @param   array   $arr
154
     * @return  bool
155
     */
156
    protected function isSequentialArray(array $arr)
157
    {
158
        if (empty($arr)) {
159
            return true;
160
        }
161
        return (range(0, count($arr) - 1) === array_keys($arr));
162
    }
163
}
164