Merge rust-bitcoin/rust-bitcoin#7168: units: add `visit_units` in optional serde visitors
What changed, and why it matters
This commit fixes a bug in how optional Bitcoin amount and fee-rate values are read from JSON when those fields are inside a flattened struct and the value is null. Previously, the code would error out because it did not handle a special serde 'unit' representation that serde uses during flattening. The fix adds handlers that treat that unit as None, and adds a test to prevent regression. It is a correctness/robustness fix rather than a typical security vulnerability, but it could cause unexpected deserialization failures in applications relying on these types.
Review downstream code that deserializes Option<Amount>, Option<SignedAmount>, or Option<FeeRate> inside flattened structs; ensure the fix is included in the next release. No immediate security mitigation is required, but consider this a reliability fix that may affect API consumers parsing JSON with optional flattened fields.
Security signals we found
Deserialization failure in optional numeric types under serde flatten
Missing serde visitor method (visit_unit) causing runtime errors
Regression test added for null handling in flattened structs
Evidence from the diff
The change adds visit_unit() implementations to the optional serde visitor modules in units/src/amount/serde.rs and units/src/fee_rate/serde.rs. When serde flattens a struct, a JSON null for an Option
Changed components
units/src/amount/serde.rs (as_sat::opt, as_btc::opt, as_str::opt visitors)units/src/fee_rate/serde.rs (as_sat_per_kwu_floor::opt, as_sat_per_vb_floor::opt, as_sat_per_vb_ceil::opt visitors)units/tests/serde.rsInspect captured patch +79 / −0
### units/src/amount/serde.rs
@@ -240,6 +240,14 @@ pub mod as_sat {
Ok(None)
}
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(None)
+ }
+
#[inline]
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
@@ -421,6 +429,14 @@ pub mod as_btc {
Ok(None)
}
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(None)
+ }
+
#[inline]
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
@@ -595,6 +611,14 @@ pub mod as_str {
Ok(None)
}
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(None)
+ }
+
#[inline]
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
### units/src/fee_rate/serde.rs
@@ -84,6 +84,14 @@ pub mod as_sat_per_kwu_floor {
Ok(None)
}
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(None)
+ }
+
#[inline]
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
@@ -211,6 +219,14 @@ pub mod as_sat_per_vb_floor {
Ok(None)
}
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(None)
+ }
+
#[inline]
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
@@ -339,6 +355,14 @@ pub mod as_sat_per_vb_ceil {
Ok(None)
}
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(None)
+ }
+
#[inline]
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
### units/tests/serde.rs
@@ -946,3 +946,34 @@ fn vec_deserializers_do_not_trust_size_hint() {
fee_rate::serde::as_sat_per_vb_ceil::vec::deserialize => FeeRate,
);
}
+
+#[test]
+fn serde_opt_null_through_flatten() {
+ #[derive(Debug, PartialEq, Serialize, Deserialize)]
+ struct Inner {
+ #[serde(with = "amount::serde::as_sat::opt")]
+ a: Option<Amount>,
+ #[serde(with = "amount::serde::as_btc::opt")]
+ b: Option<SignedAmount>,
+ #[serde(with = "amount::serde::as_str::opt")]
+ c: Option<Amount>,
+ #[serde(with = "fee_rate::serde::as_sat_per_kwu_floor::opt")]
+ d: Option<FeeRate>,
+ #[serde(with = "fee_rate::serde::as_sat_per_vb_floor::opt")]
+ e: Option<FeeRate>,
+ #[serde(with = "fee_rate::serde::as_sat_per_vb_ceil::opt")]
+ f: Option<FeeRate>,
+ }
+
+ #[derive(Debug, PartialEq, Serialize, Deserialize)]
+ struct Outer {
+ #[serde(flatten)]
+ inner: Inner,
+ }
+
+ let none = Outer { inner: Inner { a: None, b: None, c: None, d: None, e: None, f: None } };
+ let json = "{\"a\":null,\"b\":null,\"c\":null,\"d\":null,\"e\":null,\"f\":null}";
+
+ assert_eq!(serde_json::from_str::<Outer>(json).unwrap(), none);
+ assert_eq!(serde_json::to_string(&none).unwrap(), json);
+}Why this scored 36/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.