Skip to main content

Check the webhook signatures

Verify the events that HopDrive sends to your webhook endpoints.

HopDrive will sign the webhook events it sends to your endpoints by including a signature in each event’s HopDrive-Signature header. This allows you to verify that the events were sent by HopDrive, not by a third party. You can verify signatures using your own solution by following the procedure outlined below.

Before you can verify signatures, you need to retrieve your endpoint’s secret for your webhook settings used when the webhook was originally created. HopDrive signs each webhook it sends to the endpoint using this secret.

Preventing replay attacks

A replay attack is when an attacker intercepts a valid payload and its signature, then re-transmits them. To mitigate such attacks, HopDrive includes a timestamp in the HopDrive-Signature header. Because this timestamp is part of the signed payload, it is also verified by the signature, so an attacker cannot change the timestamp without invalidating the signature. If the signature is valid but the timestamp is too old, you can have your application reject the payload.

We recommend a default tolerance of five minutes between the timestamp and the current time but you can of course use any tolerance you prefer when verifying signatures of your received events. Use Network Time Protocol (NTP) to ensure that your server’s clock is accurate and synchronizes with the time on HopDrive’s servers.

HopDrive generates the timestamp and signature each time we send an event to your endpoint. If HopDrive retries an event (for example, your endpoint previously replied with a non-2xx status code), then we generate a new signature and timestamp for the new delivery attempt.

Verifying signatures manually

The HopDrive-Signature header included in each signed event contains a timestamp and one or more signatures. The timestamp is prefixed by t=, and each signature is prefixed by a scheme. Schemes start with v, followed by an integer. Currently, the only valid live signature scheme is v1.

HopDrive Signature Header Example
HopDrive-Signature:
t=1492774577,
v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
tip

Note that newlines have been added for clarity, but a real HopDrive-Signature header is on a single line.

HopDrive generates signatures using a hash-based message authentication code (HMAC) with SHA-256. To prevent downgrade attacks, you should ignore all schemes that are not v1.

You can create a custom solution by following these steps.

Step 1: Extract the timestamp and signatures from the header

Split the header, using the , character as the separator, to get a list of elements. Then split each element, using the = character as the separator, to get a prefix and value pair.

The value for the prefix t corresponds to the timestamp, and v1 corresponds to the signature. You can discard all other elements.

Step 2: Prepare the signed_payload string

The signed_payload string is created by concatenating:

  • The timestamp (as a string)
  • The character .
  • The actual JSON payload (that is, the request body)

Step 3: Determine the expected signature

Compute an HMAC with the SHA256 hash function. Use the endpoint’s signing secret as the key, and use the signed_payload string as the message.

Example Signature Calculation
const t = new Date().getTime();
const signed_payload = `${t}.${payload}`;
const hash = createHmac('sha256', secret).update(signed_payload).digest('hex');
const signature = `t=${t},v1=${hash}`;

Step 4: Compare the signatures

Compare the signature in the header to the expected signature. For an equality match, compute the difference between the current timestamp and the received timestamp, then decide if the difference is within your tolerance.

To protect against timing attacks, use a constant-time string comparison to compare the expected signature to each of the received signatures.