From a1786804c7b93ff70fe82f8de44ad329257d6bfe Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 4 Dec 2020 14:33:38 +0100 Subject: [PATCH] cli: Add a JSON parsing sanity check before sending request If some parameter is malformed on the command line we could end up with a malformed JSON-RPC request, which would then result in very unhelpful error messages. Fixes #4238 Changelog-Changed: cli: `lightning-cli` now performs better sanity checks on the JSON-RPC requests it sends. --- cli/lightning-cli.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 37f221e35c3c..c6996e6d8976 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -740,6 +740,14 @@ int main(int argc, char *argv[]) tal_append_fmt(&cmd, "] }"); } + toks = json_parse_simple(ctx, cmd, strlen(cmd)); + if (toks == NULL) + errx(ERROR_USAGE, + "Some parameters are malformed, cannot create a valid " + "JSON-RPC request: %s", + cmd); + tal_free(toks); + if (!write_all(fd, cmd, strlen(cmd))) err(ERROR_TALKING_TO_LIGHTNINGD, "Writing command");