# Boundaries

fishhook-ant checks that a webhook delivery was signed by the sender it
claims to come from, before your handler sees it. It exists because tests in
one of our projects sent replayed deliveries and deliveries with nonsense
signatures, and the handlers accepted both.

## What it owns

- Verifying a signature over the raw request bytes, for a scheme the caller
  describes. The core knows no provider names.
- Rejecting a delivery whose signed timestamp is outside the caller's
  tolerance, for schemes that sign one.
- Presets for common providers, as plain data on top of the scheme model.
- An HTTP adapter on the `fishhook-ant/http` subpath that reads a `Request`
  body once, as bytes. The core takes bytes and headers.

## The scheme model

The providers checked so far differ in four ways:

| Provider | Algorithm | Signed bytes | Signature header | Encoding |
| --- | --- | --- | --- | --- |
| Standard Webhooks | HMAC-SHA256 (`v1`), Ed25519 (`v1a`) | `id.timestamp.body` | `webhook-signature`, space-separated list | base64 |
| Stripe | HMAC-SHA256 | `timestamp.body` | `Stripe-Signature`, `t=` and one or more `v1=` | hex |
| Slack | HMAC-SHA256 | `v0:timestamp:body` | `X-Slack-Signature`, `v0=` prefix | hex |
| Discord | Ed25519 | `timestamp` then `body`, no separator | `X-Signature-Ed25519` | hex |
| GitHub | HMAC-SHA256 | `body` | `X-Hub-Signature-256`, `sha256=` prefix | hex |
| Shopify | HMAC-SHA256 | `body` | `X-Shopify-Hmac-SHA256` | base64 |
| Linear | HMAC-SHA256 | `body` | `Linear-Signature` | hex |

GitLab's signing token uses the Standard Webhooks header format. Both
algorithms are in Web Crypto, so the package needs no dependency for them.

A scheme is: the algorithm, a template for the signed bytes built from
named parts (`id`, `timestamp`, `body`, literal separators), where each part
is read from, and how the signature header is split and decoded. Several
signatures in one header pass if any one verifies, which is how providers
rotate secrets.

A scheme the caller describes goes through the same checks as a preset, and
presets use the same public path. The constructor refuses a scheme whose
signed bytes leave out the body, a scheme that signs a timestamp without a
tolerance, and a scheme that signs none without a replay decision. A wrong
description makes every delivery fail, which is loud; it cannot make a forged
one pass. So describing your own scheme needs no opt-in.

## Decisions the caller makes

- The secret or public key. Required.
- The timestamp tolerance, in seconds, for schemes that sign a timestamp.
  Required; there is no default. It is capped at 300, the window Stripe,
  Slack and Standard Webhooks all recommend. A wider window needs
  `unsafeTolerance`.
- How replays are caught. Pass `rememberReplayKey`, an atomic "record this
  key, and say whether it was new" against your own store, or
  `unsafeAcceptReplays: true`. Neither is the default. This applies to every
  scheme: a timestamp only narrows a replay to the tolerance window, and
  GitHub, Shopify and Linear sign no timestamp at all.

The replay key is the verified signature. A delivery ID that is not signed
(GitHub's `X-GitHub-Delivery`, Shopify's webhook ID) is never used for this:
whoever replays a delivery can change it.

Every option that loosens a check is named `unsafe*`, so one search finds
every loosened choice in a codebase:

```sh
grep -rnE "unsafe[A-Z]" .
```

This is the convention lacewing uses.

Fixed, with no option: comparison is constant-time, verification runs over
the received bytes and never over re-serialised JSON, a missing or malformed
signature header is a refusal, and HMAC-SHA1 is not accepted.

## Out of scope

| Excluded | Where it goes |
| --- | --- |
| SendGrid (ECDSA, DER-encoded signatures) | Web Crypto takes raw signatures, so this needs a hand-written DER parser. Not in the first version; use SendGrid's SDK |
| Twilio's request signatures | They sign the URL and sorted form fields with HMAC-SHA1, not the body. Use Twilio's SDK |
| Plain shared-secret headers (Telegram, GitLab's legacy token) | They are a password, not a signature, and cover no bytes. Compare them yourself in constant time |
| Signatures needing a downloaded certificate (AWS SNS) | Fetching keys over the network is not this package's job. Use the provider's SDK |
| Timestamps inside the signed body (Linear's `webhookTimestamp`) | The package does not parse payloads. Check the field after verification |
| Storing replay keys | Your store, behind `rememberReplayKey` (`SET NX` in Redis, `INSERT ... ON CONFLICT DO NOTHING` in Postgres) |
| Running a handler once per event when the sender retries it | mycocepurus-smithii |
| Out-of-order events, stale updates, deleted entities coming back | burying-beetle |
| Retries and timeouts | firefly |
| Parsing or validating the payload | Zod, Valibot or JSON Schema |
| RFC 9421 HTTP Message Signatures | No provider checked uses it. Revisit if one does |
| Sending webhooks | This is the receiving side only |

## Testing without accounts

Every scheme here is tested with a secret or key pair we generate, so no
provider account or API key is needed. Twitter/X is left out for that reason:
its webhooks need a paid API tier. Deliveries are cross-checked against the
providers' own libraries (`stripe`, `standardwebhooks`, `discord-interactions`,
`@octokit/webhooks-methods`) and against the worked examples in the Standard
Webhooks specification and GitHub's documentation. Slack, Shopify and Linear
have no first-party verifier to check against; their tests follow each
documented formula. `tests/origin.test.ts` holds the two cases from our
project: a replayed delivery and a nonsense signature.

## Open questions

- Each preset follows provider documentation and third-party guides read on
  2026-11-29, cross-checked as above. None has been run against a real
  captured delivery yet.
- GitLab's signing token is said to use the Standard Webhooks header format.
  There is no preset until that is confirmed against GitLab's own docs.
