Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop unnecessary CSP directives for gold view #11605

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions readthedocs/gold/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re

from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django.test import TestCase
from django.urls import reverse
from django_dynamic_fixture import get

Expand All @@ -11,17 +11,19 @@ def setUp(self):
self.user = get(User)

def test_csp_headers(self):
"""
Test CSP headers aren't altered.

This view originally altered the CSP directives based on whether we were
using the new dashboard. We weren't using inline scripts in this view
however, so this was reverted. The tests remain for now, but aren't
super useful and will break when we change `script-src` in base settings.
"""
self.client.force_login(self.user)
csp_header = "Content-Security-Policy"
script_src_regex = re.compile(r".*\s+script-src [^;]*'unsafe-inline'")
url = reverse("gold_detail")

with override_settings(RTD_EXT_THEME_ENABLED=False):
resp = self.client.get(url)
self.assertEqual(resp.status_code, 200)
self.assertIsNone(script_src_regex.match(resp[csp_header]))

with override_settings(RTD_EXT_THEME_ENABLED=True):
resp = self.client.get(url)
self.assertEqual(resp.status_code, 200)
self.assertTrue(script_src_regex.match(resp[csp_header]))
resp = self.client.get(url)
self.assertEqual(resp.status_code, 200)
self.assertIsNone(script_src_regex.match(resp[csp_header]))
12 changes: 0 additions & 12 deletions readthedocs/gold/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ class GoldSubscription(
form_class = GoldSubscriptionForm
template_name = "gold/subscription_detail.html"

def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
# Allow inline scripts for the gold view.
# We are using inline javascript to initialize Stripe Checkout.
# Allowing inline scripts defeats the purpose of using CSP,
# but we are limiting it to this view.
# TODO: use the `@csp_update` decorator once we are running
# ext-theme by default.
if settings.RTD_EXT_THEME_ENABLED:
response._csp_update = {"script-src": "'unsafe-inline'"}
return response

def get(self, *args, **kwargs):
subscribed = self.request.GET.get("subscribed", None)
if subscribed == "true":
Expand Down