Skip to content

n8n Verify Webhook HMAC Signature: Raw Body, Crypto Node, Code

How to verify a webhook HMAC signature in n8n: turn on Raw Body, hash the exact bytes with the Crypto node, compare in constant time in a Code node, and fix the usual mismatches.

By AyeWatch Team5 min read

Short answer: To verify a webhook HMAC signature in n8n, turn on the Webhook node's Raw Body option, compute an HMAC-SHA256 of the binary data property with the Crypto node (Action Hmac, Binary File on, Encoding HEX), then compare it with the signature header in a Code node using a constant-time check. Drop the item when they differ.

Why the raw body matters

An HMAC signature is computed over the exact bytes the sender POSTed. AyeWatch sends X-AyeWatch-Signature: sha256= followed by the hex HMAC-SHA256 of the raw request body, made with your signing secret.

n8n parses a JSON body into an object. If you turn that object back into a string, the spacing, key order or number format can differ from what was sent, and the HMAC will not match even with the right secret. With Raw Body on, the Webhook node still gives you the parsed JSON under body, and it also keeps the exact bytes as binary data in a property named data. Hash those bytes, never the re-serialized object.

Header names arrive in lowercase, so read headers['x-ayewatch-signature']. Setting up the Webhook node itself is in the n8n webhook setup guide.

Method 1: Crypto node plus a Code node

This keeps the secret in an n8n credential.

  1. Webhook: POST, Respond Immediately, Options, Raw Body on.
  2. Crypto: Action Hmac, Binary File on, Binary Property Name data, Type SHA256, Property Name signature, Encoding HEX. Create a Crypto credential and paste the signing secret from AyeWatch's webhook settings as its Hmac Secret.
  3. Code, "Check signature": the code below. It drops any item whose header does not match and passes on flat fields for the rest of the workflow.
// Code node "Check signature". Mode: Run Once for All Items.
// The Crypto node before it wrote the HMAC of the raw body to $json.signature.
function safeEqual(a, b) {
  if (a.length !== b.length) return false;
  let diff = 0;
  for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
  return diff === 0;
}

const out = [];
for (const item of $input.all()) {
  const sent = String(item.json.headers['x-ayewatch-signature'] || '');
  if (!safeEqual(sent, 'sha256=' + item.json.signature)) continue;
  const a = item.json.body || {};
  const evidence = Array.isArray(a.evidence) ? a.evidence : [];
  out.push({
    json: {
      topic_id: a.topic_id,
      topic_name: a.topic_name,
      update_id: a.update_id,
      headline: a.headline || a.topic_name,
      content: a.content || '',
      source_url: evidence.length ? evidence[0].source_url : '',
      created_at: a.created_at,
    },
  });
}
return out;

Why not an If node? An If node comparing the header with {{ 'sha256=' + $json.signature }} works, and the base guide uses it. A plain string comparison can stop at the first differing character, though, which in theory leaks timing. The Code node's safeEqual always walks the whole string, and it also flattens the payload so later nodes can use $json.headline instead of $json.body.headline.

Method 2: one Code node

If you would rather not use the Crypto node, Node's own crypto module can do the whole check:

// Code node "Verify in code". Mode: Run Once for All Items.
// n8n Cloud provides the crypto module. Self-hosted n8n needs
// NODE_FUNCTION_ALLOW_BUILTIN=crypto (set it on the task runners if you use them).
const crypto = require('crypto');
const SECRET = 'paste the AyeWatch signing secret here';

function safeEqual(a, b) {
  if (a.length !== b.length) return false;
  let diff = 0;
  for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
  return diff === 0;
}

const items = $input.all();
const out = [];
for (let i = 0; i < items.length; i++) {
  const raw = await this.helpers.getBinaryDataBuffer(i, 'data');
  const expected = 'sha256=' + crypto.createHmac('sha256', SECRET).update(raw).digest('hex');
  const sent = String(items[i].json.headers['x-ayewatch-signature'] || '');
  if (safeEqual(sent, expected)) out.push({ json: items[i].json.body });
}
return out;

The trade-off is the secret: here it sits in the workflow in plain text, visible to anyone who can open the workflow or its export. Method 1 keeps it in a credential. Method 2 also needs the crypto module, which n8n Cloud provides and self-hosted n8n allows through NODE_FUNCTION_ALLOW_BUILTIN.

What happens to a failed check

With Respond set to Immediately, AyeWatch already got its 2xx before the check ran, so a failed signature just ends the run quietly. That is right for forged requests. It is wrong when your own secret is out of date, because real alerts are thrown away.

To make a bad secret visible, set the Webhook node's Respond to Using 'Respond to Webhook' Node. Change the Code node to add valid: true or false instead of dropping the item, put an If node on valid, and end each branch with a Respond to Webhook node: Response Code 401 when invalid, No Data (a 200) when valid. AyeWatch retries any non-2xx answer except a redirect with exponential backoff, up to 10 attempts, so fixing the secret soon after lets later retries through. Respond within 10 seconds, and never answer with a redirect: a 3xx counts as a failed delivery and is not retried. The full delivery rules are in the webhook docs.

Fixing a signature that never matches

  • Raw Body is off. There is no binary data to hash. Turn it on under the Webhook node's Options.
  • Wrong URL. AyeWatch has the Test URL, or the workflow is not published. Publish and paste the Production URL.
  • Secret rotated. A new secret takes effect at once, with no overlap. Update the Crypto credential right after you rotate.
  • Extra characters. A space or line break pasted with the secret changes every HMAC. Copy it with the Copy button.
  • Prefix or encoding. Compare with sha256= plus the hex string. BASE64 encoding will never match.
  • Binary Property Name. It must be data unless you renamed the Webhook node's binary property.

What a signature won't stop. It proves the body was signed with your secret; it does not include a timestamp. Someone who captured a real request could send it again. The dedupe recipe drops a replayed update_id the same way it drops a retry.

Test it end to end

  1. Publish the workflow and paste the Production URL into AyeWatch's webhook settings.
  2. Copy the signing secret into the Crypto credential.
  3. Wait for a real alert, or run a monitor, and open the execution under Executions.
  4. Confirm the Crypto node shows a 64-character hex signature and the Code node outputs one item.
  5. Change one character of the secret in the credential and confirm the next alert is dropped, then change it back.
Crypto node + CodeCode node only
Where the secret livesn8n credentialPlain text in the workflow
Needs the crypto module in CodeNoYes
Constant-time compareYes, safeEqualYes, safeEqual
Nodes21

Start a monitor in under a minute

Describe a topic or paste a URL. Cancel before the trial ends and you pay nothing.

Try it free for 7 days

The bottom line

Raw Body on, hash the bytes, compare in constant time: that is the whole job. Put the check right after the Webhook node in every workflow, and everything after it can trust the payload.

FAQ

How do I verify an HMAC signature in n8n?

Turn on Raw Body in the Webhook node, hash the binary data property with the Crypto node's Hmac action using SHA256 and HEX, and compare the result with the signature header in a Code node.

Why does my n8n HMAC signature never match?

Usually because the hash was made from the parsed JSON instead of the raw bytes. Turn on Raw Body and hash the binary data property. Also check the secret for stray spaces.

Which header does AyeWatch sign with?

X-AyeWatch-Signature, with the value sha256= followed by the hex HMAC-SHA256 of the raw body. n8n shows header names in lowercase.

Can I use the crypto module in an n8n Code node?

n8n Cloud provides it. On self-hosted n8n, allow it with the NODE_FUNCTION_ALLOW_BUILTIN environment variable.

Should n8n return an error when the signature is wrong?

It can. A 401 from a Respond to Webhook node makes AyeWatch retry, which helps if your secret is out of date. Never answer with a redirect, because a 3xx is not retried.

Does an HMAC signature stop replayed requests?

No. It has no timestamp, so dedupe on update_id to drop a request you have already processed.

n8n verify webhook hmac signaturen8n webhook signaturen8n crypto node hmacn8n webhook raw bodyhmac sha256 webhook

Related articles