Skip to content

Conversation

Empact
Copy link
Contributor

@Empact Empact commented Sep 24, 2018

This change:

  • adds a length check to all calls to ParseHashStr, appropriate given its use to populate
    a 256-bit number from a hex str
  • allows the caller to handle the failure, which allows for the more
    appropriate JSONRPCError on failure in prioritisetransaction rpc

Relative to #14288

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Concept ACK. Looks like the new parse helper will reject strings that are hex but not exactly 64 chars. Could add a test that fails with this change, but passes without?

@@ -193,14 +193,13 @@ bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx,
return true;
}

uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
bool ParseHashStr(const std::string& strReq, uint256& v)
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could keep the name strHex and result to minimize the diff?

{
if (!IsHex(strHex)) // Note: IsHex("") is false
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
if (!IsHex(strReq) || (strReq.size() != 64))
Copy link
Contributor

@practicalswift practicalswift Sep 24, 2018

Choose a reason for hiding this comment

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

Nit: Redundant parentheses around strReq.size() != 64 :-)

uint256 txid = ParseHashStr(prevOut["txid"].get_str(), "txid");
uint256 txid;
if (!ParseHashStr(prevOut["txid"].get_str(), txid)) {
throw std::runtime_error("txid must be hexadecimal string (not '" + prevOut["txid"].get_str() + "')");
Copy link
Contributor

@practicalswift practicalswift Sep 24, 2018

Choose a reason for hiding this comment

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

I know this is unchanged from the previous version, but could there be circumstances where this opens up for log file injection? Safer not to echo the input?

uint256 hash = ParseHashStr(request.params[0].get_str(), "txid");
uint256 hash;
if (!ParseHashStr(request.params[0].get_str(), hash)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "txid must be hexadecimal string (not '" + request.params[0].get_str() + "')");
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is unchanged from the previous version, but could there be circumstances where this opens up for log file injection? Safer not to echo the input?

@practicalswift
Copy link
Contributor

Concept ACK

Thanks for taking on this issue. Closing #14288.

@Empact Empact force-pushed the parse-hash-str branch 2 times, most recently from e063b15 to a85bbe9 Compare September 24, 2018 15:55
@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 24, 2018

Reviewers, this pull request conflicts with the following ones:

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.

@Empact
Copy link
Contributor Author

Empact commented Sep 24, 2018

Rebased for #13424

@Empact
Copy link
Contributor Author

Empact commented Sep 24, 2018

Converted one parse from uint256S to ParseHashStr in bitcoin-tx. Was the only other place I could find with local validation of 64-char hex inputs.

{
if (!IsHex(strHex)) // Note: IsHex("") is false
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
if (!IsHex(strHex) || (strHex.size() != 64))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Redundant parentheses around strHex.size() != 64 :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could go either way but I like explicit precedence to guide the eye.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps swap the two boolean conditions? If the condition strHex.size() is false then the function can return without having to evaluate the condition IsHex(strHex)

if ((strHex.size() != 64) || !IsHex(strHex)) return false;

uint256 txid(uint256S(strTxid));
uint256 txid;
if (!ParseHashStr(vStrInputParts[0], txid)) {
throw std::runtime_error("txid must be hexadecimal string (not '" + vStrInputParts[0] + "')");
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is unchanged from the previous version, but could there be circumstances where this opens up for log file injection? Safer not to echo the input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This had been changed so I switched it back.

@practicalswift
Copy link
Contributor

practicalswift commented Sep 24, 2018

What about adding NODISCARD (cherry-pick ca130493faed271bc05a2ad24db98a9cf167d159) to ParseHashStr to make sure all future callers check the return value?

@Empact Empact force-pushed the parse-hash-str branch 2 times, most recently from c60cfb7 to f78c166 Compare September 25, 2018 04:20
@Empact
Copy link
Contributor Author

Empact commented Sep 25, 2018

I like NODISCARD but am inclined to keep the changes separate for easier review. h/t #13815
I reverted the change that added new user input to the exception message. Now the log impact should be net 0.

@Empact
Copy link
Contributor Author

Empact commented Sep 25, 2018

@MarcoFalke added tests, only two of them fail before this change because of existing checks:

  • Tests the check for invalid txid valid hex, but too short
  • Tests the check for invalid txid valid hex, but too long

Copy link
Contributor

@l2a5b1 l2a5b1 left a comment

Choose a reason for hiding this comment

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

utACK 909a8f6

Nice improvement!

src/core_io.h Outdated
@@ -25,7 +25,7 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true);
bool DecodeHexBlk(CBlock&, const std::string& strHexBlk);
bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
uint256 ParseHashStr(const std::string&, const std::string& strName);
bool ParseHashStr(const std::string& strHex, uint256& result);
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we can document bool ParseHashStr(const std::string& strHex, uint256& result) and specify the pre-condition that the strHex argument must be a hexadecimal string with a length of 64 bytes.

{
if (!IsHex(strHex)) // Note: IsHex("") is false
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
if (!IsHex(strHex) || (strHex.size() != 64))
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps swap the two boolean conditions? If the condition strHex.size() is false then the function can return without having to evaluate the condition IsHex(strHex)

if ((strHex.size() != 64) || !IsHex(strHex)) return false;

This change:
* adds a length check to ParseHashStr, appropriate given its use to populate
  a 256-bit number from a hex str.
* allows the caller to handle the failure, which allows for the more
  appropriate JSONRPCError on failure in prioritisetransaction rpc
@Empact
Copy link
Contributor Author

Empact commented Sep 25, 2018

Switched the condition and added docs.

@l2a5b1
Copy link
Contributor

l2a5b1 commented Sep 25, 2018

re-utACK 9c5af58

Thanks for addressing the review comments @Empact!

@maflcko
Copy link
Member

maflcko commented Sep 25, 2018

utACK 9c5af58

* @param[out] result the result of the parasing
* @returns true if successful, false if not
*
* @see ParseHashV for an RPC-oriented version of this
Copy link
Contributor

Choose a reason for hiding this comment

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

ParseHashV could use ParseHashStr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to do that as a follow-up.

@promag
Copy link
Contributor

promag commented Sep 26, 2018

utACK 9c5af58.

@practicalswift
Copy link
Contributor

utACK 9c5af58

@maflcko maflcko merged commit 9c5af58 into bitcoin:master Sep 27, 2018
maflcko pushed a commit that referenced this pull request Sep 27, 2018
9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to #14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
@Empact Empact deleted the parse-hash-str branch December 10, 2018 05:39
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 18, 2020
Summary:
9c5af58d51 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to #14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53

Backport of Core [[bitcoin/bitcoin#14307 | PR14307]]
bitcoin/bitcoin#14307

Depends on D5294

Test Plan:
  ninja check
  ninja check-functional

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5295
ftrader pushed a commit to bitcoin-cash-node/bitcoin-cash-node that referenced this pull request May 19, 2020
Summary:
9c5af58d51 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to #14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53

Backport of Core [[bitcoin/bitcoin#14307 | PR14307]]
bitcoin/bitcoin#14307

Depends on D5294

Test Plan:
  ninja check
  ninja check-functional

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5295
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Jul 30, 2021
…ashStr

9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to bitcoin#14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Aug 3, 2021
…ashStr

9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to bitcoin#14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Aug 4, 2021
…shStr

9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to bitcoin#14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Aug 4, 2021
…shStr

9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to bitcoin#14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Aug 4, 2021
…shStr

9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to bitcoin#14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Aug 4, 2021
…shStr

9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley)

Pull request description:

  This change:
  * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate
    a 256-bit number from a hex str
  * allows the caller to handle the failure, which allows for the more
    appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc

  Relative to bitcoin#14288

Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
@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
Development

Successfully merging this pull request may close these issues.

6 participants