Visitar URL original
Fixed #27863 -- Added support for the SameSite cookie flag. by kravietz · Pull Request #8380 · django/django · GitHub
Skip to content

Fixed #27863 -- Added support for the SameSite cookie flag. - #8380

Closed
kravietz wants to merge 24 commits into
django:masterfrom
kravietz:master
Closed

kravietz wants to merge 24 commits into
django:masterfrom
kravietz:master

Conversation

@kravietz

Copy link
Copy Markdown

@charettes charettes left a comment

Copy link
Copy Markdown
Member

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.

Comment thread django/conf/global_settings.py Outdated
LANGUAGE_COOKIE_AGE = None
LANGUAGE_COOKIE_DOMAIN = None
LANGUAGE_COOKIE_PATH = '/'
LANGUAGE_COOKIE_SAMESITE = 'lax'

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread django/contrib/sessions/middleware.py Outdated
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
Copy Markdown
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.

Comment thread django/http/response.py Outdated
if httponly:
self.cookies[key]['httponly'] = True
if samesite:
self.cookies[key]['samesite'] = samesite.capitalize()

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@jdufresne jdufresne Apr 21, 2017 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

Comment thread django/middleware/csrf.py Outdated
path=settings.CSRF_COOKIE_PATH,
secure=settings.CSRF_COOKIE_SECURE,
httponly=settings.CSRF_COOKIE_HTTPONLY,
samesite=settings.CSRF_COOKIE_SAMESITE

Copy link
Copy Markdown
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.

Comment thread django/views/i18n.py Outdated
max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN,
samesite=settings.LANGUAGE_COOKIE_SAMESITE

Copy link
Copy Markdown
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.

Comment thread docs/ref/settings.txt Outdated
from being sent in cross-site requests.

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

Copy link
Copy Markdown
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.

Comment thread tests/sessions_tests/tests.py Outdated
# Handle the response through the middleware
response = middleware.process_response(request, response)
self.assertTrue(
response.cookies[settings.SESSION_COOKIE_NAME]['samesite'])

Copy link
Copy Markdown
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

alex commented Apr 20, 2017

Copy link
Copy Markdown
Member

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
Copy Markdown
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

alex commented Apr 21, 2017

Copy link
Copy Markdown
Member

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

@kravietz

kravietz commented Apr 25, 2017 •

Copy link
Copy Markdown
Author

Release note added to 1.11.1. @alex @charettes

@charettes

Copy link
Copy Markdown
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

kravietz commented May 3, 2017 •

Copy link
Copy Markdown
Author

@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
Copy Markdown
Member

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

@kravietz

kravietz commented May 4, 2017

Copy link
Copy Markdown
Author

@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
Copy Markdown

@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
Copy Markdown

@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

alex commented Feb 21, 2018

Copy link
Copy Markdown
Member

@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

wagslane commented Mar 3, 2018

Copy link
Copy Markdown

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

@alex

alex commented Apr 7, 2018

Copy link
Copy Markdown
Member

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