From 5a9138a764f07ddcaef0231e4c90b27419439f6a Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Tue, 17 Sep 2024 08:50:38 +0100 Subject: [PATCH] support seeded deser --- proxy/src/auth/backend/jwt.rs | 2 ++ proxy/src/http.rs | 7 ++++--- proxy/src/serverless/sql_over_http.rs | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/proxy/src/auth/backend/jwt.rs b/proxy/src/auth/backend/jwt.rs index fce503a190..293fc1b355 100644 --- a/proxy/src/auth/backend/jwt.rs +++ b/proxy/src/auth/backend/jwt.rs @@ -1,5 +1,6 @@ use std::{ future::Future, + marker::PhantomData, sync::Arc, time::{Duration, SystemTime}, }; @@ -148,6 +149,7 @@ impl JwkCacheEntryLock { Ok(r) => { let resp: http::Response = r.into(); match parse_json_body_with_limit::( + PhantomData, resp.into_body(), MAX_JWK_BODY_SIZE, ) diff --git a/proxy/src/http.rs b/proxy/src/http.rs index 0a38c70ad6..4d756fc632 100644 --- a/proxy/src/http.rs +++ b/proxy/src/http.rs @@ -9,7 +9,7 @@ use std::time::Duration; use bytes::Bytes; use http_body_util::BodyExt; use hyper1::body::Body; -use serde::de::DeserializeOwned; +use serde::de::DeserializeSeed; pub(crate) use reqwest::{Request, Response}; pub(crate) use reqwest_middleware::{ClientWithMiddleware, Error}; @@ -122,7 +122,8 @@ pub(crate) enum ReadPayloadError { LengthExceeded(usize), } -pub(crate) async fn parse_json_body_with_limit( +pub(crate) async fn parse_json_body_with_limit( + seed: impl for<'de> DeserializeSeed<'de, Value = D>, mut b: impl Body + Unpin, limit: usize, ) -> Result> { @@ -151,7 +152,7 @@ pub(crate) async fn parse_json_body_with_limit( } } - Ok(serde_json::from_slice::(&bytes)?) + Ok(seed.deserialize(&mut serde_json::Deserializer::from_slice(&bytes))?) } #[cfg(test)] diff --git a/proxy/src/serverless/sql_over_http.rs b/proxy/src/serverless/sql_over_http.rs index dd017a0da8..4cfc342a11 100644 --- a/proxy/src/serverless/sql_over_http.rs +++ b/proxy/src/serverless/sql_over_http.rs @@ -1,3 +1,4 @@ +use std::marker::PhantomData; use std::pin::pin; use std::sync::Arc; @@ -605,6 +606,7 @@ async fn handle_db_inner( let fetch_and_process_request = Box::pin(async { let payload = parse_json_body_with_limit( + PhantomData, request.into_body(), config.http_config.max_request_size_bytes as usize, )