Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Make custom header name case-insensitive. (#12058)
Piotr Zabieglikdd8ead382526
diff --git a/saleor/app/manifest_validations.py b/saleor/app/manifest_validations.pyindex a6ab9b785d..7653ce79ed 100644--- a/saleor/app/manifest_validations.py+++ b/saleor/app/manifest_validations.py@@ -247,7 +247,7 @@ def clean_webhooks(manifest_data, errors):if custom_headers := webhook.get("customHeaders"):try:- custom_headers_validator(custom_headers)+ webhook["customHeaders"] = custom_headers_validator(custom_headers)except ValidationError as err:errors["webhooks"].append(ValidationError(diff --git a/saleor/app/tests/test_installation_utils.py b/saleor/app/tests/test_installation_utils.pyindex 8a632fff93..f9df651e46 100644--- a/saleor/app/tests/test_installation_utils.py+++ b/saleor/app/tests/test_installation_utils.py@@ -355,7 +355,7 @@ def test_install_app_with_webhook(assert webhook.subscription_query == app_manifest_webhook["query"]assert webhook.target_url == app_manifest_webhook["targetUrl"]assert webhook.is_active is True- assert webhook.custom_headers == custom_headers+ assert webhook.custom_headers == {"x-key": "Value"}def test_install_app_webhook_incorrect_url(diff --git a/saleor/graphql/webhook/mutations/webhook_create.py b/saleor/graphql/webhook/mutations/webhook_create.pyindex e43c1045c4..53521c9b65 100644--- a/saleor/graphql/webhook/mutations/webhook_create.py+++ b/saleor/graphql/webhook/mutations/webhook_create.py@@ -130,7 +130,7 @@ class WebhookCreate(ModelMutation):if headers := cleaned_data.get("custom_headers"):try:- custom_headers_validator(headers)+ cleaned_data["custom_headers"] = custom_headers_validator(headers)except ValidationError as err:raise_validation_error(field="customHeaders",diff --git a/saleor/graphql/webhook/tests/mutations/test_webhook_create.py b/saleor/graphql/webhook/tests/mutations/test_webhook_create.pyindex ef088a0767..9e149d4b7c 100644--- a/saleor/graphql/webhook/tests/mutations/test_webhook_create.py+++ b/saleor/graphql/webhook/tests/mutations/test_webhook_create.py@@ -58,7 +58,10 @@ def test_webhook_create_by_app(app_api_client, permission_manage_orders):new_webhook = Webhook.objects.get()assert new_webhook.name == "New integration"assert new_webhook.target_url == "https://www.example.com"- assert new_webhook.custom_headers == custom_headers+ assert new_webhook.custom_headers == {+ "x-key": "Value",+ "authorization-key": "Value",+ }events = new_webhook.events.all()assert len(events) == 1assert events[0].event_type == WebhookEventTypeAsyncEnum.ORDER_CREATED.valuediff --git a/saleor/graphql/webhook/tests/mutations/test_webhook_update.py b/saleor/graphql/webhook/tests/mutations/test_webhook_update.pyindex b1f636075e..2c60eabe6e 100644--- a/saleor/graphql/webhook/tests/mutations/test_webhook_update.py+++ b/saleor/graphql/webhook/tests/mutations/test_webhook_update.py@@ -51,7 +51,7 @@ def test_webhook_update_by_staff(staff_api_client, webhook, permission_manage_ap# givenquery = WEBHOOK_UPDATEwebhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)- custom_headers = {"X-Key": "Value", "Authorization-Key": "Value"}+ custom_headers = {"x-key": "Value", "authorization-key": "Value"}variables = {"id": webhook_id,"input": {@@ -72,7 +72,7 @@ def test_webhook_update_by_staff(staff_api_client, webhook, permission_manage_ap# thenassert webhook.is_active is False- assert webhook.custom_headers == custom_headers+ assert webhook.custom_headers == {"x-key": "Value", "authorization-key": "Value"}events = webhook.events.all()assert len(events) == 1assert events[0].event_type == WebhookEventTypeAsyncEnum.CUSTOMER_CREATED.valuediff --git a/saleor/webhook/tests/test_webhook_validators.py b/saleor/webhook/tests/test_webhook_validators.pyindex 13327dfb58..1b794064fb 100644--- a/saleor/webhook/tests/test_webhook_validators.py+++ b/saleor/webhook/tests/test_webhook_validators.py@@ -31,8 +31,12 @@ from ..validators import ("Header must consist of strings.",),(- {"Ke:y": "Value"},- 'Key "Ke:y" contains invalid character.',+ {123: "value"},+ "Header must consist of strings.",+ ),+ (+ {"ke:y": "Value"},+ 'Key "ke:y" contains invalid character.',),({"Key": "Val:ue"},@@ -58,3 +62,10 @@ def test_webhook_validator(headers, err_msg):with pytest.raises(ValidationError) as err:custom_headers_validator(headers)assert err.value.message == err_msg+++ "header", ["X-Key", "x-keY", "AuthoriZAtionKey", "authorization"]+)+def test_webhook_header_name_case_insensitive(header):+ assert {header.lower(): "Value"} == custom_headers_validator({header: "Value"})diff --git a/saleor/webhook/validators.py b/saleor/webhook/validators.pyindex 668c97c051..7a616ff5de 100644--- a/saleor/webhook/validators.py+++ b/saleor/webhook/validators.py@@ -40,10 +40,12 @@ def custom_headers_validator(headers: Dict[str, str]) -> Dict[str, str]:if not set(value).issubset(set(VALUE_CHARS_ALLOWED)):raise ValidationError(f'Value "{value}" contains invalid character.')- if not (key.startswith("X-") or key.startswith("Authorization")):+ if not (+ key.lower().startswith("x-") or key.lower().startswith("authorization")+ ):raise ValidationError(f'"{key}" does not match allowed key pattern: 'f'"X-*" or "Authorization*".')- return headers+ return {k.lower(): v for k, v in headers.items()}