Home / Learn Secure SQL / Project: Secure Webhook Handler with Database Writes
Project: Secure Webhook Handler with Database Writes
Verify webhook authenticity before writing anything to the database.
The idea, explained simply
What it is
A webhook handler receives events from an external service and writes corresponding records to the database; this project verifies the webhook's signature before trusting its payload, and makes the resulting database write idempotent so a redelivered webhook cannot create duplicate records.
Why it matters
Real projects are where secure patterns become habits instead of theory.
Mechanically speaking
Work through the steps in order; do not skip the checklist.
Shows up in
- Portfolio project
- Code review practice
- Interview preparation
What you will build
A webhook handler receives events from an external service and writes corresponding records to the database; this project verifies the webhook's signature before trusting its payload, and makes the resulting database write idempotent so a redelivered webhook cannot create duplicate records.
| Level | Time needed |
|---|---|
| Intermediate | 3-4 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| Code editor (VS Code or similar) | The program you write code in. | You need somewhere to write and run the example app. | free |
| A language runtime (Node.js, Python, PHP or similar) | Installs the interpreter so your code can run on your computer. | The project examples run as small scripts. | free |
| SQLite (bundled, no install needed) | A tiny file-based database engine. | Every project in this course can be tried with SQLite first, with zero setup. | free |
| An HTTP client (curl, Postman or your browser) | Sends requests to the small demo server. | Lets you try the feature the way a real user or attacker-shaped input would arrive. | free |
| Git | Version control: tracks every change you make. | Lets you compare the insecure version and the fixed version of the code. | free |
Step 1: see the problem and the fix in the sandbox
The event_id primary key with ON CONFLICT DO NOTHING makes the write idempotent, so a redelivered copy of the same webhook event (which providers may send more than once) has no additional effect.
Step by step
- Verify the webhook's signature/HMAC against a shared secret before parsing or trusting any part of the payload.
- Extract only the specific fields needed from the verified payload, validating their types, rather than trusting the payload's structure blindly.
- Write the resulting database record using the event's unique id as a primary key with an idempotent upsert (ON CONFLICT DO NOTHING or similar).
- Bind every extracted value as a parameter in the insert.
- Write a test that delivers the same webhook event twice and confirms only one record results.
Security checklist before you ship it
| Check | Why it matters | How to verify |
|---|---|---|
| Signature verification first | An unverified webhook payload cannot be trusted at all | Confirm signature/HMAC verification happens before any payload use |
| Idempotent write | Webhook providers may redeliver the same event | Test delivering the same event id twice produces one record |
| Bound parameters for extracted fields | Extracted payload values are still untrusted input | Confirm every value is bound, not concatenated |
Check yourself
1. What must happen before trusting any part of a webhook payload?
Signature verification establishes authenticity first.