* ``secure``
* ``version``
* ``httponly``
+ * ``samesite``
The attribute :attr:`httponly` specifies that the cookie is only transferred
in HTTP requests, and is not accessible through JavaScript. This is intended
to mitigate some forms of cross-site scripting.
+ The attribute :attr:`samesite` specifies that the browser is not allowed to
+ send the cookie along with cross-site requests. This helps to mitigate CSRF
+ attacks. Valid values for this attribute are "Strict" and "Lax".
+
The keys are case-insensitive and their default value is ``''``.
.. versionchanged:: 3.5
:attr:`~Morsel.coded_value` are read-only. Use :meth:`~Morsel.set` for
setting them.
+ .. versionchanged:: 3.8
+ Added support for the :attr:`samesite` attribute.
+
.. attribute:: Morsel.value
self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure')
+ def test_samesite_attrs(self):
+ samesite_values = ['Strict', 'Lax', 'strict', 'lax']
+ for val in samesite_values:
+ with self.subTest(val=val):
+ C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
+ C['Customer']['samesite'] = val
+ self.assertEqual(C.output(),
+ 'Set-Cookie: Customer="WILE_E_COYOTE"; SameSite=%s' % val)
+
+ C = cookies.SimpleCookie()
+ C.load('Customer="WILL_E_COYOTE"; SameSite=%s' % val)
+ self.assertEqual(C['Customer']['samesite'], val)
+
def test_secure_httponly_false_if_not_present(self):
C = cookies.SimpleCookie()
C.load('eggs=scrambled; Path=/bacon')