Skip to content

Commit 8105c7c

Browse files
authored
Task #2591: Hardening Cookies Flags (#2580)
1 parent 0e0f684 commit 8105c7c

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

config/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
AUTH_USER_MODEL = "users.User"
137137
CSRF_COOKIE_HTTPONLY = True
138+
CSRF_COOKIE_SAMESITE = "Lax"
138139
# See https://docs.djangoproject.com/en/4.2/ref/settings/#csrf-trusted-origins
139140
csrf_trusted_origins = env.list(
140141
"CSRF_TRUSTED_ORIGINS", default=["http://0.0.0.0", "http://localhost"]
@@ -257,6 +258,10 @@
257258
# Increase default cookie age from 2 to 12 weeks
258259
SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 * 12
259260

261+
# Not "Strict": the social auth providers redirect back cross-site, and that
262+
# request must still carry the session cookie for the callback to resolve.
263+
SESSION_COOKIE_SAMESITE = "Lax"
264+
260265
# Internationalization
261266
# https://docs.djangoproject.com/en/1.10/topics/i18n/
262267

@@ -500,6 +505,8 @@
500505
"HTTP_X_FORWARDED_PROTO",
501506
ACCOUNT_DEFAULT_HTTP_PROTOCOL,
502507
)
508+
SESSION_COOKIE_SECURE = True
509+
CSRF_COOKIE_SECURE = True
503510

504511
# Admin banner configuration
505512
ENV_NAME = env("ENVIRONMENT_NAME", default="Unknown Environment")
@@ -635,7 +642,7 @@
635642

636643
CORS_ALLOW_ALL_ORIGINS = True
637644
CORS_ALLOW_CREDENTIALS = True
638-
SESSION_COOKIE_HTTPONLY = False
645+
SESSION_COOKIE_HTTPONLY = True
639646

640647
CORS_ALLOW_METHODS = (
641648
"DELETE",

templates/users/profile.html

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,11 @@ <h3>{% trans 'Delete Account' %}</h3>
153153

154154
{% include "modal.html" %}
155155

156-
<script>
157-
document.body.addEventListener('htmx:configRequest', function(event) {
158-
/* only set CSRF token if config-sessionid cookie exists.
159-
we don't really need this check while this functionality is limited to this
160-
page, but I'm adding it anyway in case we move this in the future so we don't
161-
overlook it. */
162-
const hasSessionId = document.cookie.split(';').some(function(cookie) {
163-
return cookie.trim().startsWith('config-sessionid=');
164-
});
165-
if (hasSessionId) {
156+
{% if request.user.is_authenticated %}
157+
<script>
158+
document.body.addEventListener('htmx:configRequest', function(event) {
166159
event.detail.headers['X-CSRFToken'] = '{{ csrf_token }}';
167-
}
168-
});
169-
</script>
160+
});
161+
</script>
162+
{% endif %}
170163
{% endblock %}

0 commit comments

Comments
 (0)