Issues (3)

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/ResponseTest.php (3 issues)

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 Demv\JSend\Test;
4
5
use Demv\JSend\JSend;
6
use Demv\JSend\JSendResponse;
7
use Demv\JSend\ResponseFactory;
8
use Demv\JSend\Status;
9
use Demv\JSend\StatusInterface;
10
use PHPUnit\Framework\TestCase;
11
12
final class ResponseTest extends TestCase
13
{
14
    public function testDefaultHttpStatusCode(): void
15
    {
16
        $this->assertEquals(
17
            200,
18
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->success())
19
        );
20
        $this->assertEquals(
21
            200,
22
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->fail())
23
        );
24
        $this->assertEquals(
25
            500,
26
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf']))
27
        );
28
        $this->assertEquals(
29
            400,
30
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf', 'code' => 400]))
31
        );
32
    }
33
34 View Code Duplication
    public function testSuccessFactory(): void
35
    {
36
        $response = JSendResponse::success(['Erfolgreich!']);
37
38
        $this->assertTrue($response->getStatus()->isSuccess());
39
        $this->assertEquals(['Erfolgreich!'], $response->getData());
40
    }
41
42 View Code Duplication
    public function testFailFactory(): void
43
    {
44
        $response = JSendResponse::fail(['Irgendwas lief schief']);
45
46
        $this->assertTrue($response->getStatus()->isFail());
47
        $this->assertEquals(['Irgendwas lief schief'], $response->getData());
48
    }
49
50
    public function testErrorFactory(): void
51
    {
52
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten');
53
54
        $this->assertTrue($response->getStatus()->isError());
55
        $this->assertEmpty($response->getData());
56
        $this->assertEquals(
57
            'Es ist ein Fehler aufgetreten',
58
            $response->getError()->getMessage()
59
        );
60
    }
61
62
    public function testSuccessConversion(): void
63
    {
64
        $json = '{"status": "success", "data": ["Holy", "Moly"]}';
65
66
        $success = new DummyResponse();
67
        $success->withBody(new DummyStream($json));
68
        $success->withStatus(214);
69
70
        $response = ResponseFactory::instance()->convert($success);
71
        $this->assertTrue($response->getStatus()->isSuccess());
72
        $this->assertEquals(['Holy', 'Moly'], $response->getData());
73
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
74
    }
75
76
    public function testFailConversion(): void
77
    {
78
        $fail = new DummyResponse();
79
        $fail->withBody(new DummyStream('{"status": "fail", "data": null}'));
80
81
        $response = ResponseFactory::instance()->convert($fail);
82
        $this->assertTrue($response->getStatus()->isFail());
83
        $this->assertEmpty($response->getData());
84
        $this->assertJsonStringEqualsJsonString('{"status": "fail", "data": null}', json_encode($response));
85
    }
86
87
    public function testErrorConversion(): void
88
    {
89
        $json = '{"status": "error", "data": ["Invalid"], "message": "Something is not right..."}';
90
91
        $error = new DummyResponse();
92
        $error->withBody(new DummyStream($json));
93
        $error->withStatus(501);
94
95
        $result         = json_decode($json, true);
96
        $result['code'] = $error->getStatusCode();
97
98
        $response = ResponseFactory::instance()->convert($error);
99
        $this->assertTrue($response->getStatus()->isError());
100
        $this->assertEquals(['Invalid'], $response->getData());
101
        $this->assertJsonStringEqualsJsonString(json_encode($result), json_encode($response));
102
        $this->assertEquals('Something is not right...', $response->getError()->getMessage());
103
        $this->assertEquals($error->getStatusCode(), $response->getError()->getCode());
104
    }
105
106
    public function testMapping(): void
107
    {
108
        $response = new JSendResponse(Status::translate(1), null);
109
        $this->assertTrue($response->getStatus()->isSuccess());
110
111
        $response = new JSendResponse(Status::translate(0), null);
112
        $this->assertTrue($response->getStatus()->isFail());
113
114
        $response = new JSendResponse(Status::translate(-1), null);
115
        $this->assertTrue($response->getStatus()->isError());
116
117
        $response = new JSendResponse(Status::translate(true), null);
0 ignored issues
show
true is of type boolean, but the function expects a integer.

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...
118
        $this->assertTrue($response->getStatus()->isSuccess());
119
120
        $response = new JSendResponse(Status::translate(false), null);
0 ignored issues
show
false is of type boolean, but the function expects a integer.

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...
121
        $this->assertTrue($response->getStatus()->isFail());
122
123
        $response = new JSendResponse(Status::translate(false, [false => StatusInterface::STATUS_ERROR]), null);
0 ignored issues
show
false is of type boolean, but the function expects a integer.

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...
124
        $this->assertTrue($response->getStatus()->isError());
125
    }
126
127
    public function testPsr7Response(): void
128
    {
129
        $response = JSendResponse::success(['Erfolgreich!'])->asResponse(200);
130
        $this->assertEquals(200, $response->getStatusCode());
131
        $this->assertEquals('{"status":"success","data":["Erfolgreich!"]}', $response->getBody()->getContents());
132
133
        $response = JSendResponse::fail(['Irgendwas lief schief'])->asResponse(400);
134
        $this->assertEquals(400, $response->getStatusCode());
135
        $this->assertEquals('{"status":"fail","data":["Irgendwas lief schief"]}', $response->getBody()->getContents());
136
137
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten', 404)->asResponse(500);
138
        $this->assertEquals(500, $response->getStatusCode());
139
        $this->assertEquals(
140
            '{"status":"error","message":"Es ist ein Fehler aufgetreten","code":404}',
141
            $response->getBody()->getContents()
142
        );
143
144
        $response = JSendResponse::success(['Stimmt der Header?'])->asResponse();
145
        $this->assertEquals(['application/json'], $response->getHeader('content-type'));
146
147
        $response = JSendResponse::success(['Eigene Header werden übernommen'])->asResponse(null, ['foo' => 'bar']);
148
        $this->assertEquals(['bar'], $response->getHeader('foo'));
149
    }
150
151
    public function testPsr7ResponseOptionalCode(): void
152
    {
153
        $response = JSendResponse::success(['Erfolgreich!'])->asResponse();
154
        $this->assertEquals(200, $response->getStatusCode());
155
        $this->assertEquals('{"status":"success","data":["Erfolgreich!"]}', $response->getBody()->getContents());
156
157
        $response = JSendResponse::fail(['Irgendwas lief schief'])->asResponse();
158
        $this->assertEquals(200, $response->getStatusCode());
159
        $this->assertEquals('{"status":"fail","data":["Irgendwas lief schief"]}', $response->getBody()->getContents());
160
161
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten', 404)->asResponse();
162
        $this->assertEquals(404, $response->getStatusCode());
163
        $this->assertEquals(
164
            '{"status":"error","message":"Es ist ein Fehler aufgetreten","code":404}',
165
            $response->getBody()->getContents()
166
        );
167
168
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten')->asResponse();
169
        $this->assertEquals(500, $response->getStatusCode());
170
        $this->assertEquals(
171
            '{"status":"error","message":"Es ist ein Fehler aufgetreten"}',
172
            $response->getBody()->getContents()
173
        );
174
    }
175
}
176