Docs / Guides

Webhooks

webhooks signs HTTP webhook deliveries with ML-DSA-44 and verifies them with a timestamp window and a replay cache.

01Sender

import { webhooks } from 'pqlock';

const key = webhooks.generateKey();                 // store key.seed as a secret; share key.public
const body = Buffer.from(JSON.stringify(event));
await fetch(url, { method: 'POST', body, headers: { [webhooks.HEADER]: webhooks.sign(key, body) } });
from pqlock import webhooks

key = webhooks.generate_key()
header = webhooks.sign(key, body)
requests.post(url, data=body, headers={"PQL-Webhook-Signature": header})
key, err := webhooks.GenerateKey()
header, err := webhooks.Sign(key, body, time.Now())
req.Header.Set("PQL-Webhook-Signature", header)

02Receiver

const verifier = new webhooks.Verifier([senderPublicKey]);   // tolerance 300 s, replay cache built in

app.post('/hooks', express.raw({ type: '*/*' }), (req, res) => {
  try {
    const kid = verifier.verify(req.get(webhooks.HEADER), req.body);
    handle(JSON.parse(req.body));
    res.sendStatus(204);
  } catch (err) {
    res.status(401).send((err as webhooks.WebhookError).reason);
  }
});
cache = webhooks.ReplayCache()
kid = webhooks.verify([sender_public_key], request.headers.get("PQL-Webhook-Signature"), request.get_data(), cache=cache)
v, err := webhooks.NewVerifier([][]byte{senderPublic}, webhooks.WithTolerance(5*time.Minute))
kid, err := v.Verify(r.Header.Get("PQL-Webhook-Signature"), body)
if err != nil { http.Error(w, webhooks.Reason(err), 401) }

Always verify the raw body bytes, before any JSON parsing. Rejections carry a reason: malformed, unknown_key, expired, bad_signature, replay or cache_full.

03Header format

PQL-Webhook-Signature: t=1796467200,kid=3fa91c0b2d7e4410,sig=<base64url ML-DSA-44 signature>

The signed message is kid.t.body with the context string PQL webhook v1. Several public keys can be trusted at once, which is how a sender rotates its key without downtime.

pqlock v0.1.0 · unaudited preview · source of truth: spec/