Use unpadded base64 encoding for binary metadata headers; handle padded or unpadded input (#1209)

This commit is contained in:
dfawley
2017-04-28 11:32:27 -07:00
committed by GitHub
parent b610ffd3f8
commit 4d1604cc04
2 changed files with 8 additions and 3 deletions

View File

@ -167,12 +167,16 @@ func (d *decodeState) status() *status.Status {
const binHdrSuffix = "-bin" const binHdrSuffix = "-bin"
func encodeBinHeader(v []byte) string { func encodeBinHeader(v []byte) string {
return base64.StdEncoding.EncodeToString(v) return base64.RawStdEncoding.EncodeToString(v)
} }
func decodeBinHeader(v string) ([]byte, error) { func decodeBinHeader(v string) ([]byte, error) {
if len(v)%4 == 0 {
// Input was padded, or padding was not necessary.
return base64.StdEncoding.DecodeString(v) return base64.StdEncoding.DecodeString(v)
} }
return base64.RawStdEncoding.DecodeString(v)
}
func encodeMetadataHeader(k, v string) string { func encodeMetadataHeader(k, v string) string {
if strings.HasSuffix(k, binHdrSuffix) { if strings.HasSuffix(k, binHdrSuffix) {

View File

@ -158,7 +158,7 @@ func TestEncodeMetadataHeader(t *testing.T) {
{"key", "abc", "abc"}, {"key", "abc", "abc"},
{"KEY", "abc", "abc"}, {"KEY", "abc", "abc"},
{"key-bin", "abc", "YWJj"}, {"key-bin", "abc", "YWJj"},
{"key-bin", binaryValue, "woA="}, {"key-bin", binaryValue, "woA"},
} { } {
v := encodeMetadataHeader(test.kin, test.vin) v := encodeMetadataHeader(test.kin, test.vin)
if !reflect.DeepEqual(v, test.vout) { if !reflect.DeepEqual(v, test.vout) {
@ -178,6 +178,7 @@ func TestDecodeMetadataHeader(t *testing.T) {
}{ }{
{"a", "abc", "abc", nil}, {"a", "abc", "abc", nil},
{"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil}, {"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil},
{"key-bin", "Zm9vAGJhcg", "foo\x00bar", nil},
{"key-bin", "woA=", binaryValue, nil}, {"key-bin", "woA=", binaryValue, nil},
{"a", "abc,efg", "abc,efg", nil}, {"a", "abc,efg", "abc,efg", nil},
} { } {