use std::{
error::Error,
fmt,
future::Future,
marker::PhantomData,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use ::bytes::Bytes;
use ::http::{Request, Response, StatusCode};
use ::http_body_util::BodyExt;
use ::tower_layer::Layer;
use ::tower_service::Service;
use crate::core::adapter_utils::rejection_status;
use crate::{Provider, Secret, VerifyError, VerifyOptions, providers::signature_header_names};
pub type BoxError = Box<dyn Error + Send + Sync>;
#[derive(Clone)]
struct Config {
provider: Provider,
secret: Arc<Secret>,
options: Arc<VerifyOptions>,
}
impl fmt::Debug for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Config")
.field("provider", &self.provider)
.field("secret", &self.secret)
.field("options", &self.options)
.finish()
}
}
#[must_use]
#[derive(Clone, Debug)]
pub struct VerifyLayer<B = Bytes> {
config: Config,
_body: PhantomData<B>,
}
impl<B> VerifyLayer<B> {
pub fn new(provider: Provider, secret: Secret) -> Self {
Self::with_options(provider, secret, VerifyOptions::default())
}
pub fn with_options(provider: Provider, secret: Secret, options: VerifyOptions) -> Self {
Self {
config: Config {
provider,
secret: Arc::new(secret),
options: Arc::new(options),
},
_body: PhantomData,
}
}
}
impl<S, B> Layer<S> for VerifyLayer<B> {
type Service = VerifyMiddleware<S, B>;
fn layer(&self, inner: S) -> Self::Service {
VerifyMiddleware {
inner,
config: self.config.clone(),
_body: PhantomData,
}
}
}
#[must_use]
pub struct VerifyMiddleware<S, B = Bytes> {
inner: S,
config: Config,
_body: PhantomData<B>,
}
impl<S: Clone, B> Clone for VerifyMiddleware<S, B> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
config: self.config.clone(),
_body: PhantomData,
}
}
}
impl<S: fmt::Debug, B> fmt::Debug for VerifyMiddleware<S, B> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VerifyMiddleware")
.field("inner", &self.inner)
.field("config", &self.config)
.finish()
}
}
fn conflicting_signature_header(
headers: &::http::HeaderMap,
names: &[&'static str],
) -> Option<&'static str> {
names.iter().copied().find(|name| {
let Ok(key) = ::http::header::HeaderName::from_bytes(name.as_bytes()) else {
return true;
};
let mut values = headers.get_all(&key).iter();
let Some(first) = values.next() else {
return false;
};
values.any(|value| value != first)
})
}
fn rejection_response<ResB: Default>(error: &VerifyError) -> Response<ResB> {
let mut response = Response::new(ResB::default());
*response.status_mut() =
StatusCode::from_u16(rejection_status(error)).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
response
}
impl<S, ReqB, ResB, OutB> Service<Request<ReqB>> for VerifyMiddleware<S, OutB>
where
S: Service<Request<OutB>, Response = Response<ResB>> + Clone + Send + 'static,
S::Error: Into<BoxError>,
S::Future: Send + 'static,
ReqB: ::http_body::Body<Data = Bytes> + Send + 'static,
ReqB::Error: Into<BoxError>,
ResB: Default + Send + 'static,
OutB: From<Bytes> + Send + 'static,
{
type Response = Response<ResB>;
type Error = BoxError;
type Future = Pin<Box<dyn Future<Output = Result<Response<ResB>, BoxError>> + Send>>;
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, req: Request<ReqB>) -> Self::Future {
let names = signature_header_names(&self.config.provider);
if let Some(header) = conflicting_signature_header(req.headers(), &names) {
let response = rejection_response::<ResB>(&VerifyError::MalformedHeader {
header,
reason: "header present multiple times with different values",
});
return Box::pin(async { Ok(response) });
}
let mut inner = self.inner.clone();
let config = self.config.clone();
Box::pin(async move {
let (parts, body) = req.into_parts();
let raw_body = match BodyExt::collect(body).await {
Ok(collected) => collected.to_bytes(),
Err(error) => return Err(error.into()),
};
if let Err(error) = crate::verify(
config.provider,
&parts.headers,
raw_body.as_ref(),
&config.secret,
(*config.options).clone(),
) {
return Ok(rejection_response::<ResB>(&error));
}
let request = Request::from_parts(parts, OutB::from(raw_body));
inner.call(request).await.map_err(Into::into)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "paypal")]
use crate::VerifyingKeyMaterial;
use crate::test_helpers::{FixedClock, clocked_at, epoch};
use ::http_body_util::Full;
use ::tower::ServiceExt;
use futures_executor::block_on;
const GITHUB_SECRET: &str = "It's a Secret to Everybody";
const GITHUB_BODY: &[u8] = b"Hello, World!";
const GITHUB_SIGNATURE: &str =
"sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17";
const SLACK_SECRET: &str = "8f742231b10e8888abcd99yyyzzz85a5";
const SLACK_TIMESTAMP: u64 = 1_531_420_618;
const SLACK_BODY: &[u8] =
b"token=xyzz0WbapA4vBCDEFasx0q6G&team_id=T1DC2JH3J&team_domain=testteamnow&channel_id=G8PSS9T3V&channel_name=foobar&user_id=U2CERLKJA&user_name=roadrunner&command=%2Fwebhook-collect&text=&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT1DC2JH3J%2F397700885554%2F96rGlfmibIGlgcZRskXaIFfN&trigger_id=398738663015.47445629121.803a0bc887a14d10d2c447fce8b6703c";
const SLACK_SIGNATURE: &str =
"a2114d57b48eac39b9ad189dd8316235a7b4a8d21a10bd27519666489c69b503";
type TestBody = Full<Bytes>;
#[derive(Clone)]
struct EchoLen;
impl Service<Request<Bytes>> for EchoLen {
type Response = Response<TestBody>;
type Error = BoxError;
type Future = std::future::Ready<Result<Response<TestBody>, BoxError>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), BoxError>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<Bytes>) -> Self::Future {
let len = req.body().len();
std::future::ready(Ok(Response::new(TestBody::new(Bytes::from(
len.to_string(),
)))))
}
}
fn github_service() -> VerifyMiddleware<EchoLen, Bytes> {
VerifyLayer::new(Provider::GitHub, Secret::new(GITHUB_SECRET)).layer(EchoLen)
}
fn github_request(body: &'static [u8]) -> Request<TestBody> {
Request::builder()
.header("X-Hub-Signature-256", GITHUB_SIGNATURE)
.body(TestBody::new(Bytes::from_static(body)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"))
}
#[test]
fn valid_signature_reaches_inner_service_with_exact_bytes() {
block_on(async {
let svc = github_service();
let response = svc
.oneshot(github_request(GITHUB_BODY))
.await
.unwrap_or_else(|error| panic!("verification should pass: {error}"));
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_body().into_inner().unwrap_or_default(),
Bytes::from_static(b"13")
);
});
}
#[test]
fn empty_and_unicode_bodies_round_trip_byte_exactly() {
const EMPTY_SIG: &str =
"sha256=66a0c074deaa0f489ead6537e0d32f9a344b90bbeda705b6ed45ecd3b413fb40";
const UNICODE_BODY: &[u8] = "héllo, 🦀 world!".as_bytes();
const UNICODE_SIG: &str =
"sha256=815772f88bf8950c7457b57856f4b33ca9d07e7ef7a50646b067b4a613f735c4";
for (body, signature) in [
(&b""[..], EMPTY_SIG),
(UNICODE_BODY, UNICODE_SIG),
(GITHUB_BODY, GITHUB_SIGNATURE),
] {
let request = Request::builder()
.header("X-Hub-Signature-256", signature)
.body(TestBody::new(Bytes::copy_from_slice(body)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
let svc = github_service();
block_on(async {
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_body().into_inner().unwrap_or_default(),
Bytes::from(body.len().to_string())
);
});
}
}
#[test]
fn tampered_body_is_unauthorized_and_never_reaches_handler() {
let request = github_request(b"Hello, World?");
block_on(async {
let svc = github_service();
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("middleware should respond, not error: {error}"));
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
});
}
#[test]
fn missing_signature_header_is_bad_request() {
let request = Request::builder()
.body(TestBody::new(Bytes::from_static(GITHUB_BODY)))
.unwrap_or_else(|_| unreachable!("no headers to misbuild"));
block_on(async {
let svc = github_service();
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
});
}
#[test]
fn conflicting_duplicate_signature_headers_are_rejected() {
let request = Request::builder()
.header("X-Hub-Signature-256", GITHUB_SIGNATURE)
.header(
"x-hub-signature-256",
"sha256=0000000000000000000000000000000000000000000000000000000000000000",
)
.body(TestBody::new(Bytes::from_static(GITHUB_BODY)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
block_on(async {
let svc = github_service();
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
});
}
#[test]
fn identical_duplicate_signature_headers_still_verify() {
let request = Request::builder()
.header("X-Hub-Signature-256", GITHUB_SIGNATURE)
.header("x-hub-signature-256", GITHUB_SIGNATURE)
.body(TestBody::new(Bytes::from_static(GITHUB_BODY)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
block_on(async {
let svc = github_service();
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::OK);
});
}
#[test]
fn conflicting_duplicate_timestamp_header_is_rejected_for_slack() {
let request = Request::builder()
.header("X-Slack-Signature", format!("v0={SLACK_SIGNATURE}"))
.header("X-Slack-Request-Timestamp", SLACK_TIMESTAMP.to_string())
.header("x-slack-request-timestamp", "1700000001")
.body(TestBody::new(Bytes::from_static(SLACK_BODY)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
let svc = VerifyLayer::new(Provider::Slack, Secret::new(SLACK_SECRET)).layer(EchoLen);
block_on(async {
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
});
}
#[test]
fn stale_timestamp_is_unauthorized_through_adapter() {
let request = Request::builder()
.header("X-Slack-Signature", format!("v0={SLACK_SIGNATURE}"))
.header("X-Slack-Request-Timestamp", SLACK_TIMESTAMP.to_string())
.body(TestBody::new(Bytes::from_static(SLACK_BODY)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
let late = epoch(SLACK_TIMESTAMP + 600);
let svc = VerifyLayer::with_options(
Provider::Slack,
Secret::new(SLACK_SECRET),
crate::VerifyOptions {
clock: Some(Arc::new(FixedClock(late))),
..crate::VerifyOptions::default()
},
)
.layer(EchoLen);
block_on(async {
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
});
}
#[test]
fn custom_scheme_headers_are_dup_checked_too() {
const DIGEST: &str = "11316937114e6970aa59bd5326a6f38dd525f4ade64670e402bff41e2f7c4071";
let scheme = crate::CustomScheme {
hash: crate::HashAlg::Sha256,
signature_header: "X-My-Sig",
timestamp_header: None,
encoding: crate::Encoding::Hex,
prefix: Some("sha256="),
signed_string: |_headers, body| body.to_vec(),
};
let ambiguous = Request::builder()
.header("X-My-Sig", format!("sha256={DIGEST}"))
.header("x-my-sig", "sha256=00")
.body(TestBody::new(Bytes::from_static(GITHUB_BODY)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
let svc = VerifyLayer::new(Provider::Custom(scheme), Secret::new("k")).layer(EchoLen);
block_on(async {
let response = svc
.oneshot(ambiguous)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
});
let good = Request::builder()
.header("X-My-Sig", format!("sha256={DIGEST}"))
.body(TestBody::new(Bytes::from_static(GITHUB_BODY)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
block_on(async {
let svc = VerifyLayer::new(Provider::Custom(scheme), Secret::new("k")).layer(EchoLen);
let response = svc
.oneshot(good)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::OK);
});
}
#[test]
fn unsupported_provider_maps_to_internal_server_error() {
#[cfg(not(feature = "paypal"))]
{
let svc = VerifyLayer::new(Provider::PayPal, Secret::new("unused")).layer(EchoLen);
let request = Request::builder()
.body(TestBody::new(Bytes::from_static(b"{}")))
.unwrap_or_else(|_| unreachable!("no headers to misbuild"));
block_on(async {
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
});
}
#[cfg(feature = "paypal")]
{
let cert = include_bytes!("../tests/data/paypal_test_cert.pem");
let options = clocked_at(1_715_836_763, None)
.with_webhook_id("0NH55953DH663215D")
.with_verifying_material(VerifyingKeyMaterial::X509Certificate(cert.to_vec()));
let svc = VerifyLayer::with_options(Provider::PayPal, Secret::new("unused"), options)
.layer(EchoLen);
let body = include_bytes!("../tests/data/paypal_docs_body.json");
let request = Request::builder()
.header(
"PayPal-Transmission-Id",
"db49fb10-1343-11ef-ac58-e32457403f67",
)
.header("PayPal-Transmission-Time", "2024-05-16T05:19:23Z")
.header(
"PayPal-Transmission-Sig",
"aGYe/s6lwrASh2zyTRIAz8Edo705ezMKekirejT08ev3VXdWAkq4JWADiNPUGelx5qrEKxC7mPIHmAwQ5hOT6unhY9n33M/DbXTKGsuITPdXRA7qYVmc2wsIp68BpzB6pC6+5vt/YLQvflsrwrutGa0KyZc5FinuYNN8pTNomv4uiygasWqfnDyKViKQPNZecowag6tY/9pj7+bgBu/joBpYUq0+cQxfGqNnlvywBJ7HCOf4edeTIvM/c1CvvAHGtNTU54kLjWGue640twn6iXPL8tnaABZ8Fr9m0z87v8oY0vBobERV0Yu8thUToKhvQEFF26Rckqy07VVddg1CmA==",
)
.header("PayPal-Cert-Url", "https://example.invalid/cert-url")
.header("PayPal-Auth-Algo", "SHA256withRSA")
.body(TestBody::new(Bytes::from_static(body)))
.unwrap_or_else(|_| unreachable!("static parts build a valid request"));
block_on(async {
let response = svc
.oneshot(request)
.await
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(response.status(), StatusCode::OK);
});
}
}
#[test]
fn identical_opaque_duplicate_values_are_not_ambiguous_but_fail_verification() {
let mut headers = ::http::HeaderMap::new();
let value = ::http::HeaderValue::from_bytes(&[0xFF])
.unwrap_or_else(|_| unreachable!("0xFF is permitted"));
headers.append("X-Hub-Signature-256", value.clone());
headers.append("X-Hub-Signature-256", value);
assert!(conflicting_signature_header(&headers, &["X-Hub-Signature-256"]).is_none());
}
#[test]
fn unparseable_header_name_fails_closed_as_ambiguous() {
let headers = ::http::HeaderMap::new();
assert_eq!(
conflicting_signature_header(&headers, &["x-hub-signature-256 invalid"]),
Some("x-hub-signature-256 invalid")
);
}
#[test]
fn debug_output_never_contains_secrets() {
let layer = VerifyLayer::<Bytes>::with_options(
Provider::GitHub,
Secret::new("super-secret-hmac-key"),
crate::VerifyOptions::default().with_request_url("https://internal.example/hook"),
);
let debug = format!("{layer:?}");
assert!(!debug.contains("super-secret-hmac-key"));
assert!(!debug.contains("internal.example"));
}
}