From c055ecb368b3ea5103035e5c912ece6ecf591a81 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Fri, 4 Dec 2026 01:00:36 +0100 Subject: [PATCH] Allow same-origin service credential confirmation forms --- src/coulobre/hosting/credential_views.py | 13 ++++++- .../hosting/test_credential_rotation.py | 38 ++++++++++++++++++- .../hosting/test_service_credentials.py | 4 +- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/coulobre/hosting/credential_views.py b/src/coulobre/hosting/credential_views.py index f5c9271..fc763f1 100644 --- a/src/coulobre/hosting/credential_views.py +++ b/src/coulobre/hosting/credential_views.py @@ -61,8 +61,17 @@ class ServiceCredentialView(PermissionRequiredMixin, FormView): raise Http404 if getattr(request, "impersonation", None) is not None: return self.handle_no_permission() - response = super().dispatch(request, *args, **kwargs) - response["Referrer-Policy"] = "no-referrer" + return super().dispatch(request, *args, **kwargs) + + def render_to_response(self, context, **response_kwargs): + response = super().render_to_response(context, **response_kwargs) + # HTTPS form submissions need a referrer when Origin is absent. + # Never send referrers from pages displaying credentials. + response["Referrer-Policy"] = ( + "no-referrer" + if context.get("fields") or context.get("rotation_problem") + else "same-origin" + ) return response def has_permission(self): diff --git a/src/coulobre/hosting/test_credential_rotation.py b/src/coulobre/hosting/test_credential_rotation.py index fbb1184..5214fd7 100644 --- a/src/coulobre/hosting/test_credential_rotation.py +++ b/src/coulobre/hosting/test_credential_rotation.py @@ -4,7 +4,7 @@ from unittest.mock import patch from uuid import uuid4 from django.contrib.auth import get_user_model -from django.test import SimpleTestCase, TestCase, override_settings +from django.test import Client, SimpleTestCase, TestCase, override_settings from django.urls import reverse from django.utils import timezone @@ -71,6 +71,42 @@ class CredentialRotationTests(TestCase): url, {"password": password, "ticket": ticket}, secure=True ) + @override_settings( + LOCAL_MAILBOX_PROVISIONING=True, + MAILBOX_PASSWORD_CHANGES=True, + ) + def test_https_mailbox_rotation_accepts_the_same_origin_form(self): + MailboxProvisioning.objects.create( + mailbox=self.mailbox, finished_at=timezone.now() + ) + client = Client(enforce_csrf_checks=True) + client.force_login(self.owner) + url = self.url("mailbox", self.mailbox) + reply = {"user": self.mailbox.spec.address, "password": REPLACEMENT} + with patch( + "coulobre.hosting.service_credentials.apply_on_local_node", + side_effect=[{"generation": None}, reply], + ) as node: + page = client.get(url, secure=True) + self.assertEqual(page.headers["Referrer-Policy"], "same-origin") + self.assertNotContains(page, REPLACEMENT) + data = { + "csrfmiddlewaretoken": str(page.context["csrf_token"]), + "password": PASSWORD, + "ticket": page.context["form"]["ticket"].value(), + } + self.assertEqual( + client.post(url, data, secure=True).status_code, 403 + ) + self.assertEqual(node.call_count, 1) + result = client.post( + url, data, secure=True, HTTP_REFERER=f"https://testserver{url}" + ) + self.assertContains(result, REPLACEMENT) + self.assertEqual(result.headers["Referrer-Policy"], "no-referrer") + self.assertIn("no-store", result.headers["Cache-Control"]) + self.assertEqual(node.call_args.args[1], MAIL_PASSWORD_SOCKET) + def test_scope_confirmation_and_database_result(self): database = self.database url = self.url("database", database) diff --git a/src/coulobre/hosting/test_service_credentials.py b/src/coulobre/hosting/test_service_credentials.py index 008d39b..e616e72 100644 --- a/src/coulobre/hosting/test_service_credentials.py +++ b/src/coulobre/hosting/test_service_credentials.py @@ -138,7 +138,9 @@ class ServiceCredentialAccessTests(TestCase): self.mailbox.spec, ), ): - self.assertNotContains(self.client.get(url, secure=True), expected) + page = self.client.get(url, secure=True) + self.assertNotContains(page, expected) + self.assertEqual(page.headers["Referrer-Policy"], "same-origin") with patch( "coulobre.hosting.credential_views.fetch_service_credentials", return_value=reply,