use crate::telemetry::SecurityStore;
use axum::{
body::Body,
extract::Request,
http::{HeaderValue, StatusCode},
middleware::Next,
response::{IntoResponse, Response},
};
use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use std::{collections::HashSet, fmt};
mod policy;
pub use policy::{JsonSchemaPolicy, SchemaPolicyError};
pub const DEFAULT_MAX_PAYLOAD_BYTES: usize = 2 * 1024 * 1024;
pub const DEFAULT_MAX_NESTING_DEPTH: usize = 32;
const DUPLICATE_KEY_MARKER: &str = "duplicate JSON object key";
struct StrictJson;
impl<'de> Deserialize<'de> for StrictJson {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(StrictJsonVisitor)
}
}
struct StrictJsonVisitor;
impl<'de> Visitor<'de> for StrictJsonVisitor {
type Value = StrictJson;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a JSON value without duplicate object keys")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut keys = HashSet::new();
while let Some(key) = map.next_key::<String>()? {
if !keys.insert(key) {
return Err(de::Error::custom(DUPLICATE_KEY_MARKER));
}
map.next_value::<StrictJson>()?;
}
Ok(StrictJson)
}
fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
while sequence.next_element::<StrictJson>()?.is_some() {}
Ok(StrictJson)
}
fn visit_bool<E>(self, _value: bool) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_i64<E>(self, _value: i64) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_u64<E>(self, _value: u64) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_f64<E>(self, _value: f64) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_str<E>(self, _value: &str) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_string<E>(self, _value: String) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_none<E>(self) -> Result<Self::Value, E> {
Ok(StrictJson)
}
fn visit_unit<E>(self) -> Result<Self::Value, E> {
Ok(StrictJson)
}
}
pub fn inspect_json_payload(
bytes: &[u8],
max_depth: usize,
max_bytes: usize,
) -> Result<(), &'static str> {
if bytes.len() > max_bytes {
SecurityStore::global().inc_schema_violations();
return Err("Payload exceeds maximum allowed size");
}
let mut current_depth: usize = 0;
let mut in_string = false;
let mut escape = false;
for &b in bytes {
if escape {
escape = false;
continue;
}
if b == b'\\' && in_string {
escape = true;
continue;
}
if b == b'"' {
in_string = !in_string;
continue;
}
if !in_string {
if b == b'{' || b == b'[' {
current_depth += 1;
if current_depth > max_depth {
SecurityStore::global().inc_schema_violations();
return Err("Payload exceeds maximum allowed object nesting depth");
}
} else if b == b'}' || b == b']' {
current_depth = current_depth.saturating_sub(1);
}
}
}
if let Err(error) = serde_json::from_slice::<StrictJson>(bytes) {
SecurityStore::global().inc_schema_violations();
if error.to_string().contains(DUPLICATE_KEY_MARKER) {
return Err("JSON object contains a duplicate key");
}
return Err("Payload is not valid JSON");
}
Ok(())
}
fn is_json_content_type(value: &str) -> bool {
crate::media_type::is_json(crate::media_type::essence(value))
}
pub async fn schema_guard_middleware(req: Request, next: Next) -> Response {
let content_type = req
.headers()
.get(axum::http::header::CONTENT_TYPE)
.and_then(|v: &HeaderValue| v.to_str().ok())
.unwrap_or("");
if is_json_content_type(content_type) {
let (parts, body) = req.into_parts();
let bytes = match axum::body::to_bytes(body, DEFAULT_MAX_PAYLOAD_BYTES + 1).await {
Ok(b) => b,
Err(_) => {
SecurityStore::global().inc_schema_violations();
return (
StatusCode::BAD_REQUEST,
"Failed to read JSON request body or payload too large",
)
.into_response();
}
};
if let Err(err_msg) =
inspect_json_payload(&bytes, DEFAULT_MAX_NESTING_DEPTH, DEFAULT_MAX_PAYLOAD_BYTES)
{
return (StatusCode::BAD_REQUEST, err_msg).into_response();
}
let reconstructed_req = Request::from_parts(parts, Body::from(bytes));
next.run(reconstructed_req).await
} else {
next.run(req).await
}
}
pub async fn json_schema_guard_middleware(
axum::extract::State(policy): axum::extract::State<JsonSchemaPolicy>,
req: Request,
next: Next,
) -> Response {
let content_type = req
.headers()
.get(axum::http::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or("");
if !is_json_content_type(content_type) {
if matches!(
*req.method(),
axum::http::Method::GET | axum::http::Method::HEAD | axum::http::Method::OPTIONS
) {
return next.run(req).await;
}
SecurityStore::global().inc_schema_violations();
return (
StatusCode::UNSUPPORTED_MEDIA_TYPE,
"This route requires a JSON Content-Type",
)
.into_response();
}
let (parts, body) = req.into_parts();
let bytes = match axum::body::to_bytes(body, DEFAULT_MAX_PAYLOAD_BYTES + 1).await {
Ok(bytes) => bytes,
Err(_) => {
SecurityStore::global().inc_schema_violations();
return (
StatusCode::BAD_REQUEST,
"Failed to read JSON request body or payload too large",
)
.into_response();
}
};
if let Err(message) =
inspect_json_payload(&bytes, DEFAULT_MAX_NESTING_DEPTH, DEFAULT_MAX_PAYLOAD_BYTES)
{
return (StatusCode::BAD_REQUEST, message).into_response();
}
let instance = match serde_json::from_slice(&bytes) {
Ok(instance) => instance,
Err(_) => {
SecurityStore::global().inc_schema_violations();
return (StatusCode::BAD_REQUEST, "Payload is not valid JSON").into_response();
}
};
if policy.validate(&instance).is_err() {
return (
StatusCode::UNPROCESSABLE_ENTITY,
"JSON payload does not match the configured schema",
)
.into_response();
}
next.run(Request::from_parts(parts, Body::from(bytes)))
.await
}
#[cfg(test)]
mod tests {
use super::*;
use axum::{Router, http::Request, middleware, routing::post};
use tower::ServiceExt;
#[test]
fn test_inspect_json_payload_valid() {
let json = b"{\"user\": {\"name\": \"Alice\", \"roles\": [\"admin\", \"user\"]}}";
assert!(inspect_json_payload(json, 10, 1024).is_ok());
}
#[test]
fn test_inspect_json_payload_braces_in_strings() {
let json = br#"{"snippet": "{{{{{{{{{{[[[[[[[[[[}}}}}}}}}}]]]]]]]]]]", "status": 200}"#;
assert!(inspect_json_payload(json, 5, 1024).is_ok());
}
#[test]
fn test_inspect_json_payload_excessive_depth() {
let mut deep_json = Vec::new();
for _ in 0..40 {
deep_json.extend_from_slice(b"{\"a\":");
}
deep_json.extend_from_slice(b"1");
for _ in 0..40 {
deep_json.extend_from_slice(b"}");
}
let res = inspect_json_payload(&deep_json, 10, 1024 * 1024);
assert!(res.is_err());
assert_eq!(
res.unwrap_err(),
"Payload exceeds maximum allowed object nesting depth"
);
}
#[test]
fn test_inspect_json_payload_too_large() {
let json = vec![b'a'; 2000];
let res = inspect_json_payload(&json, 10, 1000);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), "Payload exceeds maximum allowed size");
}
#[test]
fn syntax_and_duplicate_keys_are_rejected_recursively() {
assert_eq!(
inspect_json_payload(br#"{"role":"user","role":"admin"}"#, 10, 1_024),
Err("JSON object contains a duplicate key")
);
assert_eq!(
inspect_json_payload(br#"{"outer":{"id":1,"id":2}}"#, 10, 1_024),
Err("JSON object contains a duplicate key")
);
assert_eq!(
inspect_json_payload(br#"{"unterminated":true"#, 10, 1_024),
Err("Payload is not valid JSON")
);
}
#[test]
fn content_type_matching_is_exact_and_supports_json_suffixes() {
assert!(is_json_content_type("application/json; charset=utf-8"));
assert!(is_json_content_type("application/problem+json"));
assert!(!is_json_content_type("text/application/json"));
assert!(!is_json_content_type("application/json-malicious"));
}
#[test]
fn content_type_matching_accepts_every_json_form_axum_parses() {
for content_type in [
"APPLICATION/JSON",
"application/vnd.api+JSON",
"Application/problem+json; charset=utf-8",
"application/json+patch",
] {
assert!(is_json_content_type(content_type), "{content_type}");
}
for content_type in ["application/xml", "text/json", "application/jsonp"] {
assert!(!is_json_content_type(content_type), "{content_type}");
}
}
#[tokio::test]
async fn case_and_suffix_variants_cannot_skip_duplicate_key_checks() {
for content_type in ["application/vnd.api+JSON", "Application/problem+json"] {
let response = guarded_app()
.oneshot(
Request::post("/")
.header(axum::http::header::CONTENT_TYPE, content_type)
.body(Body::from(r#"{"role":"user","role":"admin"}"#))
.expect("request should be valid"),
)
.await
.expect("middleware request should complete");
assert_eq!(response.status(), StatusCode::BAD_REQUEST, "{content_type}");
}
}
fn guarded_app() -> Router {
Router::new()
.route("/", post(|body: String| async move { body }))
.layer(middleware::from_fn(schema_guard_middleware))
}
#[tokio::test]
async fn middleware_preserves_valid_json_and_non_json_bodies() {
for (content_type, body) in [
("application/json; charset=utf-8", r#"{"safe":[1,2,3]}"#),
("text/plain", "not JSON { and deliberately unbalanced"),
] {
let response = guarded_app()
.oneshot(
Request::post("/")
.header(axum::http::header::CONTENT_TYPE, content_type)
.body(Body::from(body))
.expect("request should be valid"),
)
.await
.expect("middleware request should complete");
assert_eq!(response.status(), StatusCode::OK);
let returned = axum::body::to_bytes(response.into_body(), 1_024)
.await
.expect("response body should be readable");
assert_eq!(returned.as_ref(), body.as_bytes());
}
}
#[tokio::test]
async fn middleware_rejects_deep_and_oversized_json() {
let deep = format!("{}0{}", "[".repeat(33), "]".repeat(33));
let response = guarded_app()
.oneshot(
Request::post("/")
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(Body::from(deep))
.expect("request should be valid"),
)
.await
.expect("middleware request should complete");
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let response = guarded_app()
.oneshot(
Request::post("/")
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(Body::from(vec![b' '; DEFAULT_MAX_PAYLOAD_BYTES + 2]))
.expect("request should be valid"),
)
.await
.expect("middleware request should complete");
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
fn schema_bound_app() -> Router {
let policy = JsonSchemaPolicy::from_schema(serde_json::json!({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {"name": {"type": "string", "minLength": 1}},
"required": ["name"],
"additionalProperties": false
}))
.expect("route schema");
Router::new().route(
"/",
post(|body: String| async move { body }).layer(middleware::from_fn_with_state(
policy,
json_schema_guard_middleware,
)),
)
}
#[tokio::test]
async fn compiled_schema_middleware_preserves_valid_bodies_and_fails_closed() {
let valid = schema_bound_app()
.oneshot(
Request::post("/")
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"name":"Ada"}"#))
.expect("valid request"),
)
.await
.expect("valid schema response");
assert_eq!(valid.status(), StatusCode::OK);
let returned = axum::body::to_bytes(valid.into_body(), 1_024)
.await
.expect("preserved body");
assert_eq!(returned, r#"{"name":"Ada"}"#);
for (content_type, body, expected) in [
(
"application/json",
r#"{"name":"Ada","role":"admin"}"#,
StatusCode::UNPROCESSABLE_ENTITY,
),
(
"application/json",
r#"{"name":"Ada","name":"Mallory"}"#,
StatusCode::BAD_REQUEST,
),
(
"text/plain",
r#"{"name":"Ada"}"#,
StatusCode::UNSUPPORTED_MEDIA_TYPE,
),
] {
let response = schema_bound_app()
.oneshot(
Request::post("/")
.header(axum::http::header::CONTENT_TYPE, content_type)
.body(Body::from(body))
.expect("schema rejection request"),
)
.await
.expect("schema rejection response");
assert_eq!(response.status(), expected);
}
}
}