gunnar_core/webhook.rs
1//! Outbound webhook delivery signatures (the event stream, the outbound leg).
2//!
3//! A webhook is a subscriber to the event bus that POSTs each event to a
4//! configured URL. This module is the part that makes gunnar's webhook worth
5//! more than a plain POST: **every delivery is signed**, so the receiver can
6//! prove the body came from this gunnar and was not forged or replayed by
7//! anything that merely knows the URL.
8//!
9//! # Why `sshsig`, and why its own namespace
10//!
11//! The signature is an `sshsig` — the same `SSHSIG` format `ssh-keygen -Y sign`
12//! produces and `ssh-keygen -Y verify` checks — exactly as the build record
13//! ([`crate::provenance`]), the forge event ([`crate::forge`]) and the signed
14//! tree head ([`crate::transparency`]) are. gunnar signs everything this way,
15//! and a webhook is not a place to invent a second scheme (one implementation).
16//!
17//! The receiver verifies with the server's **published public key** and no
18//! shared secret: a leaked delivery URL is not a leaked signing key, and a
19//! receiver behind a CDN or a queue can still tell a real delivery from a
20//! replayed or forged one. That is the property an HMAC shared secret — the
21//! GitHub model — cannot give, because with HMAC the verifier holds the same
22//! secret the signer does and anything that can verify can also forge.
23//!
24//! The namespace is [`WEBHOOK_NAMESPACE`], distinct from `git`,
25//! `gunnar-provenance`, `gunnar-grpc` and `gunnar-transparency`. `sshsig` binds
26//! the namespace into what is signed, so a webhook delivery signature can never
27//! be lifted and presented as a commit signature, a build attestation or a
28//! control-plane credential, and none of those can be presented as a webhook
29//! delivery. A signature is scoped to the one job it was made for.
30//!
31//! # What is signed
32//!
33//! The **exact bytes of the request body** — the JSON the receiver reads off
34//! the socket, byte for byte. Not a re-serialisation the receiver has to
35//! reproduce (JSON has no canonical form, so signing the meaning rather than
36//! the bytes would make the signature depend on the receiver's serialiser), and
37//! not a digest the receiver has to trust the sender computed. The verifier
38//! hashes the body it actually received and checks the signature over that, so
39//! a single flipped byte anywhere in transit fails the check.
40
41use base64ct::Encoding as _;
42
43/// The `sshsig` namespace an outbound webhook delivery is signed under.
44///
45/// Distinct from every other namespace in this repository so a signature made
46/// for one job never verifies for another. See the module header.
47pub const WEBHOOK_NAMESPACE: &str = "gunnar-webhook";
48
49/// The HTTP header carrying the base64 of the `sshsig` PEM.
50///
51/// The PEM is `-----BEGIN SSH SIGNATURE-----…` and is multi-line, which an HTTP
52/// header value cannot be, so it travels base64-encoded and is decoded back to
53/// the PEM `ssh-keygen -Y verify` wants.
54pub const SIGNATURE_HEADER: &str = "x-gunnar-signature";
55/// The header naming the `sshsig` namespace, so a receiver can build the
56/// `ssh-keygen -Y verify -n <namespace>` invocation without hard-coding it.
57pub const NAMESPACE_HEADER: &str = "x-gunnar-signature-namespace";
58/// The header carrying `SHA256:…`, the fingerprint of the key that signed —
59/// which of the server's published keys to verify against.
60pub const KEY_FINGERPRINT_HEADER: &str = "x-gunnar-key-fingerprint";
61/// The header naming the event kind, e.g. `ref.updated`. Lets a receiver route
62/// or drop before parsing the body.
63pub const EVENT_HEADER: &str = "x-gunnar-event";
64/// The header carrying a per-delivery id (`<epoch>:<seq>`), so a receiver can
65/// deduplicate a redelivered event.
66pub const DELIVERY_HEADER: &str = "x-gunnar-delivery";
67/// The GitHub-compatible HMAC header, set only when an endpoint configures a
68/// shared secret. `sha256=<hex>` over the body. The `sshsig` above is the
69/// default and the one that does not require the receiver to hold a secret;
70/// this exists so a receiver written for GitHub's webhooks can be pointed at
71/// gunnar unchanged.
72pub const HMAC_HEADER: &str = "x-hub-signature-256";
73
74/// A signed delivery: the fingerprint of the signing key and the `sshsig`.
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct SignedDelivery {
77 /// `SHA256:…` of the key that signed. Goes in [`KEY_FINGERPRINT_HEADER`].
78 pub key_fingerprint: String,
79 /// The `sshsig`, PEM-armoured.
80 pub signature_pem: String,
81}
82
83impl SignedDelivery {
84 /// The base64 of the PEM, for [`SIGNATURE_HEADER`].
85 pub fn signature_header(&self) -> String {
86 base64ct::Base64::encode_string(self.signature_pem.as_bytes())
87 }
88}
89
90/// Sign a delivery body under [`WEBHOOK_NAMESPACE`].
91///
92/// `body` is the exact request body bytes. See the module header for why it is
93/// the bytes and not a re-rendering of their meaning.
94pub fn sign_delivery(
95 key: &ssh_key::PrivateKey,
96 body: &[u8],
97) -> Result<SignedDelivery, ssh_key::Error> {
98 let signature = key.sign(WEBHOOK_NAMESPACE, ssh_key::HashAlg::Sha512, body)?;
99 Ok(SignedDelivery {
100 key_fingerprint: key
101 .public_key()
102 .fingerprint(ssh_key::HashAlg::Sha256)
103 .to_string(),
104 signature_pem: signature.to_pem(ssh_key::LineEnding::LF)?,
105 })
106}
107
108/// What went wrong verifying a delivery. The variants a receiver distinguishes.
109#[derive(Debug, Clone, PartialEq, Eq)]
110pub enum DeliveryProblem {
111 /// The signature header was not the base64 of a well-formed `sshsig`.
112 Malformed(String),
113 /// The delivery names a key that is not the one offered to check it.
114 WrongKey {
115 /// The fingerprint the delivery claims.
116 signed_by: String,
117 /// The fingerprint of the key given to check it.
118 offered: String,
119 },
120 /// The signature was made under a different namespace — refused before the
121 /// verify, so a signature for another job is never reported as verifying.
122 WrongNamespace(String),
123 /// Well formed, right key, right namespace, and it does not check out —
124 /// the body was altered, or it was signed by a different key.
125 NotVerified,
126}
127
128impl std::fmt::Display for DeliveryProblem {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 match self {
131 DeliveryProblem::Malformed(why) => {
132 write!(f, "the delivery signature is not a well-formed sshsig: {why}")
133 }
134 DeliveryProblem::WrongKey { signed_by, offered } => write!(
135 f,
136 "the delivery says it was signed by {signed_by} and was checked against {offered}"
137 ),
138 DeliveryProblem::WrongNamespace(ns) => write!(
139 f,
140 "the delivery signature's namespace is {ns:?}, not {WEBHOOK_NAMESPACE:?}"
141 ),
142 DeliveryProblem::NotVerified => {
143 write!(f, "the delivery signature does not verify against the body")
144 }
145 }
146 }
147}
148
149/// Verify a signed delivery against a public key.
150///
151/// `signature_header` is the value of [`SIGNATURE_HEADER`] as received (the
152/// base64 of the PEM); `claimed_fingerprint` is [`KEY_FINGERPRINT_HEADER`];
153/// `body` is the exact bytes the receiver read. Mirrors
154/// [`crate::provenance::verify_build`]: fingerprint before signature, namespace
155/// before verify, so a signature by another key or for another purpose is never
156/// reported as this one verifying.
157///
158/// This is the Rust twin of the `ssh-keygen -Y verify` a shell receiver runs;
159/// the format is standard `SSHSIG`, so the two agree. The step-by-step guide
160/// gives the `ssh-keygen` recipe.
161pub fn verify_delivery(
162 key: &ssh_key::PublicKey,
163 body: &[u8],
164 signature_header: &str,
165 claimed_fingerprint: &str,
166) -> Result<(), DeliveryProblem> {
167 let offered = key.fingerprint(ssh_key::HashAlg::Sha256).to_string();
168 if offered != claimed_fingerprint {
169 return Err(DeliveryProblem::WrongKey {
170 signed_by: claimed_fingerprint.to_owned(),
171 offered,
172 });
173 }
174 let pem = base64ct::Base64::decode_vec(signature_header.trim())
175 .map_err(|err| DeliveryProblem::Malformed(format!("header is not base64: {err}")))?;
176 let signature = ssh_key::SshSig::from_pem(&pem)
177 .map_err(|err| DeliveryProblem::Malformed(err.to_string()))?;
178 if signature.namespace() != WEBHOOK_NAMESPACE {
179 return Err(DeliveryProblem::WrongNamespace(
180 signature.namespace().to_owned(),
181 ));
182 }
183 key.verify(WEBHOOK_NAMESPACE, body, &signature)
184 .map_err(|_| DeliveryProblem::NotVerified)
185}
186
187/// The GitHub-compatible `X-Hub-Signature-256` value for `body` under `secret`:
188/// `sha256=<hex>` of `HMAC-SHA256(secret, body)`.
189///
190/// Optional and off by default. It is here for a receiver already written for
191/// GitHub; the `sshsig` above is the one gunnar leads with, because it does not
192/// make the verifier hold a secret it could also forge with.
193pub fn hmac_sha256_header(secret: &[u8], body: &[u8]) -> String {
194 format!("sha256={}", crate::transparency::hex(&hmac_sha256(secret, body)))
195}
196
197/// HMAC-SHA256, computed here rather than with a new crate: `sha2` is already a
198/// dependency (the crate budget), and HMAC over a fixed hash is the RFC 2104
199/// construction and nothing more.
200fn hmac_sha256(key: &[u8], message: &[u8]) -> [u8; 32] {
201 use sha2::Digest as _;
202 const BLOCK: usize = 64; // SHA-256's block size in bytes.
203 let mut block = [0u8; BLOCK];
204 if key.len() > BLOCK {
205 let digest: [u8; 32] = sha2::Sha256::digest(key).into();
206 block[..32].copy_from_slice(&digest);
207 } else {
208 block[..key.len()].copy_from_slice(key);
209 }
210 let mut ipad = [0x36u8; BLOCK];
211 let mut opad = [0x5cu8; BLOCK];
212 for i in 0..BLOCK {
213 ipad[i] ^= block[i];
214 opad[i] ^= block[i];
215 }
216 let mut inner = sha2::Sha256::new();
217 inner.update(ipad);
218 inner.update(message);
219 let inner: [u8; 32] = inner.finalize().into();
220 let mut outer = sha2::Sha256::new();
221 outer.update(opad);
222 outer.update(inner);
223 outer.finalize().into()
224}
225
226#[cfg(test)]
227mod tests {
228 use super::*;
229
230 fn key() -> ssh_key::PrivateKey {
231 ssh_key::PrivateKey::from_openssh(crate::mint_ed25519().expect("mint")).expect("parse")
232 }
233
234 #[test]
235 fn a_signed_delivery_verifies_against_the_signing_key() {
236 let key = key();
237 let body = br#"{"kind":"ref.updated","seq":7}"#;
238 let signed = sign_delivery(&key, body).expect("sign");
239 // The applied output: the exact received bytes plus a PASSING verify.
240 verify_delivery(
241 key.public_key(),
242 body,
243 &signed.signature_header(),
244 &signed.key_fingerprint,
245 )
246 .expect("a delivery signed by this key must verify against it");
247 }
248
249 #[test]
250 fn a_delivery_signed_by_another_key_is_refused() {
251 // The red first: a body signed with the WRONG key must FAIL. Without
252 // the namespace-and-fingerprint discipline this is exactly the check
253 // that silently passes.
254 let signer = key();
255 let attacker = key();
256 let body = br#"{"kind":"ref.updated","seq":7}"#;
257 let signed = sign_delivery(&attacker, body).expect("sign");
258 // Presented as if it came from the server's key.
259 let err = verify_delivery(
260 signer.public_key(),
261 body,
262 &signed.signature_header(),
263 &signer.public_key().fingerprint(ssh_key::HashAlg::Sha256).to_string(),
264 )
265 .expect_err("a body signed by another key must not verify");
266 assert_eq!(err, DeliveryProblem::NotVerified);
267 }
268
269 #[test]
270 fn a_tampered_body_is_refused() {
271 let key = key();
272 let body = br#"{"kind":"ref.updated","seq":7}"#;
273 let signed = sign_delivery(&key, body).expect("sign");
274 let tampered = br#"{"kind":"ref.updated","seq":8}"#;
275 let err = verify_delivery(
276 key.public_key(),
277 tampered,
278 &signed.signature_header(),
279 &signed.key_fingerprint,
280 )
281 .expect_err("a single flipped byte in the body must fail the verify");
282 assert_eq!(err, DeliveryProblem::NotVerified);
283 }
284
285 #[test]
286 fn a_signature_from_another_namespace_is_refused() {
287 // A provenance signature (a different namespace) must never pass as a
288 // webhook delivery. Sign the same bytes under the provenance namespace
289 // and present it as a delivery.
290 let key = key();
291 let body = br#"{"kind":"ref.updated","seq":7}"#;
292 let other = key
293 .sign(crate::provenance::PROVENANCE_NAMESPACE, ssh_key::HashAlg::Sha512, body)
294 .expect("sign under provenance");
295 let header = base64ct::Base64::encode_string(
296 other.to_pem(ssh_key::LineEnding::LF).expect("pem").as_bytes(),
297 );
298 let fp = key.public_key().fingerprint(ssh_key::HashAlg::Sha256).to_string();
299 let err = verify_delivery(key.public_key(), body, &header, &fp)
300 .expect_err("a signature under another namespace must be refused");
301 assert_eq!(err, DeliveryProblem::WrongNamespace(crate::provenance::PROVENANCE_NAMESPACE.to_owned()));
302 }
303
304 #[test]
305 fn hmac_matches_the_rfc_construction() {
306 // A known GitHub-doc vector shape: HMAC-SHA256 is deterministic, so a
307 // stable secret/body gives a stable digest. This guards the hand-rolled
308 // HMAC against a block-padding bug.
309 let mac = hmac_sha256_header(b"It's a Secret to Everybody", b"Hello, World!");
310 assert_eq!(
311 mac,
312 "sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17"
313 );
314 }
315}