lightning icon indicating copy to clipboard operation
lightning copied to clipboard

commando-rune: jsonrpc escaped | in restrictions don't work

Open jb55 opened this issue 1 year ago • 2 comments

I'm trying to create a rune with the following restrictions:

lightning-cli commando-rune restrictions='["method=offer-summary","pnameofferid=22db2cbdb2d6e1f4d727d099e2ea987c05212d6b4af56d92497e093b82360db7","pnamedescription=@tipjar\|[email protected]","pnamelimit=5"]' 

but I'm getting this error:

lightning-cli: Some parameters are malformed, cannot create a valid JSON-RPC request: { "jsonrpc" : "2.0", "method" : "commando-rune", "id" : "lightning-cli-3959873", "params" :{ "restrictions" : ["method=offer-summary","pnameofferid=22db2cbdb2d6e1f4d727d099e2ea987c05212d6b4af56d92497e093b82360db7","pnamedescription=@tipjar\|[email protected]","pnamelimit=5"]} }

it looks like the attempt at escaping | is throwing off the json parser?

escaping the slash fails as well:

  1> lightning-cli commando-rune restrictions='["method=offer-summary","pnameofferid=22db2cbdb2d6e1f4d727d099e2ea987c05212d6b4af56d92497e093b82360db7","pnamedescription=@tipjar\\|[email protected]","pnamelimit=5"]' 
{
   "code": -32602,
   "message": "restrictions: not a valid restriction: invalid token '\"pnamedescription=@tipjar\\\\|[email protected]\"'"
}

furthest I got was this patch to debug this, but I still had double escaped \ for some reason... and I wasn't sure if I was doing the right thing in the right place

diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index ede08a6b4..08fd6a75c 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -736,6 +736,7 @@ int main(int argc, char *argv[])
 	}
 
 	toks = json_parse_simple(ctx, cmd, strlen(cmd));
+	fprintf(stderr, "cmd: '%s'\n", cmd);
 	if (toks == NULL)
 		errx(ERROR_USAGE,
 		     "Some parameters are malformed, cannot create a valid "
diff --git a/plugins/commando.c b/plugins/commando.c
index 83379aafb..f4218e225 100644
--- a/plugins/commando.c
+++ b/plugins/commando.c
@@ -772,13 +772,21 @@ static struct command_result *param_restrictions(struct command *cmd,
 		*restrs = readonly_restrictions(cmd);
 	else if (tok->type == JSMN_ARRAY) {
 		size_t i;
+		struct json_escape *esc;
+		const char *str;
 		const jsmntok_t *t;
 
 		*restrs = tal_arr(cmd, struct rune_restr *, tok->size);
 		json_for_each_arr(i, t, tok) {
-			(*restrs)[i] = rune_restr_from_string(*restrs,
-							      buffer + t->start,
-							      t->end - t->start);
+			esc = json_escape_string_(cmd, buffer + t->start,
+						  t->end - t->start);
+			str = json_escape_unescape(cmd, esc);
+
+			plugin_log(plugin, LOG_INFORM, "command param escaped: '%.*s'\n", t->end - t->start, buffer + t->start);
+			plugin_log(plugin, LOG_INFORM, "command param unescaped: '%s'\n", str);
+			if (!str)
+				return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Failed to unescape '%.*s'", t->end - t->start, buffer + t->start);
+			(*restrs)[i] = rune_restr_from_string(*restrs, str, strlen(str));
 			if (!(*restrs)[i])
 				return command_fail_badparam(cmd, name, buffer, t,
 							     "not a valid restriction");

perhaps if there was a cli tool that could create these to avoid the jsonrpc headache? 🤔

jb55 avatar Jul 28 '22 13:07 jb55

Gah! \| is not valid JSON (jq agrees). OK, so we add two, and it's now valid JSON, but it's not a valid rune restriction. Needs unescaping.... yech...

rustyrussell avatar Aug 18 '22 01:08 rustyrussell

@rustyrussell btw before I forget there's another escaping issue when handling commando parameters. I can't pay deschash invoices with lnlink over commando but I can with lightning-cli. lnurl requires passing arrays of json metadata as the description and I think it's not unescaped properly on the commando handler side. I've been meaning to put together a test case and/or fix but I have yet to get to it :(

jb55 avatar Aug 18 '22 03:08 jb55

Think I hit the same issue, once I fixed the above! I'll get your review on the fix...

rustyrussell avatar Aug 19 '22 02:08 rustyrussell