Skip to content

cln-rpc: fix deserializing, avoid panics and reject overflows - #9767

Open
daywalker90 wants to merge 4 commits into
ElementsProject:masterfrom
daywalker90:cln-rpc-fix-deserializing
Open

daywalker90 wants to merge 4 commits into
ElementsProject:masterfrom
daywalker90:cln-rpc-fix-deserializing

Conversation

@daywalker90

Copy link
Copy Markdown
Collaborator

Multiple commits for various deserialize/panic/overflow issues and some tightening when parsing certain types.

`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.
Comment thread cln-rpc/src/primitives.rs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants