mmore robust svg detection with bom fucks
Ted Unangst tedu@tedunangst.com
Tue, 03 Oct 2023 22:24:09 -0400
2 files changed,
14 insertions(+),
5 deletions(-)
M
backend.go
→
backend.go
@@ -61,18 +61,25 @@ func backendSockname() string {
return dataDir + "/backend.sock" } +var bomFuck = []byte{0xef, 0xbb, 0xbf} + func isSVG(data []byte) bool { + if bytes.HasPrefix(data, bomFuck) { + data = data[3:] + } ct := http.DetectContentType(data) - if strings.HasPrefix(ct, "text/xml") { - return strings.Index(string(data), "<!DOCTYPE svg PUBLIC") != -1 - } - if strings.HasPrefix(ct, "text/plain") { - return bytes.HasPrefix(data, []byte("<svg ")) + if strings.HasPrefix(ct, "text/xml") || strings.HasPrefix(ct, "text/plain") { + if bytes.HasPrefix(data, []byte("<svg ")) || bytes.HasPrefix(data, []byte("<!DOCTYPE svg PUBLIC")) { + return true + } } return ct == "image/svg+xml" } func imageFromSVG(data []byte) (*image.Image, error) { + if bytes.HasPrefix(data, bomFuck) { + data = data[3:] + } if len(data) > 100000 { return nil, errors.New("my svg is too big") }
M
docs/changelog.txt
→
docs/changelog.txt
@@ -2,6 +2,8 @@ changelog
### next ++ Fix handling of svg with bom fucks. + + FastCGI listening. + Finally fix slow public queries.