Skip to content

Commit 4d1604c

Browse files
authored
Use unpadded base64 encoding for binary metadata headers; handle padded or unpadded input (#1209)
1 parent b610ffd commit 4d1604c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

transport/http_util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,15 @@ func (d *decodeState) status() *status.Status {
167167
const binHdrSuffix = "-bin"
168168

169169
func encodeBinHeader(v []byte) string {
170-
return base64.StdEncoding.EncodeToString(v)
170+
return base64.RawStdEncoding.EncodeToString(v)
171171
}
172172

173173
func decodeBinHeader(v string) ([]byte, error) {
174-
return base64.StdEncoding.DecodeString(v)
174+
if len(v)%4 == 0 {
175+
// Input was padded, or padding was not necessary.
176+
return base64.StdEncoding.DecodeString(v)
177+
}
178+
return base64.RawStdEncoding.DecodeString(v)
175179
}
176180

177181
func encodeMetadataHeader(k, v string) string {

transport/http_util_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestEncodeMetadataHeader(t *testing.T) {
158158
{"key", "abc", "abc"},
159159
{"KEY", "abc", "abc"},
160160
{"key-bin", "abc", "YWJj"},
161-
{"key-bin", binaryValue, "woA="},
161+
{"key-bin", binaryValue, "woA"},
162162
} {
163163
v := encodeMetadataHeader(test.kin, test.vin)
164164
if !reflect.DeepEqual(v, test.vout) {
@@ -178,6 +178,7 @@ func TestDecodeMetadataHeader(t *testing.T) {
178178
}{
179179
{"a", "abc", "abc", nil},
180180
{"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil},
181+
{"key-bin", "Zm9vAGJhcg", "foo\x00bar", nil},
181182
{"key-bin", "woA=", binaryValue, nil},
182183
{"a", "abc,efg", "abc,efg", nil},
183184
} {

0 commit comments

Comments
 (0)