From dcc74aa118e3f2c7615cf48788cc0934707544e4 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:28:40 -0600 Subject: [PATCH 1/6] feat(id): add signed package-pinned game posts (#5525) --- pkgs/id/src/boards.rs | 206 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 182 insertions(+), 24 deletions(-) diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index 1454eec0c8..d51bc46598 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -16,8 +16,8 @@ //! or claims global erasure. It also adds signed, portable themes a board applies by reference //! ([`Board::theme_ref`]), and [`BoardGraph::compose_view`] projects the //! three-layer view (personal filter / board view / host view) under the -//! transparency rule: hidden is never erased. It does not render markup, -//! serve content, or fetch attachment/game payloads. +//! transparency rule: hidden is never erased. It does not render markup, serve +//! content, execute game packages, replay turns, or fetch attachment/game payloads. use std::collections::{BTreeMap, BTreeSet}; @@ -37,6 +37,10 @@ pub const MAX_BOARD_REFERENCES: usize = 128; pub const MAX_THEME_TOKENS: usize = 64; /// Largest design-token value accepted in a theme. pub const MAX_THEME_TOKEN_VALUE_BYTES: usize = 512; +/// Largest number of assigned participants in one game post. +pub const MAX_GAME_PARTICIPANTS: usize = 32; +/// Largest serialized configuration or genesis state accepted by a game post. +pub const MAX_GAME_DATA_BYTES: usize = 16 * 1024; /// A canonical lower-case SHA-256 artifact/content hash. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] @@ -74,15 +78,63 @@ impl<'de> Deserialize<'de> for ArtifactHash { } /// Typed reference to a signed game-post object; game execution is out of scope. -/// In v1, the target must be a same-board `BoardObject::Post`. If a dedicated -/// game-post object kind is added, update the graph's kind check with it. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct GamePostRef { - /// Hash of the immutable signed object represented in the board graph. + /// Hash of the immutable `BoardObject::GamePost` in the board graph. pub object: ArtifactHash, } +/// Immutable package identity pinned by a game post. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GamePackageRef { + /// SHA-256 hash of the package bytes. A new package build has a new hash. + pub hash: ArtifactHash, + /// Human-facing package version associated with the pinned bytes. + pub version: String, +} + +/// Play mode supported by playable posts in v1. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum GameMode { + /// Players submit signed turns asynchronously against a pinned frontier. + AsyncTurns, +} + +/// One author assigned to a package-defined game role. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GameParticipant { + /// Canonical Ed25519 public key of the assigned author. + pub author: String, + /// Canonical role identifier understood by the pinned game package. + pub role: String, +} + +/// Signed metadata for a package-pinned playable post. +/// +/// This object records the immutable package reference, package-specific +/// configuration and initial state, and the authors assigned to package-defined +/// roles. It is data, not executable code; accepting it does not run the package. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GamePost { + /// Board root hash. + pub board: ArtifactHash, + /// Immutable package bytes and their human-facing version. + pub package: GamePackageRef, + /// The v1 play mode. + pub mode: GameMode, + /// Package-defined configuration, carried as data. + pub configuration: serde_json::Value, + /// Authors assigned to package-defined roles. + pub participants: Vec, + /// Package-specific initial state, carried as data. + pub genesis: serde_json::Value, +} + /// Stable identity root for one public board. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -362,6 +414,8 @@ pub enum BoardObject { Tombstone(Tombstone), /// Signed portable theme. Theme(Theme), + /// Package-pinned playable game post metadata. + GamePost(GamePost), /// Signed opt-in host listing / withdrawal. BoardListing(BoardListing), } @@ -382,6 +436,7 @@ impl BoardObject { Self::Supersede(_) => "supersede", Self::Tombstone(_) => "tombstone", Self::Theme(_) => "theme", + Self::GamePost(_) => "game_post", Self::BoardListing(_) => "board_listing", } } @@ -400,6 +455,7 @@ impl BoardObject { Self::Supersede(value) => Some(&value.board), Self::Tombstone(value) => Some(&value.board), Self::BoardListing(value) => Some(&value.board), + Self::GamePost(value) => Some(&value.board), } } @@ -487,6 +543,33 @@ impl BoardObject { "duplicate theme asset reference" ); } + Self::GamePost(value) => { + validate_text("game package version", &value.package.version, false)?; + ensure!( + value.package.version.len() <= 128, + "game package version is at most 128 bytes" + ); + ensure!( + !value.participants.is_empty() + && value.participants.len() <= MAX_GAME_PARTICIPANTS, + "a game post assigns 1 to {MAX_GAME_PARTICIPANTS} participants" + ); + let mut participant_roles = BTreeSet::new(); + for participant in &value.participants { + ensure!( + is_public_key(&participant.author), + "game participant is not a canonical Ed25519 key" + ); + validate_token_name(&participant.role)?; + ensure!( + participant_roles + .insert((participant.author.as_str(), participant.role.as_str())), + "duplicate game participant role" + ); + } + validate_game_data("game configuration", &value.configuration)?; + validate_game_data("game genesis state", &value.genesis)?; + } Self::BoardListing(value) => { ensure!( is_public_key(&value.host), @@ -649,9 +732,12 @@ impl BoardGraph { self.require_scoped(attachment, &value.board, "attachment")?; } if let Some(embed_ref) = &value.embed_ref { - self.require_scoped(&embed_ref.object, &value.board, "post")?; + self.require_scoped(&embed_ref.object, &value.board, "game_post")?; } } + BoardObject::GamePost(value) => { + self.require_board(&value.board)?; + } BoardObject::Attachment(value) => { self.require_board(&value.board)?; } @@ -1558,6 +1644,14 @@ fn validate_text(name: &str, value: &str, allow_empty: bool) -> Result<()> { Ok(()) } +fn validate_game_data(name: &str, value: &serde_json::Value) -> Result<()> { + ensure!( + serde_json::to_vec(value)?.len() <= MAX_GAME_DATA_BYTES, + "{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes" + ); + Ok(()) +} + fn validate_token_name(value: &str) -> Result<()> { ensure!( (1..=64).contains(&value.len()) @@ -1659,6 +1753,39 @@ mod tests { .unwrap() } + fn game_post_object( + author: &SigningKey, + opponent: &SigningKey, + board: ArtifactHash, + ) -> SignedBoardObject { + SignedBoardObject::sign( + author, + 1, + "", + BoardObject::GamePost(GamePost { + board, + package: GamePackageRef { + hash: ArtifactHash::new("e".repeat(64)).unwrap(), + version: "0.1.0".to_owned(), + }, + mode: GameMode::AsyncTurns, + configuration: serde_json::json!({"board_size": 3}), + participants: vec![ + GameParticipant { + author: hex_encode(&author.verifying_key().to_bytes()), + role: "x".to_owned(), + }, + GameParticipant { + author: hex_encode(&opponent.verifying_key().to_bytes()), + role: "o".to_owned(), + }, + ], + genesis: serde_json::json!("---------"), + }), + ) + .unwrap() + } + fn theme(key: &SigningKey, seq: u64, forked_from: Option) -> SignedBoardObject { let mut tokens = BTreeMap::new(); tokens.insert("accent".to_owned(), "#cc5500".to_owned()); @@ -1785,7 +1912,7 @@ mod tests { } #[test] - fn post_embed_ref_must_name_a_same_board_post() { + fn signed_game_post_is_verified_and_post_embed_ref_is_board_scoped() { let creator = key(6); let other = key(7); let mut graph = BoardGraph::default(); @@ -1794,6 +1921,15 @@ mod tests { .insert(thread(&creator, root_hash.clone(), "main")) .unwrap(); + let signed_game = game_post_object(&creator, &other, root_hash.clone()); + signed_game.verify().unwrap(); + let decoded = SignedBoardObject::from_artifact(signed_game.artifact.clone()).unwrap(); + assert_eq!(decoded, signed_game); + + let mut tampered = signed_game.artifact.clone(); + tampered.body["data"]["package"]["version"] = serde_json::Value::String("9.9.9".to_owned()); + assert!(SignedBoardObject::from_artifact(tampered).is_err()); + let missing = ArtifactHash::new("a".repeat(64)).unwrap(); assert!( graph @@ -1819,12 +1955,29 @@ mod tests { .is_err() ); - let foreign_root = graph.insert(board(&other)).unwrap(); - let foreign_thread = graph - .insert(thread(&other, foreign_root.clone(), "foreign")) + let ordinary_post = graph + .insert(post( + &creator, + root_hash.clone(), + thread_hash.clone(), + "not a game post", + )) .unwrap(); - let foreign_post = graph - .insert(post(&other, foreign_root, foreign_thread, "foreign target")) + assert!( + graph + .insert(post_with_embed_ref( + &creator, + root_hash.clone(), + thread_hash.clone(), + ordinary_post, + "wrong object type", + )) + .is_err() + ); + + let foreign_root = graph.insert(board(&other)).unwrap(); + let foreign_game = graph + .insert(game_post_object(&other, &creator, foreign_root.clone())) .unwrap(); assert!( graph @@ -1832,29 +1985,34 @@ mod tests { &creator, root_hash.clone(), thread_hash.clone(), - foreign_post, + foreign_game, "cross-board embed", )) .is_err() ); - let local_post = graph - .insert(post( - &creator, - root_hash.clone(), - thread_hash.clone(), - "local target", - )) - .unwrap(); - graph + let local_game = graph.insert(signed_game).unwrap(); + let embedded_post = graph .insert(post_with_embed_ref( &creator, - root_hash, + root_hash.clone(), thread_hash, - local_post, + local_game.clone(), "valid embed", )) .unwrap(); + + let mut replica = BoardGraph::default(); + let report = replica.replicate_board_from(&graph, &root_hash).unwrap(); + assert_eq!(report.status.state, ReplicaState::Complete); + assert!(replica.get(&local_game).is_some()); + assert!( + replica + .compose_view(&root_hash, &ViewLayers::default()) + .unwrap() + .objects + .contains(&embedded_post) + ); } #[test] From eeefda5dbd5673902655fa00c7893ed0b28dc962 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:50:33 -0600 Subject: [PATCH 2/6] test(id): property-check signed game post validation (#5525) --- pkgs/id/src/boards.rs | 175 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 156 insertions(+), 19 deletions(-) diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index d51bc46598..58883eb26c 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -1686,6 +1686,7 @@ fn is_public_key(value: &str) -> bool { #[cfg(test)] mod tests { use ed25519_dalek::SigningKey; + use proptest::prelude::*; use super::*; use crate::world::hex_encode; @@ -1753,39 +1754,75 @@ mod tests { .unwrap() } + fn game_post_body( + board: ArtifactHash, + version: String, + configuration: serde_json::Value, + participants: Vec, + genesis: serde_json::Value, + ) -> GamePost { + GamePost { + board, + package: GamePackageRef { + hash: ArtifactHash::new("e".repeat(64)).unwrap(), + version, + }, + mode: GameMode::AsyncTurns, + configuration, + participants, + genesis, + } + } + fn game_post_object( author: &SigningKey, opponent: &SigningKey, board: ArtifactHash, ) -> SignedBoardObject { + let participants = vec![ + GameParticipant { + author: hex_encode(&author.verifying_key().to_bytes()), + role: "x".to_owned(), + }, + GameParticipant { + author: hex_encode(&opponent.verifying_key().to_bytes()), + role: "o".to_owned(), + }, + ]; SignedBoardObject::sign( author, 1, "", - BoardObject::GamePost(GamePost { + BoardObject::GamePost(game_post_body( board, - package: GamePackageRef { - hash: ArtifactHash::new("e".repeat(64)).unwrap(), - version: "0.1.0".to_owned(), - }, - mode: GameMode::AsyncTurns, - configuration: serde_json::json!({"board_size": 3}), - participants: vec![ - GameParticipant { - author: hex_encode(&author.verifying_key().to_bytes()), - role: "x".to_owned(), - }, - GameParticipant { - author: hex_encode(&opponent.verifying_key().to_bytes()), - role: "o".to_owned(), - }, - ], - genesis: serde_json::json!("---------"), - }), + "0.1.0".to_owned(), + serde_json::json!({"board_size": 3}), + participants, + serde_json::json!("---------"), + )), ) .unwrap() } + fn participant(index: usize) -> GameParticipant { + let author = key(u8::try_from(index).unwrap()); + GameParticipant { + author: hex_encode(&author.verifying_key().to_bytes()), + role: format!("role-{index}"), + } + } + + fn invalid_game_post_is_rejected_by_builder_and_receipt( + author: &SigningKey, + game: GamePost, + ) -> bool { + let typed = BoardObject::GamePost(game.clone()); + let raw = serde_json::to_value(&typed).unwrap(); + let artifact = SignedArtifact::sign(author, 1, "", BOARD_OBJECT_KIND, raw).unwrap(); + SignedBoardObject::sign(author, 1, "", typed).is_err() + && SignedBoardObject::from_artifact(artifact).is_err() + } + fn theme(key: &SigningKey, seq: u64, forked_from: Option) -> SignedBoardObject { let mut tokens = BTreeMap::new(); tokens.insert("accent".to_owned(), "#cc5500".to_owned()); @@ -3193,4 +3230,104 @@ mod tests { assert_eq!(graph.len(), before); assert!(HostServingPolicy::default().serves_board(&root)); } + + proptest! { + /// Oracle: every generated in-bound GamePost must pass the builder, + /// signature verification and strict typed decode with its body intact. + #[test] + fn generated_valid_game_posts_round_trip( + seed in any::(), + participant_count in 1usize..=MAX_GAME_PARTICIPANTS, + version_len in 1usize..=128, + configuration in proptest::collection::vec(any::(), 0..32), + genesis in proptest::collection::vec(any::(), 0..64), + ) { + let game = game_post_body( + ArtifactHash::new("d".repeat(64)).unwrap(), + "v".repeat(version_len), + serde_json::json!(configuration), + (0..participant_count).map(participant).collect(), + serde_json::json!(genesis), + ); + let signed = SignedBoardObject::sign( + &key(seed), + 1, + "", + BoardObject::GamePost(game), + ).unwrap(); + prop_assert!(signed.verify().is_ok()); + prop_assert_eq!( + SignedBoardObject::from_artifact(signed.artifact.clone()).unwrap(), + signed, + ); + } + + /// Oracle: generated invalid metadata variants are refused both by the + /// builder and by typed receipt of a valid SignedArtifact envelope. + #[test] + fn generated_invalid_game_post_variants_are_rejected( + seed in any::(), + invalid_variant in 0u8..8, + ) { + let author = key(seed); + let mut invalid = game_post_body( + ArtifactHash::new("d".repeat(64)).unwrap(), + "0.1.0".to_owned(), + serde_json::json!({"seed": seed}), + vec![participant(1), participant(2)], + serde_json::json!([seed]), + ); + match invalid_variant { + 0 => invalid.participants.clear(), + 1 => invalid.participants = (0..=MAX_GAME_PARTICIPANTS).map(participant).collect(), + 2 => { + let first = invalid.participants[0].clone(); + invalid.participants[1] = first; + } + 3 => invalid.participants[0].author = "not-an-ed25519-key".to_owned(), + 4 => invalid.participants[0].role = "Player One".to_owned(), + 5 => invalid.package.version = "v".repeat(129), + 6 => { + invalid.configuration = serde_json::Value::String( + "c".repeat(MAX_GAME_DATA_BYTES - 1), + ); + } + _ => { + invalid.genesis = serde_json::Value::String( + "g".repeat(MAX_GAME_DATA_BYTES - 1), + ); + } + } + prop_assert!(invalid_game_post_is_rejected_by_builder_and_receipt( + &author, + invalid, + )); + } + } + + #[test] + fn game_post_accepts_exact_limits_and_rejects_empty_version() { + let author = key(60); + let maximum = game_post_body( + ArtifactHash::new("d".repeat(64)).unwrap(), + "v".repeat(128), + serde_json::Value::String("c".repeat(MAX_GAME_DATA_BYTES - 2)), + (0..MAX_GAME_PARTICIPANTS).map(participant).collect(), + serde_json::Value::String("g".repeat(MAX_GAME_DATA_BYTES - 2)), + ); + let signed = + SignedBoardObject::sign(&author, 1, "", BoardObject::GamePost(maximum.clone())) + .unwrap(); + assert_eq!( + SignedBoardObject::from_artifact(signed.artifact.clone()).unwrap(), + signed + ); + + let mut empty_version = maximum; + empty_version.package.version.clear(); + assert!(invalid_game_post_is_rejected_by_builder_and_receipt( + &author, + empty_version, + )); + } } From c173e4c21becb4f4e49c47882975016c7d592445 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:57:28 -0600 Subject: [PATCH 3/6] fix(id): bound and clarify game metadata validation (#5525) --- pkgs/id/src/boards.rs | 72 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index 58883eb26c..39d2717e91 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -20,6 +20,7 @@ //! content, execute game packages, replay turns, or fetch attachment/game payloads. use std::collections::{BTreeMap, BTreeSet}; +use std::io::{self, Write}; use anyhow::{Context, Result, bail, ensure}; use ed25519_dalek::{SigningKey, VerifyingKey}; @@ -560,11 +561,11 @@ impl BoardObject { is_public_key(&participant.author), "game participant is not a canonical Ed25519 key" ); - validate_token_name(&participant.role)?; + validate_game_role(&participant.role)?; ensure!( participant_roles .insert((participant.author.as_str(), participant.role.as_str())), - "duplicate game participant role" + "duplicate game participant (author, role) assignment" ); } validate_game_data("game configuration", &value.configuration)?; @@ -1644,21 +1645,57 @@ fn validate_text(name: &str, value: &str, allow_empty: bool) -> Result<()> { Ok(()) } +struct SerializedSizeLimit { + limit: usize, + written: usize, + exceeded: bool, +} + +impl Write for SerializedSizeLimit { + fn write(&mut self, buffer: &[u8]) -> io::Result { + if buffer.len() > self.limit.saturating_sub(self.written) { + self.exceeded = true; + return Err(io::Error::other("serialized value exceeds its size limit")); + } + self.written += buffer.len(); + Ok(buffer.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + fn validate_game_data(name: &str, value: &serde_json::Value) -> Result<()> { - ensure!( - serde_json::to_vec(value)?.len() <= MAX_GAME_DATA_BYTES, - "{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes" - ); - Ok(()) + let mut writer = SerializedSizeLimit { + limit: MAX_GAME_DATA_BYTES, + written: 0, + exceeded: false, + }; + match serde_json::to_writer(&mut writer, value) { + Ok(()) => Ok(()), + Err(_) if writer.exceeded => { + bail!("{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes") + } + Err(error) => Err(error.into()), + } } fn validate_token_name(value: &str) -> Result<()> { + validate_ascii_identifier("theme token name", value, 64) +} + +fn validate_game_role(value: &str) -> Result<()> { + validate_ascii_identifier("game role identifier", value, 64) +} + +fn validate_ascii_identifier(name: &str, value: &str, max_bytes: usize) -> Result<()> { ensure!( - (1..=64).contains(&value.len()) + (1..=max_bytes).contains(&value.len()) && value.bytes().all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || b"-_.".contains(&byte)), - "theme token name must be 1-64 lower-case ASCII letters, digits, '-', '_' or '.'" + "{name} must be 1-{max_bytes} lower-case ASCII letters, digits, '-', '_' or '.'" ); Ok(()) } @@ -3323,6 +3360,23 @@ mod tests { signed ); + let mut duplicate = maximum.clone(); + duplicate.participants[1] = duplicate.participants[0].clone(); + let duplicate_error = + SignedBoardObject::sign(&author, 2, "", BoardObject::GamePost(duplicate)).unwrap_err(); + assert!( + duplicate_error + .to_string() + .contains("duplicate game participant (author, role) assignment") + ); + + let mut invalid_role = maximum.clone(); + invalid_role.participants[0].role = "Player One".to_owned(); + let role_error = + SignedBoardObject::sign(&author, 3, "", BoardObject::GamePost(invalid_role)) + .unwrap_err(); + assert!(role_error.to_string().contains("game role identifier")); + let mut empty_version = maximum; empty_version.package.version.clear(); assert!(invalid_game_post_is_rejected_by_builder_and_receipt( From a39ff058b687b12124165f944a465ef164fcc434 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 03:13:59 -0600 Subject: [PATCH 4/6] test(id): strengthen game post projection oracles (#5525) --- pkgs/id/src/boards.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index 39d2717e91..765a3462b9 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -2080,13 +2080,11 @@ mod tests { let report = replica.replicate_board_from(&graph, &root_hash).unwrap(); assert_eq!(report.status.state, ReplicaState::Complete); assert!(replica.get(&local_game).is_some()); - assert!( - replica - .compose_view(&root_hash, &ViewLayers::default()) - .unwrap() - .objects - .contains(&embedded_post) - ); + let view = replica + .compose_view(&root_hash, &ViewLayers::default()) + .unwrap(); + assert!(view.objects.contains(&local_game)); + assert!(view.objects.contains(&embedded_post)); } #[test] @@ -3290,9 +3288,10 @@ mod tests { &key(seed), 1, "", - BoardObject::GamePost(game), + BoardObject::GamePost(game.clone()), ).unwrap(); prop_assert!(signed.verify().is_ok()); + prop_assert_eq!(&signed.object, &BoardObject::GamePost(game)); prop_assert_eq!( SignedBoardObject::from_artifact(signed.artifact.clone()).unwrap(), signed, From 6c78f199b4a94ec9c3b61c1a95d472b965150955 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 03:22:42 -0600 Subject: [PATCH 5/6] fix(id): cap game metadata serialization buffer (#5525) --- pkgs/id/src/boards.rs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index 765a3462b9..dc1f77ece2 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -1645,20 +1645,17 @@ fn validate_text(name: &str, value: &str, allow_empty: bool) -> Result<()> { Ok(()) } -struct SerializedSizeLimit { - limit: usize, - written: usize, - exceeded: bool, +struct CappedBuffer { + bytes: Vec, } -impl Write for SerializedSizeLimit { +impl Write for CappedBuffer { fn write(&mut self, buffer: &[u8]) -> io::Result { - if buffer.len() > self.limit.saturating_sub(self.written) { - self.exceeded = true; - return Err(io::Error::other("serialized value exceeds its size limit")); - } - self.written += buffer.len(); - Ok(buffer.len()) + let limit = MAX_GAME_DATA_BYTES + 1; + let remaining = limit.saturating_sub(self.bytes.len()); + let written = buffer.len().min(remaining); + self.bytes.extend_from_slice(&buffer[..written]); + Ok(written) } fn flush(&mut self) -> io::Result<()> { @@ -1667,14 +1664,18 @@ impl Write for SerializedSizeLimit { } fn validate_game_data(name: &str, value: &serde_json::Value) -> Result<()> { - let mut writer = SerializedSizeLimit { - limit: MAX_GAME_DATA_BYTES, - written: 0, - exceeded: false, + let mut writer = CappedBuffer { + bytes: Vec::with_capacity(MAX_GAME_DATA_BYTES + 1), }; match serde_json::to_writer(&mut writer, value) { - Ok(()) => Ok(()), - Err(_) if writer.exceeded => { + Ok(()) => { + ensure!( + writer.bytes.len() <= MAX_GAME_DATA_BYTES, + "{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes" + ); + Ok(()) + } + Err(_) if writer.bytes.len() > MAX_GAME_DATA_BYTES => { bail!("{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes") } Err(error) => Err(error.into()), From b261017fd415ef470ee46c8605b7272552a11a64 Mon Sep 17 00:00:00 2001 From: Archer Date: Sat, 7 Nov 2026 22:09:44 -0600 Subject: [PATCH 6/6] wip: boards.rs signed-board slice1 (branch disposition 2026-11-07) --- pkgs/id/src/boards.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index dc1f77ece2..7c5473537d 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -1645,16 +1645,16 @@ fn validate_text(name: &str, value: &str, allow_empty: bool) -> Result<()> { Ok(()) } -struct CappedBuffer { - bytes: Vec, +struct SerializedSizeLimit { + limit: usize, + written: usize, } -impl Write for CappedBuffer { +impl Write for SerializedSizeLimit { fn write(&mut self, buffer: &[u8]) -> io::Result { - let limit = MAX_GAME_DATA_BYTES + 1; - let remaining = limit.saturating_sub(self.bytes.len()); + let remaining = self.limit.saturating_sub(self.written); let written = buffer.len().min(remaining); - self.bytes.extend_from_slice(&buffer[..written]); + self.written += written; Ok(written) } @@ -1664,18 +1664,13 @@ impl Write for CappedBuffer { } fn validate_game_data(name: &str, value: &serde_json::Value) -> Result<()> { - let mut writer = CappedBuffer { - bytes: Vec::with_capacity(MAX_GAME_DATA_BYTES + 1), + let mut writer = SerializedSizeLimit { + limit: MAX_GAME_DATA_BYTES, + written: 0, }; match serde_json::to_writer(&mut writer, value) { - Ok(()) => { - ensure!( - writer.bytes.len() <= MAX_GAME_DATA_BYTES, - "{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes" - ); - Ok(()) - } - Err(_) if writer.bytes.len() > MAX_GAME_DATA_BYTES => { + Ok(()) => Ok(()), + Err(_) if writer.written == MAX_GAME_DATA_BYTES => { bail!("{name} is at most {MAX_GAME_DATA_BYTES} serialized bytes") } Err(error) => Err(error.into()),