Issues (655)

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.

tests/Admin/Extension/LockExtensionTest.php (11 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\AdminBundle\Tests\Admin\Extension;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Prophecy\ObjectProphecy;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Admin\Extension\LockExtension;
20
use Sonata\AdminBundle\Builder\FormContractorInterface;
21
use Sonata\AdminBundle\Form\FormMapper;
22
use Sonata\AdminBundle\Model\LockInterface;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Sonata\AdminBundle\Tests\App\Model\ModelManager;
25
use Symfony\Component\EventDispatcher\EventDispatcher;
26
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
27
use Symfony\Component\Form\FormBuilder;
28
use Symfony\Component\Form\FormEvent;
29
use Symfony\Component\Form\FormEvents;
30
use Symfony\Component\Form\FormFactoryInterface;
31
use Symfony\Component\Form\FormInterface;
32
use Symfony\Component\HttpFoundation\Request;
33
34
class LockExtensionTest extends TestCase
35
{
36
    /**
37
     * @var LockExtension
38
     */
39
    private $lockExtension;
40
41
    /**
42
     * @var EventDispatcherInterface
43
     */
44
    private $eventDispatcher;
45
46
    /**
47
     * @var AdminInterface
48
     */
49
    private $admin;
50
51
    /**
52
     * @var LockInterface
53
     */
54
    private $modelManager;
55
56
    /**
57
     * @var stdClass
58
     */
59
    private $object;
60
61
    /**
62
     * @var Request
63
     */
64
    private $request;
65
66
    protected function setUp(): void
67
    {
68
        $this->modelManager = $this->prophesize(ModelManager::class);
69
        $this->admin = $this->prophesize(AbstractAdmin::class);
70
        $this->eventDispatcher = new EventDispatcher();
71
        $this->request = new Request();
72
        $this->object = new \stdClass();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \stdClass() of type object<stdClass> is incompatible with the declared type object<Sonata\AdminBundl...min\Extension\stdClass> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
        $this->lockExtension = new LockExtension();
74
    }
75
76
    public function testModelManagerImplementsLockInterface(): void
77
    {
78
        $this->assertInstanceOf(LockInterface::class, $this->modelManager->reveal());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
    }
80
81
    public function testConfigureFormFields(): void
82
    {
83
        $formMapper = $this->configureFormMapper();
84
        $form = $this->configureForm();
85
        $this->configureAdmin(null, null, $this->modelManager->reveal());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        $event = new FormEvent($form->reveal(), []);
87
88
        $this->modelManager->getLockVersion([])->willReturn(1);
0 ignored issues
show
array() is of type array, but the function expects a object.

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...
89
90
        $form->add(
91
            '_lock_version',
92
            HiddenType::class,
93
            ['mapped' => false, 'data' => 1]
94
        )->shouldBeCalled();
95
96
        $this->lockExtension->configureFormFields($formMapper);
97
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
98
    }
99
100
    public function testConfigureFormFieldsWhenModelManagerIsNotImplementingLockerInterface(): void
101
    {
102
        $modelManager = $this->prophesize(ModelManagerInterface::class);
103
        $formMapper = $this->configureFormMapper();
104
        $form = $this->configureForm();
105
        $this->configureAdmin(null, null, $modelManager->reveal());
106
        $event = new FormEvent($form->reveal(), []);
107
108
        $form->add()->shouldNotBeCalled();
109
110
        $this->lockExtension->configureFormFields($formMapper);
111
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
112
    }
113
114
    public function testConfigureFormFieldsWhenFormEventHasNoData(): void
115
    {
116
        $formMapper = $this->configureFormMapper();
117
        $form = $this->configureForm();
118
        $event = new FormEvent($form->reveal(), null);
119
120
        $form->add()->shouldNotBeCalled();
121
122
        $this->lockExtension->configureFormFields($formMapper);
123
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
124
    }
125
126
    public function testConfigureFormFieldsWhenFormHasParent(): void
127
    {
128
        $formMapper = $this->configureFormMapper();
129
        $form = $this->configureForm();
130
        $event = new FormEvent($form->reveal(), []);
131
132
        $form->getParent()->willReturn('parent');
133
        $form->add()->shouldNotBeCalled();
134
135
        $this->lockExtension->configureFormFields($formMapper);
136
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
137
    }
138
139
    public function testConfigureFormFieldsWhenModelManagerHasNoLockedVersion(): void
140
    {
141
        $formMapper = $this->configureFormMapper();
142
        $form = $this->configureForm();
143
        $this->configureAdmin(null, null, $this->modelManager->reveal());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $event = new FormEvent($form->reveal(), []);
145
146
        $this->modelManager->getLockVersion([])->willReturn(null);
0 ignored issues
show
array() is of type array, but the function expects a object.

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...
147
        $form->add()->shouldNotBeCalled();
148
149
        $this->lockExtension->configureFormFields($formMapper);
150
        $this->eventDispatcher->dispatch($event, FormEvents::PRE_SET_DATA);
151
    }
152
153
    public function testPreUpdateIfAdminHasNoRequest(): void
154
    {
155
        $this->configureAdmin();
156
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
The call to lock() misses some required arguments starting with $object.
Loading history...
157
158
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
159
    }
160
161
    public function testPreUpdateIfObjectIsNotVersioned(): void
162
    {
163
        $this->configureAdmin();
164
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
The call to lock() misses some required arguments starting with $object.
Loading history...
165
166
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
167
    }
168
169
    public function testPreUpdateIfRequestDoesNotHaveLockVersion(): void
170
    {
171
        $uniqid = 'admin123';
172
        $this->configureAdmin($uniqid, $this->request);
173
174
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
The call to lock() misses some required arguments starting with $object.
Loading history...
175
176
        $this->request->request->set($uniqid, ['something']);
177
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
178
    }
179
180
    public function testPreUpdateIfModelManagerIsNotImplementingLockerInterface(): void
181
    {
182
        $modelManager = $this->prophesize(ModelManagerInterface::class);
183
        $uniqid = 'admin123';
184
        $this->configureAdmin($uniqid, $this->request, $modelManager->reveal());
185
        $this->modelManager->lock()->shouldNotBeCalled();
0 ignored issues
show
The call to lock() misses some required arguments starting with $object.
Loading history...
186
187
        $this->request->request->set($uniqid, ['_lock_version' => 1]);
188
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
189
    }
190
191
    public function testPreUpdateIfObjectIsVersioned(): void
192
    {
193
        $uniqid = 'admin123';
194
        $this->configureAdmin($uniqid, $this->request, $this->modelManager->reveal());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Model\LockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
196
        $this->modelManager->lock($this->object, 1)->shouldBeCalled();
197
198
        $this->request->request->set($uniqid, ['_lock_version' => 1]);
199
        $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
200
    }
201
202
    private function configureForm(): ObjectProphecy
203
    {
204
        $form = $this->prophesize(FormInterface::class);
205
206
        $form->getData()->willReturn($this->object);
207
        $form->getParent()->willReturn(null);
208
209
        return $form;
210
    }
211
212
    private function configureFormMapper(): FormMapper
213
    {
214
        $contractor = $this->prophesize(FormContractorInterface::class);
215
        $formFactory = $this->prophesize(FormFactoryInterface::class);
216
        $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory->reveal());
217
218
        return new FormMapper($contractor->reveal(), $formBuilder, $this->admin->reveal());
219
    }
220
221
    private function configureAdmin(
222
        ?string $uniqid = null,
223
        ?Request $request = null,
224
        ?ModelManagerInterface $modelManager = null
225
    ): void {
226
        $this->admin->getUniqid()->willReturn($uniqid);
227
        $this->admin->getRequest()->willReturn($request);
228
        $this->admin->hasRequest()->willReturn(null !== $request);
229
        $this->admin->getModelManager()->willReturn($modelManager);
230
    }
231
}
232