Harden Stripe webhook endpoint and correct Flask integration points#345
Open
Copilot wants to merge 4 commits into
Open
Harden Stripe webhook endpoint and correct Flask integration points#345Copilot wants to merge 4 commits into
Copilot wants to merge 4 commits into
Conversation
Copilot created this pull request from a session on behalf of
LVT-ENG
May 27, 2026 13:12
View session
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Member
There was a problem hiding this comment.
Pull request overview
This PR hardens the Vercel-hosted Flask API by adding a Stripe webhook endpoint that performs strict signature verification before handling events, and adds initial event routing plus unit tests to validate webhook behavior.
Changes:
- Adds
POST /api/webhooktoapi/index.py, validating theStripe-Signatureheader and verifying events viastripe.Webhook.construct_event(...). - Introduces baseline handling for
payment_intent.succeeded(currently via server-side logging). - Adds Stripe SDK dependency (
stripe==11.6.0) and unit tests covering key webhook request paths.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| api/index.py | Adds the Stripe webhook route with signature verification and basic event routing. |
| api/requirements.txt | Adds the Stripe Python SDK dependency needed for webhook verification. |
| tests/test_api_webhook.py | Adds unit tests for the new webhook endpoint behavior and error paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if event_type == "payment_intent.succeeded": | ||
| print("[tryonyou] Stripe payment_intent.succeeded received", flush=True) | ||
|
|
||
| return _json_ok({"status": "success"}, 200) |
Comment on lines
+18
to
+44
| def test_invalid_payload(self): | ||
| with patch.object(index, "STRIPE_ENDPOINT_SECRET", "whsec_test"): | ||
| with patch("api.index.stripe.Webhook.construct_event", side_effect=ValueError): | ||
| response = self.client.post( | ||
| "/api/webhook", | ||
| data=b"{bad json}", | ||
| headers={"Stripe-Signature": "t=1,v1=fake"}, | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 400) | ||
| self.assertIn("invalid payload", response.get_data(as_text=True)) | ||
|
|
||
| def test_payment_intent_succeeded(self): | ||
| event = {"type": "payment_intent.succeeded", "data": {"object": {"id": "pi_123"}}} | ||
| with patch.object(index, "STRIPE_ENDPOINT_SECRET", "whsec_test"): | ||
| with patch("api.index.stripe.Webhook.construct_event", return_value=event): | ||
| with patch("api.index.print") as print_mock: | ||
| response = self.client.post( | ||
| "/api/webhook", | ||
| data=b"{}", | ||
| headers={"Stripe-Signature": "t=1,v1=fake"}, | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertIn("success", response.get_data(as_text=True)) | ||
| print_mock.assert_called_with("[tryonyou] Stripe payment_intent.succeeded received", flush=True) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The webhook handler had integration defects (
Flask(__name__),Stripe-Signatureheader usage, and__main__guard) and lacked strict Stripe signature validation. This PR introduces a production-safe/api/webhookflow that rejects untrusted requests before event handling.Webhook contract and verification
POST /api/webhookinapi/index.py.stripe.Webhook.construct_event(payload, sig_header, STRIPE_ENDPOINT_SECRET).Event handling baseline
event_type.payment_intent.succeededwith server-side logging hook for downstream async processing.Dependency alignment
api/requirements.txt(stripe==11.6.0).Webhook behavior coverage
tests/test_api_webhook.pyfor:payment_intent.succeededrequest path