-
Notifications
You must be signed in to change notification settings - Fork 2
/
sanitize_test.go
212 lines (204 loc) · 4.66 KB
/
sanitize_test.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
package gosanitize
import (
"fmt"
"testing"
)
// element 0 is actual, element 1 is expected
type T [2]string
func run_sanitize_tests(t *testing.T, tt []T, s *sanitizer) {
for _, in := range tt {
input := in[0]
expected := in[1]
actual, err := s.Sanitize(input)
if err != nil {
fmt.Println("==== Test: with error: ", err, " ====")
fmt.Printf("Input:[%#v]-Expected:[%#v]-Actual:[%#v]\n", input, expected, actual)
}
if actual != expected {
t.Errorf("\nInput:[%#v]\nExpected:[%#v]\nActual:[%#v]\n", input, expected, actual)
}
}
}
func Test_Sanitize_StripTags(t *testing.T) {
tt := []T{
T{
"<html>html document</html>",
"html document",
},
T{
`a link: <a href="http://example.com/">example.com</a>`,
`a link: example.com`,
},
T{
`<script>alert(1);</script>`,
``,
},
T{
`<meta name="abc" value="123">`,
``,
},
T{
`<span style="font-size:100">font</span>`,
`font`,
},
T{
`<a href="//abc.com"><b>abc.com</b></a><span> a Good site</span>`,
`abc.com a Good site`,
},
T{
`<<p>paragraphs</p>`,
`<paragraphs`,
},
}
s := NewStrip()
run_sanitize_tests(t, tt, s)
}
func Test_Sanitize_DefaultList(t *testing.T) {
tt := []T{
T{
"<html>html document</html>",
"html document",
},
T{
`a link: <a href="http://example.com/">example.com</a>`,
`a link: <a href="http://example.com/" rel="nofollow" target="_blank">example.com</a>`,
},
T{
`<script>alert(1);</script>`,
``,
},
T{
`<meta name="abc" value="123">`,
``,
},
T{
`<span style="font-size:100">font</span>`,
`font`,
},
T{
`<pre>Pre</pre>`,
`<pre>Pre</pre>`,
},
T{
`<this>Ignore This Tag</this>`,
`Ignore This Tag`,
},
T{
`<iframe>Not allow by default</iframe>`,
`Not allow by default`,
},
T{
`<p>default p</p>`,
`<p>default p</p>`,
},
T{
`<a href="/abc">123</a>`,
`<a href="/abc">123</a>`,
},
T{
`<a href="/abc" noattr="ignore">123</a>`,
`<a href="/abc">123</a>`,
},
T{ // use ForceTargetBlank = true to correct attr to _blank
`<a href="/abc" target="default">123</a>`,
`<a href="/abc" target="default">123</a>`,
},
T{ // use ForceHrefLink = true to prevent href to be empty
`<a href="javascript:alert(1);">javascript</a>`,
`<a href="http://javascript:alert(1);" rel="nofollow" target="_blank">javascript</a>`,
},
}
s := NewDefault()
run_sanitize_tests(t, tt, s)
}
func Test_Sanitize_TagsList(t *testing.T) {
tt := []T{
T{
"<html>html document</html>",
"html document",
},
T{
`<h1>not support heading</h1>`,
`not support heading`,
},
T{
`<pre>Pre</pre>`,
`<pre>Pre</pre>`,
},
T{
`<this>Ignore This Tag</this>`,
`Ignore This Tag`,
},
T{
`<iframe>Not allow by default</iframe>`,
`Not allow by default`,
},
T{
`<p class="no this attr">attr disallow</p>`,
`<p>attr disallow</p>`,
},
T{
`<img src="abc">`,
`<img src="http://abc">`,
},
T{
`<img src="abc" alt="not allow">`,
`<img src="http://abc">`,
},
T{ // escape attr
`<img src="<wtf>">`,
`<img src="http://<wtf>">`,
},
T{ // escape text
`<a href="/abc"><>&$#!!#+><|</a>`,
`<a href="/abc"><>&$#!!#+><|</a>`,
},
T{
`<a href="/abc" noattr="ignore">123</a>`,
`<a href="/abc">123</a>`,
},
T{ // use ForceTargetBlank but it's internal link
`<a href="/abc" target="default">123</a>`,
`<a href="/abc">123</a>`,
},
T{ // use ForceTargetBlank and ForceHrefLink
`<a href="abc.com" target="default">123</a>`,
`<a href="http://abc.com" rel="nofollow" target="_blank">123</a>`,
},
T{ // use ForceHrefLink = true
`<a href="javascript:alert(1);">javascript</a>`,
`<a href="http://javascript:alert(1);" rel="nofollow" target="_blank">javascript</a>`,
},
T{ // force target/rel,
`a link: <a href="http://example.com/">example.com</a>`,
`a link: <a href="http://example.com/" rel="nofollow" target="_blank">example.com</a>`,
},
}
s := New()
s.Elements = []string{"a", "br", "p", "pre", "img"}
s.Attributes = []string{"href", "src"}
s.URISchemes = []string{"http", "https", "ftp"}
s.StrictMode()
run_sanitize_tests(t, tt, s)
}
func Test_Sanitize_TagsPairs(t *testing.T) {
tt := []T{
T{ // the html pkg not take here as a correct start tag
`<<p>Why SO?</p>`,
`<<p>Why SO?</p>`,
},
T{ // all escape here
`<<<<<<pre>Why Why<<<</pre>`,
`<<<<<<pre>Why Why<<<</pre>`,
},
T{ // why here is ok
`<pre>so what</pre>>>>>>`,
`<pre>so what</pre>>>>>>`,
},
}
s := New()
s.Elements = []string{"a", "br", "p", "pre", "img"}
s.Attributes = []string{"href", "src"}
s.EnsureInPairs = false
run_sanitize_tests(t, tt, s)
}