Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
feat(edjs): remove deprecated behaviors (#18976)
Mikailc68b790c3950
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4a4d40687d..d46e05447d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -65,6 +65,21 @@ permissions.### EditorJS (Rich Text Editor)- Made the EditorJS parser stricter. We no longer accept unknown/extra fields. - #18969 by @NyanKiyoshi+- Removed the following deprecated behaviors:++ - `EDITOR_JS_LINK_REL` configuration behavior has changed.+ Links rendered by EditorJS (`<a href="..." rel="...">`) now default to+ `rel="noopener noreferrer"` instead of an empty value.+ Learn more in the [documentation][EDITOR_JS_LINK_REL].+ - `UNSAFE_EDITOR_JS_ALLOWED_URL_SCHEMES` has been removed.+ It's no longer possible to extend the list of allowed URL schemes via settings.++ If you require support for additional URL schemes, open a request:+ https://github.com/saleor/saleor/issues++ (Via #18976 by @NyanKiyoshi)++[EDITOR_JS_LINK_REL]: https://docs.saleor.io/setup/configuration#editor_js_link_rel### Other changesdiff --git a/saleor/core/cleaners/html.py b/saleor/core/cleaners/html.pyindex b3217592a0..fce2512ea2 100644--- a/saleor/core/cleaners/html.py+++ b/saleor/core/cleaners/html.py@@ -1,18 +1,14 @@import jsonimport os-import warningsfrom copy import deepcopyfrom dataclasses import dataclass, fieldfrom typing import Selfimport nh3-from ..deprecations import SaleorDeprecationWarning-@dataclassclass HtmlCleanerSettings:- allowed_schemes: set[str] = field(default_factory=set)allowed_attributes: dict[str, set[str]] = field(default_factory=lambda: deepcopy(nh3.ALLOWED_ATTRIBUTES))@@ -37,31 +33,7 @@ class HtmlCleanerSettings:def reload(self) -> Self:# NOTE: 'or None' is needed as blank string should be treated as None- self.link_rel = os.getenv("EDITOR_JS_LINK_REL") or None- if self.link_rel is None:- warnings.warn(- (- "EDITOR_JS_LINK_REL=None default will be removed in Saleor 3.23.0, "- 'use EDITOR_JS_LINK_REL="noopener noreferrer" instead'- ),- category=SaleorDeprecationWarning,- stacklevel=2,- )-- if allowed_schemes_str := os.getenv("UNSAFE_EDITOR_JS_ALLOWED_URL_SCHEMES"):- # This is deprecated, each URL scheme must have a cleaner implemented- # we cannot continue to allow to add custom schemes without cleaners- # as this is risky.- warnings.warn(- (- "UNSAFE_EDITOR_JS_ALLOWED_URL_SCHEMES will be removed in Saleor 3.23.0, "- "open a feature request at https://github.com/saleor/saleor/issues "- "to add out of the box support for the URL scheme(s) you need"- ),- category=SaleorDeprecationWarning,- stacklevel=3,- )- self.allowed_schemes = {x.strip() for x in allowed_schemes_str.split(",")}+ self.link_rel = os.getenv("EDITOR_JS_LINK_REL", "noopener noreferrer") or Noneif allowed_attributes_str := os.getenv("EDITOR_JS_ALLOWED_ATTRIBUTES"):for html_tag, allowed_attributes in json.loads(diff --git a/saleor/core/deprecations.py b/saleor/core/deprecations.pydeleted file mode 100644index 4a83a9b37b..0000000000--- a/saleor/core/deprecations.py+++ /dev/null@@ -1,5 +0,0 @@-class SaleorDeprecationWarning(UserWarning):- """Custom deprecation warning as DeprecationWarning is usually ignored.-- Learn more: https://sethmlarson.dev/deprecations-via-warnings-dont-work-for-python-libraries- """diff --git a/saleor/core/editorjs/cleaners.py b/saleor/core/editorjs/cleaners.pyindex 98f1867053..603bcad30b 100644--- a/saleor/core/editorjs/cleaners.py+++ b/saleor/core/editorjs/cleaners.py@@ -30,11 +30,7 @@ def _clean_url_value(dirty_url: str | None) -> str:except ValueError:parsed_url = None- if (- parsed_url is None- or parsed_url.scheme- not in ALLOWED_URL_SCHEMES | settings.HTML_CLEANER_PREFS.allowed_schemes- ):+ if parsed_url is None or parsed_url.scheme not in ALLOWED_URL_SCHEMES:warnings.warn(f"An invalid url or disallowed URL was sent: {dirty_url}",stacklevel=3,@@ -51,10 +47,6 @@ def _clean_url_value(dirty_url: str | None) -> str:url_cleaner = URL_SCHEME_CLEANERS.get(parsed_url.scheme, None)if url_cleaner is None:- if parsed_url.scheme in settings.HTML_CLEANER_PREFS.allowed_schemes:- # Deprecated: this is only for backward compatibility - it doesn't define- # a cleaner which is dangerous.- return dirty_url# NOTE: this exception should never happen unless a maintainer didn't read the# comment in ALLOWED_URL_SCHEMESraise KeyError("No URL cleaner defined", parsed_url.scheme)@@ -114,7 +106,7 @@ def _clean_text(text: str | None) -> str | None:return nh3.clean(text,- url_schemes=ALLOWED_URL_SCHEMES | settings.HTML_CLEANER_PREFS.allowed_schemes,+ url_schemes=ALLOWED_URL_SCHEMES,attributes=settings.HTML_CLEANER_PREFS.allowed_attributes,tag_attribute_values=settings.HTML_CLEANER_PREFS.allowed_attribute_values,link_rel=settings.HTML_CLEANER_PREFS.link_rel,diff --git a/saleor/core/editorjs/tests/test_editorjs.py b/saleor/core/editorjs/tests/test_editorjs.pyindex 8d2e9bc3b4..b4d0130451 100644--- a/saleor/core/editorjs/tests/test_editorjs.py+++ b/saleor/core/editorjs/tests/test_editorjs.py@@ -8,7 +8,6 @@ import pytestfrom django.utils.html import strip_tagsfrom ...cleaners.html import HtmlCleanerSettings-from ...deprecations import SaleorDeprecationWarningfrom .. import clean_editorjs, editorjs_to_textfrom ..cleaners import _clean_url_valuefrom .conftest import assert_pydantic_errors@@ -47,14 +46,11 @@ def cleaner_settings(settings):# NOTE: use env vars to control the settings (using prefs.reload())# to ensure the parsing logic of env var is tested- #- # Warnings need to be captured due to deprecation warnings for Saleor v3.23.0- with warnings.catch_warnings(record=True, category=SaleorDeprecationWarning):- prefs = HtmlCleanerSettings()- settings.HTML_CLEANER_PREFS = prefs+ prefs = HtmlCleanerSettings()+ settings.HTML_CLEANER_PREFS = prefs- yield prefs- settings.HTML_CLEANER_PREFS = old_prefs+ yield prefs+ settings.HTML_CLEANER_PREFS = old_prefs@pytest.mark.parametrize(@@ -62,6 +58,7 @@ def cleaner_settings(settings):# Invalid cases[(url, "#invalid", False) for url in XSS_URLS]+ [+ ("not-allowed:foo", "#invalid", False),# Valid cases("https://example.com", "https://example.com", True),("http://example.com", "http://example.com", True),@@ -105,31 +102,6 @@ def test_clean_url_error_handling(input_url: str, expected_error: str):_clean_url_value(input_url)-def test_clean_url_allows_custom_allow_list(monkeypatch, cleaner_settings):- """Ensure that we allow to override the settings to add more allowed URL schemes.-- This is a backward compatibility check, it will be removed in 3.23.0.- """-- url = "non-default:foo"-- # Should refuse the URL scheme if it's not allowed- with warnings.catch_warnings(record=True):- assert _clean_url_value(url) == "#invalid"-- # Catches warnings due to deprecation notices from Saleor (v3.23.0)- with warnings.catch_warnings(record=True):- monkeypatch.setenv("UNSAFE_EDITOR_JS_ALLOWED_URL_SCHEMES", "non-default,other")- cleaner_settings.reload()-- # When allowed it should allow the use of that URL scheme even if a cleaner- # isn't defined.- assert _clean_url_value(url) == url- assert _clean_url_value("other:") == "other:"- with warnings.catch_warnings(record=True):- assert _clean_url_value("not-ok:") == "#invalid"--@pytest.mark.parametrize(("_case", "text", "expected_cleaned_text"),[@@ -246,6 +218,7 @@ def test_clean_text_data_block_allow_custom_attributes(json.dumps(allowed_attributes),)cleaner_settings.reload()+ cleaner_settings.link_rel = Noneassert_paragraph_cleaned(html_input, expected_output)@@ -312,6 +285,7 @@ def test_clean_text_data_block_allow_custom_attribute_values(),)cleaner_settings.reload()+ cleaner_settings.link_rel = Noneassert_paragraph_cleaned(html_input, expected_output)diff --git a/saleor/tests/settings.py b/saleor/tests/settings.pyindex 339c66749f..3d40909026 100644--- a/saleor/tests/settings.py+++ b/saleor/tests/settings.py@@ -102,6 +102,3 @@ BREAKER_BOARD_ENABLED = FalseTELEMETRY_RAISE_UNIT_CONVERSION_ERRORS = TrueTELEMETRY_TRACER_CLASS = "saleor.core.telemetry.tests.TestTracer"TELEMETRY_METER_CLASS = "saleor.core.telemetry.tests.TestMeter"--# New default in Saleor v3.23.0-HTML_CLEANER_PREFS.link_rel = "noopener noreferrer" # noqa: F405 # HTML_CLEANER_PREFS exists