/
svg.go
215 lines (178 loc) · 4 KB
/
svg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package svg
import (
"bytes"
"fmt"
"io"
"strings"
nanoid "github.com/matoous/go-nanoid/v2"
"github.com/tdewolff/parse/v2"
"github.com/tdewolff/parse/v2/xml"
"github.com/imgproxy/imgproxy/v3/imagedata"
)
var feDropShadowName = []byte("feDropShadow")
const feDropShadowTemplate = `
<feMerge result="dsin-%[1]s"><feMergeNode %[3]s /></feMerge>
<feGaussianBlur %[4]s />
<feOffset %[5]s result="dsof-%[2]s" />
<feFlood %[6]s />
<feComposite in2="dsof-%[2]s" operator="in" />
<feMerge %[7]s>
<feMergeNode />
<feMergeNode in="dsin-%[1]s" />
</feMerge>
`
func Satitize(data *imagedata.ImageData) (*imagedata.ImageData, error) {
r := bytes.NewReader(data.Data)
l := xml.NewLexer(parse.NewInput(r))
buf, cancel := imagedata.BorrowBuffer()
ignoreTag := 0
var curTagName string
for {
tt, tdata := l.Next()
if ignoreTag > 0 {
switch tt {
case xml.ErrorToken:
cancel()
return nil, l.Err()
case xml.EndTagToken, xml.StartTagCloseVoidToken:
ignoreTag--
case xml.StartTagToken:
ignoreTag++
}
continue
}
switch tt {
case xml.ErrorToken:
if l.Err() != io.EOF {
cancel()
return nil, l.Err()
}
newData := imagedata.ImageData{
Data: buf.Bytes(),
Type: data.Type,
}
newData.SetCancel(cancel)
return &newData, nil
case xml.StartTagToken:
curTagName = strings.ToLower(string(l.Text()))
if curTagName == "script" {
ignoreTag++
continue
}
buf.Write(tdata)
case xml.AttributeToken:
attrName := strings.ToLower(string(l.Text()))
if _, unsafe := unsafeAttrs[attrName]; unsafe {
continue
}
if curTagName == "use" && (attrName == "href" || attrName == "xlink:href") {
val := strings.TrimSpace(strings.Trim(string(l.AttrVal()), `"'`))
if len(val) > 0 && val[0] != '#' {
continue
}
}
buf.Write(tdata)
default:
buf.Write(tdata)
}
}
}
func replaceDropShadowNode(l *xml.Lexer, buf *bytes.Buffer) error {
var (
inAttrs strings.Builder
blurAttrs strings.Builder
offsetAttrs strings.Builder
floodAttrs strings.Builder
finalAttrs strings.Builder
)
inID, _ := nanoid.New(8)
offsetID, _ := nanoid.New(8)
hasStdDeviation := false
hasDx := false
hasDy := false
TOKEN_LOOP:
for {
tt, tdata := l.Next()
switch tt {
case xml.ErrorToken:
if l.Err() != io.EOF {
return l.Err()
}
break TOKEN_LOOP
case xml.EndTagToken, xml.StartTagCloseVoidToken:
break TOKEN_LOOP
case xml.AttributeToken:
switch strings.ToLower(string(l.Text())) {
case "in":
inAttrs.Write(tdata)
case "stddeviation":
blurAttrs.Write(tdata)
hasStdDeviation = true
case "dx":
offsetAttrs.Write(tdata)
hasDx = true
case "dy":
offsetAttrs.Write(tdata)
hasDy = true
case "flood-color", "flood-opacity":
floodAttrs.Write(tdata)
default:
finalAttrs.Write(tdata)
}
}
}
if !hasStdDeviation {
blurAttrs.WriteString(` stdDeviation="2"`)
}
if !hasDx {
offsetAttrs.WriteString(` dx="2"`)
}
if !hasDy {
offsetAttrs.WriteString(` dy="2"`)
}
fmt.Fprintf(
buf, feDropShadowTemplate,
inID, offsetID,
inAttrs.String(),
blurAttrs.String(),
offsetAttrs.String(),
floodAttrs.String(),
finalAttrs.String(),
)
return nil
}
func FixUnsupported(data *imagedata.ImageData) (*imagedata.ImageData, bool, error) {
if !bytes.Contains(data.Data, feDropShadowName) {
return data, false, nil
}
r := bytes.NewReader(data.Data)
l := xml.NewLexer(parse.NewInput(r))
buf, cancel := imagedata.BorrowBuffer()
for {
tt, tdata := l.Next()
switch tt {
case xml.ErrorToken:
if l.Err() != io.EOF {
cancel()
return nil, false, l.Err()
}
newData := imagedata.ImageData{
Data: buf.Bytes(),
Type: data.Type,
}
newData.SetCancel(cancel)
return &newData, true, nil
case xml.StartTagToken:
if bytes.Equal(l.Text(), feDropShadowName) {
if err := replaceDropShadowNode(l, buf); err != nil {
cancel()
return nil, false, err
}
continue
}
buf.Write(tdata)
default:
buf.Write(tdata)
}
}
}