Skip to content

Commit fe5fb13

Browse files
rustyrussellLagrang3
authored andcommitted
fetchinvoice: handle weird labels in recurrence_label parameter.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 7d24364 commit fe5fb13

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

plugins/fetchinvoice.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "config.h"
2+
#include <ccan/json_escape/json_escape.h>
23
#include <ccan/json_out/json_out.h>
34
#include <ccan/mem/mem.h>
45
#include <ccan/str/hex/hex.h>
@@ -791,19 +792,19 @@ static bool payer_key(const struct offers_data *od,
791792
static u8 *recurrence_invreq_metadata(const tal_t *ctx,
792793
const struct tlv_invoice_request *invreq,
793794
const struct secret *nodealias_base,
794-
const char *rec_label)
795+
const struct json_escape *rec_label)
795796
{
796797
struct sha256 offer_id, tweak;
797798
u8 *tweak_input;
798799

799800
/* Use "offer_id || label" as tweak input */
800801
invreq_offer_id(invreq, &offer_id);
801802
tweak_input = tal_arr(tmpctx, u8,
802-
sizeof(offer_id) + strlen(rec_label));
803+
sizeof(offer_id) + strlen(rec_label->s));
803804
memcpy(tweak_input, &offer_id, sizeof(offer_id));
804805
memcpy(tweak_input + sizeof(offer_id),
805-
rec_label,
806-
strlen(rec_label));
806+
rec_label->s,
807+
strlen(rec_label->s));
807808

808809
bolt12_alias_tweak(nodealias_base,
809810
tweak_input,
@@ -854,7 +855,8 @@ struct command_result *json_fetchinvoice(struct command *cmd,
854855
{
855856
const struct offers_data *od = get_offers_data(cmd->plugin);
856857
struct amount_msat *msat;
857-
const char *rec_label, *payer_note;
858+
const char *payer_note;
859+
struct json_escape *rec_label;
858860
u8 *payer_metadata;
859861
struct out_req *req;
860862
struct tlv_invoice_request *invreq;
@@ -870,7 +872,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
870872
p_opt("quantity", param_u64, &quantity),
871873
p_opt("recurrence_counter", param_number, &recurrence_counter),
872874
p_opt("recurrence_start", param_number, &recurrence_start),
873-
p_opt("recurrence_label", param_string, &rec_label),
875+
p_opt("recurrence_label", param_label, &rec_label),
874876
p_opt_def("timeout", param_number, &timeout, 60),
875877
p_opt("payer_note", param_string, &payer_note),
876878
p_opt("payer_metadata", param_bin_from_hex, &payer_metadata),
@@ -1096,7 +1098,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
10961098
json_add_string(req->js, "bolt12", invrequest_encode(tmpctx, invreq));
10971099
json_add_bool(req->js, "savetodb", false);
10981100
if (rec_label)
1099-
json_add_string(req->js, "label", rec_label);
1101+
json_add_escaped_string(req->js, "label", rec_label);
11001102
return send_outreq(req);
11011103
}
11021104

@@ -1105,7 +1107,8 @@ struct command_result *json_cancelrecurringinvoice(struct command *cmd,
11051107
const jsmntok_t *params)
11061108
{
11071109
const struct offers_data *od = get_offers_data(cmd->plugin);
1108-
const char *rec_label, *payer_note;
1110+
const char *payer_note;
1111+
struct json_escape *rec_label;
11091112
struct out_req *req;
11101113
struct tlv_invoice_request *invreq;
11111114
struct sent *sent = tal(cmd, struct sent);
@@ -1115,7 +1118,7 @@ struct command_result *json_cancelrecurringinvoice(struct command *cmd,
11151118
if (!param_check(cmd, buffer, params,
11161119
p_req("offer", param_offer, &sent->offer),
11171120
p_req("recurrence_counter", param_number, &recurrence_counter),
1118-
p_req("recurrence_label", param_string, &rec_label),
1121+
p_req("recurrence_label", param_label, &rec_label),
11191122
p_opt("recurrence_start", param_number, &recurrence_start),
11201123
p_opt("payer_note", param_string, &payer_note),
11211124
p_opt("bip353", param_bip353, &bip353),
@@ -1250,7 +1253,7 @@ struct command_result *json_cancelrecurringinvoice(struct command *cmd,
12501253
/* We don't want this is the database: that's only for ones we publish */
12511254
json_add_string(req->js, "bolt12", invrequest_encode(tmpctx, invreq));
12521255
json_add_bool(req->js, "savetodb", false);
1253-
json_add_string(req->js, "label", rec_label);
1256+
json_add_escaped_string(req->js, "label", rec_label);
12541257
return send_outreq(req);
12551258
}
12561259

tests/test_pay.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4549,6 +4549,30 @@ def test_offer(node_factory, bitcoind):
45494549
assert enable_ret['description'] == offer_desc
45504550

45514551

4552+
def test_recurrence_escaped_label(node_factory, bitcoind):
4553+
l1, l2 = node_factory.line_graph(2)
4554+
4555+
# Recurring offer.
4556+
offer = l2.rpc.offer(amount='1msat',
4557+
description='test_recurrence_escaped_label',
4558+
recurrence='1minutes')['bolt12']
4559+
# Works the first time
4560+
weird_label = 'label \\ " \t \n'
4561+
ret = l1.rpc.fetchinvoice(offer=offer,
4562+
recurrence_counter=0,
4563+
recurrence_label=weird_label)
4564+
l1.rpc.xpay(invstring=ret['invoice'])
4565+
# Works the second time to match
4566+
l1.rpc.fetchinvoice(offer=offer,
4567+
recurrence_counter=1,
4568+
recurrence_label=weird_label)
4569+
4570+
# Works to cancel.
4571+
l1.rpc.cancelrecurringinvoice(offer=offer,
4572+
recurrence_counter=2,
4573+
recurrence_label=weird_label)
4574+
4575+
45524576
def test_offer_deprecated_api(node_factory, bitcoind):
45534577
l1, l2 = node_factory.line_graph(2, opts={'allow-deprecated-apis': True})
45544578

0 commit comments

Comments
 (0)