CVE-2022-31061: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: GLPI is a Free Asset and IT Management Software package, Data center management, ITIL Service Desk, licenses tracking and software auditing. In affected versions there is a SQL injection vulnerability which is possible on login page. No user credentials are required to exploit this vulnerability. Users are advised to upgrade as soon as possible. There are no known workarounds for this issue.
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 9.8
Published: 2022-06-28T18:15Z
Repository: https://github.com/glpi-project/glpi
Language: PHP
Files changed: src/Auth.php
Commit date: 2022-06-28 12:19:52 +0200
Commit message: Merge pull request from GHSA-w2gc-v2gm-q7wq


Diff: diff --git a/src/Auth.php b/src/Auth.php
index e2da8df8475..0efa2003661 100644
--- a/src/Auth.php
+++ b/src/Auth.php
@@ -734,23 +734,20 @@ class Auth extends CommonGLPI
        // manage the $login_auth (force the auth source of the user account)
         $this->user->fields["auths_id"] = 0;
         if ($login_auth == 'local') {
             $authtype = self::DB_GLPI;
             $this->user->fields["authtype"] = self::DB_GLPI;
-        } else if (strstr($login_auth, '-')) {
-            $auths = explode('-', $login_auth);
-            $this->user->fields["auths_id"] = $auths[1];
-            if ($auths[0] == 'ldap') {
+        } else if (preg_match('/^(?<type>ldap|mail|external)-(?<id>\d+)$/', $login_auth, $auth_matches)) {
+            $this->user->fields["auths_id"] = (int)$auth_matches['id'];
+            if ($auth_matches['type'] == 'ldap') {
                 $authtype = self::LDAP;
-                $this->user->fields["authtype"] = self::LDAP;
-            } else if ($auths[0] == 'mail') {
+            } else if ($auth_matches['type'] == 'mail') {
                 $authtype = self::MAIL;
-                $this->user->fields["authtype"] = self::MAIL;
-            } else if ($auths[0] == 'external') {
+            } else if ($auth_matches['type'] == 'external') {
                 $authtype = self::EXTERNAL;
-                $this->user->fields["authtype"] = self::EXTERNAL;
             }
+            $this->user->fields['authtype'] = $authtype;
         }
         if (!$noauto && ($authtype = self::checkAlternateAuthSystems())) {
             if (
                 $this->getAlternateAuthSystemsUserLogin($authtype)
                 && !empty($this->user->fields['name'])

Vulnerable code:         } else if (strstr($login_auth, '-')) {
            $auths = explode('-', $login_auth);
            $this->user->fields["auths_id"] = $auths[1];
            if ($auths[0] == 'ldap') {
                $this->user->fields["authtype"] = self::LDAP;
            } else if ($auths[0] == 'mail') {
                $this->user->fields["authtype"] = self::MAIL;
            } else if ($auths[0] == 'external') {
                $this->user->fields["authtype"] = self::EXTERNAL;
Fixed code:         } else if (preg_match('/^(?<type>ldap|mail|external)-(?<id>\d+)$/', $login_auth, $auth_matches)) {
            $this->user->fields["auths_id"] = (int)$auth_matches['id'];
            if ($auth_matches['type'] == 'ldap') {
            } else if ($auth_matches['type'] == 'mail') {
            } else if ($auth_matches['type'] == 'external') {
            $this->user->fields['authtype'] = $authtype;