test: return False for a too-short ECDSA signature #36186

pull fametrano wants to merge 1 commits into bitcoin:master from fametrano:test-verify-ecdsa-len changing 1 files +11 −2
  1. fametrano commented at 6:06 PM on September 7, 2026: contributor

    verify_ecdsa reads sig[1] before it checks len(sig) < 4, so a 0- or 1-byte signature raises IndexError instead of returning False, which the comment promises for any DER encoding error.

    Move the length check ahead of the byte access, and add a regression test.

  2. DrahtBot added the label Tests on Sep 7, 2026
  3. DrahtBot commented at 6:07 PM on September 7, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36186.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK optout21, sedited

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. in test/functional/test_framework/key.py:70 in bea69bc600
      64 | @@ -65,10 +65,10 @@ def verify_ecdsa(self, sig, msg, low_s=True):
      65 |  
      66 |          # Extract r and s from the DER formatted signature. Return false for
      67 |          # any DER encoding errors.
      68 | -        if (sig[1] + 2 != len(sig)):
      69 | -            return False
      70 |          if (len(sig) < 4):
      71 |              return False
      72 | +        if (sig[1] + 2 != len(sig)):
    


    optout21 commented at 12:02 PM on September 9, 2026:

    bea69bc test: return False for a too-short ECDSA signature:

    Nit: I suggest moving this if one more down, so that the checks for the 0th, 1st and 2nd bytes are in natural order.

  5. in test/functional/test_framework/key.py:321 in bea69bc600
     316 | +        """A signature too short to hold a DER header returns False, not IndexError."""
     317 | +        privkey = ECKey()
     318 | +        privkey.set(generate_privkey(), compressed=True)
     319 | +        pubkey = privkey.get_pubkey()
     320 | +        msg = bytes(32)
     321 | +        for sig in [b'', b'\x30', b'\x30\x00', b'\x30\x00\x00']:
    


    optout21 commented at 12:03 PM on September 9, 2026:

    bea69bc test: return False for a too-short ECDSA signature:

    I suggest using slightly adjusted values, so that the bytes satisfy other conditions (even though the length requirement will fail them):

            for sig in [b'', b'\x30', b'\x30\x00', b'\x30\x01\x02']:
    
  6. optout21 commented at 12:05 PM on September 9, 2026: contributor

    ACK bea69bc600c97c94b1c0bf738b92640e8b06bf03

    Trivial minor fix to key.py test code. Left some minor comments.

  7. test: return False for a too-short ECDSA signature
    verify_ecdsa reads sig[1] before it checks len(sig) < 4, so a 0- or
    1-byte signature raises IndexError instead of returning False, which the
    comment promises for any DER encoding error. Move the length check
    first, and add a regression test.
    00d0c5107b
  8. fametrano force-pushed on Sep 9, 2026
  9. fametrano commented at 2:06 PM on September 9, 2026: contributor

    Thanks — both addressed. The length check now runs before any indexed access into sig (len(sig) < 4 first, then the sig[0]/sig[1]/sig[2] checks in index order), and the test exercises [b'', b'\x30', b'\x30\x00', b'\x30\x01\x02'].

  10. optout21 commented at 8:56 AM on September 10, 2026: contributor

    reACK 00d0c5107b92c8972ab106c680579e06fd4fb100

    A simple test-only change. Fixes a harmless evident bug in a test code. The effect was, that for some low-prio special test data the test triggered an exception instead of properly failing. The change is simple, and a test for it has been added to it as well. Verified that the test passes, and that it fails without the fix.

  11. sedited approved
  12. sedited commented at 9:09 AM on September 10, 2026: contributor

    ACK 00d0c5107b92c8972ab106c680579e06fd4fb100

  13. fametrano force-pushed on Sep 12, 2026
  14. fametrano force-pushed on Sep 12, 2026
  15. DrahtBot added the label CI failed on Sep 12, 2026
  16. DrahtBot removed the label CI failed on Sep 12, 2026
  17. sedited merged this on Sep 14, 2026
  18. sedited closed this on Sep 14, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-09-21 01:52 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me