From 154370bc4c5893b28f0f3ca9003d419162e4a101 Mon Sep 17 00:00:00 2001 From: benma's agent Date: Sat, 28 Nov 2026 20:15:21 +0100 Subject: [PATCH 1/2] eth: reject unsafe EIP-712 numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EIP-712 numeric values outside the safe integer range can lose precision when decoded as floating point. Reject numeric values outside ±(2^53 - 1), requiring decimal strings for larger integers. Keep integral numeric forms such as 128.0 and 1e3, and parse JSON floats with round-trip precision at the safe-range boundaries. Related fixes: - Go: https://github.com/BitBoxSwiss/bitbox02-api-go/pull/199 - TypeScript: https://github.com/BitBoxSwiss/bitbox-api-ts/pull/18 --- CHANGELOG.md | 2 ++ Cargo.toml | 2 +- src/eth.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df68d6..9a1797e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +- Reject EIP-712 JSON numbers outside the safe integer range `±(2^53 - 1)`; use decimal strings + for larger integers. - Preserve the sign of positive EIP-712 signed integers supplied as JSON numbers. - Reset unfinished sessions when connecting to firmware v9.28.0 or newer, allowing host reconnects while the device remains powered on. diff --git a/Cargo.toml b/Cargo.toml index 5d6ba52..8050f66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ num-bigint = "0.4" # and run `make build-protos`. prost = "0.14" semver = "1.0.17" -serde_json = { version = "1.0" } +serde_json = { version = "1.0", features = ["float_roundtrip"] } thiserror = "2.0" zeroize = "1" diff --git a/src/eth.rs b/src/eth.rs index a17c4e2..7d954f5 100644 --- a/src/eth.rs +++ b/src/eth.rs @@ -285,6 +285,7 @@ fn parse_type( } fn encode_value(typ: &MemberType, value: &Value) -> Result, String> { + const MAX_SAFE_INTEGER: f64 = ((1u64 << 53) - 1) as f64; match DataType::try_from(typ.r#type).unwrap() { DataType::Bytes => { if let Value::String(v) = value { @@ -312,7 +313,7 @@ fn encode_value(typ: &MemberType, value: &Value) -> Result, String> { Value::Number(n) => { if let Some(v) = n.as_f64() { let v64: u64 = v as _; - if (v64 as f64) != v { + if (v64 as f64) != v || v > MAX_SAFE_INTEGER { Err("Number is not an uint".to_string()) } else { Ok(BigUint::from(v64).to_bytes_be()) @@ -330,7 +331,7 @@ fn encode_value(typ: &MemberType, value: &Value) -> Result, String> { Value::Number(n) => { if let Some(v) = n.as_f64() { let v64: i64 = v as _; - if (v64 as f64) != v { + if (v64 as f64) != v || v.abs() > MAX_SAFE_INTEGER { Err("Number is not an int".to_string()) } else { Ok(BigInt::from(v64).to_signed_bytes_be()) @@ -639,6 +640,8 @@ impl PairedBitBox { /// Signs an Ethereum EIP-712 typed message. It returns a 65 byte signature (R, S, and 1 byte /// recID). 27 is added to the recID to denote an uncompressed pubkey. /// If `use_antiklepto` is false, signing is deterministic and requires firmware >=9.26.0. + /// Integer fields accept JSON numbers only when finite, integral and within `±(2^53 - 1)`. + /// Use decimal strings for larger integers. pub async fn eth_sign_typed_message( &self, chain_id: u64, @@ -1247,6 +1250,65 @@ mod tests { } } + #[test] + fn test_encode_value_safe_json_numbers() { + for (typ, number, expected) in [ + ("uint64", "-0.0", "00"), + ("uint64", "128.0", "80"), + ("uint64", "1e3", "03e8"), + ("uint64", "9007199254740991", "1fffffffffffff"), + ("uint64", "9007199254740991.0", "1fffffffffffff"), + ("int64", "128.0", "0080"), + ("int64", "-1e3", "fc18"), + ("int64", "9007199254740991", "1fffffffffffff"), + ("int64", "-9007199254740991", "e0000000000001"), + ("int64", "-9007199254740991.0", "e0000000000001"), + // Match JavaScript's check of the decoded number, which is already rounded to 1. + ("int64", "1.0000000000000001", "01"), + ] { + let typ = parse_type_no_err(typ, &HashMap::new()); + let value: Value = serde_json::from_str(number).unwrap(); + assert_eq!(hex::encode(encode_value(&typ, &value).unwrap()), expected); + } + + for typ in ["uint256", "int256"] { + let typ = parse_type_no_err(typ, &HashMap::new()); + for number in [ + "0.5", + "-0.5", + "9007199254740992", + "-9007199254740992", + "9007199254740993", + "9007199254740992.0", + "18446744073709551615", + "1e20", + ] { + let value: Value = serde_json::from_str(number).unwrap(); + assert!(encode_value(&typ, &value).is_err(), "{number}"); + } + for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + assert!(encode_value(&typ, &Value::from(value)).is_err()); + } + } + let unsigned = parse_type_no_err("uint256", &HashMap::new()); + assert!(encode_value(&unsigned, &Value::from(-1)).is_err()); + } + + #[test] + fn test_encode_value_large_integer_strings() { + for (typ, number, expected) in [ + ("uint64", "9007199254740993", "20000000000001"), + ("int64", "-9007199254740993", "dfffffffffffff"), + ("uint128", "18446744073709551617", "010000000000000001"), + ] { + let typ = parse_type_no_err(typ, &HashMap::new()); + assert_eq!( + hex::encode(encode_value(&typ, &Value::from(number)).unwrap()), + expected, + ); + } + } + #[test] fn test_get_value() { let msg: Eip712Message = serde_json::from_str(EIP712_MSG).unwrap(); From 52c76139e443c1cb5c83c21818df3847a25c7acd Mon Sep 17 00:00:00 2001 From: benma's agent Date: Mon, 30 Nov 2026 08:50:32 +0100 Subject: [PATCH 2/2] fixup! eth: reject unsafe EIP-712 numbers Preserve JSON integer literals through the exact i64/u64 accessors and keep the float fallback for integral forms such as 128.0 and 1e18. Reject float conversions outside the destination integer range instead of allowing saturating casts at the upper bound. Document that decimal/exponent forms may round during JSON decoding and remove assertions that obscure the distinction between integer tokens and decoded floats. --- CHANGELOG.md | 3 +- src/eth.rs | 77 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a1797e..824cea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,7 @@ # Changelog ## [Unreleased] -- Reject EIP-712 JSON numbers outside the safe integer range `±(2^53 - 1)`; use decimal strings - for larger integers. +- Fix cases where large integer values were rounded when signing EIP-712 messages. - Preserve the sign of positive EIP-712 signed integers supplied as JSON numbers. - Reset unfinished sessions when connecting to firmware v9.28.0 or newer, allowing host reconnects while the device remains powered on. diff --git a/src/eth.rs b/src/eth.rs index 7d954f5..370459a 100644 --- a/src/eth.rs +++ b/src/eth.rs @@ -285,7 +285,6 @@ fn parse_type( } fn encode_value(typ: &MemberType, value: &Value) -> Result, String> { - const MAX_SAFE_INTEGER: f64 = ((1u64 << 53) - 1) as f64; match DataType::try_from(typ.r#type).unwrap() { DataType::Bytes => { if let Value::String(v) = value { @@ -311,9 +310,12 @@ fn encode_value(typ: &MemberType, value: &Value) -> Result, String> { } } Value::Number(n) => { - if let Some(v) = n.as_f64() { + if let Some(v) = n.as_u64() { + Ok(BigUint::from(v).to_bytes_be()) + } else if let Some(v) = n.as_f64() { let v64: u64 = v as _; - if (v64 as f64) != v || v > MAX_SAFE_INTEGER { + // The exclusive upper bound rounds up to 2^64 as f64. + if (v64 as f64) != v || !(0.0..u64::MAX as f64).contains(&v) { Err("Number is not an uint".to_string()) } else { Ok(BigUint::from(v64).to_bytes_be()) @@ -329,9 +331,14 @@ fn encode_value(typ: &MemberType, value: &Value) -> Result, String> { .map_err(|e| e.to_string())? .to_signed_bytes_be()), Value::Number(n) => { - if let Some(v) = n.as_f64() { + if let Some(v) = n.as_i64() { + Ok(BigInt::from(v).to_signed_bytes_be()) + } else if let Some(v) = n.as_u64() { + Ok(BigInt::from(v).to_signed_bytes_be()) + } else if let Some(v) = n.as_f64() { let v64: i64 = v as _; - if (v64 as f64) != v || v.abs() > MAX_SAFE_INTEGER { + // The exclusive upper bound rounds up to 2^63 as f64. + if (v64 as f64) != v || !(i64::MIN as f64..i64::MAX as f64).contains(&v) { Err("Number is not an int".to_string()) } else { Ok(BigInt::from(v64).to_signed_bytes_be()) @@ -640,8 +647,10 @@ impl PairedBitBox { /// Signs an Ethereum EIP-712 typed message. It returns a 65 byte signature (R, S, and 1 byte /// recID). 27 is added to the recID to denote an uncompressed pubkey. /// If `use_antiklepto` is false, signing is deterministic and requires firmware >=9.26.0. - /// Integer fields accept JSON numbers only when finite, integral and within `±(2^53 - 1)`. - /// Use decimal strings for larger integers. + /// Integer literals in the `i64`/`u64` ranges are preserved exactly. Other JSON numbers use + /// floating-point decoding and must be integral after decoding and fit in `i64`/`u64` for + /// signed/unsigned fields respectively. This includes forms such as `128.0` and `1e18`. + /// Use decimal strings for larger integers or to avoid floating-point rounding. pub async fn eth_sign_typed_message( &self, chain_id: u64, @@ -1251,47 +1260,75 @@ mod tests { } #[test] - fn test_encode_value_safe_json_numbers() { + fn test_encode_value_json_numbers() { for (typ, number, expected) in [ ("uint64", "-0.0", "00"), ("uint64", "128.0", "80"), ("uint64", "1e3", "03e8"), + ("uint64", "1e18", "0de0b6b3a7640000"), ("uint64", "9007199254740991", "1fffffffffffff"), ("uint64", "9007199254740991.0", "1fffffffffffff"), + ("uint64", "9007199254740993", "20000000000001"), + ("uint64", "18446744073709551614", "fffffffffffffffe"), + ("uint64", "18446744073709551615", "ffffffffffffffff"), + ("uint64", "18446744073709549568.0", "fffffffffffff800"), ("int64", "128.0", "0080"), ("int64", "-1e3", "fc18"), + ("int64", "1e18", "0de0b6b3a7640000"), + ("int64", "-1e18", "f21f494c589c0000"), ("int64", "9007199254740991", "1fffffffffffff"), ("int64", "-9007199254740991", "e0000000000001"), ("int64", "-9007199254740991.0", "e0000000000001"), - // Match JavaScript's check of the decoded number, which is already rounded to 1. - ("int64", "1.0000000000000001", "01"), + ("int64", "9007199254740993", "20000000000001"), + ("int64", "-9007199254740993", "dfffffffffffff"), + ("int64", "9223372036854775807", "7fffffffffffffff"), + ("int64", "-9223372036854775808", "8000000000000000"), + ("int64", "-9223372036854775808.0", "8000000000000000"), + ("int64", "9223372036854774784.0", "7ffffffffffffc00"), + ("int128", "9223372036854775808", "008000000000000000"), + ("int128", "18446744073709551615", "00ffffffffffffffff"), ] { let typ = parse_type_no_err(typ, &HashMap::new()); let value: Value = serde_json::from_str(number).unwrap(); - assert_eq!(hex::encode(encode_value(&typ, &value).unwrap()), expected); + assert_eq!( + hex::encode(encode_value(&typ, &value).unwrap()), + expected, + "{number}" + ); } + } + #[test] + fn test_encode_value_rejects_invalid_json_numbers() { for typ in ["uint256", "int256"] { let typ = parse_type_no_err(typ, &HashMap::new()); for number in [ "0.5", "-0.5", - "9007199254740992", - "-9007199254740992", - "9007199254740993", - "9007199254740992.0", - "18446744073709551615", + "18446744073709551615.0", + "18446744073709551616", + "18446744073709551616.0", "1e20", + "-1e20", ] { let value: Value = serde_json::from_str(number).unwrap(); assert!(encode_value(&typ, &value).is_err(), "{number}"); } - for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { - assert!(encode_value(&typ, &Value::from(value)).is_err()); - } } let unsigned = parse_type_no_err("uint256", &HashMap::new()); - assert!(encode_value(&unsigned, &Value::from(-1)).is_err()); + for number in ["-1", "-1.0"] { + let value: Value = serde_json::from_str(number).unwrap(); + assert!(encode_value(&unsigned, &value).is_err(), "{number}"); + } + let signed = parse_type_no_err("int256", &HashMap::new()); + for number in [ + "9223372036854775807.0", + "9223372036854775808.0", + "-9223372036854777856.0", + ] { + let value: Value = serde_json::from_str(number).unwrap(); + assert!(encode_value(&signed, &value).is_err(), "{number}"); + } } #[test]