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.

src/Builder/DatagridBuilder.php (3 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\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
20
use Sonata\AdminBundle\Datagrid\Datagrid;
21
use Sonata\AdminBundle\Datagrid\DatagridInterface;
22
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
23
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
24
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
25
use Symfony\Component\Form\Extension\Core\Type\FormType;
26
use Symfony\Component\Form\FormFactoryInterface;
27
28
class DatagridBuilder implements DatagridBuilderInterface
29
{
30
    /**
31
     * @var FilterFactoryInterface
32
     */
33
    protected $filterFactory;
34
35
    /**
36
     * @var FormFactoryInterface
37
     */
38
    protected $formFactory;
39
40
    /**
41
     * @var TypeGuesserInterface
42
     */
43
    protected $guesser;
44
45
    /**
46
     * Indicates that csrf protection enabled.
47
     *
48
     * @var bool
49
     */
50
    protected $csrfTokenEnabled;
51
52
    /**
53
     * @param bool $csrfTokenEnabled
54
     */
55
    public function __construct(FormFactoryInterface $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser, $csrfTokenEnabled = true)
56
    {
57
        $this->formFactory = $formFactory;
58
        $this->filterFactory = $filterFactory;
59
        $this->guesser = $guesser;
60
        $this->csrfTokenEnabled = $csrfTokenEnabled;
61
    }
62
63
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
64
    {
65
        // set default values
66
        $fieldDescription->setAdmin($admin);
67
68
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
69
            [$metadata, $lastPropertyName, $parentAssociationMappings] = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
0 ignored issues
show
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...
The variable $lastPropertyName 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 $parentAssociationMappings 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...
70
71
            // set the default field mapping
72
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
73
                $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName]));
74
75
                if ('string' === $metadata->fieldMappings[$lastPropertyName]['type']) {
76
                    $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only
77
                }
78
            }
79
80
            // set the default association mapping
81
            if (isset($metadata->associationMappings[$lastPropertyName])) {
82
                $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName]));
83
            }
84
85
            $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings));
86
        }
87
88
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
89
        $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
90
91
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
92
            $admin->attachAdminClass($fieldDescription);
93
        }
94
    }
95
96
    public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
97
    {
98
        if (null === $type) {
99
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
100
101
            $type = $guessType->getType();
102
103
            $fieldDescription->setType($type);
104
105
            $options = $guessType->getOptions();
106
107
            foreach ($options as $name => $value) {
108
                if (\is_array($value)) {
109
                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, [])));
110
                } else {
111
                    $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
112
                }
113
            }
114
        } else {
115
            $fieldDescription->setType($type);
116
        }
117
118
        $this->fixFieldDescription($admin, $fieldDescription);
119
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
120
121
        $fieldDescription->mergeOption('field_options', ['required' => false]);
122
        $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
123
124
        if (false !== $filter->getLabel() && !$filter->getLabel()) {
125
            $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
126
        }
127
128
        $datagrid->addFilter($filter);
129
    }
130
131
    /**
132
     * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
133
     */
134
    public function getBaseDatagrid(AdminInterface $admin, array $values = [])
135
    {
136
        $pager = new Pager();
137
        $pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
138
139
        $defaultOptions = [];
140
        if ($this->csrfTokenEnabled) {
141
            $defaultOptions['csrf_protection'] = false;
142
        }
143
144
        $formBuilder = $this->formFactory->createNamedBuilder('filter', FormType::class, [], $defaultOptions);
145
146
        return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
147
    }
148
}
149