Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix base64 error when input is multiple of 4 bytes #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alext
Copy link

@alext alext commented Sep 28, 2024

The base64Decode was incorrectly re-adding padding when the input is a multiple of 4 bytes. These input lengths should have no padding, but it was adding 1, which led to an error: illegal base64 data at input byte 4.

illegal base64 data at input byte 4

The base64 package has options to omit padding using WithPadding(base64.NoPadding)[1], and also defines Raw versions of the pre-defined encoders[2]. This therefore updates the functions to use these encoders instead, which means they're now just simple wrappers.

I suspect this hasn't been an issue before now, because neither the timestamp or signature encode to a base64 value which is a multiple of 4 bytes. I ran into this while working on extending this to implement URLSafeTimedSerializer that's present in the Python version, which also handles base64 encoding the value, which can be an arbirtary length.

[1]https://pkg.go.dev/encoding/base64#Encoding.WithPadding
[2]https://pkg.go.dev/encoding/base64#pkg-variables

The base64Decode function was incorrectly re-adding padding when the
input is a multiple of 4 bytes. These input lengths should have no
padding, but it was adding 1, which led to an error:

> illegal base64 data at input byte 4

The base64 package has options to omit padding using
`WithPadding(base64.NoPadding)`[1], and also defines Raw versions of the
pre-defined encoders[2]. This therefore updates the functions to use
these encoders instead, which means they're now just simple wrappers.

[1]https://pkg.go.dev/encoding/base64#Encoding.WithPadding
[2]https://pkg.go.dev/encoding/base64#pkg-variables
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.

1 participant