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.

src/ContentNegotiationServiceProvider.php (2 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 JDesrosiers\Silex\Provider;
4
5
use JDesrosiers\Silex\Provider\ContentNegotiation\JmsSerializerContentNegotiation;
6
use JDesrosiers\Silex\Provider\ContentNegotiation\SymfonySerializerContentNegotiation;
7
use JMS\Serializer as JMS;
8
use Pimple\Container;
9
use Pimple\ServiceProviderInterface;
10
use Silex\Application;
11
use Silex\Api\BootableProviderInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
14
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
15
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
16
use Symfony\Component\Serializer as SymfonySerializer;
17
18
/**
19
 * This ServiceProvider provides HTTP Content Negotiation support to a silex
20
 * application.
21
 */
22
class ContentNegotiationServiceProvider implements ServiceProviderInterface, BootableProviderInterface
23
{
24
    /**
25
     * Add filters to check that the request and repsonse formats are supported
26
     * by the application.
27
     *
28
     * @param Application $app
29
     */
30 20
    public function boot(Application $app)
31
    {
32 20
        $app->before(array($this, "setRequestFormat"), Application::EARLY_EVENT);
33 20
        $app->before(array($this, "validateRequestContentType"), Application::EARLY_EVENT);
34 20
    }
35
36
    /**
37
     * Set defaults and build the conneg service.
38
     *
39
     * @param Container $app
40
     * @throws ServiceUnavailableHttpException
41
     */
42 20
    public function register(Container $app)
43
    {
44 20
        $app["conneg.responseFormats"] = array("html");
45 20
        $app["conneg.requestFormats"] = array("form");
46 20
        $app["conneg.defaultFormat"] = "html";
47
48 20
        $app["conneg"] = function (Container $app) {
49 12
            if ($app->offsetExists("serializer")) {
50 12
                if ($app["serializer"] instanceof JMS\Serializer) {
51 6
                    if (!$app->offsetExists("conneg.serializationContext")) {
52 6
                        $app["conneg.serializationContext"] = null;
53 6
                    }
54 6
                    if (!$app->offsetExists("conneg.deserializationContext")) {
55 6
                        $app["conneg.deserializationContext"] = null;
56 6
                    }
57
58 6
                    return new JmsSerializerContentNegotiation($app);
0 ignored issues
show
$app of type object<Pimple\Container> is not a sub-type of object<Silex\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
59 6
                } elseif ($app["serializer"] instanceof SymfonySerializer\Serializer) {
60 6
                    if (!$app->offsetExists("conneg.serializationContext")) {
61 6
                        $app["conneg.serializationContext"] = array();
62 6
                    }
63 6
                    if (!$app->offsetExists("conneg.deserializationContext")) {
64 6
                        $app["conneg.deserializationContext"] = array();
65 6
                    }
66
67 6
                    return new SymfonySerializerContentNegotiation($app);
0 ignored issues
show
$app of type object<Pimple\Container> is not a sub-type of object<Silex\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
68
                }
69
            }
70
71
            throw new ServiceUnavailableHttpException(null, "No supported serializer found");
72
        };
73 20
    }
74
75
    /**
76
     * This before middleware validates whether the application will be able to
77
     * respond in a format that the client understands.
78
     *
79
     * @param Request $request
80
     * @throws NotAcceptableHttpException
81
     */
82 20
    public function setRequestFormat(Request $request, Application $app)
83
    {
84
        // If there is no Accept header, do nothing
85 20
        if (!$request->headers->get("Accept")) {
86 1
            return;
87
        }
88
89
        // Check the Accept header for acceptable formats
90 19
        foreach ($request->getAcceptableContentTypes() as $contentType) {
91
            // If any format is acceptable, do nothing
92 19
            if ($contentType === "*/*") {
93 1
                return;
94
            }
95
96 18
            $format = $request->getFormat($contentType);
97 18
            if (in_array($format, $app["conneg.responseFormats"])) {
98
                // An acceptable format was found.  Set it as the requestFormat
99
                // where it can be referenced later.
100 15
                $request->setRequestFormat($format);
101 15
                return;
102
            }
103 9
        }
104
105
        // No acceptable formats were found
106 3
        throw new NotAcceptableHttpException();
107
    }
108
109
    /**
110
     * This before middleware validates that the request body is in a format
111
     * that the application understands.
112
     *
113
     * @param Request $request
114
     * @throws UnsupportedMediaTypeHttpException
115
     */
116 17
    public function validateRequestContentType(Request $request, Application $app)
117
    {
118
        // Define the "form" format so we can use it for validation
119 17
        $request->setFormat("form", array("application/x-www-form-urlencoded", "multipart/form-data"));
120
121 17
        $format = $request->getContentType();
122 17
        if (strlen($request->getContent()) > 0 && !in_array($format, $app["conneg.requestFormats"])) {
123
            // The request has a body but it is not a supported media type
124 3
            throw new UnsupportedMediaTypeHttpException();
125
        }
126 14
    }
127
}
128