Skip to content

Commit bfd0c47

Browse files
authored
Kd/fix allow svg doctype (#14344)
* make svg regex case-insensitive & use strict word boundary * allow doctype svg * add doctype tests * allow <!DOCTYPE svg> and <svg/>
1 parent a21adf9 commit bfd0c47

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

modules/base/tool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const sniffLen = 512
3535
// SVGMimeType MIME type of SVG images.
3636
const SVGMimeType = "image/svg+xml"
3737

38-
var svgTagRegex = regexp.MustCompile(`(?s)\A\s*(?:<!--.*?-->\s*)*<svg\b`)
39-
var svgTagInXMLRegex = regexp.MustCompile(`(?s)\A<\?xml\b.*?\?>\s*(?:<!--.*?-->\s*)*<svg\b`)
38+
var svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(<!--.*?-->|<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>\/]`)
39+
var svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!--.*?-->|<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>\/]`)
4040

4141
// EncodeMD5 encodes string to md5 hex value.
4242
func EncodeMD5(str string) string {

modules/base/tool_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ func TestIsSVGImageFile(t *testing.T) {
216216
assert.True(t, IsSVGImageFile([]byte(`<!-- Multiline
217217
Comment -->
218218
<svg></svg>`)))
219+
assert.True(t, IsSVGImageFile([]byte(`<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Basic//EN"
220+
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd">
221+
<svg></svg>`)))
219222
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
220223
<!-- Comment -->
221224
<svg></svg>`)))
@@ -227,6 +230,11 @@ func TestIsSVGImageFile(t *testing.T) {
227230
<!-- Multline
228231
Comment -->
229232
<svg></svg>`)))
233+
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
234+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
235+
<!-- Multline
236+
Comment -->
237+
<svg></svg>`)))
230238
assert.False(t, IsSVGImageFile([]byte{}))
231239
assert.False(t, IsSVGImageFile([]byte("svg")))
232240
assert.False(t, IsSVGImageFile([]byte("<svgfoo></svgfoo>")))

0 commit comments

Comments
 (0)