-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
249 lines (239 loc) · 7.32 KB
/
parser_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"strings"
"testing"
)
func TestParseID(t *testing.T) {
tests := []struct {
name string
path string
want int64
wantErr bool
}{
{"Valid thread ID", "/thread/1263", 1263, false},
{"Valid member ID", "/member/23", 23, false},
{"Invalid member path", "/member/abc", 0, true},
{"Invalid thread path", "/thread/abc", 0, true},
{"Edit thread", "/thread/1263/edit", 0, true},
{"Edit member", "/member/23/edit", 0, true},
{"Edit thread post", "/thread/1263/23/edit", 0, true},
{"Empty path", "", 0, true},
{"Missing thread ID", "/thread/", 0, true},
{"Missing member ID", "/member/", 0, true},
{"Extra thread characters", "/thread/123/extra", 0, true},
{"Extra member characters", "/member/123/extra", 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseID(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("parseID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("parseID() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseHTMLLessStrict(t *testing.T) {
testCases := []struct {
name string
input string
expected string
}{
{
name: "Script tag removal",
input: `<p>Hello</p><script>alert('XSS');</script>`,
expected: `<p>Hello</p>`,
},
{
name: "No anchor tags",
input: `<a href="javascript:alert('XSS')">Click me</a>`,
expected: `Click me`,
},
{
name: "Iframe removal",
input: `<iframe src="https://malicious-site.com"></iframe>`,
expected: ``,
},
{
name: "On* event handler removal",
input: `<img src="image.jpg" onerror="alert('XSS')">`,
expected: `<img src="image.jpg">`,
},
{
// We do not allow img tags with data in the src
name: "Data URL removal",
input: `<img src="data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KDEpIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==">`,
expected: ``,
},
{
name: "CSS expression removal",
input: `<div style="background-image: url('javascript:alert(\'XSS\')')">Content</div>`,
expected: `<div>Content</div>`,
},
{
name: "Nested dangerous tags removal",
input: `<p>Safe <b>content <script>alert('XSS')</script></b></p>`,
expected: `<p>Safe <b>content </b></p>`,
},
{
name: "Malformed tag handling",
input: `<p>Text</p><script>alert('XSS');</script><p>More text`,
expected: `<p>Text</p><p>More text`,
},
{
name: "Allow safe tags and attributes",
input: `<a href="https://example.com" target="_blank">Safe link</a>`,
expected: `<a href="https://example.com" rel="nofollow">Safe link</a>`,
},
{
name: "Mixed safe and unsafe content",
input: `<p>Safe <strong>content</strong></p><img src="image.jpg" onload="alert('XSS')"><script>alert('More XSS');</script>`,
expected: `<p>Safe <strong>content</strong></p><img src="image.jpg">`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := parseHTMLLessStrict(tc.input)
if result != tc.expected {
t.Errorf("parseHTMLLessStrict(%q) = %q, want %q", tc.input, result, tc.expected)
}
})
}
}
func TestParseHTMLStrict(t *testing.T) {
testCases := []struct {
name string
input string
expected string
}{
{
name: "Script tag removal",
input: `<p>Hello</p><script>alert('XSS');</script>`,
expected: `Hello`,
},
{
name: "No anchor tags",
input: `<a href="javascript:alert('XSS')">Click me</a>`,
expected: `Click me`,
},
{
name: "Iframe removal",
input: `<iframe src="https://malicious-site.com"></iframe>`,
expected: ``,
},
{
name: "Image tag removal",
input: `<img src="image.jpg" onerror="alert('XSS')">`,
expected: ``,
},
{
// We do not allow img tags with data in the src
name: "Data URL removal",
input: `<img src="data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KDEpIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==">`,
expected: ``,
},
{
name: "CSS expression removal",
input: `<div style="background-image: url('javascript:alert(\'XSS\')')">Content</div>`,
expected: `Content`,
},
{
name: "Nested dangerous tags removal",
input: `<p>Safe <b>content <script>alert('XSS')</script></b></p>`,
expected: `Safe content `,
},
{
name: "Malformed tag handling",
input: `<p>Text</p><script>alert('XSS');</script><p>More text`,
expected: `TextMore text`,
},
{
name: "No anchor tags",
input: `<a href="https://example.com" target="_blank">Safe link</a>`,
expected: `Safe link`,
},
{
name: "Mixed safe and unsafe content",
input: `<p>Safe <strong>content</strong></p><img src="image.jpg" onload="alert('XSS')"><script>alert('More XSS');</script>`,
expected: `Safe content`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := parseHTMLStrict(tc.input)
if result != tc.expected {
t.Errorf("parseHTMLStrict(%q) = %q, want %q", tc.input, result, tc.expected)
}
})
}
}
func TestParseMarkdownToHTML(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Basic Markdown",
input: "# Hello\nThis is **bold** and *italic*.",
expected: "<h1>Hello</h1>\n<p>This is <strong>bold</strong> and <em>italic</em>.</p>\n",
},
{
name: "Links",
input: "[Google](https://www.google.com)",
expected: "<p><a href=\"https://www.google.com\">Google</a></p>\n",
},
{
name: "Code Blocks",
input: "```go\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n```",
expected: "<pre><code class=\"language-go\">func main() {\n\tfmt.Println("Hello, World!")\n}\n</code></pre>\n",
},
{
name: "Lists",
input: "- Item 1\n- Item 2\n - Subitem 2.1",
expected: "<ul>\n<li>Item 1</li>\n<li>Item 2\n<ul>\n<li>Subitem 2.1</li>\n</ul>\n</li>\n</ul>\n",
},
{
name: "Emojis",
input: "I :heart: Markdown!",
expected: "<p>I ❤️ Markdown!</p>\n",
},
{
name: "Tables (GFM)",
input: "| Column 1 | Column 2 |\n|----------|----------|\n| Cell 1 | Cell 2 |",
expected: "<table>\n<thead>\n<tr>\n<th>Column 1</th>\n<th>Column 2</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Cell 1</td>\n<td>Cell 2</td>\n</tr>\n</tbody>\n</table>\n",
},
{
name: "Raw HTML",
input: "This is <span style=\"color: red;\">red</span>.",
expected: "<p>This is <span style=\"color: red;\">red</span>.</p>\n",
},
{
name: "Empty Input",
input: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parseMarkdownToHTML(tt.input)
if result != tt.expected {
t.Errorf("parseMarkdownToHTML(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestParseMarkdownToHTMLError(t *testing.T) {
// This test case is to simulate an error condition.
// However, it's difficult to cause an error in the goldmark parser.
// You might need to mock the goldmark.Convert function to simulate an error.
// For now, we'll just test that extremely large input doesn't cause issues.
largeInput := strings.Repeat("a", 1000000) // 1 million characters
result := parseMarkdownToHTML(largeInput)
if result == "" {
t.Errorf("parseMarkdownToHTML failed to handle large input")
}
}