Issues (63)

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/Backend/MessageManagerBackend.php (6 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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Backend;
15
16
use Laminas\Diagnostics\Result\Failure;
17
use Laminas\Diagnostics\Result\Success;
18
use Laminas\Diagnostics\Result\Warning;
19
use Sonata\NotificationBundle\Consumer\ConsumerEvent;
20
use Sonata\NotificationBundle\Exception\HandlingException;
21
use Sonata\NotificationBundle\Iterator\MessageManagerMessageIterator;
22
use Sonata\NotificationBundle\Model\MessageInterface;
23
use Sonata\NotificationBundle\Model\MessageManagerInterface;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26
class MessageManagerBackend implements BackendInterface
27
{
28
    /**
29
     * @var MessageManagerInterface
30
     */
31
    protected $messageManager;
32
33
    /**
34
     * @var array
35
     */
36
    protected $checkLevel;
37
38
    /**
39
     * @var int
40
     */
41
    protected $pause;
42
43
    /**
44
     * @var int
45
     */
46
    protected $maxAge;
47
48
    /**
49
     * @var MessageManagerBackendDispatcher|null
50
     */
51
    protected $dispatcher = null;
52
53
    /**
54
     * @var array
55
     */
56
    protected $types;
57
58
    /**
59
     * @var int
60
     */
61
    protected $batchSize;
62
63
    /**
64
     * @param int $pause
65
     * @param int $maxAge
66
     * @param int $batchSize
67
     */
68
    public function __construct(MessageManagerInterface $messageManager, array $checkLevel, $pause = 500000, $maxAge = 86400, $batchSize = 10, array $types = [])
69
    {
70
        $this->messageManager = $messageManager;
71
        $this->checkLevel = $checkLevel;
72
        $this->pause = $pause;
73
        $this->maxAge = $maxAge;
74
        $this->batchSize = $batchSize;
75
        $this->types = $types;
76
    }
77
78
    /**
79
     * @param array $types
80
     */
81
    public function setTypes($types): void
82
    {
83
        $this->types = $types;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function publish(MessageInterface $message)
90
    {
91
        $this->messageManager->save($message);
0 ignored issues
show
$message is of type object<Sonata\Notificati...Model\MessageInterface>, but the function expects a object<Sonata\Doctrine\Model\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
93
        return $message;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function create($type, array $body)
100
    {
101
        $message = $this->messageManager->create();
102
        $message->setType($type);
103
        $message->setBody($body);
104
        $message->setState(MessageInterface::STATE_OPEN);
105
106
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $message; (Sonata\Doctrine\Model\T) is incompatible with the return type declared by the interface Sonata\NotificationBundl...ackendInterface::create of type Sonata\NotificationBundle\Model\MessageInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function createAndPublish($type, array $body)
113
    {
114
        return $this->publish($this->create($type, $body));
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getIterator()
121
    {
122
        return new MessageManagerMessageIterator($this->messageManager, $this->types, $this->pause, $this->batchSize);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Sonata\Notif...use, $this->batchSize); (Sonata\NotificationBundl...eManagerMessageIterator) is incompatible with the return type declared by the interface Sonata\NotificationBundl...dInterface::getIterator of type Sonata\NotificationBundl...essageIteratorInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function initialize(): void
129
    {
130
    }
131
132
    public function setDispatcher(MessageManagerBackendDispatcher $dispatcher): void
133
    {
134
        $this->dispatcher = $dispatcher;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher)
141
    {
142
        $event = new ConsumerEvent($message);
143
144
        try {
145
            $message->setStartedAt(new \DateTime());
146
            $message->setState(MessageInterface::STATE_IN_PROGRESS);
147
            $this->messageManager->save($message);
0 ignored issues
show
$message is of type object<Sonata\Notificati...Model\MessageInterface>, but the function expects a object<Sonata\Doctrine\Model\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
149
            $dispatcher->dispatch($event, $message->getType());
150
151
            $message->setCompletedAt(new \DateTime());
152
            $message->setState(MessageInterface::STATE_DONE);
153
            $this->messageManager->save($message);
0 ignored issues
show
$message is of type object<Sonata\Notificati...Model\MessageInterface>, but the function expects a object<Sonata\Doctrine\Model\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
154
155
            return $event->getReturnInfo();
156
        } catch (\Exception $e) {
157
            $message->setCompletedAt(new \DateTime());
158
            $message->setState(MessageInterface::STATE_ERROR);
159
160
            $this->messageManager->save($message);
0 ignored issues
show
$message is of type object<Sonata\Notificati...Model\MessageInterface>, but the function expects a object<Sonata\Doctrine\Model\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
161
162
            throw new HandlingException('Error while handling a message', 0, $e);
163
        }
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getStatus()
170
    {
171
        try {
172
            $states = $this->messageManager->countStates();
173
        } catch (\Exception $e) {
174
            return new Failure(sprintf('Unable to retrieve message information - %s (Database)', $e->getMessage()));
175
        }
176
177
        if ($states[MessageInterface::STATE_IN_PROGRESS] > $this->checkLevel[MessageInterface::STATE_IN_PROGRESS]) {
178
            return new Failure('Too many messages processed at the same time (Database)');
179
        }
180
181
        if ($states[MessageInterface::STATE_ERROR] > $this->checkLevel[MessageInterface::STATE_ERROR]) {
182
            return new Failure('Too many errors (Database)');
183
        }
184
185
        if ($states[MessageInterface::STATE_OPEN] > $this->checkLevel[MessageInterface::STATE_OPEN]) {
186
            return new Warning('Too many messages waiting to be processed (Database)');
187
        }
188
189
        if ($states[MessageInterface::STATE_DONE] > $this->checkLevel[MessageInterface::STATE_DONE]) {
190
            return new Warning('Too many processed messages, please clean the database (Database)');
191
        }
192
193
        return new Success('Ok (Database)');
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function cleanup(): void
200
    {
201
        $this->messageManager->cleanup($this->maxAge);
202
    }
203
}
204