From 4d1604cc04415f0ebb739b295f8b4165c88b76a5 Mon Sep 17 00:00:00 2001 From: dfawley Date: Fri, 28 Apr 2017 11:32:27 -0700 Subject: [PATCH] Use unpadded base64 encoding for binary metadata headers; handle padded or unpadded input (#1209) --- transport/http_util.go | 8 ++++++-- transport/http_util_test.go | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/transport/http_util.go b/transport/http_util.go index e5120ebf0079..795d5d18a4f8 100644 --- a/transport/http_util.go +++ b/transport/http_util.go @@ -167,11 +167,15 @@ func (d *decodeState) status() *status.Status { const binHdrSuffix = "-bin" func encodeBinHeader(v []byte) string { - return base64.StdEncoding.EncodeToString(v) + return base64.RawStdEncoding.EncodeToString(v) } func decodeBinHeader(v string) ([]byte, error) { - return base64.StdEncoding.DecodeString(v) + if len(v)%4 == 0 { + // Input was padded, or padding was not necessary. + return base64.StdEncoding.DecodeString(v) + } + return base64.RawStdEncoding.DecodeString(v) } func encodeMetadataHeader(k, v string) string { diff --git a/transport/http_util_test.go b/transport/http_util_test.go index 8e2c488d8717..b3698018bdab 100644 --- a/transport/http_util_test.go +++ b/transport/http_util_test.go @@ -158,7 +158,7 @@ func TestEncodeMetadataHeader(t *testing.T) { {"key", "abc", "abc"}, {"KEY", "abc", "abc"}, {"key-bin", "abc", "YWJj"}, - {"key-bin", binaryValue, "woA="}, + {"key-bin", binaryValue, "woA"}, } { v := encodeMetadataHeader(test.kin, test.vin) if !reflect.DeepEqual(v, test.vout) { @@ -178,6 +178,7 @@ func TestDecodeMetadataHeader(t *testing.T) { }{ {"a", "abc", "abc", nil}, {"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil}, + {"key-bin", "Zm9vAGJhcg", "foo\x00bar", nil}, {"key-bin", "woA=", binaryValue, nil}, {"a", "abc,efg", "abc,efg", nil}, } {