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

Allow web-safe and unpadded base64 encodings #30

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/testdata/basic.proto3test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,5 @@ values:
- single_int32_wrapper:
"@type": type.googleapis.com/google.protobuf.Int32Value
value: 1
- single_bytes: "nopad+"
- single_bytes: "web-safe__"
8 changes: 0 additions & 8 deletions internal/testdata/dynamic.const.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 | .............................^

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 | ..................................^

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 | ................................................^

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 | ...................................................................^
Expand Down
Loading