Skip to content

Conversation

@kravietz
Copy link

Copy link
Member

@charettes charettes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require tests for CSRF_COOKIE_SAMESITE as well and mention in the release notes. Looks like this will require adjustment to the stdlib http.cookie module as noted by Alex on the ticket, see python/cpython#214. We'd also have to make sure this doesn't break on browses that don't support the SameSite directive.

LANGUAGE_COOKIE_AGE = None
LANGUAGE_COOKIE_DOMAIN = None
LANGUAGE_COOKIE_PATH = '/'
LANGUAGE_COOKIE_SAMESITE = 'lax'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is worth adding for the language cookie as it's not carrying any critical data (notice how LANGUAGE_COOKIE_SECURE and LANGUAGE_COOKIE_HTTPONLY don't exist either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't bother, there's nothing bad you can do with cross-origin language headers.

domain=settings.SESSION_COOKIE_DOMAIN,
secure=settings.SESSION_COOKIE_SECURE or None,
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
samesite=settings.SESSION_COOKIE_SAMESITE or None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a trailing comma.

path=settings.SESSION_COOKIE_PATH,
secure=settings.SESSION_COOKIE_SECURE or None,
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
samesite=settings.SESSION_COOKIE_SAMESITE or None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a trailing comma.

if httponly:
self.cookies[key]['httponly'] = True
if samesite:
self.cookies[key]['samesite'] = samesite.capitalize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for capitalizing it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve the capitalization from the original standard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right! I misread this as upper() somehow.

Copy link
Member

@jdufresne jdufresne Apr 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just expect the setting to be the correct capitalization, 'Lax' or 'Strict'? Doing capitalize() feels like unnecessary post-processing to me. Is it a matter of settings aesthetics?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, corrected this.

path=settings.CSRF_COOKIE_PATH,
secure=settings.CSRF_COOKIE_SECURE,
httponly=settings.CSRF_COOKIE_HTTPONLY,
samesite=settings.CSRF_COOKIE_SAMESITE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a trailing comma.

max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN,
samesite=settings.LANGUAGE_COOKIE_SAMESITE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove references to LANGUAGE_COOKIE_SAMESITE.

from being sent in cross-site requests.

See :setting:`SESSION_COOKIE_SAMESITE` for details on ``SameSite``.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove reference to LANGUAGE_COOKIE_SAMESITE.

# Handle the response through the middleware
response = middleware.process_response(request, response)
self.assertTrue(
response.cookies[settings.SESSION_COOKIE_NAME]['samesite'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the explicit value. This should fit on a single line.

@charettes charettes changed the title Add support for SameSite cookie flag Fixed #27863 -- Added support for the SameSite cookie flag. Apr 20, 2017
@alex
Copy link
Member

alex commented Apr 20, 2017

Thanks for working on this!

domain=settings.SESSION_COOKIE_DOMAIN,
secure=settings.SESSION_COOKIE_SECURE or None,
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
samesite=settings.SESSION_COOKIE_SAMESITE or None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the code comment, SESSION_COOKIE_SAMESITE can be 'lax', 'strict' or None. When SESSION_COOKIE_SAMESITE is set to None, does this evaluate to None or None? If so, I don't think we need the or None, right? (Same in django/contrib/sessions/middleware.py)

@alex
Copy link
Member

alex commented Apr 21, 2017

This probably deserves a mention in the release notes, I don't remember how we maintain those these days.

@kravietz
Copy link
Author

kravietz commented Apr 25, 2017

Release note added to 1.11.1. @alex @charettes

@charettes
Copy link
Member

@kravietz the code looks good but until SameSite support lands in the stdlib I'm afraid we'll have to adapt our vendored version of cookies.

@kravietz
Copy link
Author

kravietz commented May 3, 2017

@charettes Any suggestions what would be the best approach? The factory Morsel won't serialize anything that is not in the _reserved dict. I have started patching it a bit like it was done with the Django's SimpleCookie but with Morsel it would be much more ugly, essentially rewriting half of the BaseCookie class...

@charettes
Copy link
Member

@kravietz I see, I really can't think of any other solution unfortunately.

@kravietz
Copy link
Author

kravietz commented May 4, 2017

@charettes Actually, the patching didn't seem to be that hard. It seems to be working now. Also added a test specifically for the patched SimpleCookie and Morsel version.

@OllieBuilds
Copy link

@kravietz Are you still working on this PR? Mozilla Observatory made changes that now check for SameSite (mozilla/http-observatory#313), and I think this would meet these new requirements.

@script3r
Copy link

@kravietz looking for help with this patch series? this helps reduce Spectre's attack surface. I'd love to help get this merged upstream.

@alex
Copy link
Member

alex commented Feb 21, 2018

@script3r This hasn't been updated for a while, if you're interested in working on it I think you're good to take this and run with it.

@wagslane
Copy link

wagslane commented Mar 3, 2018

Is there anyway to set the flag currently in django 1.11? Some kind of workaround?

@alex
Copy link
Member

alex commented Apr 7, 2018

Rebased and submitted as #9860

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants