Fix non-UTF8 SVG detection

This commit is contained in:
DarthSim
2020-11-11 22:13:48 +06:00
parent e2d99d397c
commit 3c9736ed8a
3 changed files with 18 additions and 1 deletions

View File

@@ -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
go.mod
View File

@@ -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
)

View File

@@ -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 {