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

Django 4.2 upgrade fixes #3874

Merged
merged 4 commits into from
Feb 13, 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
12 changes: 10 additions & 2 deletions src/csp_post_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,19 @@
wysiwyg_svg_properties = list(css_sanitizer.ALLOWED_SVG_PROPERTIES)


class SafeStringWrapper(SafeString):
# Django 4.2 added slots to the SafeString class (https://docs.djangoproject.com/en/4.2/_modules/django/utils/safestring/#SafeString)
# So we cannot set the _csp_post_processed attribute. This wrapper is a workaround
_csp_post_processed = False


def get_html_id(node):
return str(id(node)) # CPython: memory address, so should be unique enough


def post_process_html(html: str | SafeString, request: HttpRequest | Request) -> str:
def post_process_html(
html: str | SafeStringWrapper, request: HttpRequest | Request
) -> str:
"""
Replacing inline style attributes with an inline <style> element with nonce added.

Expand Down Expand Up @@ -198,7 +206,7 @@ def post_process_html(html: str | SafeString, request: HttpRequest | Request) ->
# run bleach on non-style part
modified_html = bleach_wysiwyg_content(modified_html)

result = mark_safe(f"{style_markup}{modified_html}")
result = SafeStringWrapper(mark_safe(f"{style_markup}{modified_html}"))

# mark result as processed to avoid multiple calls
result._csp_post_processed = True # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion src/openforms/prefill/contrib/demo/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from functools import partial
from typing import Any

from django.utils.crypto import get_random_string
Expand All @@ -12,7 +13,7 @@

CALLBACKS = {
Attributes.random_number: lambda: random.randint(1000, 10_000),
Attributes.random_string: get_random_string,
Attributes.random_string: partial(get_random_string, length=10),
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ def setUpTestData(cls):
)
cls.options = dict(folder_path="/open-forms/")

@classmethod
def addClassCleanup(cls):
# clear the config from cache
clear_caches()

def setUp(self):
super().setUp()

self.msgraph_config_patcher.start()
self.addCleanup(self.msgraph_config_patcher.stop)

@classmethod
def setUpClass(cls):
super().setUpClass()

cls.addClassCleanup(clear_caches)

@patch.object(MockFolder, "upload_file", return_value=None)
def test_submission(self, upload_mock):
data = {"foo": "bar", "some_list": ["value1", "value2"]}
Expand Down Expand Up @@ -212,17 +213,18 @@ def setUpTestData(cls):
),
)

@classmethod
def addClassCleanup(cls):
# clear the config from cache
clear_caches()

def setUp(self):
super().setUp()

self.msgraph_config_patcher.start()
self.addCleanup(self.msgraph_config_patcher.stop)

@classmethod
def setUpClass(cls):
super().setUpClass()

cls.addClassCleanup(clear_caches)

def test_folder_path(self, upload_mock):
submission = SubmissionFactory.from_components(
components_list=[
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/tests/test_csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setUp(self):
self.setUpMocks()

def setUpNonce(self):
middleware = CSPMiddleware()
middleware = CSPMiddleware(get_response=lambda req: None)
factory = APIRequestFactory()
request = factory.get("/irrelevant")
middleware._make_nonce(request)
Expand Down
Loading