Return an error instead of throwing on invalid UTF-8 in deserialisation helpers#1249
Open
palas wants to merge 3 commits into
Open
Return an error instead of throwing on invalid UTF-8 in deserialisation helpers#1249palas wants to merge 3 commits into
palas wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens Cardano API deserialisation helpers by avoiding Text.decodeUtf8’s impure exception on invalid UTF-8, ensuring these helpers return normal decoding errors instead of crashing.
Changes:
- Switched Bech32 deserialisation paths in
deserialiseInput/deserialiseInputAnyOfto useText.decodeUtf8'and treat invalid UTF-8 as a format mismatch. - Updated
deserialiseAnyVerificationKeyBech32to avoid throwing on invalid UTF-8 and return a decoding error instead. - Added a Herald changelog fragment documenting the bugfix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cardano-api/src/Cardano/Api/Serialise/DeserialiseAnyOf.hs | Avoids impure exceptions by using decodeUtf8' and mapping invalid UTF-8 to Bech32 format mismatch. |
| cardano-api/src/Cardano/Api/Key/Internal/SomeAddressVerificationKey.hs | Prevents invalid UTF-8 from throwing during Bech32 verification key deserialisation. |
| .changes/20260708_140000_cardano-api_pablo.lamela_nonthrowing_utf8_decode.yml | Changelog fragment describing the bugfix behavior change. |
carbolymer
reviewed
Jul 9, 2026
carbolymer
approved these changes
Jul 9, 2026
Jimbo4350
requested changes
Jul 9, 2026
Jimbo4350
left a comment
Contributor
There was a problem hiding this comment.
We shouldn't throw away the UnicodeException error. It needs to be rendered.
Instead of discarding the UnicodeException and fabricating StringToDecodeContainsInvalidChars [], deserialiseAnyVerificationKeyBech32 now returns Bech32InvalidUtf8 carrying the rendered decoding error.
Restructure the bech32 branches of deserialiseInput and deserialiseInputAnyOf as suggested in review.
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.
Context
deserialiseInput,deserialiseInputAnyOfanddeserialiseAnyVerificationKeyBech32(and therebydeserialiseAnyVerificationKey) usedText.decodeUtf8, which throws an impure exception when the input is not valid UTF-8. For example,deserialiseInputon the bytes"\xc3\x28"threw instead of returningLeft InputInvalidError.This PR switches them to
Text.decodeUtf8'and treats invalid UTF-8 as a normal decoding failure.How to trust this PR
DeserialiseInputErrorFormatMismatch/Bech32DecodingError) does not change behaviour for any valid input.Rightcase ofdecodeUtf8'.Checklist
.changes/