Skip to main content

Module webhooks

Module webhooks 

Source
Expand description

Webhooks: signatures for calls in both directions, and processing each event once.

A payment provider, a GPU service or a mail service calls the app back with a signed POST. The handler checks the signature over the raw body, then runs the effect inside once, which records the event in the webhook_events table (ocre g webhook creates it) and skips a delivery it has already processed: providers retry until they get a 2xx, so the same event often arrives twice.

use axum::{body::Bytes, extract::State, http::HeaderMap};
use ocre::{Ctx, Result, webhooks};


async fn receive(State(ctx): State<Ctx>, headers: HeaderMap, body: Bytes) -> Result<&'static str> {
    let secret = ctx.secret("PAYMENTS_WEBHOOK_SECRET").await?;
    let signature = headers.get("x-signature").and_then(|v| v.to_str().ok()).unwrap_or_default();
    webhooks::verify(secret.as_bytes(), &body, signature)?;
    let event: serde_json::Value =
        serde_json::from_slice(&body).map_err(|_| ocre::Error::bad_request("invalid JSON"))?;
    let id = event["id"].as_str().unwrap_or_default().to_owned();
    let db = ctx.db()?;
    webhooks::once(&db, "payments", &id, &body, || async {
        // ... mark the order paid: runs once per event id
        Ok(())
    })
    .await?;
    Ok("ok")
}

sign is the other direction: the app signs what it sends (a job submitted to an external service), and the service checks it the same way.

§Sending webhooks to your users

When users register their own endpoint URL (an API’s “notify me” setting), deliver POSTs each event as a Standard Webhooks delivery: JSON body, webhook-id, webhook-timestamp and webhook-signature headers signed with the endpoint’s secret (new_secret makes one), a 10-second timeout and no redirects. In release builds it first refuses URLs that are not http(s) or whose host is or resolves to a private, loopback or link-local address (check_public_url, also for validating the URL when it is saved), so a user cannot make the app call its own network (SSRF). Retries belong to a job: run deliver from one and let the queue retry it.

use ocre::{Ctx, Error, Result, webhooks};
use serde_json::json;

// src/jobs/deliver_webhook.rs: the queue retries it on an error.
async fn perform(ctx: &Ctx, endpoint_url: &str, secret: &str, event_id: &str) -> Result<()> {
    let event = json!({"type": "video.completed", "data": {"video_id": 7}});
    let response = webhooks::deliver(endpoint_url, secret, event_id, &event).await?;
    if !response.is_success() {
        return Err(Error::internal(format!("{endpoint_url} answered {}", response.status)));
    }
    Ok(())
}

Structs§

Answer
The answer of post_signed: status and body.

Enums§

Delivery
What once did with a delivery.

Constants§

DELIVERY_TIMEOUT
How long deliver waits for the endpoint’s answer: 10 seconds.
STALE_AFTER
A delivery left processing this long (seconds) is taken over by the next one: the invocation that claimed it died before finishing.
TABLE_SQL
The webhook_events table once records deliveries in, created by the migration of ocre g webhook.

Functions§

check_public_url
Checks that url may receive webhooks: http(s), no credentials, and a host that is not (and does not resolve to) a private address.
deliver
POSTs payload as JSON to the endpoint url, signed as a Standard Webhooks delivery, and returns the answer.
new_secret
A new Standard Webhooks secret for an endpoint: whsec_ and 32 random bytes in base64.
once
Runs effect for the event event_id of source unless it already ran.
post_signed
POSTs body as JSON to url, signed: the HMAC-SHA256 of the body with secret in X-Signature (lowercase hex, see sign), and Authorization: Bearer <bearer> when given (API keys of services such as RunPod). One subrequest. Any status is an answer: check status (a service’s 4xx is not an error of the call).
private_ip
Whether ip is an address a webhook must never reach: private, loopback, link-local, shared (CGNAT), unspecified, multicast, broadcast or reserved.
sign
HMAC-SHA256 of message with secret, as lowercase hex: the signature to send in a header (X-Signature) of an outgoing call.
sign_standard
The webhook-signature header value (v1,<base64>) of a Standard Webhooks delivery of body with this id and timestamp (Unix seconds): what verify_standard checks. For an app that sends Standard Webhooks, and for tests of one that receives them.
verify
Checks that signature is the HMAC-SHA256 of message with secret, in constant time. The signature may be hex (any case, optionally prefixed sha256= as GitHub sends it) or base64 (standard or URL-safe, padded or not), the common encodings of webhook providers. Sign the raw request body: parsed and re-serialized JSON may differ by a space.
verify_standard
Checks a Standard Webhooks delivery (Svix, Resend, and other providers): webhook-signature holds one or more space-separated v1,<base64> signatures of {id}.{timestamp}.{body} keyed with the base64 part of the whsec_... secret, and the webhook-timestamp must be within tolerance seconds of now (replays of an old delivery are refused). Returns the webhook-id, the event id to give once.