Projet

Général

Profil

0001-validators-verify-that-email-s-domain-idna-encoding-.patch

Benjamin Dauvergne, 24 mars 2015 10:47

Télécharger (3,88 ko)

Voir les différences:

Subject: [PATCH] validators: verify that email's domain idna encoding succeed
 before validating it (#6800)

 src/authentic2/tests.py      | 24 +++++++++++++++++++++++-
 src/authentic2/validators.py |  6 +++++-
 2 files changed, 28 insertions(+), 2 deletions(-)
src/authentic2/tests.py
1
# -*- coding: utf-8 -*-
1 2
import re
2 3
import urlparse
3 4
from xml.etree import ElementTree as ET
4 5

  
5 6
import django
6 7
from django.core import mail
7 8
from django.core.urlresolvers import reverse
8 9
from django.test import TestCase
......
276 277
    def test_digits_password_policy(self):
277 278
        from authentic2.validators import validate_password
278 279
        from django.core.exceptions import ValidationError
279 280

  
280 281
        with self.assertRaisesRegexp(ValidationError, 'pasbon'):
281 282
            validate_password('aaa')
282 283
        validate_password('12345678')
283 284

  
285
def can_resolve_dns():
286
    '''Verify that DNS resolving is available'''
287
    import socket
288
    try:
289
        return isinstance(socket.gethostbyname('entrouvert.com'), str)
290
    except:
291
        return False
284 292

  
293
@override_settings(A2_VALIDATE_EMAIL_DOMAIN=can_resolve_dns(), LANGUAGE_CODE='en-us')
285 294
class RegistrationTests(TestCase):
286 295
    def setUp(self):
287 296
        self.client = Client()
288 297

  
289
    @override_settings(A2_VALIDATE_EMAIL_DOMAIN=False)
298
    def test_registration_bad_email(self):
299
        response = self.client.post(reverse('registration_register'),
300
                                    {'email': 'fred@0d..be'})
301
        self.assertEqual(response.status_code, 200)
302
        self.assertContains(response, '<ul class="errorlist"><li>Email domain is invalid</li></ul>', count=1, html=True)
303
        response = self.client.post(reverse('registration_register'),
304
                                    {'email': u'ééééé'})
305
        self.assertEqual(response.status_code, 200)
306
        self.assertContains(response, '<ul class="errorlist"><li>Email domain is invalid</li></ul>', count=1, html=True)
307
        response = self.client.post(reverse('registration_register'),
308
                                    {'email': u''})
309
        self.assertEqual(response.status_code, 200)
310
        self.assertContains(response, ' <ul class="errorlist"><li>This field is required.</li></ul>', count=1, html=True)
311

  
290 312
    def test_registration(self):
291 313
        response = self.client.post(reverse('registration_register'),
292 314
                                    {'email': 'testbot@entrouvert.com'})
293 315
        self.assertRedirects(response, reverse('registration_complete'))
294 316
        self.assertEqual(len(mail.outbox), 1)
295 317
        links = re.findall('https?://.*/', mail.outbox[0].body)
296 318
        self.assertIsInstance(links, list) and self.assertIsNot(links, [])
297 319
        link = links[0]
src/authentic2/validators.py
21 21

  
22 22
    def check_mxs(self, domain):
23 23
        try:
24 24
            mxs = dns.resolver.query(domain, 'MX')
25 25
            mxs = [str(mx.exchange).rstrip('.') for mx in mxs]
26 26
            return mxs
27 27
        except dns.exception.DNSException:
28 28
            try:
29
                socket.gethostbyname(force_text(domain).encode('idna'))
29
                idna_encoded = force_text(domain).encode('idna')
30
            except UnicodeError:
31
                return []
32
            try:
33
                socket.gethostbyname(idna_encoded)
30 34
                return [domain]
31 35
            except socket.error:
32 36
                pass
33 37
        return []
34 38

  
35 39

  
36 40
    def __call__(self, value):
37 41
        try:
38
-