Skip to main content

HTTP Event Destinations

The HTTP adapter sends each event to your URL in a POST request. The body is one CloudEvent in JSON structured mode. Massdriver signs each request with the Standard Webhooks method.

For the events that a destination receives, retries, deliveries, and pause, refer to Event Destinations.

Settings​

ObjectFieldValue
configurlThe URL that receives the events. It must start with http:// or https:// and include a host. Required.
configheadersHeader names and values that Massdriver sends with each request. Do not put credentials here.
authheadersHeader names and values that hold credentials, such as Authorization. Massdriver sends them with each request.

When you save a destination, Massdriver refuses a URL whose host is a private address. Refer to Private addresses.

Send events to your computer​

To see the events before you build a real endpoint, run a small receiver on your computer and open a tunnel to it. The tunnel gives the receiver a public https:// URL that Massdriver can reach.

1. Open a tunnel​

Use one of these tools. Each one prints a public https:// URL that forwards to port 8080 on your computer.

Install ngrok and add your auth token. Then run:

ngrok http 8080

The Forwarding line shows the URL, such as https://<id>.ngrok-free.app. To see each request and the response of your receiver, open the ngrok inspector at http://localhost:4040.

These tools give a new URL each time that you start them, unless you have a fixed domain. When the URL changes, change the URL on the Settings tab of the destination.

2. Create the destination​

Create a destination as shown in Add a destination. Set the URL to the URL of the tunnel, and select the event type project.created.

Copy the signing secret from the Overview tab of the destination.

3. Run a receiver​

The receiver verifies the signature of each request, prints the event, and responds with 204. It refuses a request with a signature that is not correct with 401. Massdriver does not retry a 401. Both receivers listen on port 8080, and use only the standard library.

Python 3.9 or later. Save this as receiver.py:

import base64
import hashlib
import hmac
import json
import os
import time
from http.server import BaseHTTPRequestHandler, HTTPServer

SECRET = os.environ["MASSDRIVER_SIGNING_SECRET"]
TOLERANCE_SECONDS = 5 * 60


def verify(headers, body: bytes) -> bool:
msg_id = headers.get("webhook-id")
timestamp = headers.get("webhook-timestamp")
signatures = headers.get("webhook-signature")

if not (msg_id and timestamp and signatures and timestamp.isdigit()):
return False
if abs(time.time() - int(timestamp)) > TOLERANCE_SECONDS:
return False

key = base64.b64decode(SECRET.removeprefix("whsec_"))
signed = f"{msg_id}.{timestamp}.".encode() + body
expected = base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode()

for signature in signatures.split(" "):
version, _, value = signature.partition(",")
if version == "v1" and hmac.compare_digest(value, expected):
return True

return False


class Handler(BaseHTTPRequestHandler):
def do_POST(self):
body = self.rfile.read(int(self.headers.get("content-length", 0)))

if not verify(self.headers, body):
self.send_response(401)
self.end_headers()
return

event = json.loads(body)
print(event["id"], event["type"], event["subject"], flush=True)

self.send_response(204)
self.end_headers()


HTTPServer(("", 8080), Handler).serve_forever()
MASSDRIVER_SIGNING_SECRET='whsec_...' python3 receiver.py

4. Send an event​

To check the connection first, click Send test event on the destination. Your receiver prints an event_destination.tested event.

  1. Create a project. Massdriver records a project.created event.
  2. Your receiver prints the id, type, and subject of the event. On the Deliveries tab, the event is Delivered.

To see a retry, stop the receiver and create one more project. The tunnel cannot reach the receiver, so it responds with a 5xx status. On the Deliveries tab, the event is Waiting, and Last error shows the status. Start the receiver again. Massdriver sends the event at the next attempt. Refer to Retry schedule.

If your receiver responds with 401, the signing secret is not correct. Copy it again from the Overview tab.

The request​

Massdriver sends a POST request to the url in the destination's config. The body is one CloudEvent in JSON structured mode.

HeaderValue
content-typeapplication/cloudevents+json
webhook-idThe CloudEvent id. It is the same for every attempt of the event.
webhook-timestampThe time of this attempt, in seconds since the Unix epoch. It changes for each attempt.
webhook-signaturev1, followed by the base64 signature of the request. Refer to Verify the signature.

Custom headers​

Massdriver sends the headers in config.headers and auth.headers with each request. Massdriver changes header names to lowercase. If a header in auth.headers has the same name as a header in config.headers, Massdriver sends the value from auth.headers.

If a custom header has the same name as one of the headers in the table above, Massdriver sends its own value and ignores your value. Massdriver does not send a custom host header. The host header always names the host of the URL.

Verify the signature​

Each HTTP destination has its own signing secret. Get it from the Overview tab of the destination, or from the signingSecret field. The secret has the form whsec_ followed by base64.

The signature is the HMAC-SHA256 of <webhook-id>.<webhook-timestamp>.<body>. The key is the base64-decoded part of the secret after whsec_. The webhook-signature header holds v1, followed by the base64 of the signature.

To verify a request:

  1. Read the raw request body. Do not parse and encode the JSON again before you verify it.
  2. Make sure that webhook-timestamp is near your current time. The Standard Webhooks specification recommends a tolerance of 5 minutes. This stops replay of old requests.
  3. Calculate the signature and compare it to each v1 value in webhook-signature. Use a constant-time comparison.
  4. If no value is equal, refuse the request.

The verify functions in Run a receiver do these steps. The Standard Webhooks project also has libraries for many languages.

Responses​

Massdriver waits 10 seconds for a response. Massdriver does not follow redirects.

ResultWhat Massdriver does
2xxThe event is sent.
408, 429, 5xxMassdriver retries the event.
No response in 10 seconds, a connection that cannot be made, or the host does not resolveMassdriver retries the event.
Any other status, including 3xx redirects and other 4xxThe event becomes a failed event at once.
The host is a private addressThe event becomes a failed event at once. Refer to Private addresses.

The destination's lastError field shows the status, for example HTTP 503. Refer to Failures and retries.

Private addresses​

Massdriver refuses to send events to private addresses. Massdriver checks the host of the URL at two times:

  • When you save a destination, Massdriver refuses a URL whose host is a private address. Massdriver accepts a host that does not resolve yet.
  • Before each send, Massdriver resolves the host again. If one or more of its addresses is private, the event becomes a failed event at once. Massdriver sends the request to the address that it checked.

These addresses are private:

  • IPv4: 0.0.0.0/8, 10.0.0.0/8, 100.64.0.0/10, 127.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, 192.0.0.0/24, 192.168.0.0/16, 198.18.0.0/15, and all addresses from 224.0.0.0 up.
  • IPv6: fc00::/7, fe80::/10, ff00::/8, and Teredo (2001::/32).
  • IPv6 addresses that hold a private IPv4 address: IPv4-compatible (::/96, which includes :: and ::1), IPv4-mapped (::ffff:0:0/96), 6to4 (2002::/16), and NAT64 (64:ff9b::/96).

Thus a destination cannot use http://localhost:8080 directly. Use a tunnel, as shown in Send events to your computer. A self-hosted installation can allow private addresses with MD_EVENT_DESTINATIONS_ALLOW_PRIVATE_ADDRESSES.