From: Josselin Costanzi Date: Sun, 5 Mar 2017 17:55:18 +0000 (+0100) Subject: encoding/base64: add alphabet and padding restrictions X-Git-Tag: go1.9beta1~1260 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9207a7437e382e4a05436fc26a2e24740a0492ec;p=gostls13.git encoding/base64: add alphabet and padding restrictions Document and check that the alphabet cannot contain '\n' or '\r'. Document that the alphabet cannot contain the padding character. Document that the padding character must be equal or bellow '\xff'. Document that the padding character must not be '\n' or '\r'. Fixes #19343 Fixes #19318 Change-Id: I6de0034d347ffdf317d7ea55d6fe38b01c2c4c03 Reviewed-on: https://go-review.googlesource.com/37838 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go index b15754ee93..be69271d19 100644 --- a/src/encoding/base64/base64.go +++ b/src/encoding/base64/base64.go @@ -35,13 +35,19 @@ const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678 const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" // NewEncoding returns a new padded Encoding defined by the given alphabet, -// which must be a 64-byte string. +// which must be a 64-byte string that does not contains the padding character +// or CR / LF ('\r', '\n'). // The resulting Encoding uses the default padding character ('='), // which may be changed or disabled via WithPadding. func NewEncoding(encoder string) *Encoding { if len(encoder) != 64 { panic("encoding alphabet is not 64-bytes long") } + for i := 0; i < len(encoder); i++ { + if encoder[i] == '\n' || encoder[i] == '\r' { + panic("encoding alphabet contains newline character") + } + } e := new(Encoding) e.padChar = StdPadding @@ -58,7 +64,20 @@ func NewEncoding(encoder string) *Encoding { // WithPadding creates a new encoding identical to enc except // with a specified padding character, or NoPadding to disable padding. +// The padding character must not be '\r' or '\n', must not +// be contained in the encoding's alphabet and must be a rune equal or +// below '\xff'. func (enc Encoding) WithPadding(padding rune) *Encoding { + if padding == '\r' || padding == '\n' || padding > 0xff { + panic("invalid padding") + } + + for i := 0; i < len(enc.encode); i++ { + if rune(enc.encode[i]) == padding { + panic("padding contained in alphabet") + } + } + enc.padChar = padding return &enc }