Skip to content
Back to Blog
Search
Security• 15 Apr 2026• 2 min read

Verifying Shopee webhooks before using the JSON body

Shopee signs the callback URL and request body with HMAC-SHA256. If the key is right but the signature fails, check which bytes reached the verifier.

1. Reproducing it in five lines

One parse-and-serialize round trip is enough to change those bytes:

const raw = '{"code":1,"amount":10.50,"shop_id":123}'; // the body Shopee actually signed
const reparsed = JSON.stringify(JSON.parse(raw));         // what you get after express.json()

console.log(raw === reparsed);  // false
console.log(raw);               // {"code":1,"amount":10.50,"shop_id":123}
console.log(reparsed);          // {"code":1,"amount":10.5,"shop_id":123}  <- 10.50 → 10.5

The JSON numbers 10.50 and 10.5 are equal. Their serialized bytes differ, which changes the HMAC input.

2. Why this slips through review

A global express.json() can consume the body before the webhook route sees it. Serializing req.body will not recover the original bytes.

Verification just returns false, with nothing in the logs. It looks exactly like a wrong key, so most of the debugging goes into rechecking partnerKey, the one thing that was never wrong.

3. Verify the raw bytes, then parse

Register the webhook route with express.raw() before the global JSON parser. Verify the buffer, then parse it:

app.post(
  "/shopee/webhook",
  express.raw({ type: "application/json" }),   // keep the bytes, do NOT parse
  (req, res) => {
    const signature = req.header("authorization") ?? "";
    const isValid = shopee.verifyPushSignature(
      callbackUrl,
      req.body,      // raw Buffer - exactly the bytes Shopee signed
      signature,
    );
    if (!isValid) return res.status(401).end();

    const payload = shopee.parsePushPayload(req.body); // parse AFTER the signature checks out
    return res.status(204).end();
  },
);

4. NestJS and other frameworks

Any framework that parses JSON before your handler sees the raw bytes has the same problem. NestJS registers a global body parser at startup; enable rawBody to keep the original bytes:

const app = await NestFactory.create(AppModule, { rawBody: true });

// In the controller:
@Post("shopee/webhook")
handleWebhook(@Req() req: RawBodyRequest<Request>) {
  const isValid = shopee.verifyPushSignature(
    callbackUrl,
    req.rawBody,   // the original buffer - kept alongside the already-parsed req.body
    signature,
  );
  if (!isValid) throw new UnauthorizedException();

  const payload = req.body; // already parsed, safe to use AFTER verifying
}

Verify against req.rawBody, then use req.body. Next.js Route Handlers avoid the problem entirely, because you read request.text() yourself.