diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 7240fdc9c1b4..dbd7fd84b73d 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -1070,6 +1070,13 @@ parse_request(struct json_connection *jcon, "Expected string for method"); } + c->json_cmd = find_cmd(jcon->ld->jsonrpc, buffer, method); + if (!c->json_cmd) { + return command_fail( + c, JSONRPC2_METHOD_NOT_FOUND, "Unknown command '%.*s'", + method->end - method->start, buffer + method->start); + } + if (filter) { struct command_result *ret; ret = parse_filter(c, "filter", buffer, filter); @@ -1081,12 +1088,6 @@ parse_request(struct json_connection *jcon, * actually just logging the id */ log_io(jcon->log, LOG_IO_IN, NULL, c->id, NULL, 0); - c->json_cmd = find_cmd(jcon->ld->jsonrpc, buffer, method); - if (!c->json_cmd) { - return command_fail( - c, JSONRPC2_METHOD_NOT_FOUND, "Unknown command '%.*s'", - method->end - method->start, buffer + method->start); - } if (!command_deprecated_in_ok(c, NULL, c->json_cmd->depr_start, c->json_cmd->depr_end)) { diff --git a/tests/test_misc.py b/tests/test_misc.py index e17d4935fc9c..e159b1c48bb4 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -5042,3 +5042,27 @@ def test_zero_locktime_blocks(node_factory, bitcoind): l2.rpc.close(l3.info['id']) bitcoind.generate_block(1, wait_for_mempool=2) sync_blockheight(bitcoind, [l1, l2, l3]) + + +def test_filter_with_invalid_json(node_factory): + # This crashes only in *non-developer mode*: it uses command_log() + # in that case (since it doesn't print the invalid token in + # non-dev mode), and that expects cmd->json_cmd to be populated!` + l1 = node_factory.get_node(start=False) + l1.daemon.early_opts = [] + l1.daemon.opts = {k: v for k, v in l1.daemon.opts.items() if not k.startswith('dev')} + l1.start() + + out = subprocess.run(['cli/lightning-cli', + '--network={}'.format(TEST_NETWORK), + '--lightning-dir={}' + .format(l1.daemon.lightning_dir), + '-l', '1', + '-k', + 'wait', + 'subsystem=invoices', + 'indexname=created', + 'nextvalue=0'], + stdout=subprocess.PIPE) + assert 'filter: Expected object: invalid token' in out.stdout.decode('utf-8') + assert out.returncode == 1