diff --git a/docs/api/openapi.json b/docs/api/openapi.json index 0e70e391e..9810e75ae 100644 --- a/docs/api/openapi.json +++ b/docs/api/openapi.json @@ -4973,7 +4973,7 @@ }, "/webhooks/relay": { "post": { - "description": "On a hybrid self-hosted install, where CoderHelm's hosted relay forwards the events of CoderHelm's GitHub App (and Jira app) after checking the provider's own signature. Each request is signed with the install's relay key: `X-CoderHelm-Relay-Signature: t=,d=,v1=,v2=`, where `v1` is the HMAC-SHA256 of `..` and `v2` of `v2.....` (so the two headers below are signed too). Requests older than 5 minutes are refused, and so is a delivery id the install has already taken (`409`). `X-CoderHelm-Relay-Source` is `github` or `jira`; for GitHub, `X-CoderHelm-Relay-Event` is GitHub's event type and the body is GitHub's payload, unchanged.", + "description": "On a hybrid self-hosted install, where CoderHelm's hosted relay forwards the events of CoderHelm's GitHub App (and Jira app) after checking the provider's own signature. Each request is signed with the install's relay key: `X-CoderHelm-Relay-Signature: t=,d=,v1=,v2=`, where `v1` is the HMAC-SHA256 of `..` and `v2` of `v2.....` (so the two headers below are signed too). Both must match: a request without `v2` is refused. Requests older than 5 minutes are refused, and so is a delivery id the install has already taken (`409`). `X-CoderHelm-Relay-Source` is `github` or `jira`; for GitHub, `X-CoderHelm-Relay-Event` is GitHub's event type and the body is GitHub's payload, unchanged.", "operationId": "receiveRelayedEvent", "parameters": [ { diff --git a/docs/self-host/hybrid.md b/docs/self-host/hybrid.md index 10246cb40..56e9697c9 100644 --- a/docs/self-host/hybrid.md +++ b/docs/self-host/hybrid.md @@ -35,7 +35,7 @@ How the pieces are protected: - **Registration.** Your install registers once and gets an id, a secret and a relay key. Both keys are derived from a key on CoderHelm's side, so CoderHelm stores no copy of them; your install keeps them in its own Secrets Manager secret. - **Proof of ownership.** Before a name or an app is linked, CoderHelm asks your install's CloudFront address to answer a challenge with its secret. Names only ever point at a verified install's own CloudFront distribution. -- **Signed both ways.** Every call from your install is signed (`X-CoderHelm-Signature`), and every relayed event is signed with your relay key (`X-CoderHelm-Relay-Signature: t=,d=,v1=,v2=`; `v2` also covers the source and event headers). Your install refuses anything older than 5 minutes and any delivery id it has already taken, and refuses an event it couldn't record as taken. +- **Signed both ways.** Every call from your install is signed (`X-CoderHelm-Signature`), and every relayed event is signed with your relay key (`X-CoderHelm-Relay-Signature: t=,d=,v1=,v2=`; `v2` also covers the source and event headers, and an event without it is refused). Your install refuses anything older than 5 minutes and any delivery id it has already taken, and refuses an event it couldn't record as taken. - **Linking proves both sides.** Linking CoderHelm's GitHub App needs your install (its setup key in the guide, or an install admin in Settings), a GitHub sign-in that shows the installation is yours, and a press of **Link** on a CoderHelm page that names your install's address. A GitHub redirect or a link someone sends you can't link anything by itself. - **Scoped tokens.** The broker mints tokens only for installations linked to your install, narrowed to the repositories you chose and, within those, to the repositories the person who linked it can push to on GitHub. GitHub itself enforces both: they are part of the token request. - **Sign-ins are yours only once your install takes them.** A Notion, Confluence or Google Drive sign-in is held apart until your install redeems it with its secret and its PKCE verifier, so nobody else can put their account in place of yours. diff --git a/services/common/src/hybrid/sign.rs b/services/common/src/hybrid/sign.rs index 259bfe6bc..3c634c1cb 100644 --- a/services/common/src/hybrid/sign.rs +++ b/services/common/src/hybrid/sign.rs @@ -241,9 +241,9 @@ pub struct RelayStamp { pub ts: i64, } -/// Checks a relayed event's signature and age. With `v2` present, the -/// source and event headers must be the ones that were signed (a `v1` alone -/// is accepted from an older hosted side). Replays are the caller's second +/// Checks a relayed event's signature and age. Both `v1` and `v2` must +/// match, so the source and event headers are the ones that were signed. +/// Replays are the caller's second /// step: claim [`replay_key`] once, refuse when it is already held. pub fn verify_relay( key: &str, @@ -291,10 +291,12 @@ pub fn verify_relay( if !ct_eq(&got, &want) { return Err(SignatureProblem::Mismatch); } - if let Some(got2) = v2 { - if !ct_eq(&got2, &routed_mac(key, ts, &d, source, event, body)) { - return Err(SignatureProblem::Mismatch); - } + // The route (source and event) must be signed: a v1 alone is refused. + let Some(got2) = v2 else { + return Err(SignatureProblem::Malformed); + }; + if !ct_eq(&got2, &routed_mac(key, ts, &d, source, event, body)) { + return Err(SignatureProblem::Mismatch); } Ok(RelayStamp { delivery_id: d, ts }) } @@ -462,7 +464,14 @@ mod tests { fn a_relayed_event_verifies_with_its_delivery_id() { let k = relay_key(MASTER, "ins_a"); let body = br#"{"action":"opened"}"#; - let h = sign_relay(&k, NOW, "72d3162e-cc78-11e3-81ab-4c9367dc0958", body); + let h = sign_relay_routed( + &k, + NOW, + "72d3162e-cc78-11e3-81ab-4c9367dc0958", + "github", + "push", + body, + ); let stamp = verify_relay(&k, Some(&h), "github", "push", body, NOW + 5).unwrap(); assert_eq!(stamp.delivery_id, "72d3162e-cc78-11e3-81ab-4c9367dc0958"); assert_eq!(stamp.ts, NOW); @@ -476,7 +485,7 @@ mod tests { fn a_relayed_event_is_refused_when_changed_old_or_from_another_key() { let k = relay_key(MASTER, "ins_a"); let body = b"{}"; - let h = sign_relay(&k, NOW, "d1", body); + let h = sign_relay_routed(&k, NOW, "d1", "github", "push", body); assert_eq!( verify_relay(&k, Some(&h), "github", "push", b"{ }", NOW), Err(SignatureProblem::Mismatch) @@ -529,8 +538,8 @@ mod tests { } /// The source and event headers decide how the install handles a body, - /// so they are signed too (`v2`). A copy with them changed is refused; - /// an older hosted side that sends only `v1` still verifies. + /// so they are signed too (`v2`). A copy with them changed, or with no + /// `v2` at all, is refused. #[test] fn the_route_of_a_relayed_event_is_signed() { let k = relay_key(MASTER, "ins_a"); @@ -558,9 +567,12 @@ mod tests { verify_relay(&k, Some(&forged), "github", "pull_request", body, NOW), Err(SignatureProblem::Mismatch) ); - // An older hosted side (v1 only) still verifies. + // A v1 alone is refused: the route must be signed. let old = sign_relay(&k, NOW, "d1", body); - assert!(verify_relay(&k, Some(&old), "github", "push", body, NOW).is_ok()); + assert_eq!( + verify_relay(&k, Some(&old), "github", "push", body, NOW), + Err(SignatureProblem::Malformed) + ); } #[test] diff --git a/services/gateway/src/routes/hybrid_install.rs b/services/gateway/src/routes/hybrid_install.rs index 79624b691..9ad437857 100644 --- a/services/gateway/src/routes/hybrid_install.rs +++ b/services/gateway/src/routes/hybrid_install.rs @@ -1077,7 +1077,8 @@ fn note_intake_problem(p: &'static str) { request is signed with the install's relay key: `X-CoderHelm-Relay-Signature: t=,d=,v1=,v2=`, where `v1` is the HMAC-SHA256 of \ `..` and `v2` of `v2.....` (so the two \ - headers below are signed too). Requests older than 5 minutes are refused, and so is a \ + headers below are signed too). Both must match: a request without `v2` is refused. \ + Requests older than 5 minutes are refused, and so is a \ delivery id the install has already taken (`409`). \ `X-CoderHelm-Relay-Source` is `github` or `jira`; for GitHub, `X-CoderHelm-Relay-Event` is \ GitHub's event type and the body is GitHub's payload, unchanged.",