Skip to content

Commit 120738c

Browse files
authored
Fix JSON-RPC request parsing errors being ignored (#2138)
* Fix JSON-RPC request parsing errors being ignored * PR link * Fix test * Fix whitespace * Fix behavior for notifications
1 parent 9446ff2 commit 120738c

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

light-base/src/json_rpc_service/background.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -704,16 +704,28 @@ pub(super) async fn run<TPlat: PlatformRef>(
704704

705705
WakeUpReason::IncomingJsonRpcRequest(request_json) => {
706706
// New JSON-RPC request pulled from the channel.
707-
let Ok((request_id_json, request_parsed)) =
708-
methods::parse_jsonrpc_client_to_server(&request_json)
709-
else {
710-
// Request has failed to parse. Immediately return an answer.
711-
let _ = me
712-
.responses_tx
713-
.send(parse::build_parse_error_response())
714-
.await;
715-
continue;
716-
};
707+
let (request_id_json, request_parsed) =
708+
match methods::parse_jsonrpc_client_to_server(&request_json) {
709+
Ok(r) => r,
710+
Err(methods::ParseClientToServerError::JsonRpcParse(_)) => {
711+
// Request has failed to parse. Immediately return an answer.
712+
let _ = me
713+
.responses_tx
714+
.send(parse::build_parse_error_response())
715+
.await;
716+
continue;
717+
}
718+
Err(methods::ParseClientToServerError::Method { request_id, error }) => {
719+
// Invalid method or parameters. Immediately return an answer.
720+
let _ = me.responses_tx.send(error.to_json_error(request_id)).await;
721+
continue;
722+
}
723+
Err(methods::ParseClientToServerError::UnknownNotification { .. }) => {
724+
// Invalid notification-style request. As per spec, we simply
725+
// ignore them.
726+
continue;
727+
}
728+
};
717729

718730
// Print a warning for legacy JSON-RPC API functions.
719731
match request_parsed {

wasm-node/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixed
6+
7+
- A "parse error" JSON-RPC response is no longer erroneously sent back in case of an unrecognized JSON-RPC function name or wrong parameter types. ([#2138](https://github.com/smol-dot/smoldot/pull/2138))
8+
59
## 2.0.35 - 2025-05-27
610

711
### Changed

wasm-node/javascript/test/misc.mjs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,33 @@ test('malformed JSON-RPC request generates an error', async t => {
4040
.then(() => client.terminate());
4141
});
4242

43+
test('invalid JSON-RPC method generates an error', async t => {
44+
const client = start({ logCallback: () => { } });
45+
await client
46+
.addChain({ chainSpec: westendSpec })
47+
.then((chain) => {
48+
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"this method doesnt exist","params":[]}');
49+
return chain;
50+
})
51+
.then(async (chain) => {
52+
const response = await chain.nextJsonRpcResponse();
53+
const parsed = JSON.parse(response);
54+
if (parsed.id === 1 && parsed.error)
55+
t.pass();
56+
else
57+
t.fail(response);
58+
})
59+
.then(() => client.terminate());
60+
});
61+
4362
test('disableJsonRpc option forbids sendJsonRpc', async t => {
4463
const client = start({ logCallback: () => { } });
4564
await client
4665
.addChain({ chainSpec: westendSpec, disableJsonRpc: true })
4766
.then((chain) => {
4867
try {
4968
chain.sendJsonRpc('{"jsonrpc":"2.0","id":1,"method":"system_name","params":[]}');
50-
} catch(error) {
69+
} catch (error) {
5170
t.assert(error instanceof JsonRpcDisabledError);
5271
t.pass();
5372
}
@@ -62,7 +81,7 @@ test('disableJsonRpc option forbids nextJsonRpcResponse', async t => {
6281
.then(async (chain) => {
6382
try {
6483
await chain.nextJsonRpcResponse();
65-
} catch(error) {
84+
} catch (error) {
6685
t.assert(error instanceof JsonRpcDisabledError);
6786
t.pass();
6887
}

0 commit comments

Comments
 (0)