commando: unmangle JSON.

JSON needs to escape \, since it can't be in front of anything unexpected,
so no \|.  So we need to return \\ to \, and in theory handle \n etc.

Changelog-Fixed: JSON-RPC: `commando-rune` now handles \\ escapes properly.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-09-14 14:59:59 +09:30 committed by Christian Decker
parent 1f9730748c
commit d57d87ea3a
2 changed files with 19 additions and 8 deletions

View file

@ -762,6 +762,21 @@ static struct rune_restr **readonly_restrictions(const tal_t *ctx)
return restrs;
}
/* \| is not valid JSON, so they use \\|: undo it! */
static struct rune_restr *rune_restr_from_json(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok)
{
const char *unescape;
struct json_escape *e = json_escape_string_(tmpctx,
buffer + tok->start,
tok->end - tok->start);
unescape = json_escape_unescape(tmpctx, e);
if (!unescape)
return NULL;
return rune_restr_from_string(ctx, unescape, strlen(unescape));
}
static struct command_result *param_restrictions(struct command *cmd,
const char *name,
const char *buffer,
@ -776,18 +791,15 @@ static struct command_result *param_restrictions(struct command *cmd,
*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);
if (!(*restrs)[i])
(*restrs)[i] = rune_restr_from_json(*restrs, buffer, t);
if (!(*restrs)[i]) {
return command_fail_badparam(cmd, name, buffer, t,
"not a valid restriction");
}
}
} else {
*restrs = tal_arr(cmd, struct rune_restr *, 1);
(*restrs)[0] = rune_restr_from_string(*restrs,
buffer + tok->start,
tok->end - tok->start);
(*restrs)[0] = rune_restr_from_json(*restrs, buffer, tok);
if (!(*restrs)[0])
return command_fail_badparam(cmd, name, buffer, tok,
"not a valid restriction");

View file

@ -2636,7 +2636,6 @@ def test_commando(node_factory, executor):
assert exc_info.value.error['data']['erring_index'] == 0
@pytest.mark.xfail(reason="Escapes in restrictions are broken", strict=True)
def test_commando_rune(node_factory):
l1, l2 = node_factory.get_nodes(2)