Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30045 closed Bug (needsinfo)

"can't concat str to bytes error" on SessionMiddleware with python 3

Reported by: fmdra Owned by: nobody
Component: contrib.sessions Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

I recently moved to python 3 and django 2.1 (from 2.7.x and 1.11.x, respectively).

I'm hitting this issue:
"can't concat str to bytes error"

I believe the following line needs to be changed from:
return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')
to:
return base64.b64encode(hash.encode() + b":" + serialized.encode()).decode('ascii')

Below is a redacted traceback:

Environment:


Request Method: GET
Request URL: <redacted> 

Django Version: 2.1.4
Python Version: 3.6.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 '<redacted>']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:

File "<redacted>/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "<redacted>/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
  93.             response = self.process_response(request, response)

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/middleware.py" in process_response
  58.                             request.session.save()

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py" in save
  83.         obj = self.create_model_instance(data)

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py" in create_model_instance
  70.             session_data=self.encode(data),

File "<redacted>/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in encode
  98.         return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')

Exception Type: TypeError at <redacted> 
Exception Value: can't concat str to bytes

Change History (3)

comment:1 by Carlton Gibson, 5 years ago

Resolution: needsinfo
Status: newclosed

Hi.

Can you put a breakpoint into encode() here?

What is self.serializer and serialized? The serializer dumps() method should return bytes. Both the shipped serializer classes do this, so there's some question as to how this error could come up.

I recently moved... from 2.7.x...

Are you using a custom serializer that's not correctly using bytes?

You need to reduce this to a minimal case showing Django is at fault. If you need help please see TicketClosingReasons/UseSupportChannels.

comment:2 by Tim Graham, 5 years ago

Component: Uncategorizedcontrib.sessions
Description: modified (diff)
Type: UncategorizedBug

comment:3 by Simon Charette, 5 years ago

Something to consider as well is whether this happens when loading a session stored on Python 2 by using Python 3.

If it's the case then it could be worth fixing in master given a lot of users are going to do the LTS jump and likely hit a similar issue because flushing all session data is not always an option.

Note: See TracTickets for help on using tickets.
Back to Top