Issues (17)

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/Parameters.php (6 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
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
use Tobscure\JsonApi\Exception\InvalidParameterException;
15
16
class Parameters
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $input;
22
23
    /**
24
     * @param array $input
25
     */
26 42
    public function __construct(array $input)
27
    {
28 42
        $this->input = $input;
29 42
    }
30
31
    /**
32
     * Get the includes.
33
     *
34
     * @param array $available
35
     *
36
     * @throws \Tobscure\JsonApi\Exception\InvalidParameterException
37
     *
38
     * @return array
39
     */
40 9
    public function getInclude(array $available = [])
41
    {
42 9
        if ($include = $this->getInput('include')) {
43 6
            $relationships = explode(',', $include);
44
45 6
            $invalid = array_diff($relationships, $available);
46
47 6 View Code Duplication
            if (count($invalid)) {
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...
48 3
                throw new InvalidParameterException(
49 3
                    'Invalid includes ['.implode(',', $invalid).']',
50 3
                    1,
51 3
                    null,
52
                    'include'
53 3
                );
54
            }
55
56 3
            return $relationships;
57
        }
58
59 3
        return [];
60
    }
61
62
    /**
63
     * Get number of offset.
64
     *
65
     * @param int|null $perPage
66
     *
67
     * @throws \Tobscure\JsonApi\Exception\InvalidParameterException
68
     *
69
     * @return int
70
     */
71 9
    public function getOffset($perPage = null)
72
    {
73 9
        if ($perPage && ($offset = $this->getOffsetFromNumber($perPage))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $perPage of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
74 3
            return $offset;
75
        }
76
77 6
        $offset = (int) $this->getPage('offset');
78
79 6
        if ($offset < 0) {
80 3
            throw new InvalidParameterException('page[offset] must be >=0', 2, null, 'page[offset]');
81
        }
82
83 3
        return $offset;
84
    }
85
86
    /**
87
     * Calculate the offset based on the page[number] parameter.
88
     *
89
     * @param int $perPage
90
     *
91
     * @throws \Tobscure\JsonApi\Exception\InvalidParameterException
92
     *
93
     * @return int
94
     */
95 3
    protected function getOffsetFromNumber($perPage)
96
    {
97 3
        $page = (int) $this->getPage('number');
98
99 3
        if ($page <= 1) {
100
            return 0;
101
        }
102
103 3
        return ($page - 1) * $perPage;
104
    }
105
106
    /**
107
     * Get the limit.
108
     *
109
     * @param int|null $max
110
     *
111
     * @return int|null
0 ignored issues
show
Should the return type not be integer|string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
112
     */
113 6
    public function getLimit($max = null)
114
    {
115 6
        $limit = $this->getPage('limit') ?: $this->getPage('size') ?: null;
116
117 6
        if ($limit && $max) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $max of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
118
            $limit = min($max, $limit);
119
        }
120
121 6
        return $limit;
122
    }
123
124
    /**
125
     * Get the sort.
126
     *
127
     * @param array $available
128
     *
129
     * @throws \Tobscure\JsonApi\Exception\InvalidParameterException
130
     *
131
     * @return array
132
     */
133 12
    public function getSort(array $available = [])
134
    {
135 12
        $sort = [];
136
137 12
        if ($input = $this->getInput('sort')) {
138 9
            $fields = explode(',', $input);
139
140 9
            foreach ($fields as $field) {
141 9
                if (substr($field, 0, 1) === '-') {
142 3
                    $field = substr($field, 1);
143 3
                    $order = 'desc';
144 3
                } else {
145 9
                    $order = 'asc';
146
                }
147
148 9
                $sort[$field] = $order;
149 9
            }
150
151 9
            $invalid = array_diff(array_keys($sort), $available);
152
153 9 View Code Duplication
            if (count($invalid)) {
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...
154 3
                throw new InvalidParameterException(
155 3
                    'Invalid sort fields ['.implode(',', $invalid).']',
156 3
                    3,
157 3
                    null,
158
                    'sort'
159 3
                );
160
            }
161 6
        }
162
163 9
        return $sort;
164
    }
165
166
    /**
167
     * Get the fields requested for inclusion.
168
     *
169
     * @return array
170
     */
171 6
    public function getFields()
172
    {
173 6
        $fields = $this->getInput('fields');
174
175 6
        if (! is_array($fields)) {
176 3
            return [];
177
        }
178
179 3
        return array_map(function ($fields) {
180 3
            return explode(',', $fields);
181 3
        }, $fields);
182
    }
183
184
    /**
185
     * Get a filter item.
186
     *
187
     * @return mixed
188
     */
189
    public function getFilter()
190
    {
191
        return $this->getInput('filter');
192
    }
193
194
    /**
195
     * Get an input item.
196
     *
197
     * @param string $key
198
     * @param null $default
199
     *
200
     * @return mixed
201
     */
202 42
    protected function getInput($key, $default = null)
203
    {
204 42
        return isset($this->input[$key]) ? $this->input[$key] : $default;
205
    }
206
207
    /**
208
     * Get the page.
209
     *
210
     * @param string $key
211
     *
212
     * @return string
213
     */
214 15
    protected function getPage($key)
215
    {
216 15
        $page = $this->getInput('page');
217
218 15
        return isset($page[$key]) ? $page[$key] : '';
219
    }
220
}
221