Closed Bug 656232 Opened 13 years ago Closed 13 years ago

[auth] Unicode comparison problems from LDAP

Categories

(Webtools Graveyard :: Elmo, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: peterbe, Assigned: peterbe)

References

Details

Attachments

(3 files, 1 obsolete file)

If LDAP sends first- or last names in non-ascii byte code it fails to compare against the Unicode strings we have in the database. 

Also there was a bug related to `<userinstance>.last_name` not being set.
Blocks: 655739
Disclaimer: 100% coverage does NOT mean it's perfect in any way.
Attached patch Fixes Unicode problem hopefully (obsolete) — Splinter Review
Note: This patch includes moving auth/backends.py from "apps/l10n_site" to "lib" that's why the big fat "cacert.pem" is taking up so much space of the patch.

Tests are run like this:

    ./manage.py test -s lib.auth.tests

Note that the patch includes a new file called `requirements/dev.txt` which you need to `pip -r` to be able to run the tests. 

This patch assumes that the LDAP sometimes sends the first- and last name incorrectly as LATIN-1 (aka. ISO-8859-1)
Attachment #531571 - Flags: review?(l10n)
This time with -M70% (thanks stas)
Attachment #531571 - Attachment is obsolete: true
Attachment #531571 - Flags: review?(l10n)
UTF-8 in tests.py instead
Attachment #531574 - Attachment is obsolete: true
Attachment #531583 - Flags: review?(l10n)
Comment on attachment 531574 [details] [diff] [review]
Fixes Unicode problem hopefully diff with -M70%

Review of attachment 531574 [details] [diff] [review]:
-----------------------------------------------------------------

r-, we're not doing the latin1 stuff, but just plain unicode. Tested with python shell and discussed over IRC.

The basic failure for anything non-ascii is that the default encoding is ascii, and that throws. Independent on whether the data would be latin-1 or utf-8.

For setting, this probably works thanks to data-mangling hooks in django fields, I guess. Just the comparison fails.

::: apps/l10n_site/auth/backends.py
@@ +90,5 @@
> +            # 'Pet\xc3\xa3r' which is the correct UTF-8 version.
> +            try:
> +                first_name = force_unicode(first_name)
> +            except DjangoUnicodeDecodeError:
> +                first_name = force_unicode(first_name, encoding='latin1')

We don't need to do a latin1 dance here, our data should be utf-8 encoded.

If it's not, no idea what to fall back to, tbh. Log something and leave it empty?

@@ +94,5 @@
> +                first_name = force_unicode(first_name, encoding='latin1')
> +            try:
> +                last_name = force_unicode(last_name)
> +            except DjangoUnicodeDecodeError:
> +                last_name = force_unicode(last_name, encoding='latin1')

... same here.

::: lib/auth/tests.py
@@ +54,5 @@
> +          ('mail=pbengtsson@mozilla.com,...',
> +           {'cn': ['Peter Bengtsson'],
> +            'givenName': ['Pet\xe3r'], # latin-1 byte string
> +            'mail': ['peterbe@mozilla.com'],
> +            'sn': ['Bengtss\xa2n'],

Make these utf-8 strings instead, please?

@@ +131,5 @@
> +        ok_(user)
> +        _first_name = self.fake_user[0][1]['givenName'][0]
> +        eq_(user.first_name, unicode(_first_name, 'latin1'))
> +        _last_name = self.fake_user[0][1]['sn'][0]
> +        eq_(user.last_name, unicode(_last_name, 'latin-1'))

utf-8 here, too.
Attachment #531574 - Attachment is obsolete: false
Attachment #531574 - Flags: review-
Attachment #531583 - Attachment is patch: true
Attachment #531583 - Attachment mime type: text/x-patch → text/plain
Comment on attachment 531583 [details] [diff] [review]
Mocking the ldap non-ascii byte strings in UTF-8 instead

You're adding the ldap tests to build.sh later, I assume?
Attachment #531583 - Flags: review?(l10n) → review+
Landed.
https://github.com/mozilla/elmo/commit/cf27ad585afdca80023c7b5b4e682331f12ec346

It gets included in the way we run tests now. Sadly I forgot to update the script/build.sh file to the new way as I have it set up on Jenkins.
Status: NEW → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
Target Milestone: --- → 1.2
Assignee: nobody → peterbe
Product: Webtools → Webtools Graveyard
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: