Accept URL-safe and unpadded base64 in from_dict for bytes fields#227
Open
gaoflow wants to merge 1 commit into
Open
Accept URL-safe and unpadded base64 in from_dict for bytes fields#227gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
The proto3 JSON mapping accepts bytes encoded with either the standard or URL-safe base64 alphabet, with or without padding, and the reference protobuf implementation decodes all of these. from_dict used plain base64.b64decode, which only handles the standard alphabet: it silently discarded the URL-safe '-'/'_' characters (corrupting the value to empty bytes) and rejected unpadded input with 'Incorrect padding'. Normalize to the standard alphabet and restore the optional padding before decoding.
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.
The proto3 JSON mapping accepts a
bytesvalue encoded with either the standard or URL-safe base64 alphabet, with or without padding, and the reference protobuf implementation decodes all of these forms.from_dictused plainbase64.b64decode, which only handles the standard alphabet:-/_) was silently corrupted —b64decodedrops the non-standard characters, so e.g.from_dict({"v": "--__"})returnedb""instead of the real bytes, with no error.binascii.Error: Incorrect padding.Fix
Normalize to the standard alphabet and restore the optional padding before decoding (
betterproto2/src/betterproto2/__init__.py):Standard padded input is unchanged; URL-safe and unpadded inputs now decode correctly.
Test
tests/test_from_dict_base64.pyparametrizes the four accepted forms (standard/URL-safe x padded/unpadded) plus ato_dict/from_dictround trip. Without the fix, the unpadded and URL-safe cases fail (corruption /Incorrect padding); with it, all pass.This change was prepared by an AI agent under my direction; I reviewed and verified it.