-
Notifications
You must be signed in to change notification settings - Fork 3
fix(types)!: reject malformed SignedOrder JSON at deserialize time #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prestwich
wants to merge
2
commits into
main
Choose a base branch
from
prestwich/eng-2288
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| //! Proptests covering `signet-types` deserialization paths that consume | ||
| //! untrusted JSON. | ||
| //! | ||
| //! Regression coverage for ENG-2288 (`SignedOrder::order_hash` panic on | ||
| //! malformed signatures) and a forward-looking guarantee that no | ||
| //! `serde::Deserialize` impl on a SDK-exposed type panics on arbitrary | ||
| //! input — every malformed payload must surface as a `serde_json::Error`, | ||
| //! never an unwinding panic. | ||
| use alloy::primitives::Bytes; | ||
| use proptest::prelude::*; | ||
| use serde::Deserialize; | ||
| use signet_types::SignedOrder; | ||
| use signet_zenith::serde_helpers::{deserialize_non_empty_vec, deserialize_signature_bytes}; | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| struct SigWrap { | ||
| #[serde(deserialize_with = "deserialize_signature_bytes")] | ||
| signature: Bytes, | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| struct VecWrap { | ||
| #[serde(deserialize_with = "deserialize_non_empty_vec")] | ||
| items: Vec<u64>, | ||
| } | ||
|
|
||
| fn hex_string(bytes: &[u8]) -> String { | ||
| let mut s = String::with_capacity(2 + bytes.len() * 2); | ||
| s.push_str("0x"); | ||
| for b in bytes { | ||
| use std::fmt::Write; | ||
| write!(&mut s, "{b:02x}").unwrap(); | ||
| } | ||
| s | ||
| } | ||
|
|
||
| fn signed_order_json(sig_bytes: &[u8], permitted_count: usize, outputs_count: usize) -> String { | ||
| let permitted: Vec<String> = (0..permitted_count) | ||
| .map(|i| format!(r#"{{"token":"0x{:040x}","amount":"0x{i:x}"}}"#, i as u128)) | ||
| .collect(); | ||
| let outputs: Vec<String> = (0..outputs_count) | ||
| .map(|i| { | ||
| format!( | ||
| r#"{{"token":"0x{:040x}","amount":"0x{i:x}","recipient":"0x{:040x}","chainId":{i}}}"#, | ||
| i as u128, i as u128 | ||
| ) | ||
| }) | ||
| .collect(); | ||
| format!( | ||
| r#"{{ | ||
| "permit": {{ | ||
| "permitted": [{}], | ||
| "nonce": "0x0", | ||
| "deadline": "0xffffffffffffffff" | ||
| }}, | ||
| "owner": "0x0000000000000000000000000000000000000000", | ||
| "signature": "{}", | ||
| "outputs": [{}] | ||
| }}"#, | ||
| permitted.join(","), | ||
| hex_string(sig_bytes), | ||
| outputs.join(",") | ||
| ) | ||
| } | ||
|
|
||
| proptest! { | ||
| /// `deserialize_signature_bytes` accepts iff the decoded byte string | ||
| /// is exactly 65 bytes. | ||
| #[test] | ||
| fn signature_helper_accepts_only_65_bytes(bytes in prop::collection::vec(any::<u8>(), 0..200)) { | ||
| let json = format!(r#"{{"signature":"{}"}}"#, hex_string(&bytes)); | ||
| match serde_json::from_str::<SigWrap>(&json) { | ||
| Ok(w) => prop_assert_eq!(w.signature.len(), 65), | ||
| Err(_) => prop_assert_ne!(bytes.len(), 65), | ||
| } | ||
| } | ||
|
|
||
| /// `deserialize_non_empty_vec` accepts iff the decoded vector is | ||
| /// non-empty. | ||
| #[test] | ||
| fn non_empty_vec_helper_rejects_empty(items in prop::collection::vec(any::<u64>(), 0..16)) { | ||
| let json = format!( | ||
| r#"{{"items":[{}]}}"#, | ||
| items.iter().map(u64::to_string).collect::<Vec<_>>().join(",") | ||
| ); | ||
| match serde_json::from_str::<VecWrap>(&json) { | ||
| Ok(w) => prop_assert!(!w.items.is_empty()), | ||
| Err(_) => prop_assert!(items.is_empty()), | ||
| } | ||
| } | ||
|
|
||
| /// `SignedOrder` deserialization is total: any combination of | ||
| /// signature length, permitted count, and outputs count either | ||
| /// produces an `Ok` value satisfying all three structural | ||
| /// invariants, or an `Err`. It never panics. | ||
| #[test] | ||
| fn signed_order_deserialize_total( | ||
| sig_bytes in prop::collection::vec(any::<u8>(), 0..130), | ||
| permitted_count in 0usize..6, | ||
| outputs_count in 0usize..6, | ||
| ) { | ||
| let json = signed_order_json(&sig_bytes, permitted_count, outputs_count); | ||
| match serde_json::from_str::<SignedOrder>(&json) { | ||
| Ok(order) => { | ||
| prop_assert_eq!(sig_bytes.len(), 65); | ||
| prop_assert!(permitted_count > 0); | ||
| prop_assert!(outputs_count > 0); | ||
| prop_assert_eq!(order.permit().signature.len(), 65); | ||
| prop_assert!(!order.permit().permit.permitted.is_empty()); | ||
| prop_assert!(!order.outputs().is_empty()); | ||
| // order_hash() must not panic on any value the | ||
| // Deserialize impl admits. | ||
| let _ = order.order_hash(); | ||
| } | ||
| Err(_) => { | ||
| prop_assert!( | ||
| sig_bytes.len() != 65 || permitted_count == 0 || outputs_count == 0 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Well-formed `SignedOrder` JSON survives a serde round-trip with a | ||
| /// stable `order_hash`. | ||
| #[test] | ||
| fn signed_order_roundtrip( | ||
| permitted_count in 1usize..6, | ||
| outputs_count in 1usize..6, | ||
| ) { | ||
| let sig = vec![0u8; 65]; | ||
| let json = signed_order_json(&sig, permitted_count, outputs_count); | ||
| let order: SignedOrder = serde_json::from_str(&json).unwrap(); | ||
| let hash = *order.order_hash(); | ||
| let reserialized = serde_json::to_string(&order).unwrap(); | ||
| let decoded: SignedOrder = serde_json::from_str(&reserialized).unwrap(); | ||
| prop_assert_eq!(decoded.order_hash(), &hash); | ||
| } | ||
| } | ||
|
|
||
| // Sanity check that arbitrary garbage JSON doesn't panic the | ||
| // deserializer — complements the structured proptests above by covering | ||
| // completely malformed shapes. | ||
| proptest! { | ||
| #[test] | ||
| fn arbitrary_string_never_panics(s in ".{0,256}") { | ||
| let _ = serde_json::from_str::<SignedOrder>(&s); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude has spotted that this isn't really true. The following snippet probably explains the edge case best:
We probably want to tighten
check_aggregateto fail for this case (and add a regression test)?