Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Subclass HttpResponseRedirect to override allowed schemes (#14023)

Patryk Zawadzki7de29e1b82b4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 445c86ba76..5a8d7b067a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ All notable, unreleased changes to this project will be documented in this file.
### Other changes
+- Fix thumbnail redirects sometimes failing with an unsafe redirect warning - #14023 by @patrys
+
# 3.16.0
### Breaking changes
diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.py
index bcbe7ef86f..752fba77be 100644
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@ -1002,6 +1002,13 @@ def handle_webhook(request: WSGIRequest, gateway_config: "GatewayConfig"):
return HttpResponse("[accepted]")
+class HttpResponseRedirectWithTrustedProtocol(HttpResponseRedirect):
+ def __init__(self, redirect_to: str, *args, **kwargs) -> None:
+ parsed = urlparse(redirect_to)
+ self.allowed_schemes = [parsed.scheme]
+ super().__init__(redirect_to, *args, **kwargs)
+
+
@transaction_with_commit_on_errors()
def handle_additional_actions(
request: WSGIRequest, payment_details: Callable, channel_slug: str
@@ -1070,10 +1077,7 @@ def handle_additional_actions(
return HttpResponseBadRequest(str(e))
handle_api_response(payment, checkout, result, channel_slug)
redirect_url = prepare_redirect_url(payment_id, checkout_pk, result, return_url)
- parsed = urlparse(return_url)
- redirect_class = HttpResponseRedirect
- redirect_class.allowed_schemes = [parsed.scheme]
- return redirect_class(redirect_url)
+ return HttpResponseRedirectWithTrustedProtocol(redirect_url)
def prepare_api_request_data(request: WSGIRequest, data: dict):