From b18c1ea543acf441cebadc883baf7549405d44ef Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 4 Dec 2019 23:01:20 +0100 Subject: [PATCH] json-rpc: Restrict custommsgs to be odd-typed This solves a couple of issues with the need to synchronously drop the connection in case we were required to understand what the peer was talking about while still allowing users to experiment, just not kill connections. --- lightningd/peer_control.c | 10 ++++++++++ tests/test_misc.py | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index d674ba5f1..3de82c74a 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -2404,6 +2404,16 @@ static struct command_result *json_sendcustommsg(struct command *cmd, type, wire_type_name(type)); } + if (type % 2 == 0) { + return command_fail( + cmd, JSONRPC2_INVALID_REQUEST, + "Cannot send even-typed %d custom message. Currently " + "custom messages are limited to odd-numbered message " + "types, as even-numbered types might result in " + "disconnections.", + type); + } + peer = peer_by_id(cmd->ld, dest); if (!peer) { return command_fail(cmd, JSONRPC2_INVALID_REQUEST, diff --git a/tests/test_misc.py b/tests/test_misc.py index 648374fa7..fe2bd5a65 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -2096,6 +2096,12 @@ def test_sendcustommsg(node_factory): with pytest.raises(RpcError, match=r'Cannot send messages of type 18 .WIRE_PING.'): l2.rpc.dev_sendcustommsg(l2.info['id'], r'0012') + # The sendcustommsg RPC call is currently limited to odd-typed messages, + # since they will not result in disconnections or even worse channel + # failures. + with pytest.raises(RpcError, match=r'Cannot send even-typed [0-9]+ custom message'): + l2.rpc.dev_sendcustommsg(l2.info['id'], r'00FE') + # This should work since the peer is currently owned by `channeld` l2.rpc.dev_sendcustommsg(l1.info['id'], msg) l2.daemon.wait_for_log(