From 5335374f02a2bc8e998549fe893f34415ded3155 Mon Sep 17 00:00:00 2001 From: Alfred Fuller Date: Sat, 3 Feb 2024 12:11:47 -0800 Subject: [PATCH] Allow web-safe and unpadded base64 encodings --- decode.go | 10 +++++++++- internal/testdata/basic.proto3test.yaml | 2 ++ internal/testdata/dynamic.const.txt | 8 -------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/decode.go b/decode.go index 7208c01..4dfc1f5 100644 --- a/decode.go +++ b/decode.go @@ -227,8 +227,16 @@ func (u *unmarshaler) unmarshalBytes(node *yaml.Node) []byte { return nil } + enc := base64.StdEncoding + if strings.ContainsAny(node.Value, "-_") { + enc = base64.URLEncoding + } + if len(node.Value)%4 != 0 { + enc = enc.WithPadding(base64.NoPadding) + } + // base64 decode the value. - data, err := base64.StdEncoding.DecodeString(node.Value) + data, err := enc.DecodeString(node.Value) if err != nil { u.addErrorf(node, "invalid base64: %v", err) } diff --git a/internal/testdata/basic.proto3test.yaml b/internal/testdata/basic.proto3test.yaml index 06f886f..03afa62 100644 --- a/internal/testdata/basic.proto3test.yaml +++ b/internal/testdata/basic.proto3test.yaml @@ -134,3 +134,5 @@ values: - single_int32_wrapper: "@type": type.googleapis.com/google.protobuf.Int32Value value: 1 + - single_bytes: "nopad+" + - single_bytes: "web-safe__" diff --git a/internal/testdata/dynamic.const.txt b/internal/testdata/dynamic.const.txt index b2cdafb..db109cc 100644 --- a/internal/testdata/dynamic.const.txt +++ b/internal/testdata/dynamic.const.txt @@ -66,14 +66,6 @@ internal/testdata/dynamic.const.yaml:55:30 invalid base64: illegal base64 data a 55 | repeated_bytes: [1, "1", 1.5, hi, Infinity, NaN, "Zg==", true, false, null] 55 | ^................................................. invalid base64: illegal base64 data at input byte 1 -internal/testdata/dynamic.const.yaml:55:35 invalid base64: illegal base64 data at input byte 0 - 55 | repeated_bytes: [1, "1", 1.5, hi, Infinity, NaN, "Zg==", true, false, null] - 55 | ^............................................ invalid base64: illegal base64 data at input byte 0 - -internal/testdata/dynamic.const.yaml:55:49 invalid base64: illegal base64 data at input byte 0 - 55 | repeated_bytes: [1, "1", 1.5, hi, Infinity, NaN, "Zg==", true, false, null] - 55 | ^.............................. invalid base64: illegal base64 data at input byte 0 - internal/testdata/dynamic.const.yaml:55:68 invalid base64: illegal base64 data at input byte 4 55 | repeated_bytes: [1, "1", 1.5, hi, Infinity, NaN, "Zg==", true, false, null] 55 | ^........... invalid base64: illegal base64 data at input byte 4