use std::fmt;
use std::future::{ready, Ready};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use base64::Engine as _;
use futures_util::future::{Either, MapErr};
use futures_util::TryFutureExt as _;
use hmac::{KeyInit, Mac};
use http::header::HeaderName;
use http::{HeaderMap, HeaderValue};
use tower::{Layer, Service};
use crate::{Body, BoxError};
#[cfg(not(target_family = "wasm"))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(target_family = "wasm")]
use web_time::{SystemTime, UNIX_EPOCH};
type HmacSha256 = hmac::Hmac<sha2::Sha256>;
pub const WEBHOOK_ID: HeaderName = HeaderName::from_static("webhook-id");
pub const WEBHOOK_TIMESTAMP: HeaderName = HeaderName::from_static("webhook-timestamp");
pub const WEBHOOK_SIGNATURE: HeaderName = HeaderName::from_static("webhook-signature");
const PREFIX: &str = "whsec_";
#[derive(Clone, PartialEq, Eq)]
pub struct WebhookSecret(Arc<[u8]>);
impl WebhookSecret {
pub fn new(secret: &str) -> Result<Self, WebhookError> {
let encoded = secret.trim();
let encoded = encoded.strip_prefix(PREFIX).unwrap_or(encoded);
let key = base64::engine::general_purpose::STANDARD
.decode(encoded)
.map_err(|_| WebhookError::InvalidSecret)?;
if key.is_empty() {
return Err(WebhookError::InvalidSecret);
}
Ok(Self(key.into()))
}
pub fn from_bytes(key: impl Into<Vec<u8>>) -> Self {
Self(key.into().into())
}
fn sign(&self, id: &str, timestamp: u64, body: &[u8]) -> String {
let mac = self.mac(id, timestamp, body);
format!(
"v1,{}",
base64::engine::general_purpose::STANDARD.encode(mac.finalize().into_bytes())
)
}
fn mac(&self, id: &str, timestamp: u64, body: &[u8]) -> HmacSha256 {
let mut mac = HmacSha256::new_from_slice(&self.0).expect("HMAC takes a key of any length");
mac.update(id.as_bytes());
mac.update(b".");
mac.update(timestamp.to_string().as_bytes());
mac.update(b".");
mac.update(body);
mac
}
}
impl fmt::Debug for WebhookSecret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("WebhookSecret([redacted])")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum WebhookError {
InvalidSecret,
MissingHeader(HeaderName),
InvalidTimestamp,
TimestampOutOfTolerance,
NoMatchingSignature,
StreamingBody,
}
impl fmt::Display for WebhookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WebhookError::InvalidSecret => {
f.write_str("the webhook secret is not whsec_ followed by base64")
}
WebhookError::MissingHeader(name) => write!(f, "the webhook has no {name} header"),
WebhookError::InvalidTimestamp => {
f.write_str("the webhook-timestamp is not a number of seconds")
}
WebhookError::TimestampOutOfTolerance => {
f.write_str("the webhook-timestamp is too far from now")
}
WebhookError::NoMatchingSignature => f.write_str("no webhook signature matches"),
WebhookError::StreamingBody => {
f.write_str("a webhook whose body streams cannot be signed")
}
}
}
}
impl std::error::Error for WebhookError {}
#[derive(Clone, Debug)]
pub struct StandardWebhooksLayer {
secrets: Arc<[WebhookSecret]>,
}
impl StandardWebhooksLayer {
pub fn new(secret: WebhookSecret) -> Self {
Self {
secrets: Arc::from(vec![secret]),
}
}
pub fn with_secrets(secrets: impl IntoIterator<Item = WebhookSecret>) -> Self {
Self {
secrets: secrets.into_iter().collect::<Vec<_>>().into(),
}
}
}
impl<S> Layer<S> for StandardWebhooksLayer {
type Service = StandardWebhooks<S>;
fn layer(&self, inner: S) -> Self::Service {
StandardWebhooks {
inner,
secrets: self.secrets.clone(),
}
}
}
#[derive(Clone, Debug)]
pub struct StandardWebhooks<S> {
inner: S,
secrets: Arc<[WebhookSecret]>,
}
impl<S> Service<http::Request<Body>> for StandardWebhooks<S>
where
S: Service<http::Request<Body>>,
S::Error: Into<BoxError>,
{
type Response = S::Response;
type Error = BoxError;
#[allow(clippy::type_complexity)]
type Future =
Either<Ready<Result<S::Response, BoxError>>, MapErr<S::Future, fn(S::Error) -> BoxError>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx).map_err(Into::into)
}
fn call(&mut self, mut request: http::Request<Body>) -> Self::Future {
if let Err(error) = sign(&mut request, &self.secrets, unix_now()) {
return Either::Left(ready(Err(Box::new(error))));
}
Either::Right(
self.inner
.call(request)
.map_err(into_box_error::<S::Error> as fn(_) -> _),
)
}
}
fn into_box_error<E: Into<BoxError>>(error: E) -> BoxError {
error.into()
}
fn sign(
request: &mut http::Request<Body>,
secrets: &[WebhookSecret],
now: u64,
) -> Result<(), WebhookError> {
let body = request
.body()
.as_bytes()
.ok_or(WebhookError::StreamingBody)?
.to_vec();
let headers = request.headers_mut();
let id = match text(headers, &WEBHOOK_ID) {
Some(id) => id.to_owned(),
None => {
let id = new_id();
headers.insert(
WEBHOOK_ID,
HeaderValue::from_str(&id).expect("an id of ASCII letters and digits"),
);
id
}
};
let timestamp = match text(headers, &WEBHOOK_TIMESTAMP) {
Some(timestamp) => timestamp
.parse()
.map_err(|_| WebhookError::InvalidTimestamp)?,
None => {
headers.insert(WEBHOOK_TIMESTAMP, HeaderValue::from(now));
now
}
};
let signatures: Vec<String> = secrets
.iter()
.map(|secret| secret.sign(&id, timestamp, &body))
.collect();
headers.insert(
WEBHOOK_SIGNATURE,
HeaderValue::from_str(&signatures.join(" ")).expect("signatures of ASCII base64"),
);
Ok(())
}
pub fn verify(
headers: &HeaderMap,
body: &[u8],
secrets: &[WebhookSecret],
tolerance: Duration,
) -> Result<(), WebhookError> {
verify_at(headers, body, secrets, tolerance, unix_now())
}
fn verify_at(
headers: &HeaderMap,
body: &[u8],
secrets: &[WebhookSecret],
tolerance: Duration,
now: u64,
) -> Result<(), WebhookError> {
let id = text(headers, &WEBHOOK_ID).ok_or(WebhookError::MissingHeader(WEBHOOK_ID))?;
let timestamp: u64 = text(headers, &WEBHOOK_TIMESTAMP)
.ok_or(WebhookError::MissingHeader(WEBHOOK_TIMESTAMP))?
.parse()
.map_err(|_| WebhookError::InvalidTimestamp)?;
let signatures =
text(headers, &WEBHOOK_SIGNATURE).ok_or(WebhookError::MissingHeader(WEBHOOK_SIGNATURE))?;
if now.abs_diff(timestamp) > tolerance.as_secs() {
return Err(WebhookError::TimestampOutOfTolerance);
}
for signature in signatures.split(' ') {
let Some(encoded) = signature.strip_prefix("v1,") else {
continue;
};
let Ok(signature) = base64::engine::general_purpose::STANDARD.decode(encoded) else {
continue;
};
if secrets.iter().any(|secret| {
secret
.mac(id, timestamp, body)
.verify_slice(&signature)
.is_ok()
}) {
return Ok(());
}
}
Err(WebhookError::NoMatchingSignature)
}
fn text<'a>(headers: &'a HeaderMap, name: &HeaderName) -> Option<&'a str> {
headers.get(name)?.to_str().ok()
}
fn unix_now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| since.as_secs())
}
fn new_id() -> String {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| since.as_nanos() as u64);
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
let seed = {
use std::hash::{BuildHasher, Hasher};
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
hasher.write_u64(nanos ^ count);
hasher.finish()
};
format!("msg_{:016x}{:016x}", nanos ^ count.rotate_left(48), seed)
}
#[cfg(all(test, not(target_family = "wasm")))]
mod tests {
use super::*;
use tower::ServiceExt;
const SECRET: &str = concat!("whsec", "_", "MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw");
const ID: &str = "msg_p5jXN8AQM9LWM0D4loKWxJek";
const TIMESTAMP: u64 = 1614265330;
const BODY: &str = r#"{"test": 2432232314}"#;
const SIGNATURE: &str = "v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=";
fn secret() -> WebhookSecret {
WebhookSecret::new(SECRET).unwrap()
}
fn headers(id: &str, timestamp: u64, signature: &str) -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert(WEBHOOK_ID, HeaderValue::from_str(id).unwrap());
headers.insert(WEBHOOK_TIMESTAMP, HeaderValue::from(timestamp));
headers.insert(WEBHOOK_SIGNATURE, HeaderValue::from_str(signature).unwrap());
headers
}
#[test]
fn the_signature_is_standard_webhooks_v1() {
assert_eq!(secret().sign(ID, TIMESTAMP, BODY.as_bytes()), SIGNATURE);
}
#[test]
fn a_signed_webhook_verifies_within_the_tolerance() {
let tolerance = Duration::from_secs(300);
let headers = headers(ID, TIMESTAMP, SIGNATURE);
assert_eq!(
verify_at(
&headers,
BODY.as_bytes(),
&[secret()],
tolerance,
TIMESTAMP + 300
),
Ok(())
);
assert_eq!(
verify_at(
&headers,
BODY.as_bytes(),
&[secret()],
tolerance,
TIMESTAMP + 301
),
Err(WebhookError::TimestampOutOfTolerance)
);
assert_eq!(
verify_at(
&headers,
BODY.as_bytes(),
&[secret()],
tolerance,
TIMESTAMP - 301
),
Err(WebhookError::TimestampOutOfTolerance)
);
}
#[test]
fn a_changed_body_id_or_secret_does_not_verify() {
let tolerance = Duration::from_secs(300);
let check = |headers: &HeaderMap, body: &str, secrets: &[WebhookSecret]| {
verify_at(headers, body.as_bytes(), secrets, tolerance, TIMESTAMP)
};
let signed = headers(ID, TIMESTAMP, SIGNATURE);
assert_eq!(
check(&signed, r#"{"test": 2432232315}"#, &[secret()]),
Err(WebhookError::NoMatchingSignature)
);
assert_eq!(
check(
&headers("msg_other", TIMESTAMP, SIGNATURE),
BODY,
&[secret()]
),
Err(WebhookError::NoMatchingSignature)
);
let other = WebhookSecret::from_bytes(b"another key".to_vec());
assert_eq!(
check(&signed, BODY, std::slice::from_ref(&other)),
Err(WebhookError::NoMatchingSignature)
);
assert_eq!(check(&signed, BODY, &[other, secret()]), Ok(()));
let two = headers(ID, TIMESTAMP, &format!("v1,bm90IGl0 v2a,xyz {SIGNATURE}"));
assert_eq!(check(&two, BODY, &[secret()]), Ok(()));
}
#[test]
fn missing_or_invalid_headers_are_named() {
let tolerance = Duration::from_secs(300);
let mut headers = headers(ID, TIMESTAMP, SIGNATURE);
headers.insert(WEBHOOK_TIMESTAMP, HeaderValue::from_static("soon"));
assert_eq!(
verify_at(&headers, BODY.as_bytes(), &[secret()], tolerance, TIMESTAMP),
Err(WebhookError::InvalidTimestamp)
);
headers.insert(WEBHOOK_TIMESTAMP, HeaderValue::from(TIMESTAMP));
headers.remove(WEBHOOK_SIGNATURE);
assert_eq!(
verify_at(&headers, BODY.as_bytes(), &[secret()], tolerance, TIMESTAMP),
Err(WebhookError::MissingHeader(WEBHOOK_SIGNATURE))
);
headers.remove(WEBHOOK_ID);
assert_eq!(
verify_at(&headers, BODY.as_bytes(), &[secret()], tolerance, TIMESTAMP),
Err(WebhookError::MissingHeader(WEBHOOK_ID))
);
}
#[test]
fn a_secret_is_whsec_and_base64_and_does_not_show() {
assert_eq!(
WebhookSecret::new("whsec_not base64!"),
Err(WebhookError::InvalidSecret)
);
assert_eq!(
WebhookSecret::new("whsec_"),
Err(WebhookError::InvalidSecret)
);
assert_eq!(
WebhookSecret::new("MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw").unwrap(),
secret()
);
assert_eq!(format!("{:?}", secret()), "WebhookSecret([redacted])");
}
#[tokio::test]
async fn the_layer_signs_what_it_sends_and_keeps_a_given_id() {
let echo =
tower::service_fn(
|request: http::Request<Body>| async move { Ok::<_, BoxError>(request) },
);
let layer = StandardWebhooksLayer::new(secret());
let mut request = http::Request::new(Body::from(BODY));
request
.headers_mut()
.insert(WEBHOOK_ID, HeaderValue::from_static(ID));
let sent = layer.layer(echo).oneshot(request).await.unwrap();
assert_eq!(sent.headers()[WEBHOOK_ID], ID);
let timestamp: u64 = sent.headers()[WEBHOOK_TIMESTAMP]
.to_str()
.unwrap()
.parse()
.unwrap();
assert!(timestamp.abs_diff(unix_now()) <= 1);
verify(
sent.headers(),
BODY.as_bytes(),
&[secret()],
Duration::from_secs(5),
)
.unwrap();
}
#[tokio::test]
async fn the_layer_gives_each_message_its_own_id_and_signs_with_every_secret() {
let echo =
tower::service_fn(
|request: http::Request<Body>| async move { Ok::<_, BoxError>(request) },
);
let next = WebhookSecret::from_bytes(b"the next key".to_vec());
let service = StandardWebhooksLayer::with_secrets([secret(), next.clone()]).layer(echo);
let first = service
.clone()
.oneshot(http::Request::new(Body::from(BODY)))
.await
.unwrap();
let second = service
.oneshot(http::Request::new(Body::from(BODY)))
.await
.unwrap();
let id = first.headers()[WEBHOOK_ID].to_str().unwrap();
assert!(id.starts_with("msg_") && id.len() == 36, "{id}");
assert_ne!(first.headers()[WEBHOOK_ID], second.headers()[WEBHOOK_ID]);
let signatures = first.headers()[WEBHOOK_SIGNATURE].to_str().unwrap();
assert_eq!(signatures.split(' ').count(), 2);
verify(
first.headers(),
BODY.as_bytes(),
&[next],
Duration::from_secs(5),
)
.unwrap();
}
#[tokio::test]
async fn a_body_that_streams_is_not_sent_unsigned() {
let never = tower::service_fn(|_: http::Request<Body>| async move {
panic!("a request that cannot be signed is not sent");
#[allow(unreachable_code)]
Ok::<(), BoxError>(())
});
let stream =
futures_util::stream::iter([Ok::<_, std::io::Error>(bytes::Bytes::from_static(b"{}"))]);
let request = http::Request::new(Body::wrap_stream(stream));
let error = StandardWebhooksLayer::new(secret())
.layer(never)
.oneshot(request)
.await
.unwrap_err();
assert_eq!(
error.downcast_ref::<WebhookError>(),
Some(&WebhookError::StreamingBody)
);
}
}