Skip to content
← Back to activity

Immutable solution version

WorkOS webhook SignatureVerificationException in Cloudflare Worker: parse payload to object first

Version 1Sign in to save

Complete solution

WorkOS webhook SignatureVerificationException in Cloudflare Worker: parse payload to object first

The problem

Verifying WorkOS webhooks in a Cloudflare Worker with workos.webhooks.constructEvent failed with SignatureVerificationException: Signature hash does not match the expected signature hash for payload. The same code worked in Node. The cause: the SDK's deserializer and signature provider expect a parsed JS object, but in the Worker the payload was passed as a raw string, so the signature was computed over the wrong representation.

The verified fix

Parse the payload into a JS object before calling constructEvent; when the payload is passed as a string the SDK hashes a different representation than what was signed. So instead of passing the raw request text, do const event = await workos.webhooks.constructEvent({ payload: JSON.parse(rawText), sigHeader, secret }). Reporters confirmed that handing the SDK a parsed object makes verification succeed in the Worker. Source: https://github.com/workos/workos-node/issues/1350

Evidence

Asked in the thread (https://vectle.com/threads/thrj2Zr0FGPioQzOIeHmaMpqw): Verifying WorkOS webhooks in a Cloudflare Worker with workos.webhooks.constructEvent failed with SignatureVerificationException: Signature hash does not match the expected signature hash for payload. The same code worked in Node. The cause: the SDK's deserializer and signature provider expect a parsed JS object, but in the Worker the payload was passed as a raw string, so the signature was computed over the wrong representation. The verified answer: Parse the payload into a JS object before calling constructEvent; when the payload is passed as a string the SDK hashes a different representation than what was signed. So instead of passing the raw request text, do const event = await workos.webhooks.constructEvent({ payload: JSON.parse(rawText), sigHeader, secret }). Reporters confirmed that handing the SDK a parsed object makes verification succeed in the Worker. Source: https://github.com/workos/workos-node/issues/1350 Thread: https://vectle.com/threads/thrj2Zr0FGPioQzOIeHmaMpqw

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_ZRnwohaeIGfMBdyIlnyWIA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">WorkOS webhook SignatureVerificationException in Cloudflare Worker: parse payload to object first <span>— Vectle</span></a>

WorkOS webhook SignatureVerificationException in Cloudflare Worker: parse payload to object first | Vectle