Skip to content

fix: bounds-check DER length fields to prevent IndexError on malformed input - #233

Closed
abhi-0203 wants to merge 1 commit into
ofek:masterfrom
abhi-0203:fix/der-indexerror
Closed

abhi-0203 wants to merge 1 commit into
ofek:masterfrom
abhi-0203:fix/der-indexerror

Conversation

@abhi-0203

Copy link
Copy Markdown

Summary

decode_length and decode_der trusted attacker-controlled length bytes without validating offsets against the buffer. Malformed DER input (e.g. a length field claiming 254 bytes from a 66-byte buffer) raised IndexError instead of the documented ValueError.

Any service doing except ValueError around PrivateKey.from_der/from_pem on untrusted input would crash with an unhandled exception.

Changes

decode_length:

  • Check offset >= len(data) before reading the length byte
  • Reject zero-length long form (num_length_bytes == 0)
  • Validate offset + num_length_bytes <= len(data) before the long-form loop

decode_der:

  • Add offset >= len(data) guards before each tag check after decode_length + offset arithmetic
  • All malformed input now raises ValueError with descriptive messages

Verification

All five test cases pass:

  1. \x30\xfe + 64-byte padding → ValueError (was IndexError)
  2. Truncated SEQUENCE → ValueError (was IndexError)
  3. Empty input → ValueError
  4. Zero-length long form → ValueError
  5. Long-form length exceeding buffer → ValueError

Closes #229

…d input

decode_length and decode_der trusted attacker-controlled length bytes
without validating offsets against the buffer. Malformed DER input
(e.g. a length field claiming 254 bytes from a 66-byte buffer) raised
IndexError instead of the documented ValueError.

Add bounds checking in decode_length (offset bounds, num_length_bytes
overflow, zero-length long form) and in decode_der (validate offsets
after each decode_length call before indexing into the buffer).

Closes #229
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.

decode_der raises IndexError on malformed length fields

1 participant