From dfef39801fb414348922ced43078451c2c09df09 Mon Sep 17 00:00:00 2001 From: elsirion Date: Wed, 6 Nov 2024 14:04:02 -0500 Subject: [PATCH] fix: allow lenient parsing of meta values if their encoding is broken --- Cargo.lock | 1 + modules/fedimint-meta-client/src/cli.rs | 5 +++-- modules/fedimint-meta-common/Cargo.toml | 1 + modules/fedimint-meta-common/src/lib.rs | 13 ++++++++++++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9304f5848b5..9446f8d642f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2839,6 +2839,7 @@ dependencies = [ "serde", "serde_json", "thiserror", + "tracing", ] [[package]] diff --git a/modules/fedimint-meta-client/src/cli.rs b/modules/fedimint-meta-client/src/cli.rs index ed82f3f2f81..a7092287875 100644 --- a/modules/fedimint-meta-client/src/cli.rs +++ b/modules/fedimint-meta-client/src/cli.rs @@ -55,8 +55,9 @@ pub(crate) async fn handle_cli_command( let value = if hex { serde_json::to_value(value).expect("can't fail") } else { - serde_json::from_slice(value.as_slice()) - .context("deserializating consensus value as json")? + value + .to_json_lossy() + .context("deserializing consensus value as json")? }; json!({ "revision": revision, diff --git a/modules/fedimint-meta-common/Cargo.toml b/modules/fedimint-meta-common/Cargo.toml index 7cca12ed75a..aca4a455b82 100644 --- a/modules/fedimint-meta-common/Cargo.toml +++ b/modules/fedimint-meta-common/Cargo.toml @@ -22,3 +22,4 @@ hex = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } +tracing = { workspace = true } diff --git a/modules/fedimint-meta-common/src/lib.rs b/modules/fedimint-meta-common/src/lib.rs index 4e274f289a8..aa743b0c94e 100644 --- a/modules/fedimint-meta-common/src/lib.rs +++ b/modules/fedimint-meta-common/src/lib.rs @@ -16,7 +16,7 @@ use fedimint_core::plugin_types_trait_impl_common; use serde::de::{self, Visitor}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use thiserror::Error; - +use tracing::warn; // Common contains types shared by both the client and server // The client and server configuration @@ -106,6 +106,17 @@ impl MetaValue { pub fn to_json(&self) -> anyhow::Result { Ok(serde_json::from_slice(&self.0)?) } + + /// Converts the value to a JSON value, ignoring invalid utf-8. + pub fn to_json_lossy(&self) -> anyhow::Result { + let maybe_lossy_str = String::from_utf8_lossy(self.as_slice()); + + if maybe_lossy_str.as_bytes() != self.as_slice() { + warn!("Value contains invalid utf-8, converting to lossy string"); + } + + Ok(serde_json::from_str(&maybe_lossy_str)?) + } } impl Decodable for MetaValue {