webhook-handler
Skill1 starsChanged 5 months agoMITView on GitHub
Safely receives inbound third-party webhooks and provider callbacks by holding a four-step contract — verify authenticity with a constant-time HMAC or verify token before parsing, resolve the owning tenant from the verified payload before persisting anything, dedupe idempotently by the provider event id, then ACK 200 fast and do the real work in a background task. Use when adding or reviewing a webhook or callback endpoint, wiring signature verification, handling retried or duplicate deliveries, guarding against SSRF from payload URLs, or asking how inbound provider events are processed here. Not for calling out to a provider's API (see ai-integration) or the background worker mechanics themselves (see celery-tasks).
2 other projects use a version of this.
Adopted by
Most recently taken up by
- YepAPI/skillstheir own version, first seen Apr 12, 2026
- agentsdance/agentskillstheir own version, first seen Jan 15, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- YepAPI/skills★ 8same name
- agentsdance/agentskills★ 3same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository49 other files it gives its agents.
- accessibilitySkill
- ai-evalsSkill
- ai-integrationSkill
- architecture-decisionsSkill
- audit-loggingSkill
- aws-servicesSkill
- browser-e2e-testingSkill
- celery-tasksSkill
- changelogSkill
- ci-cdSkill
- codebase-guideSkill
- code-reviewSkill
- commit-messageSkill
- cookie-auth-csrfSkill
- dashboard-data-designSkill
- db-concurrencySkill
- debugSkill
- dependency-auditSkill
- deploy-awsSkill
- design-systemSkill
- drf-apiSkill
- drf-zod-contractSkill
- frontend-perfSkill
- git-workflowSkill
- i18n-rtlSkill
- incident-responseSkill
- migrationsSkill
- money-decimalSkill
- multi-tenancySkill
- nextjs-moduleSkill
- observabilitySkill
- perf-reviewSkill
- production-readinessSkill
- product-ux-designSkill
- rbac-permissionsSkill
- react-querySkill
- security-reviewSkill
- skill-authoringSkill
- tailwind-shadcnSkill
- tenant-session-switchSkill
- threat-modelingSkill
- verifySkill
- version-checkSkill
- visual-ui-designSkill
- websockets-channelsSkill
- write-testsSkill
- zod-formsSkill
- zustand-stateSkill
- example-skillSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 4 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 24
No tenant resolved → reject, never fall through to a default account.
- Distinctive: line 37
Verify → resolve tenant → dedupe → enqueue on commit → 200, in one thin view.
- Very specific: line 81
Use a constant-time compare (hmac.compare_digest), never ==, for signatures.
- Distinctive: line 82
SSRF: never fetch, redirect to, or enqueue a URL taken from the payload without a strict host allowlist — an attacker-controlled callback …
- Distinctive: line 86
Do the heavy work (API calls, emails, DB writes) in the background task, not in the request — but persist enough in the view that a retry …
- Very specific: line 98
Check the signature timestamp: If the provider signs a timestamp (t=...,v1=...), include it in the HMAC input and reject deliveries outside …
- Very specific: line 102
Support two secrets during rotation: Verify against [SECRET_CURRENT, SECRET_PREVIOUS] with any(hmac.compare_digest(sig, ...)) so a rotation …
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Webhook handler (verify, resolve, dedupe, ack)
When to use
Adding or reviewing any endpoint a third party POSTs to — payment events, chat platform callbacks, delivery receipts, provider status updates. Treat the whole request body as a form submitted by a stranger: nothing in it is trusted until you have proven where it came from.
Pattern
Four steps, always in this order. Skipping or reordering any one is a security or reliability bug, not a style nit:
- Verify authenticity first. Compute the provider's HMAC over the raw body and compare with a constant-time equality check, or match the provider's verify token. Do this before JSON-parsing or trusting a single field. Reject with 401 on mismatch.
- Resolve the owning tenant explicitly from the verified payload, so the event
lands in the right account and can never leak across tenants (see
multi-tenancy). No tenant resolved → reject, never fall through to a default account. Do this before you persist: a dedupe row written for an event you then reject makes the provider's retry a no-op, losing the event permanently. - Dedupe idempotently by the event's external id. Providers retry and
re-deliver the same event; a unique constraint or
get_or_createmakes reprocessing a no-op instead of a double charge or duplicate row. Dedupe on handled, not merely seen — a row stillpendingmust be re-enqueued, not swallowed. - ACK 200 fast, then work in the background. Persist the row and enqueue the task
in one
atomic()block, with the enqueue ontransaction.on_commitso it can never fire for a row that rolled back. Then return 200 and let the task do the real work — slow handlers make the provider time out and retry, multiplying load.
Steps / idioms
Verify → resolve tenant → dedupe → enqueue on commit → 200, in one thin view. The tenant is resolved before the dedupe row exists, so a rejected event leaves no row for the provider's retry to collide with:
@csrf_exempt
def provider_webhook(request):
raw = request.body # raw bytes, not parsed
sig = request.headers.get("X-Provider-Signature", "")
expected = hmac.new(SECRET, raw, hashlib.sha256).hexdigest()
if not hmac.compare_digest(sig, expected): # 1. constant-time verify
return HttpResponse(status=401) # before trusting body
event = json.loads(raw)
tenant = resolve_tenant(event) # 2. tenant BEFORE we persist
if tenant is None:
return HttpResponse(status=400) # no owner → reject, no row
with transaction.atomic():
obj, created = WebhookEvent.objects.get_or_create( # 3. idempotent dedupe
provider="provider", external_id=event["id"], # (unique together)
defaults={"payload": event, "entreprise": tenant, "status": "pending"},
)
if not created and obj.status != "pending":
return HttpResponse(status=200) # already handled → no-op
# only enqueue once the row is durably committed
transaction.on_commit(lambda: process_webhook_event.delay(obj.id))
return HttpResponse(status=200) # 4. ack now, work laterAdapt to your repo
Rename the signature header, secret key, and provider name to match each integration.
Confirm you hash the raw request body — re-serializing parsed JSON changes bytes and
breaks the HMAC. Set the unique constraint on (provider, external_id). Give
WebhookEvent a status field (pending/done/failed); the task must flip it to
done on success, so a re-delivery of a still-pending event is re-enqueued rather
than swallowed. Point the background call at your task runner (see celery-tasks),
and resolve the tenant through whatever your project treats as the account owner.
Gotchas
- Verifying against parsed-then-re-dumped JSON fails intermittently — always sign the raw bytes exactly as received.
- Use a constant-time compare (
hmac.compare_digest), never==, for signatures. - SSRF: never fetch, redirect to, or enqueue a URL taken from the payload without a strict host allowlist — an attacker-controlled callback can hit internal services.
- Some providers send an unsigned verification handshake on setup; handle it explicitly rather than weakening signature checks for real events.
- Do the heavy work (API calls, emails, DB writes) in the background task, not in the request — but persist enough in the view that a retry after a crash is still idempotent.
- Return 200 only after the event is durably stored and marked for processing — a dedupe row written before the tenant is resolved or before the task is enqueued turns the provider's retry into a silent 200 and loses the event for good.
- Bound the body before you hash it.
request.bodyis capped byDATA_UPLOAD_MAX_MEMORY_SIZE(2.5 MB default) — set it to your provider's real maximum rather than inheriting the default, and return 413 above it. - Parse defensively after verifying. Wrap
json.loads(raw)and theevent["id"]lookup intry/except (ValueError, KeyError): return HttpResponse(status=400)— a signed-but-malformed body should be a 400 you can see, not a 500 the provider silently retries forever. - Check the signature timestamp. If the provider signs a timestamp
(
t=...,v1=...), include it in the HMAC input and reject deliveries outside a short window (±5 min). Dedupe by event id blunts replay but does not stop a captured request being replayed before the original lands. - Support two secrets during rotation. Verify against
[SECRET_CURRENT, SECRET_PREVIOUS]withany(hmac.compare_digest(sig, ...))so a rotation does not 401 in-flight deliveries; drop the old secret once the provider has cut over.
See also
celery-taskssecurity-reviewmulti-tenancy
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

