SANDBOX VERIFIED · 2 BLOCKS (91.08 ms) Friday Pillar by Tharusha Nuwansara

Constant-Time HMAC Webhook Verification & Integer-Cent Double-Entry SQL Ledgers in Python

How to verify raw webhook bytes in constant time, block intra-window replay attacks with SQLite WAL nonces, and record zero-drift integer-cent journal postings.

#python#security#sql#backend

Three Subtle Bugs Hiding in Typical Payment Webhook Handlers

Handling incoming order.paid or subscription webhooks from Stripe, Patreon, Gumroad, or Lemon Squeezy looks deceptively simple until real network retries and adversarial traffic hit your endpoint. Across dozens of production codebases, the same three vulnerabilities show up repeatedly:

  1. Verifying Re-Serialized JSON Instead of Raw Bytes: Parsing the HTTP request body into a Python dict and calling json.dumps(body) before computing HMAC-SHA256 alters whitespace and key ordering, breaking legitimate signatures—while comparing hex strings with == instead of hmac.compare_digest leaks timing side-channels.
  2. Relying Exclusively on a 5-Minute Timestamp Tolerance: Checking abs(now - timestamp) <= 300 stops yesterday's captured curl payload, but does nothing to stop an attacker—or an aggressive upstream retry proxy—from replaying the exact same valid signed request six times within that 300-second window.
  3. Storing Currency in FLOAT Columns With Mutable Balance Counters: Updating SET balance = balance + 13.29 in IEEE-754 floating point accumulates binary rounding drift (13.29 * 3 = 39.870000000000005) and leaves zero audit trail when a refund or processor fee adjustment arrives out of order.

In this Friday reliability teardown—Defending payment webhooks against timing side-channels, 5-minute replay windows, and floating-point rounding drift. (reference documentation: Python 3 Standard Library Docs)—we build and test a constant-time HMAC webhook ingester backed by atomic SQLite WAL replay-nonce deduplication and an immutable, integer-cent double-entry ledger.

Security & Accounting Invariants

Threat / FailureVulnerable ShortcutProduction Invariant
Timing Side-Channel & Body Mutationcomputed_hex == header_hex on re-serialized JSONCompute HMAC-SHA256(secret, f"{ts}.{nonce}." + raw_bytes) and compare via hmac.compare_digest
Intra-Window Replay (< 300s)Stateless timestamp check onlyInsert nonce into webhook_nonces(nonce PRIMARY KEY) inside the same BEGIN IMMEDIATE transaction as the ledger write
Floating-Point DriftREAL / FLOAT dollar columnsStore strictly positive integer minor units (amount_cents INTEGER CHECK (amount_cents > 0))
Unbalanced MutationsUPDATE accounts SET balance = ...Record immutable DEBIT and CREDIT postings per tx_id and assert SUM(DEBIT) == SUM(CREDIT)

Need a lightweight idempotent cron runner and HTTP retry guard to pair with your reconciliation jobs? Download the Free Idempotent Cron & Circuit-Breaker Mini-Kit ($0+ PWYW) to get started at zero cost.

Runnable Constant-Time HMAC & Double-Entry Ledger Verification

The self-contained Python 3.11+ script below processes 24 signed $14.00 (1400 cents gross, 71 cents fee, 1329 cents net) order webhooks, fires 6 intra-window replay attacks, 1 tampered-amount signature attack, and 1 stale-timestamp attack, and verifies that total debits equal total credits down to the exact cent:

PYTHON
from __future__ import annotations

import hashlib
import hmac
import json
import sqlite3
import time
from pathlib import Path


def sign_event(secret: bytes, ts: int, nonce: str, raw_body: bytes) -> str:
    msg = f"{ts}.{nonce}.".encode("utf-8") + raw_body
    sig = hmac.new(secret, msg, hashlib.sha256).hexdigest()
    return f"t={ts},id={nonce},v1={sig}"


def open_ledger(db_path: Path) -> sqlite3.Connection:
    conn = sqlite3.connect(str(db_path), timeout=5.0, isolation_level=None)
    conn.execute("PRAGMA journal_mode=WAL;")
    conn.execute("PRAGMA foreign_keys=ON;")
    conn.executescript(
        """
        CREATE TABLE IF NOT EXISTS webhook_nonces (
            nonce TEXT PRIMARY KEY,
            received_at INTEGER NOT NULL
        );
        CREATE TABLE IF NOT EXISTS journal_entries (
            tx_id TEXT PRIMARY KEY,
            external_event_id TEXT NOT NULL UNIQUE,
            created_at INTEGER NOT NULL
        );
        CREATE TABLE IF NOT EXISTS postings (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            tx_id TEXT NOT NULL REFERENCES journal_entries(tx_id),
            account TEXT NOT NULL,
            direction TEXT NOT NULL CHECK (direction IN ('DEBIT', 'CREDIT')),
            amount_cents INTEGER NOT NULL CHECK (amount_cents > 0)
        );
        """
    )
    return conn


def ingest_payment_webhook(
    conn: sqlite3.Connection,
    secret: bytes,
    header: str,
    raw_body: bytes,
    now_ts: int,
    tolerance_sec: int = 300,
) -> str:
    kv = dict(part.split("=", 1) for part in header.split(",") if "=" in part)
    ts = int(kv["t"])
    nonce = kv["id"]
    provided_sig = kv["v1"]

    if abs(now_ts - ts) > tolerance_sec:
        return "rejected_stale_timestamp"

    expected_sig = hmac.new(secret, f"{ts}.{nonce}.".encode("utf-8") + raw_body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected_sig, provided_sig):
        return "rejected_bad_signature"

    event = json.loads(raw_body.decode("utf-8"))
    gross_cents = int(event["gross_cents"])
    fee_cents = int(event["fee_cents"])
    net_cents = gross_cents - fee_cents
    if net_cents <= 0 or (net_cents + fee_cents) != gross_cents:
        raise ValueError("Unbalanced integer-cent posting")

    conn.execute("BEGIN IMMEDIATE;")
    try:
        dup = conn.execute("SELECT 1 FROM webhook_nonces WHERE nonce = ?", (nonce,)).fetchone()
        if dup is not None:
            conn.execute("ROLLBACK;")
            return "rejected_replay_nonce"

        conn.execute("INSERT INTO webhook_nonces (nonce, received_at) VALUES (?, ?)", (nonce, now_ts))
        tx_id = f"tx_{nonce}"
        conn.execute(
            "INSERT INTO journal_entries (tx_id, external_event_id, created_at) VALUES (?, ?, ?)",
            (tx_id, event["event_id"], now_ts),
        )
        conn.executemany(
            "INSERT INTO postings (tx_id, account, direction, amount_cents) VALUES (?, ?, ?, ?)",
            [
                (tx_id, "cash_settlement", "DEBIT", net_cents),
                (tx_id, "processor_fees", "DEBIT", fee_cents),
                (tx_id, "digital_product_revenue", "CREDIT", gross_cents),
            ],
        )
        conn.execute("COMMIT;")
        return "posted"
    except Exception:
        conn.execute("ROLLBACK;")
        raise


def run_ledger_verification() -> None:
    secret = b"whsec_production_grade_key_991"
    conn = open_ledger(Path("ledger_verify.db"))
    now_ts = 1_760_500_000
    t0 = time.perf_counter()

    outcomes = {"posted": 0, "rejected_replay_nonce": 0, "rejected_bad_signature": 0, "rejected_stale_timestamp": 0}
    for i in range(1, 25):
        body = json.dumps({"event_id": f"ord_{i}", "gross_cents": 1400, "fee_cents": 71}).encode("utf-8")
        hdr = sign_event(secret, now_ts - 5, f"nonce_{i}", body)
        outcomes[ingest_payment_webhook(conn, secret, hdr, body, now_ts)] += 1
        if i <= 6:
            outcomes[ingest_payment_webhook(conn, secret, hdr, body, now_ts + 2)] += 1

    tampered_hdr = sign_event(secret, now_ts - 2, "nonce_tamper", b'{"event_id":"x","gross_cents":1400,"fee_cents":0}')
    outcomes[ingest_payment_webhook(conn, secret, tampered_hdr, b'{"event_id":"x","gross_cents":9900,"fee_cents":0}', now_ts)] += 1

    stale_body = b'{"event_id":"stale_1","gross_cents":1400,"fee_cents":71}'
    stale_hdr = sign_event(secret, now_ts - 900, "nonce_stale", stale_body)
    outcomes[ingest_payment_webhook(conn, secret, stale_hdr, stale_body, now_ts)] += 1

    debits = conn.execute("SELECT SUM(amount_cents) FROM postings WHERE direction = 'DEBIT'").fetchone()[0]
    credits = conn.execute("SELECT SUM(amount_cents) FROM postings WHERE direction = 'CREDIT'").fetchone()[0]
    net_cash = conn.execute("SELECT SUM(amount_cents) FROM postings WHERE account = 'cash_settlement'").fetchone()[0]
    elapsed_ms = round((time.perf_counter() - t0) * 1000.0, 2)

    summary = {
        **outcomes,
        "total_debits_cents": int(debits),
        "total_credits_cents": int(credits),
        "ledger_imbalance_cents": int(debits) - int(credits),
        "net_settlement_usd": round(int(net_cash) / 100.0, 2),
        "elapsed_ms": elapsed_ms,
    }
    Path("ledger_report.json").write_text(json.dumps(summary, indent=2), encoding="utf-8")
    print(json.dumps(summary, indent=2))
    conn.close()


if __name__ == "__main__":
    run_ledger_verification()

Next, we run an independent assertion script over ledger_report.json to confirm zero ledger imbalance (ledger_imbalance_cents == 0) and 100% rejection across all 8 simulated attacks:

BASH
python3 -I -c '
import json
d = json.load(open("ledger_report.json"))
assert d["ledger_imbalance_cents"] == 0, "Double-entry ledger out of balance!"
assert d["rejected_replay_nonce"] == 6 and d["rejected_bad_signature"] == 1 and d["rejected_stale_timestamp"] == 1
print(f"Verified zero-imbalance ledger (${d["net_settlement_usd"]} net across {d["posted"]} orders; 8 attacks blocked).")
'

Real Captured Execution Output

Both blocks above ran inside a clean isolated sandbox prior to staging. Here is the exact stdout captured from the run:

TEXT
# Block 1 (python, exit=0, 55.3 ms)
{
  "posted": 24,
  "rejected_replay_nonce": 6,
  "rejected_bad_signature": 1,
  "rejected_stale_timestamp": 1,
  "total_debits_cents": 33600,
  "total_credits_cents": 33600,
  "ledger_imbalance_cents": 0,
  "net_settlement_usd": 318.96,
  "elapsed_ms": 3.96
}

# Block 2 (bash, exit=0, 35.7 ms)
Verified zero-imbalance ledger ($318.96 net across 24 orders; 8 attacks blocked).

Why Atomic Nonce + Ledger Coupling Eliminates Race Conditions

Examine the transaction boundary inside ingest_payment_webhook above:

  • Single Atomic Commit (BEGIN IMMEDIATE): Checking webhook_nonces, inserting the journal_entries header (UNIQUE on external_event_id), and writing all three DEBIT/CREDIT postings happen inside one atomic WAL write lock. If two identical webhooks arrive on parallel threads 50 microseconds apart, the second writer deterministically hits the webhook_nonces constraint and rolls back with rejected_replay_nonce—zero chance of double-crediting entitlements.
  • Exact Fee Visibility: By splitting every $14.00 order into DEBIT cash_settlement (1329), DEBIT processor_fees (71), and CREDIT digital_product_revenue (1400), reconciling bank payouts at the end of the month is a single SELECT SUM(amount_cents) query with 0 cents of rounding discrepancy ($318.96 net across 24 orders).
  • Bounded Nonce Table Growth: Because timestamps older than tolerance_sec (300s) are rejected before touching SQLite, your webhook_nonces table only ever needs to retain rows from the last 2 * tolerance_sec (10 minutes), keeping disk usage under a few kilobytes even at high volume.

Handling Refunds, Chargebacks, and Multi-Gateway Payouts

When you later add a refund or dispute webhook handler (charge.refunded), never DELETE or UPDATE an existing row in postings. Instead, insert a new compensating journal_entries transaction whose postings reverse the original directions (DEBIT digital_product_revenue, CREDIT cash_settlement, and CREDIT processor_fees if the payment processor returns its fee). That immutable append-only discipline lets you reconstruct exact point-in-time account balances for any historical UTC timestamp, compare Patreon, Gumroad, or Lemon Squeezy settlement CSV exports line-by-line against journal_entries.external_event_id, and spot missing webhook deliveries in milliseconds using a single LEFT JOIN reconciliation query.

Get the Full SQL Schema, Python/Node Guards & Queue Kit

To skip wiring boilerplate from scratch, grab the Production AI Agent & Zero-Cost Backend Reliability Starter Kit ($14, MIT License)—it includes the portable SQLite/PostgreSQL sql/idempotent_ledger.sql schema with account-balance views, webhook_hmac.py, Node/TypeScript verifyWebhookSignature (agent_guardrails.mjs), the transactional SQLite WAL queue + dead-letter CLI, and 4 GitHub Actions blueprints.


Verification & Disclosure: Published under the Zero-Cost Production Reliability column by Tharusha Nuwansara. Every Python and shell block above was executed and verified inside an isolated python3 -I sandbox (returncode == 0) prior to publication.