Skip to content

encoding/base64: wrongly decodes bad base64 as valid #15656

Closed
@harshavardhana

Description

@harshavardhana

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?

go1.6

  1. What operating system and processor architecture are you using (go env)?
    All operating systems.
  2. What did you do?
    https://play.golang.org/p/DiwWTDmGyC
package main

import (
    "encoding/base64"
    "encoding/hex"
    "fmt"
)

func main() {
    // Good base64.
    goodBytes, err := base64.StdEncoding.DecodeString("WvLTlMrX9NpYDQlEIFlnDA==")
    if err != nil {
        fmt.Println(err)
        return
    }
    // Bad base64.
    badBytes, err := base64.StdEncoding.DecodeString("WvLTlMrX9NpYDQlEIFlnDB==")
    if err != nil {
        fmt.Println(err)
        return
    }
    goodHex := hex.EncodeToString(goodBytes)
    badHex := hex.EncodeToString(badBytes)
    fmt.Println(goodHex)
    fmt.Println(badHex)

    goodBytes, err = hex.DecodeString(goodHex)
    if err != nil {
        fmt.Println(err)
        return
    }

    badBytes, err = hex.DecodeString(badHex)
    if err != nil {
        fmt.Println(err)
        return
    }

    goodBase64 := base64.StdEncoding.EncodeToString(goodBytes)
    badBase64 := base64.StdEncoding.EncodeToString(badBytes)
    fmt.Println(goodBase64)
    fmt.Println(badBase64)
}
  1. What did you expect to see?

Error for the second bad base64

    badBytes, err := base64.StdEncoding.DecodeString("WvLTlMrX9NpYDQlEIFlnDB==")

Here err should be non 'nil'.

An example of ruby decoder throwing ArgumentError for similar conditions under RFC4648.

require "base64"

good = Base64.strict_decode64("WvLTlMrX9NpYDQlEIFlnDA==") ## RFC4648
puts good.length

begin
  bad = Base64.strict_decode64("WvLTlMrX9NpYDQlEIFlnDB==") ## RFC4648
  puts bad.length
rescue ArgumentError
  puts "Invalid base64"
end
  1. What did you see instead?

Success for the second bad base64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions