From e93129a8fb182b5f5c7d0fe82fab8b1b66745d84 Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Mon, 3 Jun 2024 08:12:01 -0700 Subject: [PATCH] Don't log sensitive user info in GLIDE code (#1443) --- glide-core/src/client/mod.rs | 11 ++--- glide-core/src/client/value_conversion.rs | 56 +++++++++++++++-------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/glide-core/src/client/mod.rs b/glide-core/src/client/mod.rs index 0f1ab26506..3197c82a23 100644 --- a/glide-core/src/client/mod.rs +++ b/glide-core/src/client/mod.rs @@ -17,7 +17,7 @@ use std::ops::Deref; use std::time::Duration; pub use types::*; -use self::value_conversion::{convert_to_expected_type, expected_type_for_cmd}; +use self::value_conversion::{convert_to_expected_type, expected_type_for_cmd, get_value_type}; mod reconnecting_connection; mod standalone_client; mod value_conversion; @@ -135,10 +135,9 @@ fn parse_timeout_to_f64(cmd: &Cmd, timeout_idx: usize) -> RedisResult { ErrorKind::ResponseError, err_msg, format!( - "Expected to find timeout value at index {:?} for command {:?}. Recieved command = {:?}", + "Expected to find timeout value at index {:?} for command {:?}.", timeout_idx, std::str::from_utf8(&cmd.command().unwrap_or_default()), - cmd ), )) }; @@ -166,7 +165,7 @@ fn get_timeout_from_cmd_arg( Err(RedisError::from(( ErrorKind::ResponseError, "Timeout cannot be negative", - format!("Recieved timeout={:?}", timeout_secs), + format!("Received timeout = {:?}.", timeout_secs), ))) } else if timeout_secs == 0.0 { // `0` means we should set no timeout @@ -177,7 +176,7 @@ fn get_timeout_from_cmd_arg( Err(RedisError::from(( ErrorKind::ResponseError, "Timeout is out of range, max timeout is 2^32 - 1 (u32::MAX)", - format!("Recieved timeout={:?}", timeout_secs), + format!("Received timeout = {:?}.", timeout_secs), ))) } else { // Extend the request timeout to ensure we don't timeout before receiving a response from the server. @@ -262,7 +261,7 @@ impl Client { return Err(( ErrorKind::ResponseError, "Received non-array response for transaction", - format!("Response: `{value:?}`"), + format!("(response was {:?})", get_value_type(&value)), ) .into()); } diff --git a/glide-core/src/client/value_conversion.rs b/glide-core/src/client/value_conversion.rs index 8edbfff2ba..d4990c9db3 100644 --- a/glide-core/src/client/value_conversion.rs +++ b/glide-core/src/client/value_conversion.rs @@ -44,14 +44,13 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to map", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, ExpectedReturnType::MapOfStringToDouble => match value { Value::Nil => Ok(value), Value::Map(map) => { - let map_clone = map.clone(); let result = map .into_iter() .map(|(key, inner_value)| { @@ -68,7 +67,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to map of {string: double}", - format!("(response was {:?})", map_clone), + format!("(response was {:?})", get_value_type(&inner_value)), ) .into()), } @@ -85,7 +84,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to map of {string: double}", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -96,7 +95,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to set", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -125,7 +124,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to Array (ZRankResponseType)", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -143,7 +142,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Could not convert value to boolean", - format!("(value was {:?})", item), + format!("(response was {:?})", get_value_type(&item)), ) .into()), }, @@ -152,20 +151,20 @@ pub(crate) fn convert_to_expected_type( converted_array.map(Value::Array) } - Value::BulkString(bytes) => match std::str::from_utf8(&bytes) { + Value::BulkString(ref bytes) => match std::str::from_utf8(bytes) { Ok("true") => Ok(Value::Boolean(true)), Ok("false") => Ok(Value::Boolean(false)), _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to boolean", - format!("(response was {:?})", bytes), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to Json Toggle return type", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -174,7 +173,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to an array of boolean", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -191,7 +190,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to an array of doubles", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -223,7 +222,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to ZMPOP return type", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -257,7 +256,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to an array of array of double or null. Inner value of Array must be Array or Null", - format!("(Inner value was {:?})", item), + format!("(response was {:?})", get_value_type(&item)), ) .into()), }) @@ -268,7 +267,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to an array of array of double or null", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -310,7 +309,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "LOLWUT response couldn't be converted to a user-friendly format", - (&format!("(response was {:?}...)", value)[..100]).into(), + format!("(response was {:?})", get_value_type(&value)), ) .into()), } @@ -375,7 +374,7 @@ pub(crate) fn convert_to_expected_type( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to an array containing a key, member, and score", - format!("(response was {:?})", value), + format!("(response was {:?})", get_value_type(&value)), ) .into()), }, @@ -490,7 +489,7 @@ fn convert_to_array_of_pairs( _ => Err(( ErrorKind::TypeError, "Response couldn't be converted to an array of key-value pairs", - format!("(response was {:?})", response), + format!("(response was {:?})", get_value_type(&response)), ) .into()), } @@ -571,6 +570,27 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option { } } +/// Gets the enum variant as a string for the `value` given. +pub(crate) fn get_value_type<'a>(value: &Value) -> &'a str { + match value { + Value::Nil => "Nil", + Value::Int(_) => "Int", + Value::BulkString(_) => "BulkString", + Value::Array(_) => "Array", + Value::SimpleString(_) => "SimpleString", + Value::Okay => "OK", + Value::Map(_) => "Map", + Value::Attribute { .. } => "Attribute", + Value::Set(_) => "Set", + Value::Double(_) => "Double", + Value::Boolean(_) => "Boolean", + Value::VerbatimString { .. } => "VerbatimString", + Value::BigNumber(_) => "BigNumber", + Value::Push { .. } => "Push", + // TODO Value::ServerError from https://github.com/redis-rs/redis-rs/pull/1093 + } +} + #[cfg(test)] mod tests { use super::*;