diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go index b15754ee933..be69271d195 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 }