]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base64: add alphabet and padding restrictions
authorJosselin Costanzi <josselin@costanzi.fr>
Sun, 5 Mar 2017 17:55:18 +0000 (18:55 +0100)
committerIan Lance Taylor <iant@golang.org>
Tue, 7 Mar 2017 02:53:23 +0000 (02:53 +0000)
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>

src/encoding/base64/base64.go

index b15754ee933865706f414788cdb469266ac01fbd..be69271d195129ca56bb4c7f0be373c798cfbd9f 100644 (file)
@@ -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
 }