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/ListBuilder.php (4 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\DoctrineMongoDBAdminBundle\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Builder\ListBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
23
class ListBuilder implements ListBuilderInterface
24
{
25
    protected $guesser;
26
27
    protected $templates = [];
28
29
    public function __construct(TypeGuesserInterface $guesser, array $templates = [])
30
    {
31
        $this->guesser = $guesser;
32
        $this->templates = $templates;
33
    }
34
35
    public function getBaseList(array $options = [])
36
    {
37
        return new FieldDescriptionCollection();
38
    }
39
40
    public function buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
41
    {
42
        if (null === $type) {
43
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
44
            $fieldDescription->setType($guessType->getType());
45
        } else {
46
            $fieldDescription->setType($type);
47
        }
48
49
        $this->fixFieldDescription($admin, $fieldDescription);
50
    }
51
52
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin): void
53
    {
54
        $this->buildField($type, $fieldDescription, $admin);
55
        $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
56
57
        $list->add($fieldDescription);
58
    }
59
60
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void
61
    {
62
        if ('_action' === $fieldDescription->getName() || 'actions' === $fieldDescription->getType()) {
63
            $this->buildActionFieldDescription($fieldDescription);
64
        }
65
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
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
71
72
            // set the default field mapping
73
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
74
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
75
                if (false !== $fieldDescription->getOption('sortable')) {
76
                    $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true));
77
                    $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
78
                    $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
79
                }
80
            }
81
82
            // set the default association mapping
83
            if (isset($metadata->associationMappings[$lastPropertyName])) {
84
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
85
            }
86
87
            $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
88
        }
89
90
        if (!$fieldDescription->getType()) {
91
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
92
        }
93
94
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
95
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
96
97
        if (!$fieldDescription->getTemplate()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDescription->getTemplate() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
98
            if ('id' === $fieldDescription->getType()) {
99
                $fieldDescription->setType('string');
100
            }
101
102
            if ('int' === $fieldDescription->getType()) {
103
                $fieldDescription->setType('integer');
104
            }
105
106
            $template = $this->getTemplate($fieldDescription->getType());
107
108
            if (null === $template) {
109
                if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
110
                    $template = '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig';
111
                } elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
112
                    $template = '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig';
113
                }
114
            }
115
116
            $fieldDescription->setTemplate($template);
117
        }
118
119
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
120
            $admin->attachAdminClass($fieldDescription);
121
        }
122
    }
123
124
    /**
125
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
126
     */
127
    public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
128
    {
129
        if (null === $fieldDescription->getTemplate()) {
130
            $fieldDescription->setTemplate('@SonataAdmin/CRUD/list__action.html.twig');
131
        }
132
133
        if (null === $fieldDescription->getType()) {
134
            $fieldDescription->setType('actions');
135
        }
136
137
        if (null === $fieldDescription->getOption('name')) {
138
            $fieldDescription->setOption('name', 'Action');
139
        }
140
141
        if (null === $fieldDescription->getOption('code')) {
142
            $fieldDescription->setOption('code', 'Action');
143
        }
144
145
        if (null !== $fieldDescription->getOption('actions')) {
146
            $actions = $fieldDescription->getOption('actions');
147
            foreach ($actions as $k => $action) {
148
                if (!isset($action['template'])) {
149
                    $actions[$k]['template'] = sprintf('@SonataAdmin/CRUD/list__action_%s.html.twig', $k);
150
                }
151
            }
152
153
            $fieldDescription->setOption('actions', $actions);
154
        }
155
156
        return $fieldDescription;
157
    }
158
159
    /**
160
     * @param int|string $type
161
     */
162
    private function getTemplate($type): ?string
163
    {
164
        if (!isset($this->templates[$type])) {
165
            return null;
166
        }
167
168
        return $this->templates[$type];
169
    }
170
}
171