Validating Meta's X-Hub-Signature-256 signature

How to check the X-Hub-Signature-256 header on Meta webhooks with HMAC-SHA256, why the raw body matters, and how to compare without leaking timing.

A webhook endpoint is a public door: anyone who discovers the URL can POST JSON that looks like a WhatsApp event. The signature is what separates the real event from the imitation. This article explains what Meta documents about the X-Hub-Signature-256 header, how to validate it in Node, and which implementation traps break verification even with the right secret.

What the X-Hub-Signature-256 header is

It is the header Meta includes on every event POST, in the format sha256= followed by a hexadecimal hash. The documentation says the hash is an HMAC-SHA256 of the payload, computed with the app secret as the key. If the value you compute equals what came after sha256=, the payload is valid; if it differs, treat it as invalid and do not process it.

Two practical consequences follow. First, the key is the app secret, not the verify token from the GET verification. Second, the signature proves the body was produced by someone who knows the app secret. It does not prove the event is new: a legitimate body that was captured and replayed would pass the check, which is why idempotency is a separate topic.

Why the body must be the raw body

The computation must run over the exact bytes that arrived, because any change in the text changes the hash. The Meta page consulted says the hash is computed over the "post body payload", but does not spell out whether that means the received bytes or a re-serialized copy. There is no official confirmation of tolerant behavior, so the safe stance is to assume raw bytes.

In practice, the trap is frameworks that parse JSON before your code runs. The object left over is a reconstruction: whitespace, key order and the way non-ASCII characters are written can differ from the original. An accent written as a unicode escape by the sender and rewritten as a literal character by your JSON.stringify yields a different hash. The documentation consulted does not mention unicode escaping, which reinforces the rule: rebuild nothing.

In Express, that means using a parser that hands you a Buffer on that route only, such as express.raw. In frameworks that offer a verify hook on the JSON parser, keep a copy of the raw body before parsing. In any case, the simplest test is to log the byte length of what you received and compare it with the request's Content-Length.

A concrete example in Node

The function below takes the raw body, the header value and the app secret, and returns true or false. It does three things beyond the computation: it strips the sha256= prefix, rejects up front anything that is not a 64-character hexadecimal string, and compares with timingSafeEqual.

import { createHmac, timingSafeEqual } from 'node:crypto' // rawBody: Buffer with the exact bytes received export function assinaturaDaMetaValida(rawBody, header, appSecret) { const recebida = header?.replace('sha256=', '') if (!recebida || !/^[0-9a-f]{64}$/.test(recebida)) return false const esperada = createHmac('sha256', appSecret).update(rawBody).digest('hex') return timingSafeEqual(Buffer.from(recebida, 'hex'), Buffer.from(esperada, 'hex')) }

The format check comes before Buffer.from for a concrete reason: a 64-character string with non-hex content yields a shorter buffer, and timingSafeEqual throws RangeError when lengths differ. Without that guard, a malformed header crashes the handler instead of being rejected, and an attacker gets a cheap way to provoke 500 errors on your server.

In Python the equivalent of timingSafeEqual is hmac.compare_digest; in PHP, hash_equals. The rule is the same in all three languages: the comparison must take constant time with respect to the content.

Why compare with timingSafeEqual and not ===

Because ordinary string comparison can stop at the first differing character, and the time it takes reveals how many leading characters were right. By measuring that time many times, an attacker can in principle discover a valid signature character by character. Constant-time comparison functions remove that signal.

The real risk depends on your environment: network noise often hides microsecond differences. Still, the fix costs one line, and the cost of skipping it is asymmetric. There is no reason to keep the naive comparison in new code.

What to do when the signature does not match

Respond with an error status such as 401 and do not process the body. Do not write the whole payload to an open log, because it may contain personal data from whoever wrote. Log what helps debugging: the body length, the prefix of the received header and the request identifier.

If verification fails on every request, the culprit is almost never an attacker. Common causes are a body re-serialized by a parser, the secret of another app (for example staging's secret in production), a secret that was rotated or copied with a trailing space, and the verify token used in place of the secret. If it fails only on some, look at non-ASCII characters in the body and at proxies that rewrite the payload.

  • Fails on 100% of events: body parsed first, or wrong secret.
  • Fails only on messages with accents or emoji: text re-serialization.
  • Fails after switching app or environment: another app's secret.
  • 500 error on a strange header: format not validated before Buffer.from.

Meta's signature and the Hub's signature are not the same thing

If your system receives events straight from Meta, this article is what applies. If it receives from CRPRO Hub, the signature that reaches you is the Hub's, with its own headers and algorithm. In the Hub's native format the HMAC covers the timestamp, the delivery id and the body, not just the body; it is described in CRPRO Hub webhooks and in the next article of this cluster. Mixing the two formulas is the most common cause of "verification fails on 100% of deliveries".

For authenticating the calls you make to the Hub itself, see authentication. The Hub offers a test event precisely so you can validate your verification before depending on a real message.

Frequently asked questions

Which secret do I use to validate Meta's X-Hub-Signature-256?

The app secret at Meta, as the HMAC-SHA256 key over the payload. It is not the verify token used in the GET verification.

Can I validate after parsing the JSON?

It is not safe. The documentation consulted does not spell out tolerance to re-serialization, so always use the raw bytes received.

Does the signature stop replays of a legitimate event?

No. It proves the origin of the body, but a valid body sent again stays valid. For that you need to deduplicate by event identifier.

Why does timingSafeEqual sometimes throw?

Because it requires buffers of equal length. Validate the 64-character hexadecimal format before converting, or a malformed header becomes an exception.

Fontes

Documentação oficial da Meta, conferida em 31 de outubro de 2026.