diff options
author | Josselin Costanzi <josselin@costanzi.fr> | 2017-03-05 18:55:18 +0100 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2017-03-07 02:53:23 +0000 |
commit | 9207a7437e382e4a05436fc26a2e24740a0492ec (patch) | |
tree | f5a93f0f10d38cc42c8e0dc124f57f464cba3fb5 /src/encoding/base64 | |
parent | 8d3f29577d95aa06b2653d20e331aa47f759db06 (diff) | |
download | go-9207a7437e382e4a05436fc26a2e24740a0492ec.tar.gz go-9207a7437e382e4a05436fc26a2e24740a0492ec.zip |
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 <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/base64')
-rw-r--r-- | src/encoding/base64/base64.go | 21 |
1 files changed, 20 insertions, 1 deletions
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 } |