~/bend-docscommunity

webhooks.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-webhooks@0.15.0.1/webhooks.bend as Webhooks

Webhook signatures: Standard Webhooks sign and verify, Stripe and GitHub verify, over the raw body. Source: https://github.com/paymog/bend-kit/tree/main/webhooks

11 imports
import Base
import bend-kit-crypto@0.2.2.2/crypto.bend as Crypto
import bend-kit-time@0.3.0.3/pure.bend as Time
import bend-kit-int@0.2.0.0/int.bend as Int
import bend-kit-encoding@0.5.0.1/encoding.bend as Enc
import bend-kit-bytes@0.3.3.1/bytes.bend as Bytes
import bend-kit-hairpin@0.17.0.1/hairpin.bend as Hairpin
import bend-kit-retry@0.3.0.0/retry.bend as Retry
import bend-kit-http@0.44.0.2/http.bend as Http
import bend-kit-http@0.44.0.2/pure.bend as HttpPure
import ./pure.bend as Pure

Types

type Err source · line 19 · raw

Data

type SendErr source · line 367 · raw

Data

Definitions

def err.from.pure source · line 29 · raw

@e:0xd90ba86811933d769d3773d91f15e17c/pure.Err -> Err

def result.from.pure source · line 48 · raw

@-A:Type -> @r:Result<&1, &1, 0xd90ba86811933d769d3773d91f15e17c/pure.Err, A> -> Result<&1, &1, Err, A>

def field.head source · line 55 · raw

@xs:List<&2, String> -> Maybe<&2, String>

def field.of source · line 58 · raw

@r:Pair(Map<&2, List<&2, String>>, List<&2, String>) -> Maybe<&2, String>

def field source · line 61 · raw

@+h:Map<&2, List<&2, String>> -> @k:String -> Maybe<&2, String>

def need source · line 64 · raw

@+name:String -> @m:Maybe<&2, String> -> Result<&1, &1, Err, String>

def strip source · line 67 · raw

@+p:String -> @+s:String -> Maybe<&2, String>

def push source · line 70 · raw

@m:Maybe<&2, String> -> @rest:List<&2, String> -> List<&2, String>

def put source · line 73 · raw

@m:Map<&2, List<&2, String>> -> @k:String -> @v:String -> Map<&2, List<&2, String>>

def digits source · line 76 · raw

@s:String -> Bool

def ts.some source · line 79 · raw

@ok:Bool -> @m:Maybe<&2, 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64> -> Result<&1, &1, Err, 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64>

def ts.read source · line 82 · raw

@+s:String -> Result<&1, &1, Err, 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64>

def window.pick source · line 85 · raw

@old:Bool -> @ahead:Bool -> Result<&1, &1, Err, Unit>

def window source · line 88 · raw

@+tol:U32 -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @t:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> Result<&1, &1, Err, Unit>

def id.check source · line 91 · raw

@+id:String -> Result<&1, &1, Err, Unit>

def b64.same source · line 96 · raw

@+s:String -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def b64.canon source · line 100 · raw

@+s:String -> @m:Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def b64 source · line 108 · raw

@+s:String -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

Padded standard base64, only in its canonical text: Bytes.from_base64 alone accepts nonzero pad bits.

def pad source · line 111 · raw

@+s:String -> String

def nonempty.of source · line 114 · raw

@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def nonempty source · line 118 · raw

@m:Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def key.std source · line 126 · raw

@+s:String -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

A Standard Webhooks secret: whsec_ then base64 of the key, padded or not. The prefix is optional.

def key source · line 130 · raw

@+std:Bool -> @+s:String -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

std: a Standard Webhooks secret. Otherwise the secret's UTF-8 is the key, as Stripe and GitHub use it.

def decode source · line 138 · raw

@+hex:Bool -> @+s:String -> Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

A signature's octets: hex (Stripe, GitHub) or canonical base64 (Standard Webhooks).

def mac.of source · line 145 · raw

@r:Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)> -> Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def mac source · line 152 · raw

@k:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @data:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>)

def ct.go source · line 159 · raw

@m:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @s:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Bool)

def ct source · line 165 · raw

@s:Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @m:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Bool)

A signature that does not decode matches nothing.

def any.sig source · line 173 · raw

@sigs:List<&2, String> -> @+hex:Bool -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Bool)

Does any signature equal the MAC? r holds the MAC and a copy; every signature is compared.

def key.cmp source · line 184 · raw

@r:Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @+hex:Bool -> @+sigs:List<&2, String> -> IO(Result<&1, &1, Err, Bool>)

def key.check source · line 193 · raw

@k:Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @+hex:Bool -> @+sigs:List<&2, String> -> @msg:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Result<&1, &1, Err, Bool>)

def both.done source · line 202 · raw

@+x:Bool -> @b:Result<&1, &1, Err, Bool> -> Result<&1, &1, Err, Bool>

def both source · line 210 · raw

@a:Result<&1, &1, Err, Bool> -> @b:Result<&1, &1, Err, Bool> -> Result<&1, &1, Err, Bool>

A bad secret fails the check, even when another secret matches.

def any.key source · line 218 · raw

@keys:List<&2, String> -> @+std:Bool -> @+hex:Bool -> @+sigs:List<&2, String> -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Result<&1, &1, Err, Bool>)

Does any signature sign the message under any secret? r holds the message and a copy.

def found source · line 230 · raw

@-A:Data -> @r:Result<&1, &1, Err, Bool> -> @v:A -> Result<&1, &1, Err, A>

def back source · line 238 · raw

@-A:Data -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> @e:Err -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, A>))

The body back beside a failure.

def run.mac source · line 242 · raw

@-A:Data -> @v:A -> @pre:String -> @+sigs:List<&2, String> -> @+std:Bool -> @+hex:Bool -> @secrets:List<&2, String> -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, A>))

def run source · line 251 · raw

@-A:Data -> @c:Result<&1, &1, Err, Pair(A, Pair(String, List<&2, String>))> -> @+std:Bool -> @+hex:Bool -> @secrets:List<&2, String> -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, A>))

c: the header checks' answer, the signed prefix before the body, and the candidate signatures.

def std.sigs source · line 261 · raw

@xs:List<&2, String> -> List<&2, String>

def std.check source · line 264 · raw

@+tol:U32 -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @+h:Map<&2, List<&2, String>> -> Result<&1, &1, Err, Pair(String, Pair(String, List<&2, String>))>

def verify.with source · line 269 · raw

@+tol:U32 -> @secrets:List<&2, String> -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, String>))

Done with the webhook-id when a v1 signature in webhook-signature signs id.timestamp.body under a secret, and webhook-timestamp is within tol seconds of now.

def verify source · line 274 · raw

@secrets:List<&2, String> -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, String>))

verify.with a 300 s tolerance.

def sign.check source · line 278 · raw

@+id:String -> @+ts:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> Result<&1, &1, Err, Pair(String, String)>

def sign.headers source · line 281 · raw

@+id:String -> @+ts:String -> @m:Result<&1, &1, Err, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> Result<&1, &1, Err, Map<&2, List<&2, String>>>

def sign.mac source · line 288 · raw

@k:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+id:String -> @+ts:String -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Map<&2, List<&2, String>>>))

def sign.key source · line 295 · raw

@k:Maybe<&1, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @+id:String -> @+ts:String -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Map<&2, List<&2, String>>>))

def sign.go source · line 303 · raw

@c:Result<&1, &1, Err, Pair(String, String)> -> @+secret:String -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Map<&2, List<&2, String>>>))

def sign source · line 313 · raw

@+id:String -> @+ts:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+secret:String -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Map<&2, List<&2, String>>>))

The webhook-id, webhook-timestamp, and webhook-signature ("v1,<base64 MAC>") headers for body. id: not empty, no '.'. ts: unix seconds, not negative. secret: whsec_<base64>; send the prefix.

def stripe.t source · line 319 · raw

@s:String -> @r:Pair(List<&2, String>, List<&2, String>) -> Pair(List<&2, String>, List<&2, String>)

def stripe.v.push source · line 322 · raw

@s:String -> @r:Pair(List<&2, String>, List<&2, String>) -> Pair(List<&2, String>, List<&2, String>)

def stripe.v source · line 325 · raw

@m:Maybe<&2, String> -> @r:Pair(List<&2, String>, List<&2, String>) -> Pair(List<&2, String>, List<&2, String>)

def stripe.item source · line 328 · raw

@t:Maybe<&2, String> -> @v:Maybe<&2, String> -> @r:Pair(List<&2, String>, List<&2, String>) -> Pair(List<&2, String>, List<&2, String>)

def stripe.items source · line 331 · raw

@xs:List<&2, String> -> Pair(List<&2, String>, List<&2, String>)

def stripe.single source · line 334 · raw

@ts:List<&2, String> -> @vs:List<&2, String> -> Result<&1, &1, Err, Pair(String, List<&2, String>)>

def stripe.one source · line 337 · raw

@r:Pair(List<&2, String>, List<&2, String>) -> Result<&1, &1, Err, Pair(String, List<&2, String>)>

def stripe.when source · line 340 · raw

@+tol:U32 -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @p:Pair(String, List<&2, String>) -> Result<&1, &1, Err, Pair(Unit, Pair(String, List<&2, String>))>

def stripe.check source · line 343 · raw

@+tol:U32 -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @+h:Map<&2, List<&2, String>> -> Result<&1, &1, Err, Pair(Unit, Pair(String, List<&2, String>))>

def stripe.verify.with source · line 348 · raw

@+tol:U32 -> @secrets:List<&2, String> -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Unit>))

Done when a v1 hex signature in stripe-signature signs t.body under a secret (the whsec_ text itself, as UTF-8), and t is within tol seconds of now.

def stripe.verify source · line 353 · raw

@secrets:List<&2, String> -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Unit>))

stripe.verify.with a 300 s tolerance, Stripe's default.

def github.check source · line 359 · raw

@+h:Map<&2, List<&2, String>> -> Result<&1, &1, Err, Pair(Unit, Pair(String, List<&2, String>))>

def github.verify source · line 363 · raw

@secrets:List<&2, String> -> @+headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Unit>))

Done when x-hub-signature-256 is sha256=<hex> of the body under a secret's UTF-8. GitHub signs no timestamp.

def send.result source · line 371 · raw

@x:Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, 0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Err, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)) -> Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>))

def send.again source · line 381 · raw

@r:Result<&1, &1, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Err, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res> -> Pair(Result<&1, &1, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Err, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>, Maybe<&2, String>)

A timeout, a refused connect, 408, 429, or any 5xx may clear.

def send.judge source · line 391 · raw

@r:Result<&1, &1, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Err, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res> -> Pair(Result<&1, &1, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Err, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>, 0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Verdict)

def send.signed source · line 395 · raw

@c:0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client -> @b:0x14e48621ef66156b0eb652524b8594d4/retry.Budget -> @+url:String -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Result<&1, &1, Err, Map<&2, List<&2, String>>>) -> IO(Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)))

Receivers deduplicate on the event ID, so the POST is safe to repeat.

def send.at source · line 409 · raw

@c:0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client -> @b:0x14e48621ef66156b0eb652524b8594d4/retry.Budget -> @url:String -> @id:String -> @secret:String -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @now:0xf6004ed2773adf12acc8863c4d424242/pure.Instant -> IO(Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)))

def send.in source · line 420 · raw

@c:0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client -> @b:0x14e48621ef66156b0eb652524b8594d4/retry.Budget -> @url:String -> @id:String -> @secret:String -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)))

One event and one signature per delivery, inside a caller's retry layer. Every attempt takes from b and reuses this event ID, timestamp, headers, and raw body; the client's retry setting caps this delivery. The budget left comes back: pass it to the next delivery of the same operation.

def send.drop source · line 426 · raw

@x:Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)) -> Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)

def send.budget source · line 430 · raw

@url:String -> @id:String -> @secret:String -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @y:Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, 0x14e48621ef66156b0eb652524b8594d4/retry.Budget) -> IO(Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Pair(0x14e48621ef66156b0eb652524b8594d4/retry.Budget, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>)))

def send.with source · line 436 · raw

@+n:U32 -> @c:0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client -> @url:String -> @id:String -> @secret:String -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>))

n extra attempts, on a budget of n + 1 and the client's deadline, with Hairpin's backoff. The client comes back with retry n.

def send source · line 444 · raw

@c:0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client -> @url:String -> @id:String -> @secret:String -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xdcae937f8a0cbc4f84e3afa443553fde/hairpin.Client, Result<&1, &1, SendErr, 0xaba1cd8e58dfba7b7b1fa48aa885aa3b/pure.Res>))

Three extra attempts by default.