Issues (154)

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/Filter/ModelFilterTest.php (8 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 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\DoctrineMongoDBAdminBundle\Tests\Filter;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Doctrine\ODM\MongoDB\Query\Builder;
18
use MongoDB\BSON\ObjectId;
19
use PHPUnit\Framework\TestCase;
20
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
21
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
22
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
23
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter;
24
25
class DocumentStub
26
{
27
    private $id;
28
29
    public function __construct()
30
    {
31
        // NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x
32
        $this->id = class_exists(ObjectId::class) ? new ObjectId() : new MongoId();
33
    }
34
35
    public function getId()
36
    {
37
        return (string) ($this->id);
38
    }
39
}
40
41
class ModelFilterTest extends TestCase
42
{
43
    private $queryBuilder;
44
45
    protected function setUp(): void
46
    {
47
        $this->queryBuilder = $this->createMock(Builder::class);
48
    }
49
50
    /**
51
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
52
     */
53
    public function getFieldDescription(array $options)
54
    {
55
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
56
        $fieldDescription->expects($this->once())->method('getOptions')->willReturn($options);
57
        $fieldDescription->expects($this->once())->method('getName')->willReturn('field_name');
58
59
        return $fieldDescription;
60
    }
61
62
    public function testFilterEmpty(): void
63
    {
64
        $filter = new ModelFilter();
65
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
66
67
        $builder = new ProxyQuery($this->queryBuilder);
68
69
        $builder->getQueryBuilder()
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
70
            ->expects($this->never())
71
            ->method('field')
72
        ;
73
74
        $filter->filter($builder, 'alias', 'field', null);
75
        $filter->filter($builder, 'alias', 'field', []);
76
77
        $this->assertFalse($filter->isActive());
78
    }
79
80
    public function testFilterArray(): void
81
    {
82
        $filter = new ModelFilter();
83
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
84
85
        $builder = new ProxyQuery($this->queryBuilder);
86
87
        $builder->getQueryBuilder()
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
88
            ->expects($this->once())
89
            ->method('field')
90
            ->with('field._id')
91
            ->willReturnSelf()
92
        ;
93
94
        $oneDocument = new DocumentStub();
95
        $otherDocument = new DocumentStub();
96
97
        $builder->getQueryBuilder()
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
98
            ->expects($this->once())
99
            ->method('in')
100
            ->with([$this->getMongoIdentifier($oneDocument->getId()), $this->getMongoIdentifier($otherDocument->getId())])
101
        ;
102
103
        $filter->filter($builder, 'alias', 'field', [
104
            'type' => EqualOperatorType::TYPE_EQUAL,
105
            'value' => [$oneDocument, $otherDocument],
106
        ]);
107
108
        $this->assertTrue($filter->isActive());
109
    }
110
111
    public function testFilterScalar(): void
112
    {
113
        $filter = new ModelFilter();
114
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
115
116
        $builder = new ProxyQuery($this->queryBuilder);
117
118
        $builder->getQueryBuilder()
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
119
            ->expects($this->once())
120
            ->method('field')
121
            ->with('field._id')
122
            ->willReturnSelf()
123
        ;
124
125
        $document1 = new DocumentStub();
126
127
        $builder->getQueryBuilder()
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
128
            ->expects($this->once())
129
            ->method('equals')
130
            ->with($this->getMongoIdentifier($document1->getId()))
131
        ;
132
133
        $filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => $document1]);
134
135
        $this->assertTrue($filter->isActive());
136
    }
137
138
    public function testAssociationWithInvalidMapping(): void
139
    {
140
        $this->expectException(\RuntimeException::class);
141
142
        $filter = new ModelFilter();
143
        $filter->initialize('field_name', ['mapping_type' => 'foo', 'field_mapping' => true]);
144
145
        $builder = new ProxyQuery($this->queryBuilder);
146
147
        $filter->apply($builder, 'asd');
148
    }
149
150
    public function testAssociationWithValidMappingAndEmptyFieldName(): void
151
    {
152
        $this->expectException(\RuntimeException::class);
153
154
        $filter = new ModelFilter();
155
        $filter->initialize('field_name', ['mapping_type' => ClassMetadata::ONE, 'field_mapping' => true]);
156
157
        $builder = new ProxyQuery($this->queryBuilder);
158
159
        $filter->apply($builder, 'asd');
160
        $this->assertTrue($filter->isActive());
161
    }
162
163
    public function testAssociationWithValidMapping(): void
164
    {
165
        $filter = new ModelFilter();
166
        $filter->initialize('field_name', [
167
            'mapping_type' => ClassMetadata::ONE,
168
            'field_name' => 'field_name',
169
            'association_mapping' => [
170
                'fieldName' => 'association_mapping',
171
            ], 'field_mapping' => true,
172
        ]);
173
174
        $builder = new ProxyQuery($this->queryBuilder);
175
176
        $builder->getQueryBuilder()
0 ignored issues
show
The method method() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
177
            ->method('field')
178
            ->with('field_name._id')
179
            ->willReturnSelf()
180
        ;
181
182
        $filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);
183
184
        $this->assertTrue($filter->isActive());
185
    }
186
187
    public function testAssociationWithValidParentAssociationMappings(): void
188
    {
189
        $filter = new ModelFilter();
190
        $filter->initialize('field_name', [
191
            'mapping_type' => ClassMetadata::ONE,
192
            'field_name' => 'field_name',
193
            'parent_association_mappings' => [
194
                [
195
                    'fieldName' => 'association_mapping',
196
                ],
197
                [
198
                    'fieldName' => 'sub_association_mapping',
199
                ],
200
            ],
201
            'association_mapping' => [
202
                'fieldName' => 'sub_sub_association_mapping',
203
            ], 'field_mapping' => true,
204
        ]);
205
206
        $builder = new ProxyQuery($this->queryBuilder);
207
208
        $builder->getQueryBuilder()
0 ignored issues
show
The method method() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
209
            ->method('field')
210
            ->with('field_name._id')
211
            ->willReturnSelf()
212
        ;
213
214
        $filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);
215
216
        $this->assertTrue($filter->isActive());
217
    }
218
219
    /**
220
     * @dataProvider getMappings
221
     */
222
    public function testDifferentIdentifiersBasedOnMapping(string $storeAs, string $fieldIdentifier): void
223
    {
224
        $filter = new ModelFilter();
225
        $filter->initialize('field_name', [
226
            'mapping_type' => ClassMetadata::ONE,
227
            'field_name' => 'field_name',
228
            'field_mapping' => [
229
                'storeAs' => $storeAs,
230
            ],
231
        ]);
232
233
        $builder = new ProxyQuery($this->queryBuilder);
234
235
        $builder->getQueryBuilder()
0 ignored issues
show
The method method() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

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...
236
            ->method('field')
237
            ->with('field_name'.$fieldIdentifier)
238
            ->willReturnSelf()
239
        ;
240
241
        $filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);
242
243
        $this->assertTrue($filter->isActive());
244
    }
245
246
    public function getMappings(): array
247
    {
248
        return [
249
            [ClassMetadata::REFERENCE_STORE_AS_REF, '.id'],
250
            [ClassMetadata::REFERENCE_STORE_AS_ID, ''],
251
            [ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB, '.$id'],
252
            [ClassMetadata::REFERENCE_STORE_AS_DB_REF, '.$id'],
253
        ];
254
    }
255
256
    /**
257
     * NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x.
258
     *
259
     * @return ObjectId|\MongoId
260
     */
261
    private function getMongoIdentifier(string $id)
262
    {
263
        return class_exists(ObjectId::class) ? new ObjectId($id) : new \MongoId($id);
264
    }
265
}
266