Skip to content

[Bug] Decoding problem of UTF-8 strings (used e.g. for serialization in session) #698

@lhaemmerle

Description

@lhaemmerle

Environment:

  • LDAP Server Type: OpenLDAP
  • PHP Version: 8.1

Describe the bug:
We want to store LDAP user objects (extends LdapRecord\Models\Model) in the Laravel session (serialization). However, this causes the problem that LDAP users with names containing Umlaut characters (e.g. "René") are wrongly decoded when the session is loaded (=LDAP user unserialized) again. The UTF-8 string "René" is considered an 'ISO-8859-1' encoded string and converted to UTF-8, which then becomes "René".

It seems that the cause for this a bug in the function decodeValue(string $value) in HasAttributes.php:
https://github.com/DirectoryTree/LdapRecord/blob/master/src/Models/Concerns/HasAttributes.php#L223

I suspect the condition in

if (MbString::isLoaded() && MbString::isUtf8($value)) {
      return mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
}

Should rather be (notice the !):

if (MbString::isLoaded() && !MbString::isUtf8($value)) {
      return mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
}

because it makes no sense to convert a string that is already utf-8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions