Projet

Général

Profil

0001-utils-use-iri_to_uri-uri_to_iri-to-manipulate-URLs-i.patch

Benjamin Dauvergne, 12 décembre 2018 18:08

Télécharger (4,68 ko)

Voir les différences:

Subject: [PATCH] utils: use iri_to_uri/uri_to_iri to manipulate URLs in
 make_url (fixes #28935)

This patch add non-ASCII characters to URLs in OIDC and CAS tests to
check make_url() works correctly.
 src/authentic2/utils.py | 13 +++++++------
 tests/test_cas.py       |  2 +-
 tests/test_idp_oidc.py  |  4 ++--
 tests/utils.py          |  3 ++-
 4 files changed, 12 insertions(+), 10 deletions(-)
src/authentic2/utils.py
35 35
from django.contrib import messages
36 36
from django.utils.functional import empty
37 37
from django.utils.http import urlsafe_base64_encode
38
from django.utils.encoding import force_bytes
38
from django.utils.encoding import iri_to_uri, force_bytes, uri_to_iri
39 39
from django.shortcuts import render
40 40

  
41 41

  
......
269 269
        url = resolve_url(to, *args, **kwargs)
270 270
    else:
271 271
        url = to
272
    url = iri_to_uri(url)
272 273
    scheme, netloc, path, query_string, o_fragment = urlparse.urlsplit(url)
273
    url = urlparse.urlunsplit((scheme, netloc, path, '', ''))
274
    url = uri_to_iri(urlparse.urlunsplit((scheme, netloc, path, '', '')))
274 275
    fragment = fragment or o_fragment
275 276
    # Django < 1.6 compat, query_string is not optional
276 277
    url_params = QueryDict(query_string=query_string, mutable=True)
......
295 296
            else:
296 297
                url_params.appendlist(key, value)
297 298
    if url_params:
298
        url += '?%s' % url_params.urlencode(safe='/')
299
        url += u'?%s' % url_params.urlencode(safe='/')
299 300
    if fragment:
300
        url += '#%s' % fragment
301
        url += u'#%s' % fragment
301 302
    if absolute:
302 303
        if request:
303 304
            url = request.build_absolute_uri(url)
304 305
        else:
305 306
            raise TypeError('make_url() absolute cannot be used without request')
306
    # URL must be ASCII, always
307
    return url.encode('ascii')
307
    # keep using unicode
308
    return url
308 309

  
309 310

  
310 311
# improvement over django.shortcuts.redirect
tests/test_cas.py
28 28
    LAST_NAME = 'Doe'
29 29
    NAME = 'CAS service'
30 30
    SLUG = 'cas-service'
31
    URL = 'https://casclient.com/'
31
    URL = 'https://casclient.com/%C3%A9/'
32 32
    NAME2 = 'CAS service2'
33 33
    SLUG2 = 'cas-service2'
34 34
    URL2 = 'https://casclient2.com/ https://other.com/'
tests/test_idp_oidc.py
117 117
    response.form.set('slug', 'oidcclient')
118 118
    response.form.set('ou', get_default_ou().pk)
119 119
    response.form.set('unauthorized_url', 'https://example.com/southpark/')
120
    response.form.set('redirect_uris', 'https://example.com/callback')
120
    response.form.set('redirect_uris', 'https://example.com/callbac%C3%A9')
121 121
    for key, value in request.param.iteritems():
122 122
        response.form.set(key, value)
123 123
    response = response.form.submit().follow()
......
397 397
                      fragment=fragment)
398 398
    logrecord = [rec for rec in caplog.records if rec.funcName == 'authorization_error'][0]
399 399
    assert logrecord.levelname == 'WARNING'
400
    assert logrecord.redirect_uri == 'https://example.com/callback'
400
    assert logrecord.redirect_uri == 'https://example.com/callbac%C3%A9'
401 401
    assert 'missing parameter \'response_type\'' in logrecord.message
402 402

  
403 403
    # missing scope
tests/utils.py
8 8
from django.test import TestCase
9 9
from django.core.urlresolvers import reverse
10 10
from django.conf import settings
11
from django.utils.encoding import iri_to_uri
11 12

  
12 13
from authentic2 import utils
13 14

  
......
70 71
           value.
71 72
        '''
72 73
        splitted1 = urlparse.urlsplit(url1)
73
        url2 = utils.make_url(url2, params=kwargs)
74
        url2 = iri_to_uri(utils.make_url(url2, params=kwargs))
74 75
        splitted2 = urlparse.urlsplit(url2)
75 76
        for i, (elt1, elt2) in enumerate(zip(splitted1, splitted2)):
76 77
            if i == 3:
77
-