|
| 1 | +import json |
| 2 | +from datetime import datetime, timedelta, timezone |
| 3 | + |
| 4 | +import pytest |
| 5 | +from svix.webhooks import Webhook, WebhookVerificationError |
| 6 | + |
| 7 | +from seam import ( |
| 8 | + SeamError, |
| 9 | + SeamInvalidWebhookPayloadError, |
| 10 | + SeamWebhook, |
| 11 | + SeamWebhookVerificationError, |
| 12 | +) |
| 13 | + |
| 14 | +SECRET = "MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw" |
| 15 | + |
| 16 | +EVENT_PAYLOAD = json.dumps( |
| 17 | + { |
| 18 | + "event_id": "11111111-1111-1111-1111-111111111111", |
| 19 | + "event_type": "device.connected", |
| 20 | + "workspace_id": "22222222-2222-2222-2222-222222222222", |
| 21 | + "device_id": "33333333-3333-3333-3333-333333333333", |
| 22 | + "created_at": "2026-08-27T00:00:00.000Z", |
| 23 | + "occurred_at": "2026-08-27T00:00:00.000Z", |
| 24 | + } |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def sign_headers(payload, msg_id="msg_1", timestamp=None, secret=SECRET): |
| 29 | + timestamp = timestamp or datetime.now(timezone.utc) |
| 30 | + signature = Webhook(secret).sign(msg_id, timestamp, payload) |
| 31 | + |
| 32 | + return { |
| 33 | + "svix-id": msg_id, |
| 34 | + "svix-timestamp": str(int(timestamp.timestamp())), |
| 35 | + "svix-signature": signature, |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +def test_verifies_and_parses_a_signed_event(): |
| 40 | + webhook = SeamWebhook(SECRET) |
| 41 | + |
| 42 | + event = webhook.verify(EVENT_PAYLOAD, sign_headers(EVENT_PAYLOAD)) |
| 43 | + |
| 44 | + assert event.event_type == "device.connected" |
| 45 | + assert event.event_id == "11111111-1111-1111-1111-111111111111" |
| 46 | + |
| 47 | + |
| 48 | +def test_accepts_mixed_case_headers(): |
| 49 | + webhook = SeamWebhook(SECRET) |
| 50 | + headers = {k.upper(): v for k, v in sign_headers(EVENT_PAYLOAD).items()} |
| 51 | + |
| 52 | + event = webhook.verify(EVENT_PAYLOAD, headers) |
| 53 | + |
| 54 | + assert event.event_type == "device.connected" |
| 55 | + |
| 56 | + |
| 57 | +def test_a_tampered_payload_fails_verification(): |
| 58 | + webhook = SeamWebhook(SECRET) |
| 59 | + headers = sign_headers(EVENT_PAYLOAD) |
| 60 | + tampered = EVENT_PAYLOAD.replace("device.connected", "device.disconnected") |
| 61 | + |
| 62 | + with pytest.raises(SeamWebhookVerificationError, match="No matching signature"): |
| 63 | + webhook.verify(tampered, headers) |
| 64 | + |
| 65 | + |
| 66 | +def test_a_wrong_secret_fails_verification(): |
| 67 | + webhook = SeamWebhook(SECRET) |
| 68 | + headers = sign_headers(EVENT_PAYLOAD, secret="WrongQ9r8GKYqrTwjUPD8ILPZIo2LaLa") |
| 69 | + |
| 70 | + with pytest.raises(SeamWebhookVerificationError, match="No matching signature"): |
| 71 | + webhook.verify(EVENT_PAYLOAD, headers) |
| 72 | + |
| 73 | + |
| 74 | +def test_an_expired_timestamp_fails_verification(): |
| 75 | + webhook = SeamWebhook(SECRET) |
| 76 | + headers = sign_headers( |
| 77 | + EVENT_PAYLOAD, |
| 78 | + timestamp=datetime.now(timezone.utc) - timedelta(hours=1), |
| 79 | + ) |
| 80 | + |
| 81 | + with pytest.raises(SeamWebhookVerificationError, match="too old"): |
| 82 | + webhook.verify(EVENT_PAYLOAD, headers) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("missing", ["svix-id", "svix-timestamp", "svix-signature"]) |
| 86 | +def test_a_missing_header_fails_verification(missing): |
| 87 | + webhook = SeamWebhook(SECRET) |
| 88 | + headers = sign_headers(EVENT_PAYLOAD) |
| 89 | + del headers[missing] |
| 90 | + |
| 91 | + with pytest.raises(SeamWebhookVerificationError, match="Missing required headers"): |
| 92 | + webhook.verify(EVENT_PAYLOAD, headers) |
| 93 | + |
| 94 | + |
| 95 | +def test_conflicting_duplicate_headers_fail_verification(): |
| 96 | + webhook = SeamWebhook(SECRET) |
| 97 | + headers = sign_headers(EVENT_PAYLOAD) |
| 98 | + headers["SVIX-ID"] = "msg_other" |
| 99 | + |
| 100 | + with pytest.raises( |
| 101 | + SeamWebhookVerificationError, |
| 102 | + match="Conflicting values for webhook header svix-id", |
| 103 | + ): |
| 104 | + webhook.verify(EVENT_PAYLOAD, headers) |
| 105 | + |
| 106 | + |
| 107 | +def test_identical_duplicate_headers_are_accepted(): |
| 108 | + webhook = SeamWebhook(SECRET) |
| 109 | + headers = sign_headers(EVENT_PAYLOAD) |
| 110 | + headers["SVIX-ID"] = headers["svix-id"] |
| 111 | + |
| 112 | + event = webhook.verify(EVENT_PAYLOAD, headers) |
| 113 | + |
| 114 | + assert event.event_type == "device.connected" |
| 115 | + |
| 116 | + |
| 117 | +def test_a_malformed_signature_header_fails_verification(): |
| 118 | + webhook = SeamWebhook(SECRET) |
| 119 | + headers = sign_headers(EVENT_PAYLOAD) |
| 120 | + headers["svix-signature"] = "garbage-without-a-version" |
| 121 | + |
| 122 | + with pytest.raises(SeamWebhookVerificationError): |
| 123 | + webhook.verify(EVENT_PAYLOAD, headers) |
| 124 | + |
| 125 | + |
| 126 | +def test_a_signed_but_unparseable_payload_is_not_forgery(): |
| 127 | + webhook = SeamWebhook(SECRET) |
| 128 | + payload = '{"event_id": "trailing-comma",}' |
| 129 | + |
| 130 | + with pytest.raises( |
| 131 | + SeamInvalidWebhookPayloadError, |
| 132 | + match="The verified webhook payload is not valid JSON", |
| 133 | + ): |
| 134 | + webhook.verify(payload, sign_headers(payload)) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.parametrize("payload", ["null", "[1]", "42", '"event"', "{}"]) |
| 138 | +def test_a_signed_non_event_payload_is_not_forgery(payload): |
| 139 | + webhook = SeamWebhook(SECRET) |
| 140 | + |
| 141 | + with pytest.raises( |
| 142 | + SeamInvalidWebhookPayloadError, |
| 143 | + match="The verified webhook payload did not contain a Seam event", |
| 144 | + ): |
| 145 | + webhook.verify(payload, sign_headers(payload)) |
| 146 | + |
| 147 | + |
| 148 | +def test_an_unknown_event_type_still_parses(): |
| 149 | + webhook = SeamWebhook(SECRET) |
| 150 | + payload = json.dumps( |
| 151 | + { |
| 152 | + "event_id": "11111111-1111-1111-1111-111111111111", |
| 153 | + "event_type": "future.event_type", |
| 154 | + "future_field": {"nested": True}, |
| 155 | + } |
| 156 | + ) |
| 157 | + |
| 158 | + event = webhook.verify(payload, sign_headers(payload)) |
| 159 | + |
| 160 | + assert event.event_type == "future.event_type" |
| 161 | + assert event.future_field.nested is True |
| 162 | + |
| 163 | + |
| 164 | +def test_webhook_errors_are_seam_errors(): |
| 165 | + assert issubclass(SeamWebhookVerificationError, SeamError) |
| 166 | + assert issubclass(SeamInvalidWebhookPayloadError, SeamError) |
| 167 | + # Existing code catching the svix error keeps working. |
| 168 | + assert issubclass(SeamWebhookVerificationError, WebhookVerificationError) |
0 commit comments