From fbdea303753ff361a753926cadc1c1ba20638897 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 11 Jul 2023 17:13:06 +0200 Subject: [PATCH] fix: treat bool params as invalid in logs subscription (#3716) --- crates/rpc/rpc-types/src/eth/pubsub.rs | 29 +++++++++++++++++++++++++- crates/rpc/rpc/src/eth/pubsub.rs | 3 +++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc-types/src/eth/pubsub.rs b/crates/rpc/rpc-types/src/eth/pubsub.rs index e363cb754c..d54f6f849a 100644 --- a/crates/rpc/rpc-types/src/eth/pubsub.rs +++ b/crates/rpc/rpc-types/src/eth/pubsub.rs @@ -96,7 +96,7 @@ pub enum SubscriptionKind { Syncing, } -/// Subscription kind. +/// Any additional parameters for a subscription. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] pub enum Params { /// No parameters passed. @@ -108,6 +108,20 @@ pub enum Params { Bool(bool), } +impl Params { + /// Returns true if it's a bool parameter. + #[inline] + pub fn is_bool(&self) -> bool { + matches!(self, Params::Bool(_)) + } + + /// Returns true if it's a log parameter. + #[inline] + pub fn is_logs(&self) -> bool { + matches!(self, Params::Logs(_)) + } +} + impl Serialize for Params { fn serialize(&self, serializer: S) -> Result where @@ -141,3 +155,16 @@ impl<'a> Deserialize<'a> for Params { .map_err(|e| D::Error::custom(format!("Invalid Pub-Sub parameters: {e}"))) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn params_serde() { + let s: Params = serde_json::from_str("true").unwrap(); + assert_eq!(s, Params::Bool(true)); + let s: Params = serde_json::from_str("null").unwrap(); + assert_eq!(s, Params::None); + } +} diff --git a/crates/rpc/rpc/src/eth/pubsub.rs b/crates/rpc/rpc/src/eth/pubsub.rs index a669300113..5b823ea40c 100644 --- a/crates/rpc/rpc/src/eth/pubsub.rs +++ b/crates/rpc/rpc/src/eth/pubsub.rs @@ -115,6 +115,9 @@ where // if no params are provided, used default filter params let filter = match params { Some(Params::Logs(filter)) => FilteredParams::new(Some(*filter)), + Some(Params::Bool(_)) => { + return Err(invalid_params_rpc_err("Invalid params for logs").into()) + } _ => FilteredParams::default(), }; let stream =