rpc: handle malformed jsonrpc 2.0 requests correctly

bitcoin/bitcoin: PR #36758

4 suggestions

Optional improvements

Link

[suggestion] · The revised HTTP-status row omits malformed whitelist batches

The revised row omits a pre-existing exception: malformed batches from authenticated -rpcwhitelist users can still return HTTP 500/400; no in-tree client producing these batches was found.

Location: doc/JSON-RPC-interface.md:92 (head).

Details and suggested fix

For example, [{"jsonrpc":"2.0","id":1,"method":1}] reaches the batch authorization pre-check, where get_str() throws before any element is parsed. The outer catch passes the untouched batch-level jreq to JSONErrorReply, so its default legacy version selects HTTP 500 with a single legacy-shaped error instead of per-element replies. A missing method takes the same path; a non-object element instead produces HTTP 400. The pre-check aborts the whole batch before executing any elements. Qualify this row to retain the whitelist parsing exception rather than implying that these valid-JSON batches receive HTTP 200.


Link

[suggestion] · Valid-notification-only batches lose their HTTP 204 coverage

The updated malformed-element fixture always expects a reply, leaving no test that checks HTTP 204 with no body for a non-empty batch of valid notifications.

Location: test/functional/interface_rpc.py:155 (head).

Details and suggested fix

The fourth element now correctly produces an Invalid Request reply, so this invocation takes the helper's HTTP 200 assertion. The remaining batch cases also expect replies, except the empty batch, which expects HTTP 200 with []. Singleton notification assertions do not exercise the batch-only empty-reply branch. Keep this new case and add self.test_batch_request(lambda idx: None if idx == 4 else BatchOptions(version=2, notification=True)). That reaches the existing 204/no-body assertions and retains invalidmethod as a valid notification whose execution error must be suppressed. Otherwise, changing only the batch branch to return HTTP 200 with [] would leave these tests passing.


Link

[suggestion] · The additional batch-reply change lacks a compatibility note

Beyond the singleton fix described in the PR, malformed id-less JSON-RPC 2.0 batch elements now produce error replies, potentially changing HTTP 204 to 200; no release note explains these client-visible transitions.

Location: doc/JSON-RPC-interface.md:93 (head).

Details and suggested fix

Previously, an element such as {"jsonrpc":"2.0","pizza":"sausage"} acquired the V2 version and absent id before parsing failed, so IsNotification() caused the batch loop to drop its error. The new successful-parse flag correctly retains that error with "id": null. A batch containing only such elements, or mixing them with valid notifications, therefore changes from HTTP 204 with no body to HTTP 200 with an error array. Update the PR description's statement that malformed batches were already handled correctly, and add a short release note covering this transition alongside the malformed-singleton change from HTTP 400 to 200 in release builds. The new word valid in this table states the rule but does not explain the changed responses.


Link

[suggestion] · The notification definition is reconciled one commit after the reply change

Commit 5004f241 replies to malformed id-less V2 singletons while still classifying them as notifications and suppressing their batch errors; f5ed688f resolves this intermediate inconsistency, but reviewing the first fix requires both commits.

Location: src/rpc/request.cpp:71 (head).

Details and suggested fix

Keep d847ecd6b3bf1799bbeae5973b25d341d3e4d7f6 as the reproducer, and consider squashing f5ed688fa1379c3703764538a27d66266e758968 into 5004f241c58113fbc29ddd2dbda3af0040827e90. The latter adds the known-V2 HTTP 200 error path, id: null fallback, and singleton expectations while retaining the old IsNotification() predicate. At that point malformed id-less requests receive singleton replies despite the new comment saying notification replies are never sent, and their batch errors remain suppressed. The final commit introduces the successful-parse notification flag and changes the batch expectation, resolving the mismatch at head. Combining the fixes would let the notification definition and both response paths be reviewed together. Moving only the flag and batch test earlier would not suffice because the new batch expectation needs the id: null fallback. If the separate boundaries are intentional, a note in 5004f241 explaining the temporary mismatch would also reduce this review burden.


Coverage details