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

Explicitly error on non-standard sighash types #233

Merged
merged 1 commit into from Jul 8, 2021

Conversation

darosior
Copy link
Contributor

@darosior darosior commented Feb 3, 2021

I forgot to append the ANYONECANPAY flag with my boutique signer for hand-testing and spent some time before realising we were actually treating the last byte of the sig as a valid SIGHASH_ALL in the finalizer !
I believe it's cleaner to error early on too short signatures.

EDIT (made after the comments below): actually if i was just using SIGHASH_ALL i could have messed up my sighash type and not notifying it. Even though checking this is on the user of the lib, i believe it would be much nicer to not silently accept invalid-by-standardness signatures in rust-miniscript.

src/psbt/finalizer.rs Outdated Show resolved Hide resolved
@apoelstra
Copy link
Member

An alternate strategy would be to just try and parse the signature with the "sighash byte" removed and fail early if that doesn't work.

@darosior
Copy link
Contributor Author

Yes, can try this approach. Alternatively we could check the hashtype is standard: what do you think about adding a new method to SigHashType upstream which would fail on non-standard types (something like from_u32_standard)?

@sanket1729
Copy link
Member

An alternate strategy would be to just try and parse the signature with the "sighash byte" removed and fail early if that doesn't work.

We already do that in the current finalizer code. Maybe this catches all errors as it unlikely that we form another valid der formatted secp signature if we remove one byte.

Treating unknown flags as all must have a backward compatibility good reason, that I can't think of right now.
https://github.com/rust-bitcoin/rust-bitcoin/blob/5bd61967b231939626dd847fd82cc5da33052dc1/src/blockdata/transaction.rs#L676-L689

I think for this case, it is useful to have a method like from_u32_standard.

@darosior
Copy link
Contributor Author

Treating unknown flags as all must have a backward compatibility good reason

Yep, this is consensus behaviour (but not standard!).

I think for this case, it is useful to have a method like from_u32_standard.

Will PR it upstream, should be trivial.

@sanket1729
Copy link
Member

Yeah, bitcoin core has aIsDefinedHashtypeSignature, but I think this may be context dependant because, in tapscript, we have a SIGHASH_DEFAULT too. But we can think about that later.

@darosior
Copy link
Contributor Author

Done here rust-bitcoin/rust-bitcoin#573

Yeah, bitcoin core has aIsDefinedHashtypeSignature, but I think this may be context dependant because, in tapscript, we have a SIGHASH_DEFAULT too. But we can think about that later.

Yes. Bitcoin-Core has EvalChecksigPreTapscript which indirectly call this check, for Tapscript it's checked in SignatureHashSchnorr.

@sanket1729
Copy link
Member

Should we close this PR?

@darosior
Copy link
Contributor Author

iirc i was waiting for a release of rust-bitcoin. Will check and close if needed

@darosior
Copy link
Contributor Author

No, yeah i'm waiting for a rust-bicoin release since #233 (comment)

@apoelstra
Copy link
Member

Is the recent 0.26.2 release sufficient?

@darosior
Copy link
Contributor Author

Yes, just pushed an update that leverages the new from_u32_standard from rust-bitcoin/rust-bitcoin#573 instead.

@darosior darosior changed the title psbt finalizer: error early on too short signatures Explicitly error on non-standard sighash types Jun 14, 2021
This moves from silently accepting non-standard sighash types with u32
to explicitly Erroring in such a case.

We should never be managing signatures with such sighash types anyways,
as they would not relay on today's Bitcoin network.

Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
@@ -757,7 +757,8 @@ where
F: FnOnce(&bitcoin::PublicKey, BitcoinSig) -> bool,
{
if let Some((sighash_byte, sig)) = sigser.split_last() {
let sighashtype = bitcoin::SigHashType::from_u32(*sighash_byte as u32);
let sighashtype = bitcoin::SigHashType::from_u32_standard(*sighash_byte as u32)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if the interpreter should use the from_u32_consensus. It is used to interpret the transactions that are already present in the blockchain. The main use is for block explorers which should accept non-standard transactions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, but i thought we shouldn't be parsing invalid-by-standardness Script? Or is it called by parse_insane_*?

Copy link
Member

Choose a reason for hiding this comment

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

The goal of parse_insane was to parse non-safe miniscripts or miniscripts containing duplicate keys or miniscripts possibly exceeding resource limits while satisfaction.

parse_insane does not currently support parsing non-standard scripts, so what you are doing in this PR seems correct. Having a non-standard miniscript is out of scope for now. This is can be addressed later(while we address #165)

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

utACk b8432e1 . We should not waste review cycles for non-standard support because

  1. It is a low-priority feature
  2. It is currently inconsistently supported/partially implemented currently.

@apoelstra
Copy link
Member

ACK b8432e1

@apoelstra apoelstra merged commit d64668d into rust-bitcoin:master Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants