jsonrpc: make sure even errors are valid json.

We often quote their msg in our reply; sanitize it!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-12-03 09:30:39 +10:30 committed by Christian Decker
parent 164c76454c
commit aee2197a66
2 changed files with 12 additions and 2 deletions

View File

@ -480,13 +480,13 @@ struct json_stream *json_stream_fail_nodata(struct command *cmd,
const char *errmsg)
{
struct json_stream *r = json_start(cmd);
struct json_escaped *e = json_partial_escape(tmpctx, errmsg);
assert(code);
assert(errmsg);
json_stream_append_fmt(r, " \"error\" : "
"{ \"code\" : %d,"
" \"message\" : \"%s\"", code, errmsg);
" \"message\" : \"%s\"", code, e->s);
return r;
}

View File

@ -1060,3 +1060,13 @@ def test_configfile_before_chdir(node_factory):
assert l1.rpc.listconfigs()['always-use-proxy']
assert l1.rpc.listconfigs()['proxy'] == '127.0.0.1:100'
os.chdir(olddir)
def test_json_error(node_factory):
"""Must return valid json even if it quotes our weirdness"""
l1 = node_factory.get_node()
with pytest.raises(RpcError, match=r'Given id is not a channel ID or short channel ID'):
l1.rpc.close({"tx": "020000000001011490f737edd2ea2175a032b58ea7cd426dfc244c339cd044792096da3349b18a0100000000ffffffff021c900300000000001600140e64868e2f752314bc82a154c8c5bf32f3691bb74da00b00000000002200205b8cd3b914cf67cdd8fa6273c930353dd36476734fbd962102c2df53b90880cd0247304402202b2e3195a35dc694bbbc58942dc9ba59cc01d71ba55c9b0ad0610ccd6a65633702201a849254453d160205accc00843efb0ad1fe0e186efa6a7cee1fb6a1d36c736a012103d745445c9362665f22e0d96e9e766f273f3260dea39c8a76bfa05dd2684ddccf00000000", "txid": "2128c10f0355354479514f4a23eaa880d94e099406d419bbb0d800143accddbb", "channel_id": "bbddcc3a1400d8b0bb19d40694094ed980a8ea234a4f5179443555030fc12820"})
# Should not corrupt following RPC
l1.rpc.getinfo()