From a6d31a7eaf8b25e47673ec7d7a9702b9d1ec7e94 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:00:13 -0800 Subject: [PATCH] Feature: add PAPERLESS_COOKIE_SECURE to mark session, CSRF and language cookies Secure --- docs/configuration.md | 14 ++++++++++++++ src/paperless/settings/__init__.py | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 5b43c8540..a6d64ff41 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -641,6 +641,20 @@ you're running two instances of paperless on the same host. Defaults to `""`, which does not alter the cookie names. +#### [`PAPERLESS_COOKIE_SECURE=`](#PAPERLESS_COOKIE_SECURE) {#PAPERLESS_COOKIE_SECURE} + +: Marks the session, CSRF and language cookies with the `Secure` attribute, so browsers only send them over HTTPS. This sets the Django settings [`SESSION_COOKIE_SECURE`](https://docs.djangoproject.com/en/stable/ref/settings/#session-cookie-secure), [`CSRF_COOKIE_SECURE`](https://docs.djangoproject.com/en/stable/ref/settings/#csrf-cookie-secure) and [`LANGUAGE_COOKIE_SECURE`](https://docs.djangoproject.com/en/stable/ref/settings/#language-cookie-secure) together. + + Enable this if you access paperless exclusively over HTTPS, for example behind a reverse proxy which terminates TLS. + + !!! warning + + Browsers will not send `Secure` cookies over plain HTTP, so logging in over HTTP, for example directly via a LAN address which bypasses the reverse proxy, will fail. Some browsers make an exception for `localhost`. + + After changing this, you may have to login again. + + Defaults to `false`. + #### [`PAPERLESS_ENABLE_HTTP_REMOTE_USER=`](#PAPERLESS_ENABLE_HTTP_REMOTE_USER) {#PAPERLESS_ENABLE_HTTP_REMOTE_USER} : Allows authentication via HTTP_REMOTE_USER which is used by some SSO diff --git a/src/paperless/settings/__init__.py b/src/paperless/settings/__init__.py index dca4b3537..9856aea64 100644 --- a/src/paperless/settings/__init__.py +++ b/src/paperless/settings/__init__.py @@ -556,6 +556,13 @@ CSRF_COOKIE_NAME = f"{COOKIE_PREFIX}csrftoken" SESSION_COOKIE_NAME = f"{COOKIE_PREFIX}sessionid" LANGUAGE_COOKIE_NAME = f"{COOKIE_PREFIX}django_language" +# Mark the session, CSRF and language cookies as Secure, so browsers only send +# them over HTTPS. Off by default, as it breaks any access over plain HTTP. +COOKIE_SECURE = get_bool_from_env("PAPERLESS_COOKIE_SECURE", "false") +SESSION_COOKIE_SECURE = COOKIE_SECURE +CSRF_COOKIE_SECURE = COOKIE_SECURE +LANGUAGE_COOKIE_SECURE = COOKIE_SECURE + EMAIL_CERTIFICATE_FILE = get_path_from_env("PAPERLESS_EMAIL_CERTIFICATE_LOCATION") EMAIL_ALLOW_INTERNAL_HOSTS = get_bool_from_env( "PAPERLESS_EMAIL_ALLOW_INTERNAL_HOSTS",