Reject octet-stream content type when we expect json (#5862)

* reject octet-stream content type when we expect json

* added a test, fixed a bug with checking for 415's
This commit is contained in:
Eitan Seri-Levi
2024-07-12 07:13:52 +01:00
committed by GitHub
parent 4c7277c646
commit 69d84e785b
4 changed files with 107 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
use bytes::Bytes;
use eth2::{CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER};
use serde::de::DeserializeOwned;
use std::error::Error as StdError;
use warp::{Filter, Rejection};
@@ -16,7 +17,17 @@ impl Json {
}
pub fn json<T: DeserializeOwned + Send>() -> impl Filter<Extract = (T,), Error = Rejection> + Copy {
warp::body::bytes().and_then(|bytes: Bytes| async move {
Json::decode(bytes).map_err(|err| reject::custom_deserialize_error(format!("{:?}", err)))
})
warp::header::optional::<String>(CONTENT_TYPE_HEADER)
.and(warp::body::bytes())
.and_then(|header: Option<String>, bytes: Bytes| async move {
if let Some(header) = header {
if header == SSZ_CONTENT_TYPE_HEADER {
return Err(reject::unsupported_media_type(
"The request's content-type is not supported".to_string(),
));
}
}
Json::decode(bytes)
.map_err(|err| reject::custom_deserialize_error(format!("{:?}", err)))
})
}