Browse Source

Fix non-UTF8 SVG detection

DarthSim 4 years ago
parent
commit
3c9736ed8a
3 changed files with 18 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 1 0
      go.mod
  3. 16 1
      imagemeta/svg.go

+ 1 - 0
CHANGELOG.md

@@ -11,6 +11,7 @@
 ### Fix
 - Fix `dpr` option.
 - Fix non-strict SVG detection.
+- Fix non-UTF8 SVG detection.
 - Fix checking of connections in queue.
 
 ## [2.15.0] - 2020-09-03

+ 1 - 0
go.mod

@@ -26,6 +26,7 @@ require (
 	golang.org/x/image v0.0.0-20200609002522-3f4726a040e8
 	golang.org/x/net v0.0.0-20200707034311-ab3426394381
 	golang.org/x/sys v0.0.0-20200803210538-64077c9b5642
+	golang.org/x/text v0.3.3
 	google.golang.org/api v0.30.0
 )
 

+ 16 - 1
imagemeta/svg.go

@@ -3,8 +3,13 @@ package imagemeta
 import (
 	"bytes"
 	"encoding/xml"
+	"fmt"
 	"io"
+	"log"
+	"strings"
 	"sync/atomic"
+
+	"golang.org/x/text/encoding/charmap"
 )
 
 var maxSvgBytes int64 = 32 * 1024
@@ -13,6 +18,13 @@ type svgHeader struct {
 	XMLName xml.Name
 }
 
+func xmlCharsetReader(charset string, input io.Reader) (io.Reader, error) {
+	if strings.EqualFold(charset, "iso-8859-1") {
+		return charmap.ISO8859_1.NewDecoder().Reader(input), nil
+	}
+	return nil, fmt.Errorf("Unknown SVG charset: %s", charset)
+}
+
 func SetMaxSvgCheckRead(n int) {
 	atomic.StoreInt64(&maxSvgBytes, int64(n))
 }
@@ -41,8 +53,11 @@ func IsSVG(r io.Reader) (bool, error) {
 
 		dec := xml.NewDecoder(rr)
 		dec.Strict = false
-		if dec.Decode(&h); h.XMLName.Local == "svg" {
+		dec.CharsetReader = xmlCharsetReader
+		if err := dec.Decode(&h); h.XMLName.Local == "svg" {
 			return true, nil
+		} else {
+			log.Printf("SVG err: %s", err)
 		}
 
 		if len(buf) >= maxBytes {