Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 3 min read

Our webhook signing secret is not stored anywhere, and rolling it is adding one to a number

Our outgoing webhooks tell a customer's system when a candidate starts or finishes an assessment. The customer-facing part is on the developers page. This post is about four decisions underneath it that I would make agai

Our outgoing webhooks tell a customer's system when a candidate starts or finishes an assessment. The customer-facing part is on the developers page. This post is about four decisions underneath it that I would make again.

The secret is derived, not stored

Each endpoint has a signing secret that starts whsec_. We do not keep it in the database. It is computed from the endpoint's id, a version number, and a server-side key:

export function deriveSecret(endpointId: string, secretVersion: number): string {
  const digest = createHmac('sha256', serverKey()).update(/* endpoint id and version */).digest('base64url');
  return `${SECRET_PREFIX}${digest}`;
}

Three things follow from that without any extra code. We can show the secret again whenever the owner asks, so there is no "copy it now, you will never see it again" moment. A copy of the database contains nothing that can sign a request. And rolling the secret is secret_version + 1: the old one stops being the answer and the new one is computed on the next send.

The trade is that the server key becomes the thing to protect, which it already was.

Sign the bytes you send

The header is t=<unix seconds>,v1=<signature>, and the signature is an HMAC-SHA256 of "<t>.<body>". The one mistake worth naming is signing an object and then sending a different serialisation of it. We stringify the payload once, sign that string, and send that same string. The receiver has to do the mirror image: verify the raw body as it arrived, before any JSON parsing.

On the receiving side, the check refuses a timestamp more than five minutes from now, so a captured request cannot be replayed next week, and compares with timingSafeEqual. Before it does, it checks the header's shape, a digit string for the time and exactly 64 hex characters for the signature. timingSafeEqual throws on buffers of different lengths, so an unchecked malformed header would be a 500 instead of a refusal.

A retry ladder with an end

A delivery counts as delivered only on a 2xx. Anything else is retried after 1 minute, 5 minutes, 30 minutes, 2 hours and 6 hours, which makes six tries in all. After twenty failures in a row an endpoint is switched off and its owners are told, because an address that has failed for that long is not coming back by itself.

The delivery id stays the same on every retry. That is what a receiver should key on to ignore an event it has already handled. We say so in the docs, because at-least-once delivery without it is a trap.

A run that sends a delivery holds it for ten minutes. That is longer than a send or a whole maintenance step can take, so a second run never sends the same one, and short enough that if a run dies the delivery is picked up soon after.

Announce an event once, and not months later

This is the one that surprised me. "Has this event already been announced?" is answered by looking for its delivery. But deliveries are deleted after thirty days. So if a report is sent again months later, there is no delivery to find, and the completion would be announced a second time.

The fix is a freshness window. An event is announced only if it happened within the last three days. The time is read from the stored invite, not from the code path that triggered it, which means an event whose transaction was rolled back is never announced either, because the stored invite never said it happened.

Who is never sent anything

Made-up candidates should not reach a customer's systems. A sample role's candidates, a benchmark taken by the employer's own staff, demo and beta organisations, and suspended accounts are all skipped. It is one pure function, which is why every case has a test rather than a comment promising it works.

If you want to see the request and the check a receiver makes, the developers page has both, and the pricing page lists what each plan includes.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.