Issues (12)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/Identifier/Ldap/ExtensionAdapter.php (7 issues)

1
<?php
2
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12
 * @link          https://cakephp.org CakePHP(tm) Project
13
 * @since         1.0.0
14
 * @license       https://opensource.org/licenses/mit-license.php MIT License
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phauthentic\Authentication\Identifier\Ldap;
20
21
use ErrorException;
22
use RuntimeException;
23
24
/**
25
 * Provides a very thin OOP wrapper around the ldap_* functions.
26
 *
27
 * We don't need and want a huge LDAP lib for our purpose.
28
 *
29
 * But this makes it easier to unit test code that is using LDAP because we can
30
 * mock it very easy. It also provides some convenience.
31
 */
32
class ExtensionAdapter implements AdapterInterface
33
{
34
    /**
35
     * LDAP Object
36
     *
37
     * @var \LDAP\Connection|null
0 ignored issues
show
The type LDAP\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
     */
39
    protected $connection;
40
41
    /**
42
     * Constructor
43
     *
44
     * @throws \RuntimeException
45
     */
46
    public function __construct()
47
    {
48
        if (!extension_loaded('ldap')) {
49
            throw new RuntimeException('You must enable the ldap extension to use the LDAP identifier.');
50
        }
51
52
        if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) {
53
            define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
54
        }
55
    }
56
57
    /**
58
     * Bind to LDAP directory
59
     *
60
     * @param string $bind Bind rdn
61
     * @param string $password Bind password
62
     * @return bool
63
     * @throws \ErrorException
64
     */
65
    public function bind(string $bind, string $password): bool
66
    {
67
        $this->setErrorHandler();
68
        $result = ldap_bind($this->getConnection(), $bind, $password);
0 ignored issues
show
$this->getConnection() of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $result = ldap_bind(/** @scrutinizer ignore-type */ $this->getConnection(), $bind, $password);
Loading history...
69
        $this->unsetErrorHandler();
70
71
        return $result;
72
    }
73
74
    /**
75
     * Get the LDAP connection
76
     *
77
     * @return mixed
78
     * @throws \RuntimeException If the connection is empty
79
     */
80
    public function getConnection()
81
    {
82
        if (empty($this->connection)) {
83
            throw new RuntimeException('You are not connected to a LDAP server.');
84
        }
85
86
        return $this->connection;
87
    }
88
89
    /**
90
     * Connect to an LDAP server
91
     *
92
     * @param string $host Hostname
93
     * @param int $port Port
94
     * @param array<int, int|bool|string> $options Additional LDAP options
95
     * @return void
96
     * @throws \ErrorException
97
     */
98
    public function connect(string $host, int $port, array $options): void
99
    {
100
        $this->setErrorHandler();
101
        $this->connection = ldap_connect($host, $port) ?: null;
0 ignored issues
show
Documentation Bug introduced by
It seems like ldap_connect($host, $port) ?: null can also be of type resource. However, the property $connection is declared as type LDAP\Connection|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
102
        $this->unsetErrorHandler();
103
104
        if (is_array($options)) {
0 ignored issues
show
The condition is_array($options) is always true.
Loading history...
105
            foreach ($options as $option => $value) {
106
                $this->setOption($option, $value);
107
            }
108
        }
109
    }
110
111
    /**
112
     *  Set the value of the given option
113
     *
114
     * @param int $option Option to set
115
     * @param int|bool|string $value The new value for the specified option
116
     * @return void
117
     */
118
    public function setOption(int $option, $value)
119
    {
120
        $this->setErrorHandler();
121
        ldap_set_option($this->getConnection(), $option, $value);
0 ignored issues
show
$this->getConnection() of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_set_option(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        ldap_set_option(/** @scrutinizer ignore-type */ $this->getConnection(), $option, $value);
Loading history...
122
        $this->unsetErrorHandler();
123
    }
124
125
    /**
126
     * Get the current value for given option
127
     *
128
     * @param int $option Option to get
129
     * @return mixed This will be set to the option value.
130
     */
131
    public function getOption($option)
132
    {
133
        $returnValue = null;
134
        $this->setErrorHandler();
135
        ldap_get_option($this->getConnection(), $option, $returnValue);
0 ignored issues
show
$this->getConnection() of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_get_option(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        ldap_get_option(/** @scrutinizer ignore-type */ $this->getConnection(), $option, $returnValue);
Loading history...
136
        $this->unsetErrorHandler();
137
138
        return $returnValue;
139
    }
140
141
    /**
142
     * Get the diagnostic message
143
     *
144
     * @return string|null
145
     */
146
    public function getDiagnosticMessage()
147
    {
148
        return $this->getOption(LDAP_OPT_DIAGNOSTIC_MESSAGE);
149
    }
150
151
    /**
152
     * Unbind from LDAP directory
153
     *
154
     * @return void
155
     */
156
    public function unbind()
157
    {
158
        $this->setErrorHandler();
159
        if ($this->connection) {
160
            ldap_unbind($this->connection);
0 ignored issues
show
$this->connection of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_unbind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            ldap_unbind(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
161
        }
162
        $this->unsetErrorHandler();
163
164
        $this->connection = null;
165
    }
166
167
    /**
168
     * Set an error handler to turn LDAP errors into exceptions
169
     *
170
     * @return void
171
     * @throws \ErrorException
172
     */
173
    protected function setErrorHandler()
174
    {
175
        set_error_handler(
176
            function ($errorNumber, $errorText) {
177
                 throw new ErrorException($errorText);
178
            },
179
            E_ALL
180
        );
181
    }
182
183
    /**
184
     * Restore the error handler
185
     *
186
     * @return void
187
     */
188
    protected function unsetErrorHandler()
189
    {
190
        restore_error_handler();
191
    }
192
}
193