Issues (3)

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.

tests/ContentNegotiationServiceProviderTest.php (1 issue)

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace JDesrosiers\Tests\Silex\Provider;
4
5
use JDesrosiers\Silex\Provider\ContentNegotiationServiceProvider;
6
use JDesrosiers\Silex\Provider\JmsSerializerServiceProvider;
7
use Silex\Application;
8
use Silex\Provider\SerializerServiceProvider;
9
use Symfony\Component\HttpKernel\Client;
10
11
require_once __DIR__ . "/../vendor/autoload.php";
12
require __DIR__ . "/Fixtures/Foo.php";
13
14
class CartServiceTest extends \PHPUnit_Framework_TestCase
15
{
16
    protected $app;
17
18
    public function setUp()
19
    {
20
        $this->app = new Application();
21
        $this->app["debug"] = true;
22
23
        $this->app->register(new ContentNegotiationServiceProvider(), array(
24
             "conneg.responseFormats" => array("json", "xml"),
25
             "conneg.requestFormats" => array("json", "xml"),
26
             "conneg.defaultFormat" => "json",
27
        ));
28
    }
29
30
    public function dataProviderNotAcceptableReturns406()
31
    {
32
        return array(
33
            array("text/html"),
34
            array("image/jpeg"),
35
            array("foo/bar"),
36
        );
37
    }
38
39
    /**
40
     * @dataProvider dataProviderNotAcceptableReturns406
41
     */
42
    public function testNotAcceptableReturns406($accept)
43
    {
44
        $this->app->get("/foo", function (Application $app) {
45
            return $app->json(array("foo" => "bar"));
46
        });
47
48
        $headers = array(
49
            "HTTP_ACCEPT" => $accept,
50
        );
51
52
        $client = new Client($this->app, $headers);
53
        $client->request("GET", "/foo");
54
55
        $response = $client->getResponse();
56
57
        $this->assertEquals("406", $response->getStatusCode());
58
        $this->assertEquals("text/html; charset=UTF-8", $response->headers->get("Content-Type"));
59
    }
60
61
    public function testNoAcceptHeaders()
62
    {
63
        $this->app->get("/foo", function (Application $app) {
64
            return $app->json(array("foo" => "bar"));
65
        });
66
67
        $headers = array(
68
            "HTTP_ACCEPT" => null,
69
            "HTTP_ACCEPT_CHARSET" => null,
70
            "HTTP_ACCEPT_LANGUAGE" => null,
71
        );
72
73
        $client = new Client($this->app, $headers);
74
        $client->request("GET", "/foo");
75
76
        $response = $client->getResponse();
77
78
        $this->assertEquals("200", $response->getStatusCode());
79
        $this->assertEquals('{"foo":"bar"}', $response->getContent());
80
        $this->assertEquals($response->headers->get("Content-Type"), "application/json");
81
    }
82
83
    public function testAcceptAny()
84
    {
85
        $this->app->get("/foo", function (Application $app) {
86
            return $app->json(array("foo" => "bar"));
87
        });
88
89
        $headers = array(
90
            "HTTP_ACCEPT" => "*/*",
91
        );
92
93
        $client = new Client($this->app, $headers);
94
        $client->request("GET", "/foo");
95
96
        $response = $client->getResponse();
97
98
        $this->assertEquals("200", $response->getStatusCode());
99
        $this->assertEquals('{"foo":"bar"}', $response->getContent());
100
        $this->assertEquals($response->headers->get("Content-Type"), "application/json");
101
    }
102
103
    public function dataProviderUnsupportedMediaTypeReturns415()
104
    {
105
        return array(
106
            array("application/json", "image/jpeg", "application/json"),
107
            array("application/xml", "image/jpeg", "text/xml; charset=UTF-8"),
108
            array("text/xml; charset=UTF-8", "image/jpeg", "text/xml; charset=UTF-8"),
109
        );
110
    }
111
112
    /**
113
     * @dataProvider dataProviderUnsupportedMediaTypeReturns415
114
     */
115
    public function testUnsupportedMediaTypeReturns415($accept, $contentType, $expectedContentType)
116
    {
117
        $this->app->post("/foo", function (Application $app) {
118
            return $app->json(array("foo" => "bar"));
119
        });
120
121
        $headers = array(
122
            "HTTP_ACCEPT" => $accept,
123
            "HTTP_CONTENT_TYPE" => $contentType,
124
        );
125
126
        $client = new Client($this->app, $headers);
127
        $client->request("POST", "/foo", array(), array(), $headers, "testdata");
128
129
        $response = $client->getResponse();
130
131
        $this->assertEquals("415", $response->getStatusCode());
132
        $this->assertEquals($expectedContentType, $response->headers->get("Content-Type"));
133
    }
134
135
    public function dataProviderJmsCreateResponse()
136
    {
137
        $xml = <<<XML
138
<?xml version="1.0" encoding="UTF-8"?>
139
<result>
140
  <entry><![CDATA[bar]]></entry>
141
</result>
142
143
XML;
144
145
        return array(
146
            array("application/json", "application/json", '{"foo":"bar"}'),
147
            array("text/xml", "text/xml; charset=UTF-8", $xml),
148
            array("application/xml", "text/xml; charset=UTF-8", $xml),
149
        );
150
    }
151
152
    /**
153
     * @dataProvider dataProviderJmsCreateResponse
154
     */
155
    public function testJmsCreateResponse($accept, $expectedContentType, $expectedContent)
156
    {
157
        $this->app->register(new JmsSerializerServiceProvider(), array(
158
            "serializer.srcDir" => __DIR__ . "/../vendor/jms/serializer/src",
159
        ));
160
161
        $this->app->get("/foo", function (Application $app) {
162
            return $app["conneg"]->createResponse(array("foo" => "bar"));
163
        });
164
165
        $headers = array(
166
            "HTTP_ACCEPT" => $accept,
167
        );
168
169
        $client = new Client($this->app, $headers);
170
        $client->request("GET", "/foo");
171
172
        $response = $client->getResponse();
173
174
        $this->assertEquals("200", $response->getStatusCode());
175
        $this->assertEquals($expectedContentType, $response->headers->get("Content-Type"));
176
        $this->assertEquals($expectedContent, $response->getContent());
177
    }
178
179
    public function dataProviderJmsDeserializeRequest()
180
    {
181
        $xml = <<<XML
182
<?xml version="1.0" encoding="UTF-8"?>
183
<request><![CDATA[foo]]></request>
184
XML;
185
186
        return array(
187
            array("application/json", '"foo"'),
188
            array("text/xml; charset=UTF-8", $xml),
189
            array("application/xml", $xml),
190
        );
191
    }
192
193
    /**
194
     * @dataProvider dataProviderJmsDeserializeRequest
195
     */
196
    public function testJmsDeserializeRequest($contentType, $content)
197
    {
198
        $this->app->register(new JmsSerializerServiceProvider(), array(
199
            "serializer.srcDir" => __DIR__ . "/../vendor/jms/serializer/src",
200
        ));
201
202
        $this->app->post("/foo", function (Application $app) {
203
            return print_r($app["conneg"]->deserializeRequest("string"), true);
204
        });
205
206
        $headers = array(
207
            "CONTENT_TYPE" => $contentType,
208
        );
209
210
        $client = new Client($this->app, $headers);
211
        $client->request("POST", "/foo", array(), array(), $headers, $content);
212
213
        $response = $client->getResponse();
214
215
        $this->assertEquals("200", $response->getStatusCode());
216
        $this->assertEquals("foo", $response->getContent());
217
    }
218
219
    public function dataProviderSymfonyCreateResponse()
220
    {
221
        $xml = <<<XML
222
<?xml version="1.0"?>
223
<response><foo>bar</foo></response>
224
225
XML;
226
227
        return array(
228
            array("application/json", "application/json", '{"foo":"bar"}'),
229
            array("text/xml", "text/xml; charset=UTF-8", $xml),
230
            array("application/xml", "text/xml; charset=UTF-8", $xml),
231
        );
232
    }
233
234
    /**
235
     * @dataProvider dataProviderSymfonyCreateResponse
236
     */
237
    public function testSymfonyCreateResponse($accept, $expectedContentType, $expectedContent)
238
    {
239
        $this->app->register(new SerializerServiceProvider());
240
241
        $this->app->get("/foo", function (Application $app) {
242
            return $app["conneg"]->createResponse(array("foo" => "bar"));
243
        });
244
245
        $headers = array(
246
            "HTTP_ACCEPT" => $accept,
247
        );
248
249
        $client = new Client($this->app, $headers);
250
        $client->request("GET", "/foo");
251
252
        $response = $client->getResponse();
253
254
        $this->assertEquals("200", $response->getStatusCode());
255
        $this->assertEquals($expectedContentType, $response->headers->get("Content-Type"));
256
        $this->assertEquals($expectedContent, $response->getContent());
257
    }
258
259
    public function dataProviderSymfonyDeserializeRequest()
260
    {
261
        $xml = <<<XML
262
<?xml version="1.0" encoding="UTF-8"?>
263
<Foo><foo><![CDATA[bar]]></foo></Foo>
264
XML;
265
266
        return array(
267
            array("application/json", '{"foo":"bar"}'),
268
            array("text/xml; charset=UTF-8", $xml),
269
            array("application/xml", $xml),
270
        );
271
    }
272
273
    /**
274
     * @dataProvider dataProviderSymfonyDeserializeRequest
275
     */
276
    public function testSymfonyDeserializeRequest($contentType, $content)
277
    {
278
        $this->app->register(new SerializerServiceProvider());
279
280
        $expectedContent = <<<CONTENT
281
JDesrosiers\Tests\Silex\Provider\Fixtures\Foo Object
282
(
283
    [foo:protected] => bar
284
)
285
286
CONTENT;
287
288
        $this->app->post("/foo", function (Application $app) {
289
            return print_r($app["conneg"]->deserializeRequest("JDesrosiers\Tests\Silex\Provider\Fixtures\Foo"), true);
290
        });
291
292
        $headers = array(
293
            "CONTENT_TYPE" => $contentType,
294
        );
295
296
        $client = new Client($this->app, $headers);
297
        $client->request("POST", "/foo", array(), array(), $headers, $content);
298
299
        $response = $client->getResponse();
300
301
        $this->assertEquals("200", $response->getStatusCode());
302
        $this->assertEquals($expectedContent, $response->getContent());
303
    }
304
}
305