diff --git a/common/Makefile b/common/Makefile index 51f96ee6cfcb..b5a1591e9afc 100644 --- a/common/Makefile +++ b/common/Makefile @@ -56,6 +56,7 @@ COMMON_SRC_NOGEN := \ common/iso4217.c \ common/json_blinded_path.c \ common/json_channel_type.c \ + common/json_command.c \ common/json_filter.c \ common/json_param.c \ common/json_parse.c \ @@ -115,7 +116,6 @@ COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \ common/gossip_constants.h \ common/hsm_version.h \ common/htlc.h \ - common/json_command.h \ common/jsonrpc_errors.h \ common/overflows.h diff --git a/common/json_command.c b/common/json_command.c new file mode 100644 index 000000000000..613b02e1d646 --- /dev/null +++ b/common/json_command.c @@ -0,0 +1,33 @@ +#include "config.h" +#include + +struct command_result * +command_fail_badparam(struct command *cmd, + const char *paramname, + const char *buffer, + const jsmntok_t *tok, + const char *msg) +{ + if (command_dev_apis(cmd)) { + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "%s: %s: invalid token '%.*s'", + paramname, msg, + json_tok_full_len(tok), + json_tok_full(buffer, tok)); + } + + /* Someone misconfigured LNBITS with "" around the rune, and so the + * user got a message about a bad rune parameter which *contained the + * rune itself*!. LNBITS should probably swallow any JSONRPC2_* error + * itself, but it is quite possibly not the only case where this case + * where this can happen. So we are a little circumspect in this + * case. */ + command_log(cmd, LOG_INFORM, + "Invalid parameter %s (%s): token '%.*s'", + paramname, msg, + json_tok_full_len(tok), + json_tok_full(buffer, tok)); + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "%s: %s: invalid token (see logs for details)", + paramname, msg); +} diff --git a/common/json_command.h b/common/json_command.h index 4e9112c85b1b..d0c0667f4281 100644 --- a/common/json_command.h +++ b/common/json_command.h @@ -54,35 +54,10 @@ struct command_result *command_check_done(struct command *cmd) WARN_UNUSED_RESULT; /* Convenient wrapper for "paramname: msg: invalid token '.*%s'" */ -static inline struct command_result * -command_fail_badparam(struct command *cmd, - const char *paramname, - const char *buffer, - const jsmntok_t *tok, - const char *msg) -{ - if (command_dev_apis(cmd)) { - return command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "%s: %s: invalid token '%.*s'", - paramname, msg, - json_tok_full_len(tok), - json_tok_full(buffer, tok)); - } - - /* Someone misconfigured LNBITS with "" around the rune, and so the - * user got a message about a bad rune parameter which *contained the - * rune itself*!. LNBITS should probably swallow any JSONRPC2_* error - * itself, but it is quite possibly not the only case where this case - * where this can happen. So we are a little circumspect in this - * case. */ - command_log(cmd, LOG_INFORM, - "Invalid parameter %s (%s): token '%.*s'", - paramname, msg, - json_tok_full_len(tok), - json_tok_full(buffer, tok)); - return command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "%s: %s: invalid token (see logs for details)", - paramname, msg); -} +struct command_result *command_fail_badparam(struct command *cmd, + const char *paramname, + const char *buffer, + const jsmntok_t *tok, + const char *msg); #endif /* LIGHTNING_COMMON_JSON_COMMAND_H */ diff --git a/common/test/run-json_filter.c b/common/test/run-json_filter.c index 233f7e54078f..d4e68801a637 100644 --- a/common/test/run-json_filter.c +++ b/common/test/run-json_filter.c @@ -33,11 +33,13 @@ struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_e const char *fmt UNNEEDED, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_set_usage */ void command_set_usage(struct command *cmd UNNEEDED, const char *usage UNNEEDED) { fprintf(stderr, "command_set_usage called!\n"); abort(); } diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c index 422c580cb16f..93c844143415 100644 --- a/common/test/run-json_remove.c +++ b/common/test/run-json_remove.c @@ -62,11 +62,13 @@ struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_e const char *fmt UNNEEDED, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_set_usage */ void command_set_usage(struct command *cmd UNNEEDED, const char *usage UNNEEDED) { fprintf(stderr, "command_set_usage called!\n"); abort(); } diff --git a/common/test/run-json_stream-filter.c b/common/test/run-json_stream-filter.c index cebca0f9cf55..1e484b475868 100644 --- a/common/test/run-json_stream-filter.c +++ b/common/test/run-json_stream-filter.c @@ -50,22 +50,16 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u /* Generated stub for amount_tx_fee */ struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) { fprintf(stderr, "amount_tx_fee called!\n"); abort(); } -/* Generated stub for command_dev_apis */ -bool command_dev_apis(const struct command *cmd UNNEEDED) -{ fprintf(stderr, "command_dev_apis called!\n"); abort(); } -/* Generated stub for command_fail */ -struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_fail called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_filter_ptr */ struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED) { fprintf(stderr, "command_filter_ptr called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } /* Generated stub for fmt_amount_sat */ char *fmt_amount_sat(const tal_t *ctx UNNEEDED, struct amount_sat sat UNNEEDED) { fprintf(stderr, "fmt_amount_sat called!\n"); abort(); } diff --git a/common/test/run-param.c b/common/test/run-param.c index 38cbd8b31f37..3cb94bf854c7 100644 --- a/common/test/run-param.c +++ b/common/test/run-param.c @@ -1,5 +1,6 @@ #include "config.h" #include "../bech32.c" +#include "../json_command.c" #include "../json_filter.c" #include "../json_parse.c" #include "../json_parse_simple.c" @@ -56,14 +57,17 @@ bool command_deprecated_in_ok(struct command *cmd, return true; } +/* Generated stub for command_dev_apis */ +bool command_dev_apis(const struct command *cmd) +{ + return true; +} + /* AUTOGENERATED MOCKS START */ /* Generated stub for command_check_done */ struct command_result *command_check_done(struct command *cmd) { fprintf(stderr, "command_check_done called!\n"); abort(); } -/* Generated stub for command_dev_apis */ -bool command_dev_apis(const struct command *cmd UNNEEDED) -{ fprintf(stderr, "command_dev_apis called!\n"); abort(); } /* Generated stub for command_filter_ptr */ struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED) { fprintf(stderr, "command_filter_ptr called!\n"); abort(); } diff --git a/lightningd/Makefile b/lightningd/Makefile index 0c8aa0ba5a68..e9766343b60b 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -116,6 +116,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/key_derive.o \ common/keyset.o \ common/json_channel_type.o \ + common/json_command.o \ common/json_filter.o \ common/json_param.o \ common/json_parse.o \ diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index aa48c84c368f..1b3dec608cf7 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -156,14 +156,18 @@ bool command_deprecated_out_ok(struct command *cmd UNNEEDED, const char *depr_start UNNEEDED, const char *depr_end UNNEEDED) { fprintf(stderr, "command_deprecated_out_ok called!\n"); abort(); } -/* Generated stub for command_dev_apis */ -bool command_dev_apis(const struct command *cmd UNNEEDED) -{ fprintf(stderr, "command_dev_apis called!\n"); abort(); } /* Generated stub for command_fail */ struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_failed */ struct command_result *command_failed(struct command *cmd UNNEEDED, struct json_stream *result) @@ -172,11 +176,6 @@ struct command_result *command_failed(struct command *cmd UNNEEDED, /* Generated stub for command_its_complicated */ struct command_result *command_its_complicated(const char *why UNNEEDED) { fprintf(stderr, "command_its_complicated called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } /* Generated stub for command_logger */ struct logger *command_logger(struct command *cmd UNNEEDED) { fprintf(stderr, "command_logger called!\n"); abort(); } diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 3b2772e53ec2..4975d66b326b 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -7,6 +7,13 @@ #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for db_begin_transaction_ */ void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED) { fprintf(stderr, "db_begin_transaction_ called!\n"); abort(); } diff --git a/lightningd/test/run-log-pruning.c b/lightningd/test/run-log-pruning.c index 149ffa17a1ff..7e2c2756552d 100644 --- a/lightningd/test/run-log-pruning.c +++ b/lightningd/test/run-log-pruning.c @@ -3,19 +3,13 @@ #include /* AUTOGENERATED MOCKS START */ -/* Generated stub for command_dev_apis */ -bool command_dev_apis(const struct command *cmd UNNEEDED) -{ fprintf(stderr, "command_dev_apis called!\n"); abort(); } -/* Generated stub for command_fail */ -struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_fail called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_param_failed */ struct command_result *command_param_failed(void) diff --git a/lightningd/test/run-log_filter.c b/lightningd/test/run-log_filter.c index 81f7c37af01d..1d373cbd227e 100644 --- a/lightningd/test/run-log_filter.c +++ b/lightningd/test/run-log_filter.c @@ -14,19 +14,13 @@ void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEED { } /* AUTOGENERATED MOCKS START */ -/* Generated stub for command_dev_apis */ -bool command_dev_apis(const struct command *cmd UNNEEDED) -{ fprintf(stderr, "command_dev_apis called!\n"); abort(); } -/* Generated stub for command_fail */ -struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_fail called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_param_failed */ struct command_result *command_param_failed(void) diff --git a/plugins/Makefile b/plugins/Makefile index 3c34f72481e0..e7530431e4a4 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -159,6 +159,7 @@ PLUGIN_COMMON_OBJS := \ common/deprecation.o \ common/features.o \ common/hash_u5.o \ + common/json_command.o \ common/json_param.o \ common/json_parse.o \ common/json_parse_simple.o \ diff --git a/plugins/bkpr/test/run-bkpr_db.c b/plugins/bkpr/test/run-bkpr_db.c index bd9104d80ff4..308110548ec1 100644 --- a/plugins/bkpr/test/run-bkpr_db.c +++ b/plugins/bkpr/test/run-bkpr_db.c @@ -24,6 +24,13 @@ /* Generated stub for account_entry_tag_str */ const char *account_entry_tag_str(enum account_entry_tag tag UNNEEDED) { fprintf(stderr, "account_entry_tag_str called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for daemon_developer_mode */ bool daemon_developer_mode(char *argv[]) { fprintf(stderr, "daemon_developer_mode called!\n"); abort(); } diff --git a/plugins/bkpr/test/run-recorder.c b/plugins/bkpr/test/run-recorder.c index 62c317746102..5ebac38ab05e 100644 --- a/plugins/bkpr/test/run-recorder.c +++ b/plugins/bkpr/test/run-recorder.c @@ -30,6 +30,13 @@ /* Generated stub for account_entry_tag_str */ const char *account_entry_tag_str(enum account_entry_tag tag UNNEEDED) { fprintf(stderr, "account_entry_tag_str called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for daemon_developer_mode */ bool daemon_developer_mode(char *argv[]) { fprintf(stderr, "daemon_developer_mode called!\n"); abort(); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 915b4100b1ce..87633de95b2f 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -135,24 +135,23 @@ bool command_deprecated_out_ok(struct command *cmd UNNEEDED, const char *depr_start UNNEEDED, const char *depr_end UNNEEDED) { fprintf(stderr, "command_deprecated_out_ok called!\n"); abort(); } -/* Generated stub for command_dev_apis */ -bool command_dev_apis(const struct command *cmd UNNEEDED) -{ fprintf(stderr, "command_dev_apis called!\n"); abort(); } /* Generated stub for command_fail */ struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } +/* Generated stub for command_fail_badparam */ +struct command_result *command_fail_badparam(struct command *cmd UNNEEDED, + const char *paramname UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char *msg UNNEEDED) +{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); } /* Generated stub for command_failed */ struct command_result *command_failed(struct command *cmd UNNEEDED, struct json_stream *result) { fprintf(stderr, "command_failed called!\n"); abort(); } -/* Generated stub for command_log */ -void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED, - const char *fmt UNNEEDED, ...) - -{ fprintf(stderr, "command_log called!\n"); abort(); } /* Generated stub for command_param_failed */ struct command_result *command_param_failed(void) @@ -599,9 +598,6 @@ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEE /* Generated stub for json_tok_full */ const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEEDED) { fprintf(stderr, "json_tok_full called!\n"); abort(); } -/* Generated stub for json_tok_full_len */ -int json_tok_full_len(const jsmntok_t *t UNNEEDED) -{ fprintf(stderr, "json_tok_full_len called!\n"); abort(); } /* Generated stub for json_tok_streq */ bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED) { fprintf(stderr, "json_tok_streq called!\n"); abort(); }