Skip to content

fix(types): conform JSON-RPC parsing to spec by properly trimming whitespace and newlines #30

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

Merged
merged 5 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions src/types/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ impl TryFrom<Bytes> for InboundData {
}
debug!("Parsing inbound data");

// Special-case a single request, rejecting invalid JSON.
if bytes.starts_with(b"{") {
let rv: &RawValue = serde_json::from_slice(bytes.as_ref())?;

// First, check if it's a single request
let rv: &RawValue = serde_json::from_slice(bytes.as_ref())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does mean batch requests are doing 2 linear passes, once for this from_slice and once for the from_str

we should be able to do that all in one pass

what if we did something like

enum DeserHelper<'a> {
    Batch(Vec<&'a RawValue>),
    Single(&'a RawValue)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this doesn't work because of serde-rs/serde#1183 but that's fine, we can use the deserializer directly

if rv.get().starts_with("{") {
let range = find_range!(bytes, rv.get());

return Ok(Self {
Expand All @@ -74,7 +73,7 @@ impl TryFrom<Bytes> for InboundData {
}

// Otherwise, parse the batch
let DeserHelper(reqs) = serde_json::from_slice(bytes.as_ref())?;
let DeserHelper(reqs) = serde_json::from_str(rv.get())?;
let reqs = reqs
.into_iter()
.map(|raw| find_range!(bytes, raw.get()))
Expand All @@ -90,3 +89,48 @@ impl TryFrom<Bytes> for InboundData {

#[derive(Debug, Deserialize)]
struct DeserHelper<'a>(#[serde(borrow)] Vec<&'a RawValue>);

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_deser_batch() {
let batch = r#"[
{"id": 1, "method": "foo", "params": [1, 2, 3]},
{"id": 2, "method": "bar", "params": [4, 5, 6]}
]"#;

let bytes = Bytes::from(batch);
let batch = InboundData::try_from(bytes).unwrap();

assert_eq!(batch.len(), 2);
assert!(!batch.single());
}

#[test]
fn test_deser_single() {
let single = r#"{"id": 1, "method": "foo", "params": [1, 2, 3]}"#;

let bytes = Bytes::from(single);
let batch = InboundData::try_from(bytes).unwrap();

assert_eq!(batch.len(), 1);
assert!(batch.single());
}

#[test]
fn test_deser_single_with_whitespace() {
let single = r#"

{"id": 1, "method": "foo", "params": [1, 2, 3]}

"#;

let bytes = Bytes::from(single);
let batch = InboundData::try_from(bytes).unwrap();

assert_eq!(batch.len(), 1);
assert!(batch.single());
}
}
25 changes: 25 additions & 0 deletions src/types/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl Request {

#[cfg(test)]
mod test {

use crate::types::METHOD_LEN_LIMIT;

use super::*;
Expand Down Expand Up @@ -236,4 +237,28 @@ mod test {

assert_eq!(size, METHOD_LEN_LIMIT + 1);
}

#[test]
fn test_with_linebreak() {
let bytes = Bytes::from_static(
r#"

{ "id": 1,
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": ["0x4444d38c385d0969C64c4C8f996D7536d16c28B9", "latest"]
}

"#
.as_bytes(),
);
let req = Request::try_from(bytes).unwrap();

assert_eq!(req.id(), Some("1"));
assert_eq!(req.method(), r#"eth_getBalance"#);
assert_eq!(
req.params(),
r#"["0x4444d38c385d0969C64c4C8f996D7536d16c28B9", "latest"]"#
);
}
}