AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Moderate 62 Bitcoin

Merge rust-bitcoin/rust-bitcoin#6734: units: Prevent panic when parsing non-ASCII target and work hex

Public commit record

What the developer wrote

Authored by Andrew Poelstra

91/100 · Strong
Merge rust-bitcoin/rust-bitcoin#6734: units: Prevent panic when parsing non-ASCII target and work hex

bd7d8653c2600bf4cff6361823d67c5c6cab994f units: Prevent panic when parsing non-ASCII target and work hex (questfever)

Pull request description:

## Summary

Prevent `Target` and `Work` hex parsing from panicking on certain non-ASCII UTF-8 inputs.

Add a regression test verifying that invalid non-ASCII hex input is returned as an error.

## Details

The U256 hex parser uses `str::len()`, which returns the UTF-8 byte length, to calculate the boundary between its high and low halves:

```rust
let high_len = s.len() - 32;
let high_s = &s[..high_len];
let low_s = &s[high_len..];
```


For an input such as:

`é0000000000000000000000000000000`


the string contains 32 characters but 33 bytes. This makes `high_len == 1`, which falls inside the two-byte UTF-8 encoding of `é`.

Slicing the string at that offset therefore panics.


This path is reachable through the public `Target::{from_hex, from_unprefixed_hex}` and `Work::{from_hex, from_unprefixed_hex}` APIs. Invalid hexadecimal input should produce an error rather than panic.


The parser now rejects non-ASCII input before splitting the string.


## Testing

- Added a regression test for the non-ASCII input above.
- Ran cargo test -p bitcoin-units --all-features.


ACKs for top commit:
apoelstra:
ACK bd7d8653c2600bf4cff6361823d67c5c6cab994f; successfully ran local tests


Tree-SHA512: 9e579ee9cafd889cd8b386e114636df08acfafef3eef74d7cd92d5dd165d653352f1eba5f5d1312fa253f036477da900bf26a3b951a795623ba7422f69445c7c
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference
The short version

What changed, and why it matters

This commit fixes a bug where certain unusual text inputs containing non-ASCII characters (like 'é') could crash the program when parsing Bitcoin 'Target' or 'Work' values from hexadecimal strings. The code was measuring string length in bytes instead of characters, so it could slice through the middle of a multi-byte character and panic. The fix rejects non-ASCII input before doing any byte-based slicing, turning a crash into a normal error.

Recommended action

Treat as a security-hardening fix with denial-of-service impact. Backport to maintained release branches and include in release notes. No immediate CVE required unless a downstream service processes untrusted hex strings, but users should upgrade promptly if they expose Target/Work hex parsing to external input.

Security signals we found

01

Panic on untrusted input reachable through public parsing APIs

02

String slicing by byte offset without ASCII validation

03

Denial-of-service vector via malformed hex string

04

Regression test added for non-ASCII input handling

Risk score

Why this scored 62/100

Our methodology →
Potential impact 18/30
Exploitability 12/25
Stealth signal 8/15
Affected reach 10/15
Confidence 9/10
Evidence quality 5/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.