Skip to content

Commit be77cb8

Browse files
committed
add more SVG checks, remove unnecessary <svg/> test case
1 parent 68e1b6a commit be77cb8

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

modules/typesniffer/typesniffer.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package typesniffer
55

66
import (
7+
"bytes"
78
"fmt"
89
"io"
910
"net/http"
@@ -25,8 +26,8 @@ const (
2526

2627
var (
2728
svgComment = regexp.MustCompile(`(?s)<!--.*?-->`)
28-
svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>/]`)
29-
svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>/]`)
29+
svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg\b`)
30+
svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg\b`)
3031
)
3132

3233
// SniffedType contains information about a blobs type.
@@ -80,6 +81,8 @@ func (ct SniffedType) GetMimeType() string {
8081
return strings.SplitN(ct.contentType, ";", 2)[0]
8182
}
8283

84+
var svgCloseTag = []byte("</svg>")
85+
8386
// DetectContentType extends http.DetectContentType with more content types. Defaults to text/unknown if input is empty.
8487
func DetectContentType(data []byte) SniffedType {
8588
if len(data) == 0 {
@@ -94,16 +97,16 @@ func DetectContentType(data []byte) SniffedType {
9497

9598
// SVG is unsupported by http.DetectContentType, https://github.com/golang/go/issues/15888
9699

97-
if strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html") {
98-
dataNoComment := svgComment.ReplaceAll(data, nil)
99-
if svgTagRegex.Match(dataNoComment) {
100-
ct = SvgMimeType
101-
}
102-
}
103-
if strings.Contains(ct, "text/xml") {
104-
dataNoComment := svgComment.ReplaceAll(data, nil)
105-
if svgTagInXMLRegex.Match(dataNoComment) {
106-
ct = SvgMimeType
100+
detectByHTML := strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html")
101+
detectByXML := strings.Contains(ct, "text/xml")
102+
if detectByHTML || detectByXML {
103+
dataProcessed := svgComment.ReplaceAll(data, nil)
104+
dataProcessed = bytes.TrimSpace(dataProcessed)
105+
if bytes.HasSuffix(dataProcessed, svgCloseTag) {
106+
if detectByHTML && svgTagRegex.Match(dataProcessed) ||
107+
detectByXML && svgTagInXMLRegex.Match(dataProcessed) {
108+
ct = SvgMimeType
109+
}
107110
}
108111
}
109112

modules/typesniffer/typesniffer_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func TestIsSvgImage(t *testing.T) {
2828
assert.True(t, DetectContentType([]byte("<svg></svg>")).IsSvgImage())
2929
assert.True(t, DetectContentType([]byte(" <svg></svg>")).IsSvgImage())
3030
assert.True(t, DetectContentType([]byte(`<svg width="100"></svg>`)).IsSvgImage())
31-
assert.True(t, DetectContentType([]byte("<svg/>")).IsSvgImage())
3231
assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?><svg></svg>`)).IsSvgImage())
3332
assert.True(t, DetectContentType([]byte(`<!-- Comment -->
3433
<svg></svg>`)).IsSvgImage())

0 commit comments

Comments
 (0)