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 gistVerify webhook authenticity before writing anything to the database.

The idea, explained simply

Webhook handling pathwebhook receivedsignature verifiedfields extracted andvalidatedidempotent,bound-parameter write
Webhook handling path

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.

LevelTime needed
Intermediate3-4 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
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
GitVersion 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

Idempotent webhook write using a unique event idSQL: edit and run

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

  1. Verify the webhook's signature/HMAC against a shared secret before parsing or trusting any part of the payload.
  2. Extract only the specific fields needed from the verified payload, validating their types, rather than trusting the payload's structure blindly.
  3. 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).
  4. Bind every extracted value as a parameter in the insert.
  5. Write a test that delivers the same webhook event twice and confirms only one record results.

Security checklist before you ship it

CheckWhy it mattersHow to verify
Signature verification firstAn unverified webhook payload cannot be trusted at allConfirm signature/HMAC verification happens before any payload use
Idempotent writeWebhook providers may redeliver the same eventTest delivering the same event id twice produces one record
Bound parameters for extracted fieldsExtracted payload values are still untrusted inputConfirm every value is bound, not concatenated
Tip:Commit the insecure version and the fixed version as two separate commits, so the diff itself becomes a teaching example.

Check yourself

1. What must happen before trusting any part of a webhook payload?