Skip to content

Bug: verifyWebhookSignature re-serializes payload instead of using raw body bytes #313

Closed

Description

@EmmaLouise2018

Summary

SingleAgentClient.verifyWebhookSignature() computes the expected HMAC over JSON.stringify(payload) (a re-serialized parsed object) instead of the raw HTTP body bytes. This is the same class of bug as adcontextprotocol/adcp-client-python#151 on the Python signing side.

The Problem

// SingleAgentClient.js line 653
const message = `${ts}.${JSON.stringify(payload)}`;

payload here is the parsed JavaScript object from req.body. JSON.stringify re-serializes it, which may not produce the same bytes that were on the wire. For example:

  • Sender (Python) sends: {"key": "value", "num": 1.0}
  • JSON.stringify(JSON.parse(...)) produces: {"key":"value","num":1}
  • Key ordering, spacing, number formatting, and unicode escaping all vary by language

The HMAC will only match if JSON.stringify happens to produce byte-identical output to the sender's serializer. This is fragile and breaks cross-language interop.

The Correct Approach

HMAC verification must always use the raw HTTP body bytes — the exact bytes received on the wire, before JSON parsing. This is how GitHub, Stripe, and Slack all implement webhook HMAC verification.

The AdCP spec itself says: HMAC-SHA256(secret, rawBody) — not HMAC-SHA256(secret, JSON.stringify(parsed)).

Current Workaround

We bypass this method entirely by passing enforceWebhookSignature=false and implementing our own raw-body verification:

// Our workaround in adcp-webhook.ts
const message = `${ts}.${rawBody}`;  // rawBody = actual wire bytes
const expected = `sha256=${createHmac('sha256', secret).update(message).digest('hex')}`;

Suggested Fix

Change verifyWebhookSignature to accept raw body bytes instead of a parsed object:

// BEFORE (broken):
verifyWebhookSignature(payload: object, signature: string, timestamp: string | number): boolean {
    const message = `${ts}.${JSON.stringify(payload)}`;
    // ...
}

// AFTER (correct):
verifyWebhookSignature(rawBody: string, signature: string, timestamp: string | number): boolean {
    const message = `${ts}.${rawBody}`;
    // ...
}

This is a breaking API change, so it could be offered alongside the old method during a deprecation period:

verifyWebhookSignatureRaw(rawBody: string, signature: string, timestamp: string | number): boolean

Related

Activity

  1. EmmaLouise2018 commented on Mar 11, 2026

    @EmmaLouise2018
    ContributorAuthor

    The spec (now on main) explicitly requires raw bytes:

    The rawBody must be the exact bytes sent on the wire. Implementations must sign the same serialization that goes in the HTTP body — signing a re-serialized or reformatted version of the payload will cause verification failures.

    See: https://docs.adcontextprotocol.org/docs/building/implementation/webhooks#hmac-sha256-recommended-for-production

    Raw bytes is the only correct approach because the raw HTTP body is the one string both sender and receiver are guaranteed to have in common. The sender created it, the receiver received it. The moment either side re-serializes a parsed object, you lose that guarantee — JSON.stringify() in JS and json.dumps() in Python produce different output for the same logical data (key ordering, whitespace, number formatting like 1.0 vs 1, unicode escaping). This is how GitHub, Stripe, and Slack all implement webhook HMAC verification.

    verifyWebhookSignature currently takes a parsed object and calls JSON.stringify(payload) on line 653 of SingleAgentClient.js. This needs to accept rawBody: string instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions