Skip to content
This repository

Convert unicode tokens to 'str' #314

Merged
merged 1 commit into from over 2 years ago

2 participants

Brian Sutherland Chris McDonough
Brian Sutherland

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.

Brian Sutherland 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
Chris McDonough mcdonc merged commit c3c0bea into from October 11, 2011
Chris McDonough mcdonc closed this October 11, 2011
Chris McDonough
Owner

Thank you Brian...

Chris McDonough mcdonc referenced this pull request from a commit November 20, 2011
Chris McDonough - 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

Showing 1 unique commit by 1 author.

Oct 11, 2011
Brian Sutherland 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
This page is out of date. Refresh to see the latest.
5  pyramid/authentication.py
@@ -726,7 +726,8 @@ def remember(self, request, userid, max_age=None, tokens=()):
726 726
             encoding, encoder = encoding_data
727 727
             userid = encoder(userid)
728 728
             user_data = 'userid_type:%s' % encoding
729  
-        
  729
+
  730
+        new_tokens = []
730 731
         for token in tokens:
731 732
             if isinstance(token, text_type):
732 733
                 try:
@@ -735,6 +736,8 @@ def remember(self, request, userid, max_age=None, tokens=()):
735 736
                     raise ValueError("Invalid token %r" % (token,))
736 737
             if not (isinstance(token, str) and VALID_TOKEN.match(token)):
737 738
                 raise ValueError("Invalid token %r" % (token,))
  739
+            new_tokens.append(token)
  740
+        tokens = tuple(new_tokens)
738 741
 
739 742
         if hasattr(request, '_authtkt_reissued'):
740 743
             request._authtkt_reissue_revoked = True
6  pyramid/tests/test_authentication.py
@@ -703,7 +703,7 @@ def test_identify_cookie_reissue_with_tokens_default(self):
703 703
         request.callbacks[0](None, response)
704 704
         self.assertEqual(len(response.headerlist), 3)
705 705
         self.assertEqual(response.headerlist[0][0], 'Set-Cookie')
706  
-        self.assertTrue("'tokens': []" in response.headerlist[0][1])
  706
+        self.assertTrue("'tokens': ()" in response.headerlist[0][1])
707 707
 
708 708
     def test_remember(self):
709 709
         helper = self._makeOne('secret')
@@ -912,7 +912,9 @@ def test_remember_unicode_but_ascii_token(self):
912 912
         helper = self._makeOne('secret')
913 913
         request = self._makeRequest()
914 914
         la = text_(b'foo', 'utf-8')
915  
-        helper.remember(request, 'other', tokens=(la,))
  915
+        result = helper.remember(request, 'other', tokens=(la,))
  916
+        # tokens must be str type on both Python 2 and 3
  917
+        self.assertTrue("'tokens': ('foo',)" in result[0][1])
916 918
 
917 919
     def test_remember_nonascii_token(self):
918 920
         helper = self._makeOne('secret')
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.