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

Consistently validate txid / blockhash length and encoding in rpc calls #13424

Merged
merged 1 commit into from Sep 24, 2018

Conversation

@Empact
Copy link
Member

@Empact Empact commented Jun 8, 2018

ParseHashV validates the length and encoding of the string and throws
an informative RPC error on failure, which is as good or better than
these alternative calls.

Note I switched ParseHashV to check string length first, because
IsHex tests that the length is even, and an error like:
"must be of length 64 (not 63, for X)" is much more informative than
"must be hexadecimal string (not X)" in that case.

Split from #13420

@@ -247,7 +247,7 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)

LOCK(cs_main);

uint256 hash = ParseHashStr(request.params[0].get_str(), "txid");
uint256 hash = ParseHashV(request.params[0], "txid");
Copy link
Member Author

@Empact Empact Jun 8, 2018

Note this trades ParseHashStr's std::runtime_error-based encoding-only test for ParseHashV's JSONRPCError-based length and encoding tests.

@Empact Empact force-pushed the parse-hash-v branch 2 times, most recently from f329819 to 6b43949 Jun 8, 2018
@promag
Copy link
Member

@promag promag commented Jun 10, 2018

How about getchaintxstats?

uint256 hash = uint256S(request.params[1].get_str());

And there may be other examples.

Concept ACK.

@Empact Empact force-pushed the parse-hash-v branch 4 times, most recently from 0db81f9 to 2428c3a Jun 11, 2018
@Empact
Copy link
Member Author

@Empact Empact commented Jun 11, 2018

Thanks @promag, I audited uint256S use and found 4 more cases. Also added more testing wherever the test outcomes changed.

@Empact Empact changed the title Consistently use ParseHashV to validate hash inputs in rpc Validate txid / blockhash length and encoding separately from validity/presence in rpc calls Jun 11, 2018
@Empact Empact changed the title Validate txid / blockhash length and encoding separately from validity/presence in rpc calls Validate txid / blockhash length and encoding separately from validity / presence in rpc calls Jun 11, 2018
@Empact Empact changed the title Validate txid / blockhash length and encoding separately from validity / presence in rpc calls Validate txid / blockhash length and encoding separate from validity / presence in rpc calls Jun 11, 2018
@Empact
Copy link
Member Author

@Empact Empact commented Jun 11, 2018

Made the get_str call guaranteed, which means non-string values fail with -1, "JSON value is not a string as expected", rather than a length failure.

@Empact Empact changed the title Validate txid / blockhash length and encoding separate from validity / presence in rpc calls Consistently validate txid / blockhash length and encoding in rpc calls Jun 11, 2018
@DrahtBot
Copy link
Contributor

@DrahtBot DrahtBot commented Jun 14, 2018

Note to reviewers: This pull request conflicts with the following ones:
  • #14307 (Consolidate redundant implementations of ParseHashStr by Empact)
  • #12153 (Avoid permanent cs_main lock in getblockheader by promag)
  • #10973 (Refactor: separate wallet from node by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@DrahtBot
Copy link
Contributor

@DrahtBot DrahtBot commented Aug 2, 2018

The last travis run for this pull request was 52 days ago and is thus outdated. To trigger a fresh travis build, this pull request should be closed and re-opened.

@DrahtBot DrahtBot closed this Aug 2, 2018
@DrahtBot DrahtBot reopened this Aug 2, 2018
Copy link
Member

@kallewoof kallewoof left a comment

utACK

Small nit: you're mixing uint256 hash = ParseHashV(...) and uint256 hash(ParseHashV(...)). Would be cool to do only one (latter one looks better IMO but either works).

ParseHashV validates the length and encoding of the string and throws
an informative RPC error on failure, which is as good or better than
these alternative calls.

Note I switched ParseHashV to check string length first, because
IsHex tests that the length is even, and an error like:
"must be of length 64 (not 63, for X)" is much more informative than
"must be hexadecimal string (not X)"
@Empact
Copy link
Member Author

@Empact Empact commented Aug 7, 2018

@kallewoof fair enough since I'm touching those lines anyway. Updated.

Copy link
Member

@kallewoof kallewoof left a comment

re-utACK 5eb20f8

@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Aug 8, 2018

utACK 5eb20f8

@MarcoFalke MarcoFalke merged commit 5eb20f8 into bitcoin:master Sep 24, 2018
2 checks passed
MarcoFalke added a commit that referenced this issue Sep 24, 2018
…ing in rpc calls

5eb20f8 Consistently use ParseHashV to validate hash inputs in rpc (Ben Woosley)

Pull request description:

  ParseHashV validates the length and encoding of the string and throws
  an informative RPC error on failure, which is as good or better than
  these alternative calls.

  Note I switched ParseHashV to check string length first, because
  IsHex tests that the length is even, and an error like:
  "must be of length 64 (not 63, for X)" is much more informative than
  "must be hexadecimal string (not X)" in that case.

  Split from #13420

Tree-SHA512: f0786b41c0d7793ff76e4b2bb35547873070bbf7561d510029e8edb93f59176277efcd4d183b3185532ea69fc0bbbf3dbe9e19362e8017007ae9d51266cd78ae
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this issue Feb 18, 2020
…ing in rpc calls

Summary:
5eb20f81d9 Consistently use ParseHashV to validate hash inputs in rpc (Ben Woosley)

Pull request description:

  ParseHashV validates the length and encoding of the string and throws
  an informative RPC error on failure, which is as good or better than
  these alternative calls.

  Note I switched ParseHashV to check string length first, because
  IsHex tests that the length is even, and an error like:
  "must be of length 64 (not 63, for X)" is much more informative than
  "must be hexadecimal string (not X)" in that case.

  Split from #13420

Tree-SHA512: f0786b41c0d7793ff76e4b2bb35547873070bbf7561d510029e8edb93f59176277efcd4d183b3185532ea69fc0bbbf3dbe9e19362e8017007ae9d51266cd78ae

Backport of Core [[bitcoin/bitcoin#13424 | PR13424]]
bitcoin/bitcoin#13424

Depends on D5292

Test Plan:
  ninja check
  ninja check-functional

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5294
ftrader pushed a commit to bitcoin-cash-node/bitcoin-cash-node that referenced this issue Apr 16, 2020
…ing in rpc calls

Summary:
5eb20f81d9 Consistently use ParseHashV to validate hash inputs in rpc (Ben Woosley)

Pull request description:

  ParseHashV validates the length and encoding of the string and throws
  an informative RPC error on failure, which is as good or better than
  these alternative calls.

  Note I switched ParseHashV to check string length first, because
  IsHex tests that the length is even, and an error like:
  "must be of length 64 (not 63, for X)" is much more informative than
  "must be hexadecimal string (not X)" in that case.

  Split from #13420

Tree-SHA512: f0786b41c0d7793ff76e4b2bb35547873070bbf7561d510029e8edb93f59176277efcd4d183b3185532ea69fc0bbbf3dbe9e19362e8017007ae9d51266cd78ae

Backport of Core [[bitcoin/bitcoin#13424 | PR13424]]
bitcoin/bitcoin#13424

Depends on D5292

Test Plan:
  ninja check
  ninja check-functional

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5294

ABC cherry-picker: Nico Guiton <nico@bitframe.org>
pravblockc added a commit to pravblockc/dash that referenced this issue Jul 23, 2021
…nd encoding in rpc calls

5eb20f8 Consistently use ParseHashV to validate hash inputs in rpc (Ben Woosley)

Pull request description:

  ParseHashV validates the length and encoding of the string and throws
  an informative RPC error on failure, which is as good or better than
  these alternative calls.

  Note I switched ParseHashV to check string length first, because
  IsHex tests that the length is even, and an error like:
  "must be of length 64 (not 63, for X)" is much more informative than
  "must be hexadecimal string (not X)" in that case.

  Split from bitcoin#13420

Tree-SHA512: f0786b41c0d7793ff76e4b2bb35547873070bbf7561d510029e8edb93f59176277efcd4d183b3185532ea69fc0bbbf3dbe9e19362e8017007ae9d51266cd78ae
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants