This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git

commit c66d56bcb8ae2f2a18bddeac5b67304ca8a4bf46
Author: Mingshen Sun <[email protected]>
AuthorDate: Wed Feb 5 15:38:32 2020 -0800

    [rpc] Default max frame length is 8MB
---
 rpc/src/protocol.rs  | 15 ++++++++++++++-
 rpc/src/transport.rs |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/rpc/src/protocol.rs b/rpc/src/protocol.rs
index a4a51c8..0230c79 100644
--- a/rpc/src/protocol.rs
+++ b/rpc/src/protocol.rs
@@ -13,6 +13,8 @@ pub enum ProtocolError {
     IoError(#[from] io::Error),
     #[error("SerdeError")]
     SerdeError(#[from] serde_json::error::Error),
+    #[error(transparent)]
+    Other(#[from] anyhow::Error),
 }
 
 pub(crate) struct JsonProtocol<'a, T>
@@ -20,6 +22,7 @@ where
     T: io::Read + io::Write,
 {
     pub transport: &'a mut T,
+    max_frame_len: u64,
 }
 
 impl<'a, T> JsonProtocol<'a, T>
@@ -27,7 +30,11 @@ where
     T: io::Read + io::Write,
 {
     pub fn new(transport: &'a mut T) -> JsonProtocol<'a, T> {
-        Self { transport }
+        Self {
+            transport,
+            // Default max frame length is 8MB
+            max_frame_len: 8 * 1_024 * 1_024,
+        }
     }
 
     pub fn read_message<V>(&mut self) -> std::result::Result<V, ProtocolError>
@@ -39,6 +46,12 @@ where
         self.transport.read_exact(&mut header)?;
         let buf_len = u64::from_be(unsafe { transmute::<[u8; 8], u64>(header) 
});
 
+        if buf_len > self.max_frame_len {
+            return Err(ProtocolError::Other(anyhow::anyhow!(
+                "Exceed max frame length"
+            )));
+        }
+
         let mut recv_buf: Vec<u8> = vec![0u8; buf_len as usize];
         self.transport.read_exact(&mut recv_buf)?;
 
diff --git a/rpc/src/transport.rs b/rpc/src/transport.rs
index ab75160..7c967bd 100644
--- a/rpc/src/transport.rs
+++ b/rpc/src/transport.rs
@@ -89,7 +89,7 @@ where
                         debug!("Connection disconnected.");
                         return Ok(());
                     }
-                    protocol::ProtocolError::SerdeError(_) => {
+                    _ => {
                         debug!("{:?}", e);
                         let response: JsonProtocolResult<U, 
TeaclaveServiceResponseError> =
                             Err(TeaclaveServiceResponseError::RequestError(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to