Repository navigation
cln-rpc: fix deserializing, avoid panics and reject overflows - #9767
Open
daywalker90 wants to merge 4 commits into
Open
daywalker90 wants to merge 4 commits into
daywalker90 wants to merge 4 commits into
Conversation
`Feerate::try_from(&str)` accumulated leading digits with unchecked
`acc * 10 + digit` arithmetic (panicking in debug builds and silently
wrapping in release builds on values above `u32::MAX`) and only checked
`ends_with("perkw")` / `ends_with("perkb")` without validating the full
prefix. As a result, malformed inputs such as `"perkw"`, `"-500perkw"`,
`"abcperkb"`, and `"123garbageperkw"` silently parsed as valid feerates.
Check the `"perkw"` / `"perkb"` suffix case-insensitively on the raw
bytes, require the entire prefix to consist of ASCII digits, and parse
it with `str::parse::<u32>()`.
Add unit tests covering `u32::MAX`, overflow, empty prefixes, signs,
trailing garbage, and non-ASCII inputs.
Changelog-Fixed: cln-rpc: reject malformed and overflowing `Feerate` strings in `TryFrom<&str>` instead of wrapping or silently accepting invalid input.
`OutputDesc::deserialize` deserialized into a `HashMap<String, Amount>`
and called `map.iter().next().unwrap()`, which panicked on an empty JSON
object `{}` and silently returned a random entry when multiple keys were
present.
Replace it with a `serde::de::Visitor` using `MapAccess` that requires
exactly one `{address: amount}` entry and avoids allocating a `HashMap`.
Add unit tests rejecting `{}` and multi-entry objects.
Changelog-Fixed: cln-rpc: return a deserialization error instead of panicking on empty `{}` or silently ignoring extra keys in `OutputDesc`.
`TlvStream::deserialize` deserialized into a `HashMap<u64, String>` and called `hex::decode(v).unwrap()`. Invalid or odd-length hex panicked instead of returning a serde error, and `HashMap`'s randomized iteration order scrambled `entries` rather than preserving BOLT 1's canonical ascending numeric `typ` order. Replace it with a `serde::de::Visitor` using `MapAccess` that propagates hex-decoding errors, sorts `entries` by ascending numeric `typ: u64`, and rejects duplicate TLV type keys. Add unit tests covering ascending numeric ordering (e.g., `2` before `10`), empty streams, invalid/odd-length hex, and duplicate TLV types. Changelog-Fixed: cln-rpc: return a deserialization error on invalid hex or duplicate keys in `TlvStream` instead of panicking, and sort entries in ascending numeric `typ` order.
…r` validation
Remove avoidable panics and fix validation gaps across `cln-rpc`:
- `ClnRpc::call_enum`: return `Err(RpcError)` instead of calling
`.unwrap()` / `panic!()` if request serialization fails or lacks a
string `method`.
- `JsonRpc::serialize`: propagate `serde_json::to_value` errors via
`serde::ser::Error::custom` instead of calling `.unwrap()`.
- `JsonRpc::deserialize`: check `v.as_object()?.get("id")` directly
instead of `IdHelper { id: Option<Value> }` so `"id": null` is
deserialized as `JsonRpc::Request(Value::Null, _)` rather than
misclassified as `JsonRpc::Notification`.
- `ChannelTypeName` and `HtlcState`: replace panicking `From<i32>` impls
with `TryFrom<i32>` returning `crate::Error`, matching `ChannelState`,
`ChannelSide`, `AutocleanSubsystem`, and `PluginSubcommand`.
- `ShortChannelIdDir::from_str`: enforce `direction <= 1` (0 or 1) and
split once on `'/'` instead of allocating a `Vec<String>`.
Add unit tests for `"id": null` vs missing `"id"`, failing `JsonRpc`
serialization, `ShortChannelIdDir` direction bounds, and
`ChannelTypeName::try_from` / `HtlcState::try_from`.
Changelog-Fixed: cln-rpc: eliminate avoidable panics in `call_enum`, `JsonRpc::serialize`, `ChannelTypeName`, and `HtlcState`, classify `"id": null` as a `JsonRpc::Request`, and reject invalid `ShortChannelIdDir` directions.
jaonoctus
reviewed
Dec 1, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Multiple commits for various deserialize/panic/overflow issues and some tightening when parsing certain types.