Skip to content

Commit c43c06d

Browse files
committed
Reject Stripe webhooks when no webhook secret is configured
Signed-off-by: adibmbrk <adibmbrk@gmail.com>
1 parent 9b76601 commit c43c06d

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎contentcuration/contentcuration/tests/views/test_subscription.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import hashlib
2+
import hmac
3+
import json
4+
import time
15
from unittest import mock
26

37
import stripe
@@ -433,3 +437,75 @@ def test_idempotent_checkout_processing(self, mock_construct, mock_retrieve):
433437
self.assertEqual(response.status_code, 200)
434438
# Should still only have one subscription
435439
self.assertEqual(UserSubscription.objects.filter(user=self.user).count(), 1)
440+
441+
442+
def _signed_subscription_updated(secret, subscription_id, quantity):
443+
payload = json.dumps(
444+
{
445+
"id": "evt_test",
446+
"object": "event",
447+
"type": "customer.subscription.updated",
448+
"data": {
449+
"object": {
450+
"id": subscription_id,
451+
"status": "active",
452+
"cancel_at_period_end": False,
453+
"items": {"data": [{"quantity": quantity}]},
454+
}
455+
},
456+
}
457+
)
458+
timestamp = int(time.time())
459+
signature = hmac.new(
460+
secret.encode(), f"{timestamp}.{payload}".encode(), hashlib.sha256
461+
).hexdigest()
462+
return payload, f"t={timestamp},v1={signature}"
463+
464+
465+
class StripeWebhookSignatureTest(StudioAPITestCase):
466+
"""Exercise real signature verification, without mocking construct_event."""
467+
468+
def setUp(self):
469+
self.user = testdata.user(email="webhook-signature@test.com")
470+
self.subscription = UserSubscription.objects.create(
471+
user=self.user,
472+
stripe_customer_id="cus_test123",
473+
stripe_subscription_id="sub_test123",
474+
stripe_subscription_status="active",
475+
subscription_disk_space=0,
476+
)
477+
self.webhook_url = reverse("stripe_webhook")
478+
479+
def _post(self, secret):
480+
payload, signature = _signed_subscription_updated(secret, "sub_test123", 50)
481+
return self.client.post(
482+
self.webhook_url,
483+
data=payload,
484+
content_type="application/json",
485+
HTTP_STRIPE_SIGNATURE=signature,
486+
)
487+
488+
@override_settings(STRIPE_WEBHOOK_SECRET="")
489+
def test_rejects_event_signed_with_empty_secret_when_none_configured(self):
490+
with self.assertLogs("contentcuration.views.subscription", "ERROR"):
491+
response = self._post("")
492+
493+
self.assertEqual(response.status_code, 400)
494+
self.subscription.refresh_from_db()
495+
self.assertEqual(self.subscription.subscription_disk_space, 0)
496+
497+
@override_settings(STRIPE_WEBHOOK_SECRET="whsec_test_fake")
498+
def test_handles_correctly_signed_event_when_secret_configured(self):
499+
response = self._post("whsec_test_fake")
500+
501+
self.assertEqual(response.status_code, 200)
502+
self.subscription.refresh_from_db()
503+
self.assertEqual(self.subscription.subscription_disk_space, 50 * 10 ** 9)
504+
505+
@override_settings(STRIPE_WEBHOOK_SECRET="whsec_test_fake")
506+
def test_rejects_event_signed_with_wrong_secret(self):
507+
response = self._post("")
508+
509+
self.assertEqual(response.status_code, 400)
510+
self.subscription.refresh_from_db()
511+
self.assertEqual(self.subscription.subscription_disk_space, 0)

‎contentcuration/contentcuration/views/subscription.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ def save(self):
265265
@csrf_exempt
266266
@require_POST
267267
def stripe_webhook(request):
268+
if not settings.STRIPE_WEBHOOK_SECRET:
269+
logger.error("Stripe webhook secret is not configured; rejecting webhook.")
270+
return JsonResponse({"error": "Webhook is not configured"}, status=400)
271+
268272
try:
269273
event = stripe.Webhook.construct_event(
270274
request.body,

0 commit comments

Comments
 (0)