Issues (7)

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.

tests/unit/ResponseTest.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
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 2/14/18
6
 * Time: 12:02 PM
7
 */
8
9
namespace PhpRestfulApiResponse\Tests\unit;
10
11
use PhpRestfulApiResponse\Response;
12
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
13
use ReflectionClass;
14
use InvalidArgumentException;
15
use Zend\Diactoros\Response\ArraySerializer;
16
17
class ResponseTest extends Base
18
{
19
    /**
20
     * @var Response
21
     */
22
    private $response;
23
24
    public function setUp()
25
    {
26
        parent::setUp(); // TODO: Change the autogenerated stub
27
        $this->response = new Response();
28
    }
29
30
    public function test_withArray()
31
    {
32
        /** @var Response $response */
33
        $response = $this->response->withArray(['status' => 'success'], 200);
34
        $this->assertEquals(200, $response->getStatusCode());
35
        $this->assertEquals('{"status":"success"}', ArraySerializer::toArray($response)['body']);
36
        $this->assertEquals('{"status":"success"}', $response->getBody()->__toString());
37
    }
38
39
    public function test_withItem()
40
    {
41
        /** @var Response $response */
42
        $response = $this->response->withItem(
43
            new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017),
44
            new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
0 ignored issues
show
new \PhpRestfulApiRespon...\Lib\Transformer\Book() is of type object<PhpRestfulApiResp...t\Lib\Transformer\Book>, but the function expects a callable.

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...
45
            200
46
        );
47
        $this->assertEquals(200, $response->getStatusCode());
48
        $this->assertEquals('{"data":{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000}}', $response->getBody()->__toString());
49
    }
50
51
    public function test_withCollection()
52
    {
53
        /** @var Response $response */
54
        $response = $this->response->withCollection(
55
            [
56
                new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017),
57
                new Book('harry', 'harryosmarsitohang', 'how to be a mage', 500000, 2016),
58
                new Book('harry', 'harryosmarsitohang', 'how to be a samurai', 25000, 2000),
59
            ],
60
            new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
0 ignored issues
show
new \PhpRestfulApiRespon...\Lib\Transformer\Book() is of type object<PhpRestfulApiResp...t\Lib\Transformer\Book>, but the function expects a callable.

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...
61
            200
62
        );
63
        $this->assertEquals(200, $response->getStatusCode());
64
        $this->assertEquals('{"data":[{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000},{"title":"how to be a mage","author":{"name":"harry","email":"harryosmarsitohang"},"year":2016,"price":500000},{"title":"how to be a samurai","author":{"name":"harry","email":"harryosmarsitohang"},"year":2000,"price":25000}]}', $response->getBody()->__toString());
65
    }
66
67
    public function test_withError()
68
    {
69
        $code = 400;
70
        $message = 'error occured';
71
        $this->withError(
72
            $this->response->withError($message, $code),
73
            $code,
74
            $message
75
        );
76
    }
77
78
    public function test_withError_with_param_errorCode()
79
    {
80
        $code = 400;
81
        $message = 'error occured';
82
        $errorCode = 'ERROR-CODE';
83
        $this->withError(
84
            $this->response->withError($message, $code, $errorCode),
85
            $code,
86
            $message,
87
            $errorCode
88
        );
89
    }
90
91
    public function test_errorNotFound()
92
    {
93
        $code = 404;
94
        $message = '';
95
        $this->withError(
96
            $this->response->errorNotFound($message),
97
            $code,
98
            $message
99
        );
100
    }
101
102
    public function test_errorNotFound_with_message()
103
    {
104
        $code = 404;
105
        $message = 'go back to home page';
106
        $this->withError(
107
            $this->response->errorNotFound($message),
108
            $code,
109
            $message
110
        );
111
    }
112
113
    public function test_errorForbidden()
114
    {
115
        $code = 403;
116
        $message = '';
117
        $this->withError(
118
            $this->response->errorForbidden($message),
119
            $code,
120
            $message
121
        );
122
    }
123
124
    public function test_errorForbidden_with_message()
125
    {
126
        $code = 403;
127
        $message = 'forbid to access this';
128
        $this->withError(
129
            $this->response->errorForbidden($message),
130
            $code,
131
            $message
132
        );
133
    }
134
135
    public function test_errorInternalError()
136
    {
137
        $code = 500;
138
        $message = '';
139
        $this->withError(
140
            $this->response->errorInternalError($message),
141
            $code,
142
            $message
143
        );
144
    }
145
146
    public function test_errorInternalError_with_message()
147
    {
148
        $code = 500;
149
        $message = 'something wrong';
150
        $this->withError(
151
            $this->response->errorInternalError($message),
152
            $code,
153
            $message
154
        );
155
    }
156
157
    public function test_errorUnauthorized()
158
    {
159
        $code = 401;
160
        $message = '';
161
        $this->withError(
162
            $this->response->errorUnauthorized($message),
163
            $code,
164
            $message
165
        );
166
    }
167
168
    public function test_errorUnauthorized_with_message()
169
    {
170
        $code = 401;
171
        $message = 'access token required';
172
        $this->withError(
173
            $this->response->errorUnauthorized($message),
174
            $code,
175
            $message
176
        );
177
    }
178
179
    public function test_errorWrongArgs()
180
    {
181
        $code = 400;
182
        $message = [
183
            'username' => 'required',
184
            'password' => 'required'
185
        ];
186
        $this->withError(
187
            $this->response->errorWrongArgs($message),
188
            $code,
189
            $message
190
        );
191
    }
192
193
    public function test_errorGone()
194
    {
195
        $code = 410;
196
        $message = '';
197
        $this->withError(
198
            $this->response->errorGone($message),
199
            $code,
200
            $message
201
        );
202
    }
203
204
    public function test_errorGone_with_message()
205
    {
206
        $code = 410;
207
        $message = 'mysql gone away';
208
        $this->withError(
209
            $this->response->errorGone($message),
210
            $code,
211
            $message
212
        );
213
    }
214
215
    public function test_errorMethodNotAllowed()
216
    {
217
        $code = 405;
218
        $message = '';
219
        $this->withError(
220
            $this->response->errorMethodNotAllowed($message),
221
            $code,
222
            $message
223
        );
224
    }
225
226
    public function test_errorMethodNotAllowed_with_message()
227
    {
228
        $code = 405;
229
        $message = 'GET method is not allowed for this endpoint';
230
        $this->withError(
231
            $this->response->errorMethodNotAllowed($message),
232
            $code,
233
            $message
234
        );
235
    }
236
237
    public function test_errorUnwillingToProcess()
238
    {
239
        $code = 431;
240
        $message = '';
241
        $this->withError(
242
            $this->response->errorUnwillingToProcess($message),
243
            $code,
244
            $message
245
        );
246
    }
247
248
    public function test_errorUnwillingToProcess_with_message()
249
    {
250
        $code = 431;
251
        $message = 'Request size is too big';
252
        $this->withError(
253
            $this->response->errorUnwillingToProcess($message),
254
            $code,
255
            $message
256
        );
257
    }
258
259
    public function test_errorUnprocessable()
260
    {
261
        $code = 422;
262
        $message = '';
263
        $this->withError(
264
            $this->response->errorUnprocessable($message),
265
            $code,
266
            $message
267
        );
268
    }
269
270
    public function test_errorUnprocessable_with_message()
271
    {
272
        $code = 422;
273
        $message = 'Your request cannot be processed';
274
        $this->withError(
275
            $this->response->errorUnprocessable($message),
276
            $code,
277
            $message
278
        );
279
    }
280
281
    public function test_withStatus()
282
    {
283
        $this->response->withStatus(200);
284
        $this->assertEquals(200, $this->response->getStatusCode());
285
    }
286
287
    public function test_setStatusCode_less_than_min_status_code()
288
    {
289
        $this->run_setStatusCode($this->getMethodSetStatusCode(), 99);
290
    }
291
292
    public function test_setStatusCode_greater_than_max_status_code()
293
    {
294
        $this->run_setStatusCode($this->getMethodSetStatusCode(), 600);
295
    }
296
297
    public function test_setStatusCode()
298
    {
299
        $this->run_setStatusCode($this->getMethodSetStatusCode(), 200);
300
    }
301
302
    public function test_setErrorCode()
303
    {
304
        $this->run_setErrorCode($this->getMethodSetErrorCode(), "ERROR-CODE");
305
    }
306
307
    private function run_setStatusCode(\ReflectionMethod $method, $code)
308
    {
309
        try {
310
            $method->invokeArgs($this->response, [$code]);
311
            $this->assertEquals($code, $this->response->getStatusCode());
312
        } catch (InvalidArgumentException $exception) {
313
            $this->assertEquals(
314
                sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', $code, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE),
315
                $exception->getMessage()
316
            );
317
        }
318
    }
319
320
    private function run_setErrorCode(\ReflectionMethod $method, $code)
321
    {
322
        $method->invokeArgs($this->response, [$code]);
323
        $this->assertEquals($code, $this->response->getErrorCode());
324
    }
325
326 View Code Duplication
    private function getMethodSetErrorCode()
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...
327
    {
328
        $responseReflect = new ReflectionClass(Response::class);
329
        $method = $responseReflect->getMethod('setErrorCode');
330
        $method->setAccessible(true);
331
        return $method;
332
    }
333
334 View Code Duplication
    private function getMethodSetStatusCode()
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...
335
    {
336
        $responseReflect = new ReflectionClass(Response::class);
337
        $method = $responseReflect->getMethod('setStatusCode');
338
        $method->setAccessible(true);
339
        return $method;
340
    }
341
342
343
    private function withError(Response $response, $code, $message = null, $errorCode = null)
344
    {
345
        $this->assertEquals($code, $response->getStatusCode());
346
        $this->assertEquals($errorCode, $response->getErrorCode());
347
348
        $this->assertEquals(json_encode([
349
            'error' => array_filter([
350
                'http_code' => $response->getStatusCode(),
351
                'code' => $response->getErrorCode(),
352
                'phrase' => $response->getReasonPhrase(),
353
                'message' => $message
354
            ])
355
        ]), $response->getBody()->__toString());
356
357
    }
358
}