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.

Security/User/LdapUserProvider.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
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\Security\User;
12
13
use LdapTools\BatchModify\BatchCollection;
14
use LdapTools\Bundle\LdapToolsBundle\Event\LoadUserEvent;
15
use LdapTools\Exception\EmptyResultException;
16
use LdapTools\Exception\MultiResultException;
17
use LdapTools\LdapManager;
18
use LdapTools\Object\LdapObject;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
21
use Symfony\Component\Security\Core\User\UserInterface;
22
use Symfony\Component\Security\Core\User\UserProviderInterface;
23
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
24
25
/**
26
 * Loads a user from LDAP.
27
 *
28
 * @author Chad Sikorra <[email protected]>
29
 */
30
class LdapUserProvider implements UserProviderInterface
31
{
32
    /**
33
     * @var LdapManager
34
     */
35
    protected $ldap;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    protected $dispatcher;
41
42
    /**
43
     * @var LdapRoleMapper
44
     */
45
    protected $roleMapper;
46
47
    /**
48
     * @var array Default attributes selected for the Advanced User Interface.
49
     */
50
    protected $defaultAttributes = [
51
        'username',
52
        'guid',
53
        'accountExpirationDate',
54
        'enabled',
55
        'groups',
56
        'locked',
57
        'passwordMustChange',
58
    ];
59
60
    /**
61
     * @var array
62
     */
63
    protected $options = [
64
        'refresh_user_roles' => false,
65
        'refresh_user_attributes' => false,
66
        'search_base' => null,
67
        'ldap_object_type' => 'user',
68
        'user' => LdapUser::class,
69
        'additional_attributes' => [],
70
    ];
71
72
    /**
73
     * @param LdapManager $ldap
74
     * @param EventDispatcherInterface $dispatcher
75
     * @param LdapRoleMapper $roleMapper
76
     * @param array $options
77
     */
78
    public function __construct(LdapManager $ldap, EventDispatcherInterface $dispatcher, LdapRoleMapper $roleMapper, array $options)
79
    {
80
        $this->ldap = $ldap;
81
        $this->dispatcher = $dispatcher;
82
        $this->roleMapper = $roleMapper;
83
        $this->options = array_merge($this->options, $options);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function loadUserByUsername($username)
90
    {
91
        $this->dispatcher->dispatch(LoadUserEvent::BEFORE, new LoadUserEvent($username, $this->ldap->getDomainContext()));
92
        $ldapUser = $this->getLdapUser('username', $username);
93
        $user = $this->constructUserClass($ldapUser);
94
        $this->roleMapper->setRoles($user);
95
        $this->dispatcher->dispatch(LoadUserEvent::AFTER, new LoadUserEvent($username, $this->ldap->getDomainContext(), $user, $ldapUser));
0 ignored issues
show
$user is of type object<LdapTools\Bundle\...User\LdapUserInterface>, but the function expects a null|object<Symfony\Comp...ore\User\UserInterface>.

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...
96
97
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $user; (LdapTools\Bundle\LdapToo...\User\LdapUserInterface) is incompatible with the return type declared by the interface Symfony\Component\Securi...ace::loadUserByUsername of type Symfony\Component\Security\Core\User\UserInterface.

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...
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function refreshUser(UserInterface $user)
104
    {
105
        if (!$user instanceof LdapUserInterface) {
106
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
107
        }
108
        $roles = $user->getRoles();
109
110
        if ($this->options['refresh_user_attributes']) {
111
            $user = $this->constructUserClass($this->getLdapUser('guid', $user->getLdapGuid()));
112
        }
113
        if ($this->options['refresh_user_roles']) {
114
            $this->roleMapper->setRoles($user);
115
        } else {
116
            $user->setRoles($roles);
117
        }
118
119
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $user; (LdapTools\Bundle\LdapToo...\User\LdapUserInterface) is incompatible with the return type declared by the interface Symfony\Component\Securi...rInterface::refreshUser of type Symfony\Component\Security\Core\User\UserInterface.

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...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function supportsClass($class)
126
    {
127
        return is_subclass_of($class, LdapUserInterface::class);
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \LdapTools\Bundle\LdapTo...dapUserInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
128
    }
129
130
    /**
131
     * Search for, and return, the LDAP user by a specific attribute.
132
     *
133
     * @param string $attribute
134
     * @param string $value
135
     * @return LdapObject
136
     */
137
    public function getLdapUser($attribute, $value)
138
    {
139
        try {
140
            $query = $this->ldap->buildLdapQuery()
141
                ->select($this->getAttributesToSelect())
142
                ->from($this->options['ldap_object_type'])
143
                ->where([$attribute => $value]);
144
            if (!is_null($this->options['search_base'])) {
145
                $query->setBaseDn($this->options['search_base']);
146
            }
147
            return $query->getLdapQuery()->getSingleResult();
0 ignored issues
show
Bug Compatibility introduced by
The expression $query->getLdapQuery()->getSingleResult(); of type array|LdapTools\Object\LdapObject adds the type array to the return on line 147 which is incompatible with the return type documented by LdapTools\Bundle\LdapToo...erProvider::getLdapUser of type LdapTools\Object\LdapObject.
Loading history...
148
        } catch (EmptyResultException $e) {
149
            throw new UsernameNotFoundException(sprintf('Username "%s" was not found.', $value));
150
        } catch (MultiResultException $e) {
151
            throw new UsernameNotFoundException(sprintf('Multiple results for "%s" were found.', $value));
152
        }
153
    }
154
155
    /**
156
     * Get all the attributes that should be selected for when querying LDAP.
157
     *
158
     * @return array
159
     */
160
    protected function getAttributesToSelect()
161
    {
162
        return array_values(array_unique(array_filter(array_merge(
163
            $this->defaultAttributes,
164
            $this->options['additional_attributes']
165
        ))));
166
    }
167
168
    /**
169
     * @param LdapObject $ldapObject
170
     * @return LdapUserInterface
171
     */
172
    protected function constructUserClass(LdapObject $ldapObject)
173
    {
174
        if (!$this->supportsClass($this->options['user'])) {
175
            throw new UnsupportedUserException(sprintf(
176
                'The LDAP user provider class "%s" must implement "%s".',
177
                $this->options['user'],
178
                LdapUserInterface::class
179
            ));
180
        }
181
182
        $errorMessage = 'Unable to instantiate user class "%s". Error was: %s';
183
        try {
184
            /** @var LdapUserInterface $user */
185
            $user = new $this->options['user']();
186
            $user->setUsername($ldapObject->get('username'));
187
            $user->setLdapGuid($ldapObject->get('guid'));
188
        } catch (\Throwable $e) {
0 ignored issues
show
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
189
            throw new UnsupportedUserException(sprintf($errorMessage, $this->options['user'], $e->getMessage()));
190
        // Unlikely to help much in PHP 5.6, but oh well...
191
        } catch (\Exception $e) {
192
            throw new UnsupportedUserException(sprintf($errorMessage, $this->options['user'], $e->getMessage()));
193
        }
194
        // If the class also happens to extend the LdapTools LdapObject class, then set the attributes and type...
195
        if ($user instanceof LdapObject) {
196
            $this->hydrateLdapObjectUser($ldapObject, $user);
197
        }
198
199
        return $user;
200
    }
201
202
    /**
203
     * @param LdapObject $ldapObject
204
     * @param $user
205
     */
206
    protected function hydrateLdapObjectUser(LdapObject $ldapObject, LdapObject $user)
207
    {
208
        $user->setBatchCollection(new BatchCollection($ldapObject->get('dn')));
209
        $user->refresh($ldapObject->toArray());
210
211
        // This is to avoid the constructor
212
        $refObject = new \ReflectionObject($user);
213
        $refProperty = $refObject->getProperty('type');
214
        $refProperty->setAccessible(true);
215
        $refProperty->setValue($user, $this->options['ldap_object_type']);
216
    }
217
}
218