Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Convert unicode tokens to 'str' #314

Merged
merged 1 commit into from

2 participants

@jinty

This was resulting in unicode cookie values (and thus headers) on
Python 2 causing mod_wsgi to complain:
TypeError: expected string object for header value

PEP 3333 also says:
"Native" strings (which are always implemented using the type named
str) that are used for request/response headers and metadata

So mod_wsgi is right to complain about unicode headers and Pyramid is
wrong to send them.

@jinty jinty Convert unicode tokens to 'str'
This was resulting in unicode cookie values (and thus headers) on
Python 2 causing mod_wsgi to complain:
        TypeError: expected string object for header value

PEP 3333 also says:
  "Native" strings (which are always implemented using the type named
  str) that are used for request/response headers and metadata

So mod_wsgi is right to complain about unicode headers and Pyramid is
wrong to send them.
c3c0bea
@mcdonc mcdonc merged commit c3c0bea into Pylons:master
@mcdonc
Owner

Thank you Brian...

@mcdonc mcdonc referenced this pull request from a commit
@mcdonc mcdonc - Backport from master: The AuthTktCookieHelper could potentially gen…
…erate

  Unicode headers inappropriately when the ``tokens`` argument to remember
  was used.  See #314.

References issue #314.
4791d7b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 11, 2011
  1. @jinty

    Convert unicode tokens to 'str'

    jinty authored
    This was resulting in unicode cookie values (and thus headers) on
    Python 2 causing mod_wsgi to complain:
            TypeError: expected string object for header value
    
    PEP 3333 also says:
      "Native" strings (which are always implemented using the type named
      str) that are used for request/response headers and metadata
    
    So mod_wsgi is right to complain about unicode headers and Pyramid is
    wrong to send them.
This page is out of date. Refresh to see the latest.
View
5 pyramid/authentication.py
@@ -726,7 +726,8 @@ def remember(self, request, userid, max_age=None, tokens=()):
encoding, encoder = encoding_data
userid = encoder(userid)
user_data = 'userid_type:%s' % encoding
-
+
+ new_tokens = []
for token in tokens:
if isinstance(token, text_type):
try:
@@ -735,6 +736,8 @@ def remember(self, request, userid, max_age=None, tokens=()):
raise ValueError("Invalid token %r" % (token,))
if not (isinstance(token, str) and VALID_TOKEN.match(token)):
raise ValueError("Invalid token %r" % (token,))
+ new_tokens.append(token)
+ tokens = tuple(new_tokens)
if hasattr(request, '_authtkt_reissued'):
request._authtkt_reissue_revoked = True
View
6 pyramid/tests/test_authentication.py
@@ -703,7 +703,7 @@ def test_identify_cookie_reissue_with_tokens_default(self):
request.callbacks[0](None, response)
self.assertEqual(len(response.headerlist), 3)
self.assertEqual(response.headerlist[0][0], 'Set-Cookie')
- self.assertTrue("'tokens': []" in response.headerlist[0][1])
+ self.assertTrue("'tokens': ()" in response.headerlist[0][1])
def test_remember(self):
helper = self._makeOne('secret')
@@ -912,7 +912,9 @@ def test_remember_unicode_but_ascii_token(self):
helper = self._makeOne('secret')
request = self._makeRequest()
la = text_(b'foo', 'utf-8')
- helper.remember(request, 'other', tokens=(la,))
+ result = helper.remember(request, 'other', tokens=(la,))
+ # tokens must be str type on both Python 2 and 3
+ self.assertTrue("'tokens': ('foo',)" in result[0][1])
def test_remember_nonascii_token(self):
helper = self._makeOne('secret')
Something went wrong with that request. Please try again.