Issues (101)

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.

EventDispatcher/SyncEventDispatcher.php (2 issues)

Labels
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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\EventDispatcher;
16
17
use Acme\App\Core\Port\EventDispatcher\BufferedEventDispatcherInterface;
18
use Acme\App\Core\Port\Lock\LockManagerInterface;
19
use Acme\App\Core\Port\Persistence\TransactionServiceInterface;
20
use Acme\App\Core\SharedKernel\Port\EventDispatcher\EventInterface;
21
use Acme\PhpExtension\Helper\TypeHelper;
22
use Acme\PhpExtension\ObjectDispatcher\AbstractDispatcher;
23
use Psr\Log\LoggerInterface;
24
use Psr\Log\NullLogger;
25
use Throwable;
26
27
/**
28
 * Conceptually, the code triggering an event must not rely on the logic that the event triggers. So that logic doesn't
29
 * need to, and should not, be executed in-line with the code triggering it.
30
 * This means that, technically, all events could be asynchronous, meaning that the logic they trigger could be
31
 * executed after the HTTP request has been handled and the HTTP response was sent back to the client. Ideally in
32
 * another process, started by a message bus/queue.
33
 * However, for UX reasons, sometimes we want to handle an event in the same request where it is triggered, so that we
34
 * make sure the user can see the expected results in the next request he makes.
35
 *
36
 * Thus, we have the need for two types of event dispatchers (which can be behind a proxy dispatcher):
37
 *      - A SyncEventDispatcher which dispatcher the events in the same request
38
 *      - An AsyncEventDispatcher, which sends the events to a message bus so they will be executed later, in parallel
39
 *
40
 * Nevertheless, despite the SyncEventDispatcher running the event listeners in the same HTTP request as the code that
41
 * triggered the events, it does not mean it must run those listeners in-line with the code that triggered the events.
42
 * Thus, this SyncEventDispatcher will put the events in a buffer, and it will flush them all to the listeners after
43
 * the system handles the HTTP request, and just before sending the HTTP response back to the client.
44
 */
45
final class SyncEventDispatcher extends AbstractDispatcher implements BufferedEventDispatcherInterface
46
{
47
    /**
48
     * @var [EventInterface,[]][]
49
     */
50
    private $eventBuffer = [];
51
52
    /**
53
     * @var TransactionServiceInterface
54
     */
55
    private $transactionService;
56
57
    /**
58
     * @var LockManagerInterface
59
     */
60
    private $lockManager;
61
62
    /**
63
     * @var LoggerInterface
64
     */
65
    private $logger;
66
67
    public function __construct(
68
        TransactionServiceInterface $transactionService,
69
        LockManagerInterface $lockManager,
70
        LoggerInterface $logger = null
71
    ) {
72
        $this->transactionService = $transactionService;
73
        $this->lockManager = $lockManager;
74
        $this->logger = $logger ?? new NullLogger();
75
    }
76
77
    public function dispatch(EventInterface $event, array $metadata = []): void
78
    {
79
        $this->eventBuffer[] = [$event, $metadata];
80
    }
81
82
    public function flush(): void
83
    {
84
        foreach ($this->eventBuffer as [$event, $metadata]) {
85
            foreach ($this->getDestinationListForObject(\get_class($event)) as $listener) {
86
                /*
87
                 * Since these events are going to be handled outside of the main use case transaction,
88
                 * they will also be executed outside of the request transaction (after it is closed) so we will need
89
                 * to open a new transaction for the event listeners.
90
                 * Furthermore, since each event is independent from each other, we need to open a new transaction
91
                 * for each individual event listener.
92
                 */
93
                try {
94
                    $this->transactionService->startTransaction();
95
                    $listener($event, $metadata);
0 ignored issues
show
The variable $event does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $metadata does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
96
                    $this->transactionService->finishTransaction();
97
                } catch (Throwable $e) {
98
                    /*
99
                     * We simply log the exception and continue because if we rethrow the exception, the remaining
100
                     * event listeners will not be triggered.
101
                     */
102
                    $this->logger->error(
103
                        'Error occurred while handling event \'' . TypeHelper::getType($event)
104
                        . "' with listener '" . TypeHelper::getType($listener) . "'.\n"
105
                        . 'Exception message: ' . $e->getMessage() . "\n"
106
                        . 'Exception trace: ' . $e->getTraceAsString() . "\n"
107
                    );
108
                    $this->transactionService->rollbackTransaction();
109
                } finally {
110
                    // We release all locks here, so that they can be reacquired when processing the next event
111
                    $this->lockManager->releaseAll();
112
                }
113
            }
114
        }
115
    }
116
117
    public function reset(): void
118
    {
119
        $this->eventBuffer = [];
120
    }
121
}
122