Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Build jwt signature when secret key is an empty string (#10139)
Maciej Korycinskic47de896c403
diff --git a/saleor/plugins/webhook/__init__.py b/saleor/plugins/webhook/__init__.pyindex 835e99af8b..52b14e76c3 100644--- a/saleor/plugins/webhook/__init__.py+++ b/saleor/plugins/webhook/__init__.py@@ -1,11 +1,12 @@import hashlibimport hmac+from typing import Optionalfrom ...core.jwt_manager import get_jwt_manager-def signature_for_payload(body: bytes, secret_key):- if secret_key is None:+def signature_for_payload(body: bytes, secret_key: Optional[str]):+ if not secret_key:return get_jwt_manager().jws_encode(body)hash = hmac.new(bytes(secret_key, "utf-8"), body, hashlib.sha256)return hash.hexdigest()diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/plugins/webhook/tests/test_webhook_protocols.pyindex 765c2508e7..621f2f9ea1 100644--- a/saleor/plugins/webhook/tests/test_webhook_protocols.py+++ b/saleor/plugins/webhook/tests/test_webhook_protocols.py@@ -2,6 +2,7 @@ from datetime import timedeltafrom unittest.mock import MagicMock, patchimport boto3+import jwtimport pytestfrom django.core.serializers import serializefrom google.cloud.pubsub_v1 import PublisherClient@@ -291,3 +292,50 @@ def test_trigger_webhooks_with_http_and_secret_key(headers=expected_headers,timeout=10,)+++@patch("saleor.plugins.webhook.tasks.requests.post")+def test_trigger_webhooks_with_http_and_secret_key_as_empty_string(+ mock_request, webhook, order_with_lines, permission_manage_orders+):+ mock_request.return_value = MagicMock(+ text="{response: body}",+ headers={"response": "header"},+ elapsed=timedelta(seconds=2),+ status_code=200,+ ok=True,+ )+ webhook.app.permissions.add(permission_manage_orders)+ webhook.target_url = "https://webhook.site/48978b64-4efb-43d5-a334-451a1d164009"+ webhook.secret_key = ""+ webhook.save()++ expected_data = serialize("json", [order_with_lines])+ trigger_webhooks_async(+ expected_data, WebhookEventAsyncType.ORDER_CREATED, [webhook]+ )++ expected_signature = signature_for_payload(expected_data.encode("utf-8"), "")+ expected_headers = {+ "Content-Type": "application/json",+ # X- headers will be deprecated in Saleor 4.0, proper headers are without X-+ "X-Saleor-Event": "order_created",+ "X-Saleor-Domain": "mirumee.com",+ "X-Saleor-Signature": expected_signature,+ "Saleor-Event": "order_created",+ "Saleor-Domain": "mirumee.com",+ "Saleor-Signature": expected_signature,+ }++ signature_headers = jwt.get_unverified_header(expected_signature)++ # make sure that the signature has been build as a jwt token+ assert signature_headers["typ"] == "JWT"+ assert signature_headers["alg"] == "RS256"++ mock_request.assert_called_once_with(+ webhook.target_url,+ data=bytes(expected_data, "utf-8"),+ headers=expected_headers,+ timeout=10,+ )