Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validation of txprepare arguments #4259

Merged
merged 10 commits into from
Dec 8, 2020

Conversation

cdecker
Copy link
Member

@cdecker cdecker commented Dec 4, 2020

There are two issues we address in this PR:

  • Broken JSON strings when forwarding params as raw strings to further JSON-RPC calls, causing the infamous id is null error because the side receiving the mangled request cannot extract the id and returns a generic error instead, which doesn't match up with our expectations.
  • Interpreting a string param as an array, resulting in an empty array, and thus only adding the change output to a prepared tx.

In a couple of places we accept arrays of strings and don't validate
them. If we forward them, e.g., call a JSON-RPC method from the
plugin, we end up embedding the unverified string in the JSON-RPC
call without escaping, which then leads to invalid JSON being passed
on.

We were not checking that outputs is indeed an array, and just going
ahead creating the array of outputs. Since tok->size for a string is
0 we ended up ignoring the argument altogether and thus the created
transaction would end up only with a single change output.

Since I was looking at the cli params already I also added some more feedback
to lightning-cli. It now parses the request before sending it to lightningd, so if
a malformed param results in a malformed request we catch it early and tell the
user to check the formatting of the params.

Fixes #4238
Fixes #4258

Copy link
Collaborator

@niftynei niftynei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 10f842e

Awesome work finding this!

Copy link
Contributor

@rustyrussell rustyrussell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix the others as a separate series.

Ack 6c308bf

common/json_tok.c Outdated Show resolved Hide resolved
cmd, JSONRPC2_INVALID_PARAMS,
"Invalid element in %s: element %zu is not a valid "
"outpoint \"%s\"",
name, i, curr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesh, can we implement json_to_outpoint and use this instead?

const char *name,
const char *buffer,
const jsmntok_t *tok,
const char ***strings);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite the inefficiency, I prefer converting rather than leaving strings. Maybe we need a struct outpoint as a separate patch though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure whether the added effort and conversions would cause some pushback, but generally I agree that we should always verify as much as possible by doing all conversions, even when doing passthru of arguments (which caused this issue in the first place).

@cdecker cdecker force-pushed the issue-4258 branch 2 times, most recently from d681793 to a178680 Compare December 7, 2020 13:17
@cdecker
Copy link
Member Author

cdecker commented Dec 7, 2020

Ok, took two steps back and one forward: we now have a struct bitcoin_outpoint (distinct from outpoiint in wallet which also contains metadata used when spending), json_to_outpoint and param_outpoint_arr. We now fully parse the txprepare utxos and report errors correctly.

@cdecker
Copy link
Member Author

cdecker commented Dec 7, 2020

Couple of force-pushes to remove some drive-by fixes.

bitcoin/tx.h Outdated Show resolved Hide resolved
@rustyrussell
Copy link
Contributor

Added a couple of cleanups for you feedback

We promised to deprecate the old "destination satoshi feerate utxos"
variant some time on mid-2020 so let's do just that. `txprepare`
doesn't parse it either anymore.

Changelog-Changed: pyln: `txprepare` no longer supports the deprecated `destination satoshi feerate utxos` call format.
In a couple of places we accept arrays of strings and don't validate
them. If we forward them, e.g., call a JSON-RPC method from the
plugin, we end up embedding the unverified string in the JSON-RPC
call without escaping, which then leads to invalid JSON being passed
on.

This at least partially causes ElementsProject#4238
We were not checking that outputs is indeed an array, and just going
ahead creating the array of outputs. Since `tok->size` for a string is
0 we ended up ignoring the argument altogether and thus the created
transaction would end up only with a single change output.

Fixes ElementsProject#4258
cdecker and others added 4 commits December 8, 2020 12:18
We were masquerading errors when parsing the request by reporting only
a bogus malformed `id` field in the response, when the real issue was
that we were unable to parse the request in the first place (which
caused the null-id error to be returned).

Fixes ElementsProject#4238
If some parameter is malformed on the command line we could end up
with a malformed JSON-RPC request, which would then result in very
unhelpful error messages.

Fixes ElementsProject#4238
Changelog-Changed: cli: `lightning-cli` now performs better sanity checks on the JSON-RPC requests it sends.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
No reason to use a temp var here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
@rustyrussell rustyrussell merged commit 094889f into ElementsProject:master Dec 8, 2020
@cdecker cdecker deleted the issue-4258 branch December 16, 2020 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants