fix(contract): guard the OCC-21 repair slice against non-ASCII input#810
Merged
Conversation
Contract::from_str repairs a 20-byte OCC-21 string by slicing it at byte index 5. The length check uses the byte count, so a non-ASCII input could reach 20 bytes with the split landing inside a multi-byte codepoint, panicking on a non-char-boundary slice. OCC-21 identifiers are ASCII by spec, so require ASCII before the slice and let anything else fall through to the bare-root validator, which returns a clean error. Adds a regression test for the panicking input. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A blank line before the explanatory comment stops rustfmt from aligning it to the trailing-comment column of the preceding const.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Contract::from_strrepairs a 20-byte OCC-21 identifier by peeling the trailing 15-char suffix and re-padding the root, slicing the input at byte index 5. The length guard tests the byte count (trimmed.len()), so a non-ASCII input could reach 20 bytes with the split landing inside a multi-byte codepoint. Slicing on a non-char-boundary panics, so"ABCDé260417C0055000".parse::<Contract>()aborted instead of returning an error.Fix
OCC-21 identifiers are ASCII by spec (the strict 21-char
parse_occ21already enforces this before its own slices). Requiretrimmed.is_ascii()before the repair slices; non-ASCII input falls through to the bare-root validator, which returns a cleanContract::from_strerror naming the offending input. Adds a regression test for the previously-panicking string.Test
cargo test -p thetadatadx --lib fpss::protocol::contract— 45 pass including the newfrom_str_non_ascii_at_repair_length_does_not_panic.🤖 Generated with Claude Code