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.

CoreBundle/Consumer/AnalyticsEventConsumer.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
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Consumer;
18
19
use Doctrine\Common\Persistence\ObjectManager;
20
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
21
use PhpAmqpLib\Message\AMQPMessage;
22
use SWP\Bundle\AnalyticsBundle\Model\ArticleEventInterface;
23
use SWP\Bundle\AnalyticsBundle\Services\ArticleStatisticsServiceInterface;
24
use SWP\Bundle\CoreBundle\Model\ArticleStatistics;
25
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
26
use SWP\Component\MultiTenancy\Exception\TenantNotFoundException;
27
use SWP\Component\MultiTenancy\Resolver\TenantResolver;
28
use Symfony\Component\HttpFoundation\Request;
29
30
final class AnalyticsEventConsumer implements ConsumerInterface
31
{
32
    private $articleStatisticsService;
33
34
    private $tenantResolver;
35
36
    private $tenantContext;
37
38
    private $articleStatisticsObjectManager;
39
40
    public function __construct(
41
        ArticleStatisticsServiceInterface $articleStatisticsService,
42
        TenantResolver $tenantResolver,
43
        TenantContextInterface $tenantContext,
44
        ObjectManager $articleStatisticsObjectManager
45
    ) {
46
        $this->articleStatisticsService = $articleStatisticsService;
47
        $this->tenantResolver = $tenantResolver;
48
        $this->tenantContext = $tenantContext;
49
        $this->articleStatisticsObjectManager = $articleStatisticsObjectManager;
50
    }
51
52
    public function execute(AMQPMessage $message)
53
    {
54
        /** @var Request $request */
55
        $request = unserialize($message->getBody());
56
        if (!$request instanceof Request) {
57
            return ConsumerInterface::MSG_REJECT;
58
        }
59
60
        try {
61
            $this->setTenant($request);
62
        } catch (TenantNotFoundException $e) {
63
            echo $e->getMessage()."\n";
64
65
            return ConsumerInterface::MSG_REJECT;
66
        }
67
        echo 'Set tenant: '.$this->tenantContext->getTenant()->getCode()."\n";
68
69
        if ($request->query->has('articleId')) {
70
            $this->handleArticlePageViews($request);
71
72
            echo 'Pageview for article '.$request->query->get('articleId')." was processed \n";
0 ignored issues
show
Security Cross-Site Scripting introduced by
'Pageview for article ' ...') . ' was processed ' can contain request data and is used in output 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
    in src/SWP/Bundle/CoreBundle/Consumer/AnalyticsEventConsumer.php on line 72
  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
    in src/SWP/Bundle/CoreBundle/Consumer/AnalyticsEventConsumer.php on line 72

Preventing Cross-Site-Scripting Attacks

Cross-Site-Scripting allows an attacker to inject malicious code into your website - in particular Javascript code, and have that code executed with the privileges of a visiting user. This can be used to obtain data, or perform actions on behalf of that visiting user.

In order to prevent this, make sure to escape all user-provided data:

// for HTML
$sanitized = htmlentities($tainted, ENT_QUOTES);

// for URLs
$sanitized = urlencode($tainted);

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...
73
        }
74
75
        return ConsumerInterface::MSG_ACK;
76
    }
77
78
    private function handleArticlePageViews(Request $request): void
79
    {
80
        $articleId = $request->query->get('articleId', null);
81
        if (null !== $articleId && 0 !== (int) $articleId) {
82
            $articleStatistics = $this->articleStatisticsService->addArticleEvent((int) $articleId, ArticleEventInterface::ACTION_PAGEVIEW, [
83
                'pageViewSource' => $this->getPageViewSource($request),
84
            ]);
85
86
            $query = $this->articleStatisticsObjectManager->createQuery('UPDATE '.ArticleStatistics::class.' s SET s.pageViewsNumber = s.pageViewsNumber + 1 WHERE s.id = :id');
87
            $query->setParameter('id', $articleStatistics->getId());
88
            $query->execute();
89
        }
90
    }
91
92
    private function getPageViewSource(Request $request): string
93
    {
94
        $pageViewReferer = $request->query->get('ref', null);
95
        if (null !== $pageViewReferer) {
96
            $refererHost = $this->getFragmentFromUrl($pageViewReferer, 'host');
97
            if ($refererHost && $this->isHostMatchingTenant($refererHost)) {
98
                return ArticleEventInterface::PAGEVIEW_SOURCE_INTERNAL;
99
            }
100
        }
101
102
        return ArticleEventInterface::PAGEVIEW_SOURCE_EXTERNAL;
103
    }
104
105
    private function getFragmentFromUrl(string $url, string $fragment): ?string
106
    {
107
        $fragments = \parse_url($url);
108
        if (!\array_key_exists($fragment, $fragments)) {
109
            return null;
110
        }
111
112
        return $fragments[$fragment];
113
    }
114
115
    private function isHostMatchingTenant(string $host): bool
116
    {
117
        $tenant = $this->tenantContext->getTenant();
118
        $tenantHost = $tenant->getDomainName();
119
        if (null !== ($subdomain = $tenant->getSubdomain())) {
120
            $tenantHost = $subdomain.'.'.$tenantHost;
121
        }
122
123
        return $host === $tenantHost;
124
    }
125
126
    private function setTenant(Request $request): void
127
    {
128
        $this->tenantContext->setTenant(
129
            $this->tenantResolver->resolve(
130
                $request->server->get('HTTP_REFERER',
131
                    $request->query->get('host',
132
                        $request->getHost()
133
                    )
134
                )
135
            )
136
        );
137
    }
138
}
139