Issues (180)

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.

code/authenticators/LDAPAuthenticator.php (11 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
 * Class LDAPAuthenticator
4
 *
5
 * Authenticate a user against LDAP, without the single sign-on component.
6
 *
7
 * See SAMLAuthenticator for further information.
8
 */
9
class LDAPAuthenticator extends Authenticator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name = 'LDAP';
0 ignored issues
show
The property $name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    /**
17
     * Set to 'yes' to indicate if this module should look up usernames in LDAP by matching the email addresses.
18
     *
19
     * CAVEAT #1: only set to 'yes' for systems that enforce email uniqueness.
20
     * Otherwise only the first LDAP user with matching email will be accessible.
21
     *
22
     * CAVEAT #2: this is untested for systems that use LDAP with principal style usernames (i.e. [email protected]).
23
     * The system will misunderstand emails for usernames with uncertain outcome.
24
     *
25
     * @var string 'no' or 'yes'
26
     */
27
    private static $allow_email_login = 'no';
0 ignored issues
show
The property $allow_email_login is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    /**
30
     * Set to 'yes' to fallback login attempts to {@link $fallback_authenticator}.
31
     * This will occur if LDAP fails to authenticate the user.
32
     *
33
     * @var string 'no' or 'yes'
34
     */
35
    private static $fallback_authenticator = 'no';
0 ignored issues
show
The property $fallback_authenticator is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * The class of {@link Authenticator} to use as the fallback authenticator.
39
     *
40
     * @var string
41
     */
42
    private static $fallback_authenticator_class = 'MemberAuthenticator';
0 ignored issues
show
The property $fallback_authenticator_class is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
43
44
    /**
45
     * @return string
46
     */
47
    public static function get_name()
48
    {
49
        return Config::inst()->get('LDAPAuthenticator', 'name');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Config::inst()->...uthenticator', 'name'); (array|integer|double|string|boolean) is incompatible with the return type of the parent method Authenticator::get_name of type string|null.

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...
50
    }
51
52
    /**
53
     * @param Controller $controller
54
     * @return LDAPLoginForm
55
     */
56
    public static function get_login_form(Controller $controller)
57
    {
58
        return new LDAPLoginForm($controller, 'LoginForm');
59
    }
60
61
    /**
62
     * Performs the login, but will also create and sync the Member record on-the-fly, if not found.
63
     *
64
     * @param array $data
65
     * @param Form $form
66
     * @return bool|Member|void
67
     * @throws SS_HTTPResponse_Exception
68
     */
69
    public static function authenticate($data, Form $form = null)
70
    {
71
        $service = Injector::inst()->get('LDAPService');
72
        $login = trim($data['Login']);
73
        if (Email::validEmailAddress($login)) {
0 ignored issues
show
Deprecated Code introduced by
The method Email::validEmailAddress() has been deprecated with message: 4.0 Use the "is_valid_address" method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
74
            if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')!='yes') {
75
                $form->sessionMessage(
0 ignored issues
show
It seems like $form is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
76
                    _t(
77
                        'LDAPAuthenticator.PLEASEUSEUSERNAME',
78
                        'Please enter your username instead of your email to log in.'
79
                    ),
80
                    'bad'
81
                );
82
                return;
83
            }
84
85
            $username = $service->getUsernameByEmail($login);
86
87
            // No user found with this email.
88 View Code Duplication
            if (!$username) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
                if (Config::inst()->get('LDAPAuthenticator', 'fallback_authenticator') === 'yes') {
90
                    $fallbackMember = self::fallback_authenticate($data, $form);
91
                    if ($fallbackMember) {
92
                        return $fallbackMember;
93
                    }
94
                }
95
96
                $form->sessionMessage(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials'), 'bad');
97
                return;
98
            }
99
        } else {
100
            $username = $login;
101
        }
102
103
        $result = $service->authenticate($username, $data['Password']);
104
        $success = $result['success'] === true;
105 View Code Duplication
        if (!$success) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            if (Config::inst()->get('LDAPAuthenticator', 'fallback_authenticator') === 'yes') {
107
                $fallbackMember = self::fallback_authenticate($data, $form);
108
                if ($fallbackMember) {
109
                    return $fallbackMember;
110
                }
111
            }
112
113
            if ($form) {
114
                $form->sessionMessage($result['message'], 'bad');
115
            }
116
            return;
117
        }
118
119
        $data = $service->getUserByUsername($result['identity']);
120
        if (!$data) {
121
            if ($form) {
122
                $form->sessionMessage(
123
                    _t('LDAPAuthenticator.PROBLEMFINDINGDATA', 'There was a problem retrieving your user data'),
124
                    'bad'
125
                );
126
            }
127
            return;
128
        }
129
130
        // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields
131
        $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first();
132 View Code Duplication
        if (!($member && $member->exists())) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            $member = new Member();
134
            $member->GUID = $data['objectguid'];
135
        }
136
137
        // Update the users from LDAP so we are sure that the email is correct.
138
        // This will also write the Member record.
139
        $service->updateMemberFromLDAP($member);
140
141
        Session::clear('BackURL');
142
143
        return $member;
144
    }
145
146
    /**
147
     * Try to authenticate using the fallback authenticator.
148
     *
149
     * @param array $data
150
     * @param null|Form $form
151
     * @return null|Member
152
     */
153
    protected static function fallback_authenticate($data, Form $form = null)
154
    {
155
        return call_user_func(
156
            [Config::inst()->get('LDAPAuthenticator', 'fallback_authenticator_class'), 'authenticate'],
157
            array_merge($data, ['Email' => $data['Login']]),
158
            $form
159
        );
160
    }
161
162
}
163