From 3447385c1c3b25912fccae657d356c6cf0189a53 Mon Sep 17 00:00:00 2001 From: Amartya Singh Date: Fri, 6 Nov 2026 20:48:58 +0530 Subject: [PATCH 1/2] fix(sv1_api): reject numeric ntime and nonce wider than 32 bits Submit accepted ntime and nonce as JSON numbers and narrowed them with as u32, so an ntime of 4294967296 parsed as 0 and the share was checked against a time the miner never sent. Convert them with u32::try_from and reject values that do not fit, keeping ImpossibleToParseAsU64 for numbers that are not unsigned integers at all. --- sv1/src/methods/client_to_server.rs | 73 ++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/sv1/src/methods/client_to_server.rs b/sv1/src/methods/client_to_server.rs index ac98b9136b..c1df59076e 100644 --- a/sv1/src/methods/client_to_server.rs +++ b/sv1/src/methods/client_to_server.rs @@ -278,6 +278,19 @@ impl From for Message { } } +/// Reads a `mining.submit` `ntime` or `nonce` sent as a JSON number. +/// +/// Both are 32-bit fields, so a value that does not fit in a `u32` is rejected instead of being +/// truncated to its low bits. +fn numeric_u32(value: &serde_json::Number) -> Result { + let value_u64 = value + .as_u64() + .ok_or_else(|| ParsingMethodError::ImpossibleToParseAsU64(Box::new(value.clone())))?; + u32::try_from(value_u64) + .map(HexU32Be) + .map_err(|_| ParsingMethodError::unexpected_value_from_value(JNumber(value.clone()))) +} + impl TryFrom for Submit { type Error = ParsingMethodError; @@ -291,12 +304,8 @@ impl TryFrom for Submit { a.into(), b.into(), Extranonce::try_from(hex_decode(c)?)?, - HexU32Be(d.as_u64().ok_or_else(|| { - ParsingMethodError::ImpossibleToParseAsU64(Box::new(d.clone())) - })? as u32), - HexU32Be(e.as_u64().ok_or_else(|| { - ParsingMethodError::ImpossibleToParseAsU64(Box::new(e.clone())) - })? as u32), + numeric_u32(d)?, + numeric_u32(e)?, Some((f.as_str()).try_into()?), ), [JString(a), JString(b), JString(c), JString(d), JString(e), JString(f)] => ( @@ -311,12 +320,8 @@ impl TryFrom for Submit { a.into(), b.into(), Extranonce::try_from(hex_decode(c)?)?, - HexU32Be(d.as_u64().ok_or_else(|| { - ParsingMethodError::ImpossibleToParseAsU64(Box::new(d.clone())) - })? as u32), - HexU32Be(e.as_u64().ok_or_else(|| { - ParsingMethodError::ImpossibleToParseAsU64(Box::new(e.clone())) - })? as u32), + numeric_u32(d)?, + numeric_u32(e)?, None, ), [JString(a), JString(b), JString(c), JString(d), JString(e)] => ( @@ -1013,6 +1018,50 @@ fn test_submit_float_nonce_does_not_panic() { ); } +#[test] +fn submit_rejects_numeric_ntime_and_nonce_wider_than_32_bits() { + let submit = |time: Value, nonce: Value, version_bits: Option<&str>| { + let mut params = vec![ + serde_json::json!("user"), + serde_json::json!("1"), + serde_json::json!("ab"), + time, + nonce, + ]; + params.extend(version_bits.map(Value::from)); + Submit::try_from(StandardRequest { + id: 1, + method: "mining.submit".to_string(), + params: params.into(), + }) + }; + + for version_bits in [None, Some("00002000")] { + let max = submit( + serde_json::json!(u32::MAX), + serde_json::json!(u32::MAX), + version_bits, + ) + .unwrap(); + assert_eq!( + (max.time, max.nonce), + (HexU32Be(u32::MAX), HexU32Be(u32::MAX)) + ); + + // 2^32 used to be truncated to 0 + let too_wide = serde_json::json!(1_u64 << 32); + for (time, nonce) in [ + (too_wide.clone(), serde_json::json!(0)), + (serde_json::json!(0), too_wide.clone()), + ] { + assert!(matches!( + submit(time, nonce, version_bits), + Err(ParsingMethodError::UnexpectedValue(_)) + )); + } + } +} + #[test] fn test_configure_negative_min_bit_count_does_not_panic() { // The mining.configure handler also panics: when min-bit-count is a From 1cff6ae0776da920e5dbbaad623b4885c1d94618 Mon Sep 17 00:00:00 2001 From: Amartya Singh Date: Fri, 6 Nov 2026 20:49:03 +0530 Subject: [PATCH 2/2] chore: bump sv1_api to 7.0.1 Only mining.submit parsing changes, so sv1_api takes a patch bump. --- Cargo.lock | 2 +- fuzz/Cargo.lock | 2 +- sv1/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58a34da8dd..17df68abb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1203,7 +1203,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sv1_api" -version = "7.0.0" +version = "7.0.1" dependencies = [ "binary_sv2", "bitcoin", diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 79d118bb53..3840f93d77 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -559,7 +559,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sv1_api" -version = "7.0.0" +version = "7.0.1" dependencies = [ "binary_sv2", "bitcoin", diff --git a/sv1/Cargo.toml b/sv1/Cargo.toml index 33b1b3233c..a91e0c7435 100644 --- a/sv1/Cargo.toml +++ b/sv1/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sv1_api" -version = "7.0.0" +version = "7.0.1" authors = ["The Stratum V2 Developers"] edition = "2021" readme = "README.md"