diff options
| author | Adam Langley <agl@golang.org> | 2015-06-13 13:50:02 -0700 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2015-06-29 02:50:49 +0000 |
| commit | 0a6df4a87b8333e1029c1940c84e39fad66352fa (patch) | |
| tree | 426e5295f4ea9745056c0d8ff77330c8c07e89da /src | |
| parent | fdd921c9f40f75843838d8f0218106cf078810ed (diff) | |
| download | go-0a6df4a87b8333e1029c1940c84e39fad66352fa.tar.xz | |
encoding/asn1: don't parse invalid UTF-8.
Invalid UTF-8 triggers an error when marshaling but, previously, not
when unmarshaling. This means that ASN.1 structures were not
round-tripping.
This change makes invalid UTF-8 in a string marked as UTF-8 to be an
error when Unmarshaling.
Fixes #11126.
Change-Id: Ic37be84d21dc5c03983525e244d955a8b1e1ff14
Reviewed-on: https://go-review.googlesource.com/11056
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/encoding/asn1/asn1.go | 4 | ||||
| -rw-r--r-- | src/encoding/asn1/asn1_test.go | 18 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go index 3eab6aa384..2ac411af88 100644 --- a/src/encoding/asn1/asn1.go +++ b/src/encoding/asn1/asn1.go @@ -26,6 +26,7 @@ import ( "reflect" "strconv" "time" + "unicode/utf8" ) // A StructuralError suggests that the ASN.1 data is valid, but the Go type @@ -389,6 +390,9 @@ func parseT61String(bytes []byte) (ret string, err error) { // parseUTF8String parses a ASN.1 UTF8String (raw UTF-8) from the given byte // array and returns it. func parseUTF8String(bytes []byte) (ret string, err error) { + if !utf8.Valid(bytes) { + return "", errors.New("asn1: invalid UTF-8 string") + } return string(bytes), nil } diff --git a/src/encoding/asn1/asn1_test.go b/src/encoding/asn1/asn1_test.go index acba0965a2..893d0801b0 100644 --- a/src/encoding/asn1/asn1_test.go +++ b/src/encoding/asn1/asn1_test.go @@ -9,6 +9,7 @@ import ( "fmt" "math/big" "reflect" + "strings" "testing" "time" ) @@ -922,3 +923,20 @@ func TestTruncatedExplicitTag(t *testing.T) { t.Error("Unmarshal returned without error") } } + +type invalidUTF8Test struct { + Str string `asn1:"utf8"` +} + +func TestUnmarshalInvalidUTF8(t *testing.T) { + data := []byte("0\x05\f\x03a\xc9c") + var result invalidUTF8Test + _, err := Unmarshal(data, &result) + + const expectedSubstring = "UTF" + if err == nil { + t.Fatal("Successfully unmarshaled invalid UTF-8 data") + } else if !strings.Contains(err.Error(), expectedSubstring) { + t.Fatalf("Expected error to mention %q but error was %q", expectedSubstring, err.Error()) + } +} |
