Skip to content

Commit

Permalink
Add support for no padding in base64 decode (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones authored Sep 9, 2024
1 parent a8c50bf commit 1fa4f15
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
3 changes: 0 additions & 3 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ _TESTS_TO_SKIP = [
"macros/map/map_extract_keys",
"timestamps/duration_converters/get_milliseconds",

# Failing conformance test for unknown reasons
"encoders_ext/decode/hello_without_padding",

# Future enhancments.
"enums/strong_proto2",
"enums/strong_proto3",
Expand Down
11 changes: 9 additions & 2 deletions ext/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// Examples:
//
// base64.decode('aGVsbG8=') // return b'hello'
// base64.decode('aGVsbG8') // error
// base64.decode('aGVsbG8') // return b'hello'
//
// # Base64.Encode
//
Expand Down Expand Up @@ -79,7 +79,14 @@ func (encoderLib) ProgramOptions() []cel.ProgramOption {
}

func base64DecodeString(str string) ([]byte, error) {
return base64.StdEncoding.DecodeString(str)
b, err := base64.StdEncoding.DecodeString(str)
if err == nil {
return b, nil
}
if _, tryAltEncoding := err.(base64.CorruptInputError); tryAltEncoding {
return base64.RawStdEncoding.DecodeString(str)
}
return nil, err
}

func base64EncodeBytes(bytes []byte) (string, error) {
Expand Down
5 changes: 1 addition & 4 deletions ext/encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ func TestEncoders(t *testing.T) {
parseOnly bool
}{
{expr: "base64.decode('aGVsbG8=') == b'hello'"},
{
expr: "base64.decode('aGVsbG8') == b'error'",
err: "illegal base64 data at input byte 4",
},
{expr: "base64.decode('aGVsbG8') == b'hello'"},
{
expr: "base64.decode(b'aGVsbG8=') == b'hello'",
err: "no such overload",
Expand Down

0 comments on commit 1fa4f15

Please sign in to comment.