/algo-trading-skills:zerodha-kite-postback-webhook-verificationfrom algo-trading-skills
Verifies SHA-256 checksums on Zerodha Kite order postbacks. The checksum only covers order_id, order_timestamp, and api_secret — status and fill details are unauthenticated, so a verified postback should only trigger reconciliation, not trade execution.
Ships in algo-trading-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Invoke this whenever a bot exposes an HTTPS endpoint as its Kite Connect app's postback
URL and consumes order updates from it. Kite sends the update as a raw JSON POST body
— there are no signature headers — and anyone on the internet who learns that URL can
POST to it. Without verification, a fabricated "status": "COMPLETE" postback can drive
a strategy to hedge against a fill that never happened, or a position ledger to record
inventory the account does not hold.
Read the checksum's scope before you design around it. Kite documents the
checksum field as SHA-256(order_id + order_timestamp + api_secret). That covers the
order identifier and the update instant. It does not cover status,
filled_quantity, average_price, pending_quantity, or anything else in the body.
An attacker who captures one postback in transit can rewrite those fields and the
checksum still matches. So the correct reading of a verified postback is "Zerodha says
order X changed at instant T" — the what must come from GET /orders/:order_id.
This is the single most consequential thing to get right here, and it is a property of
Kite's scheme, not something a verifier can fix.
GET /orders reconciliation sweep regardless.references/standards.md and webhook-based-order-fill-notifications for the
generic HMAC-over-raw-body pattern most other venues use.order-placement-idempotency.kill-switch-and-drawdown-circuit-breakers.api_secret loaded from an environment variable or vault — never from
source. The digest is forgeable by anyone who holds it.scripts/postback_verifier.py is bounded and per-process.GET /orders/:order_id from the handler, for reconciliation.Read the raw body, do not re-serialize it.
Kite posts JSON with no signature headers. Parse order_id, order_timestamp,
checksum, status, filled_quantity, average_price from the body. The field is
order_timestamp — not timestamp — and it is the exact string that must be fed to
the digest. Reformatting it (normalising the separator, attaching a timezone) changes
the pre-image and every checksum will then fail.
Reject structurally unusable payloads first.
Both order_id and order_timestamp are checksum inputs; if either is absent there
is nothing to verify. Return 200 and drop it — do not raise out of the handler.
Verify the checksum, failing closed.
Compute SHA-256(order_id + order_timestamp + api_secret) and compare with
hmac.compare_digest on bytes, after checking the received value is 64 hex
characters. If the checksum field is missing or empty, reject — an absent
signature is not a pass. Comparing str values directly raises TypeError on
non-ASCII input that the attacker controls.
Verify before the freshness check, so an unauthenticated payload cannot influence
order_id-keyed logging or timing, and so "stale" and "forged" stay distinguishable.
Bound freshness in both directions, in the right timezone.
order_timestamp is YYYY-MM-DD HH:MM:SS with no timezone marker and is
exchange-local IST (UTC+05:30). Parsing it as server-local time on a UTC host makes
every postback look 5.5 hours stale. Bound the past (default 300s) and the future
(small, for clock skew) separately: an old payload is a delayed or replayed delivery;
a post-dated one is skew or tampering, and they warrant different responses.
Note the limit of this check — because the checksum is static for a given order
update, an attacker replaying inside the window still verifies. Freshness is a
staleness guard; deduplication is the actual replay defense.
Deduplicate on the full state fingerprint, not on order_id + status.
Kite emits an UPDATE postback for every partial fill, and successive partial
fills of the same order all carry status == "OPEN". Keying dedup on the status
silently discards every fill after the first and leaves the ledger short. Fingerprint
order_id, order_timestamp, status, filled_quantity and average_price
together, and claim it atomically — test-and-insert under one lock, or a unique
constraint in the database — so two concurrent redeliveries cannot both win.
Reconcile before mutating state.
On an accepted postback, call GET /orders/:order_id and apply that response to
the ledger. The postback's own status/filled_quantity are unauthenticated
(step 3's scope) and are safe only as a hint about which order to re-read.
Acknowledge and alert.
Return 2xx quickly regardless of outcome — a non-2xx buys you nothing, since there is
no documented retry. Log rejections with the outcome class, order id, source IP and a
truncated checksum fragment. Never log the api_secret or a full received checksum.
A checksum mismatch is a security event; staleness usually is not.
Full step-by-step procedure with broker-specific detail: see
references/workflows.md. Broker/framework coverage table for this skill: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
if received_checksum: around the
comparison is a fail-open authentication bypass: an attacker simply omits the field
and the payload sails through as verified.order_id and
order_timestamp only. A replayed postback with filled_quantity rewritten verifies
perfectly. Reconcile against GET /orders/:order_id.order_id:status. Every partial fill after the first shares its
predecessor's OPEN status and gets thrown away, so the ledger under-reports quantity
on exactly the orders that fill in pieces.if result.valid: apply()
then double-applies the redelivery the dedup step just caught. "Authentic" and "safe
to apply" are different answers and need different fields.order_timestamp as UTC or as epoch seconds. It is naive IST text. A
parser that only understands epoch numbers rejects 100% of genuine postbacks — and a
test suite that feeds it epoch strings will never notice.str. hmac.compare_digest raises TypeError on
non-ASCII str, turning an attacker-supplied checksum into an unhandled 500.set of processed events. A long-lived receiver leaks memory for the
life of the process. Bound it, and persist it if restarts must stay idempotent.ACCEPTED.checksum absent, empty, or null is rejected as
REJECTED_MISSING_CHECKSUM — never accepted.OPEN status, rising
filled_quantity) are all accepted; a byte-identical redelivery returns DUPLICATE
with valid is False.ACCEPTED.python -m unittest discover -s skills/zerodha-kite-postback-webhook-verification/scripts and confirm a
100% pass rate.order-placement-idempotencywebhook-based-order-fill-notificationsheadless-broker-auth-patternsmulti-broker-rate-limit-handlingstructured-logging-for-post-incident-forensicsWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub himanshuj16/algo-trading-skills --plugin algo-trading-broker-integration36 skills in this plugin
/alpaca-paper-live-key-separationUse when a bot connects to the Alpaca Trading API and must never cross paper keys with the live endpoint; pins the base URL per environment, checks the key prefix, probes /v2/account and requires an explicit ALLOW_LIVE_TRADING flag./api-key-least-privilege-audit-toolUse before deploying a live bot, to audit existing broker API keys for scopes they do not need. Flags withdrawal, transfer and admin rights that turn a leaked execution key into theft./broker-account-margin-call-handlingUse when a bot trades a Reg T, portfolio or futures margin account and must act before the broker liquidates: tiered maintenance-margin warnings cross-checked against broker excess liquidity, initial-margin order gating and liquidity-aware de-leveraging.Other plugins' skills for the same job