Skip to main content

Module webhook

Module webhook 

Source
Available on crate feature webhook only.
Expand description

Webhook signature verification for common providers, gated behind the webhook Cargo feature (hmac + sha2 — the same RustCrypto crates already used by crate::auth and crate::crypto).

Every webhook-receiving handler needs to verify that a request actually came from the provider, not an attacker who guessed (or found leaked) the endpoint URL. All three schemes below are HMAC-SHA256-based, but the header format, signed-payload construction, and encoding differ per provider — verify_webhook_signature and its per-provider siblings encode the exact convention each one uses, so handlers don’t have to re-derive it from provider docs.

§Example

use rust_web_server::webhook::{verify_webhook_signature, WebhookProvider};

fn handle_github_webhook(body: &[u8], signature_header: &str, secret: &[u8]) -> bool {
    verify_webhook_signature(WebhookProvider::GitHub, body, secret, signature_header)
}

§Which header to read

ProviderHeaderFormat
GitHubX-Hub-Signature-256sha256=<hex>
ShopifyX-Shopify-Hmac-Sha256<base64>
StripeStripe-Signaturet=<unix_ts>,v1=<hex>[,v1=<hex>...]

Enums§

WebhookProvider
Which provider’s signature convention to verify a webhook request against.

Constants§

STRIPE_DEFAULT_TOLERANCE_SECS
Default timestamp tolerance for verify_stripe_signature — 300 seconds (5 minutes), matching Stripe’s own recommended default. Requests signed further in the past (or future, guarding against clock skew abuse) than this are rejected as a replay-protection measure.

Functions§

verify_github_signature
Verifies a GitHub webhook’s X-Hub-Signature-256 header value — "sha256=<hex-encoded-hmac-sha256-of-body>", keyed by the webhook’s configured secret.
verify_shopify_signature
Verifies a Shopify webhook’s X-Shopify-Hmac-Sha256 header value — the standard base64 encoding of an HMAC-SHA256 digest of the raw body, keyed by the app’s configured secret.
verify_stripe_signature
Verifies a Stripe webhook’s Stripe-Signature header using the default STRIPE_DEFAULT_TOLERANCE_SECS timestamp tolerance. See verify_stripe_signature_with_tolerance for the full format and to customize the tolerance window.
verify_stripe_signature_with_tolerance
Verifies a Stripe webhook’s Stripe-Signature header value: "t=<unix_timestamp>,v1=<hex_hmac_sha256>[,v1=<hex_hmac_sha256>...]".
verify_webhook_signature
Verifies header_value against body and secret using provider’s signature convention. Returns false on any failure: wrong secret, tampered body, malformed header, or (Stripe only) a timestamp outside the default tolerance window.