Issues (541)

Security Analysis    8 potential vulnerabilities

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

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.
  Cross-Site Scripting (6)
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 (1)
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 (1)
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.

EventListener/LinkRequestListener.php (3 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 the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\EventListener;
16
17
use SWP\Component\Common\Response\ResourcesListResponseInterface;
18
use SWP\Component\Common\Response\SingleResourceResponseInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
22
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
23
use Symfony\Component\HttpKernel\Event\ControllerEvent;
24
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
25
use Symfony\Component\HttpKernel\Event\RequestEvent;
26
use Symfony\Component\HttpKernel\HttpKernelInterface;
27
use Symfony\Component\HttpKernel\KernelEvents;
28
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
29
30
class LinkRequestListener
31
{
32
    /**
33
     * @var ControllerResolverInterface
34
     */
35
    protected $resolver;
36
37
    /**
38
     * @var UrlMatcherInterface
39
     */
40
    protected $urlMatcher;
41
42 96
    /**
43
     * @param ControllerResolverInterface $controllerResolver
44 96
     * @param UrlMatcherInterface         $urlMatcher
45 96
     */
46 96
    public function __construct(ControllerResolverInterface $controllerResolver, UrlMatcherInterface $urlMatcher)
47
    {
48
        $this->resolver = $controllerResolver;
49
        $this->urlMatcher = $urlMatcher;
50
    }
51
52
    /**
53 96
     * @param GetResponseEvent $event
54
     *
55 96
     * @return array
56 96
     */
57
    public function onKernelRequest(RequestEvent $event, $eventName, EventDispatcherInterface $dispatcher)
0 ignored issues
show
The parameter $eventName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59 2
        if (!$event->getRequest()->headers->has('link')) {
60 2
            return;
61
        }
62
63
        $links = [];
64
        $header = $event->getRequest()->headers->get('link');
65
66
        /*
67
         * Due to limitations, multiple same-name headers are sent as comma
68
         * separated values.
69 2
         *
70 1
         * This breaks those headers into Link headers following the format
71 1
         * http://tools.ietf.org/html/rfc2068#section-19.6.2.4
72
         */
73
        while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) {
74 2
            $header = trim(substr($header, strlen($matches[0])));
75 2
            $links[] = $matches[1];
76
        }
77
78 2
        if ($header) {
79
            $links[] = $header;
80
        }
81 2
82
        $requestMethod = $this->urlMatcher->getContext()->getMethod();
83 2
84
        // The controller resolver needs a request to resolve the controller.
85 2
        $stubRequest = new Request();
86
87 2
        foreach ($links as $idx => $link) {
88 2
            // Force the GET method to avoid the use of the previous method (LINK/UNLINK)
89 2
            $this->urlMatcher->getContext()->setMethod('GET');
90 2
91 2
            $linkParams = explode(';', trim($link));
92
            $resourceType = null;
93 2 View Code Duplication
            if (count($linkParams) > 1) {
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...
94 2
                $resourceType = trim(preg_replace('/<|>/', '', $linkParams[1]));
95
                $resourceType = str_replace('"', '', str_replace('rel=', '', $resourceType));
96
            }
97 2
            $resource = array_shift($linkParams);
98 1
            $resource = preg_replace('/<|>/', '', $resource);
99
100 2
            // Assume that no resource is specified here if there is no path separator, because urlMatcher will return homepage
101
            if (false === strpos($resource, '/')) {
102
                continue;
103 2
            }
104
            $tempRequest = Request::create($resource);
105
106
            try {
107
                $route = $this->urlMatcher->match($tempRequest->getRequestUri());
108
            } catch (\Exception $e) {
109 2
                // If we don't have a matching route we return the original Link header
110 2
                continue;
111 2
            }
112
113
            $stubRequest->attributes->replace($route);
114
            $stubRequest->server = $event->getRequest()->server;
115 2
            $stubRequest::setTrustedProxies(['192.0.0.1', '10.0.0.0/8', $event->getRequest()->server->get('REMOTE_ADDR')], Request::HEADER_X_FORWARDED_ALL);
116 2
            // Keep server name in sync with forwarded host
117 2
            if ($stubRequest->isFromTrustedProxy() && $stubRequest->server->has('HTTP_X_FORWARDED_HOST')) {
118 2
                $stubRequest->server->set('SERVER_NAME', $stubRequest->server->has('HTTP_X_FORWARDED_HOST'));
119 2
            }
120
121 2
            if (false === $controller = $this->resolver->getController($stubRequest)) {
122 2
                continue;
123
            }
124
125
            $subEvent = new ControllerEvent($event->getKernel(), $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST);
126 2
            $kernelSubEvent = new RequestEvent($event->getKernel(), $stubRequest, HttpKernelInterface::SUB_REQUEST);
127
            $dispatcher->dispatch(KernelEvents::REQUEST, $kernelSubEvent);
128 2
            $dispatcher->dispatch(KernelEvents::CONTROLLER, $subEvent);
129
            $controller = $subEvent->getController();
130 2
131
            $argumentResolver = new ArgumentResolver();
132
            $arguments = $argumentResolver->getArguments($stubRequest, $controller);
133
134 2
            try {
135
                $result = call_user_func_array($controller, $arguments);
0 ignored issues
show
Security Code Execution introduced by
$controller can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

2 paths for user data to reach this point

  1. Path: $this->parameters['HTTP_AUTHORIZATION'] seems to return tainted data, and $authorizationHeader is assigned in ServerBag.php on line 59
  1. $this->parameters['HTTP_AUTHORIZATION'] seems to return tainted data, and $authorizationHeader is assigned
    in vendor/ServerBag.php on line 59
  2. ParameterBag::$parameters is assigned
    in vendor/ServerBag.php on line 74
  3. Tainted property ParameterBag::$parameters is read
    in vendor/ParameterBag.php on line 77
  4. ParameterBag::get() returns tainted data, and $controller is assigned
    in vendor/Controller/ControllerResolver.php on line 38
  5. ControllerResolver::getController() returns tainted data, and $controller is assigned
    in vendor/HttpKernel.php on line 129
  6. $controller is passed to FilterControllerEvent::__construct()
    in vendor/HttpKernel.php on line 133
  7. $controller is passed to FilterControllerEvent::setController()
    in vendor/Event/FilterControllerEvent.php on line 28
  8. FilterControllerEvent::$controller is assigned
    in vendor/Event/FilterControllerEvent.php on line 43
  9. Tainted property FilterControllerEvent::$controller is read
    in vendor/Event/FilterControllerEvent.php on line 38
  10. FilterControllerEvent::getController() returns tainted data, and $controller is assigned
    in src/SWP/Bundle/ContentBundle/EventListener/LinkRequestListener.php on line 129
  2. Path: Read from $_POST, and $_POST is passed to Request::createRequestFromFactory() in Request.php on line 285
  1. Read from $_POST, and $_POST is passed to Request::createRequestFromFactory()
    in vendor/Request.php on line 285
  2. $request is passed to Request::__construct()
    in vendor/Request.php on line 1981
  3. $request is passed to Request::initialize()
    in vendor/Request.php on line 239
  4. $request is passed to ParameterBag::__construct()
    in vendor/Request.php on line 257
  5. ParameterBag::$parameters is assigned
    in vendor/ParameterBag.php on line 28
  6. Tainted property ParameterBag::$parameters is read
    in vendor/ParameterBag.php on line 77
  7. ParameterBag::get() returns tainted data, and $controller is assigned
    in vendor/Controller/ControllerResolver.php on line 38
  8. ControllerResolver::getController() returns tainted data, and $controller is assigned
    in vendor/HttpKernel.php on line 129
  9. $controller is passed to FilterControllerEvent::__construct()
    in vendor/HttpKernel.php on line 133
  10. $controller is passed to FilterControllerEvent::setController()
    in vendor/Event/FilterControllerEvent.php on line 28
  11. FilterControllerEvent::$controller is assigned
    in vendor/Event/FilterControllerEvent.php on line 43
  12. Tainted property FilterControllerEvent::$controller is read
    in vendor/Event/FilterControllerEvent.php on line 38
  13. FilterControllerEvent::getController() returns tainted data, and $controller is assigned
    in src/SWP/Bundle/ContentBundle/EventListener/LinkRequestListener.php on line 129

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
136
                // Our api returns objects for single resources
137
                if (!is_object($result)) {
138 2
                    continue;
139
                }
140
141
                // return clean object for LINK requests
142 2
                if ($result instanceof ResourcesListResponseInterface) {
143 2
                    $result = $result->getResources();
144
                } elseif ($result instanceof SingleResourceResponseInterface) {
145 2
                    $result = $result->getResource();
146
                }
147
148
                $links[$idx] = ['object' => $result, 'resourceType' => $resourceType];
149
            } catch (\Exception $e) {
150
                $links[$idx] = ['object' => $e, 'resourceType' => 'exception'];
151
152
                continue;
153
            }
154
        }
155
156
        $event->getRequest()->attributes->set('links', $links);
157
        $this->urlMatcher->getContext()->setMethod($requestMethod);
158
159
        return $links;
160
    }
161
}
162