Skip to content

Commit 7e81a93

Browse files
authored
fix: correctly handle padded base64 input (#119)
Also, switch to URL-safe base64 instead of standard base64, for compatibility with original httpbin. Fixes #118.
1 parent f981f94 commit 7e81a93

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

httpbin/handlers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,6 +2828,24 @@ func TestBase64(t *testing.T) {
28282828
"/base64/encode/valid_base64_encoded_string",
28292829
"dmFsaWRfYmFzZTY0X2VuY29kZWRfc3RyaW5n",
28302830
},
2831+
{
2832+
// make sure we correctly handle padding
2833+
// https://github.com/mccutchen/go-httpbin/issues/118
2834+
"/base64/dGVzdC1pbWFnZQ==",
2835+
"test-image",
2836+
},
2837+
{
2838+
// URL-safe base64 is used for decoding (note the - instead of + in
2839+
// encoded input string)
2840+
"/base64/decode/YWJjMTIzIT8kKiYoKSctPUB-",
2841+
"abc123!?$*&()'-=@~",
2842+
},
2843+
{
2844+
// URL-safe base64 is used for encoding (note the - instead of + in
2845+
// encoded output string)
2846+
"/base64/encode/abc123%21%3F%24%2A%26%28%29%27-%3D%40~",
2847+
"YWJjMTIzIT8kKiYoKSctPUB-",
2848+
},
28312849
}
28322850

28332851
for _, test := range okTests {
@@ -2883,6 +2901,12 @@ func TestBase64(t *testing.T) {
28832901
"/base64/unknown/dmFsaWRfYmFzZTY0X2VuY29kZWRfc3RyaW5n",
28842902
"invalid operation: unknown",
28852903
},
2904+
{
2905+
// we only support URL-safe base64 encoded strings (note the +
2906+
// instead of - in encoded input string)
2907+
"/base64/decode/YWJjMTIzIT8kKiYoKSctPUB+",
2908+
"illegal base64 data",
2909+
},
28862910
}
28872911

28882912
for _, test := range errorTests {

httpbin/helpers.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse) error
184184

185185
// return provided string as base64 encoded data url, with the given content type
186186
func encodeData(body []byte, contentType string) string {
187-
data := base64.StdEncoding.EncodeToString(body)
187+
data := base64.URLEncoding.EncodeToString(body)
188188

189189
// If no content type is provided, default to application/octet-stream
190190
if contentType == "" {
@@ -370,14 +370,12 @@ func newBase64Helper(path string) (*base64Helper, error) {
370370

371371
// Encode - encode data as base64
372372
func (b *base64Helper) Encode() ([]byte, error) {
373-
buff := make([]byte, base64.StdEncoding.EncodedLen(len(b.data)))
374-
base64.StdEncoding.Encode(buff, []byte(b.data))
373+
buff := make([]byte, base64.URLEncoding.EncodedLen(len(b.data)))
374+
base64.URLEncoding.Encode(buff, []byte(b.data))
375375
return buff, nil
376376
}
377377

378378
// Decode - decode data from base64
379379
func (b *base64Helper) Decode() ([]byte, error) {
380-
buff := make([]byte, base64.StdEncoding.DecodedLen(len(b.data)))
381-
_, err := base64.StdEncoding.Decode(buff, []byte(b.data))
382-
return buff, err
380+
return base64.URLEncoding.DecodeString(b.data)
383381
}

0 commit comments

Comments
 (0)