Repository navigation
Expand file tree
/
Copy pathhtml2text_test.go
More file actions
415 lines (356 loc) · 25.7 KB
/
Copy pathhtml2text_test.go
File metadata and controls
415 lines (356 loc) · 25.7 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package html2text
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestHTML2Text(t *testing.T) {
Convey("HTML2Text should work", t, func() {
Convey("Links", func() {
So(HTML2Text(`<div></div>`), ShouldEqual, "")
So(HTML2Text(`<div>simple text</div>`), ShouldEqual, "simple text")
// the original behavior
So(HTML2Text(`click <a href="test">here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <A hRef="test">here</A>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href='test'>here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href=test>here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href =test>here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href = test>here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href = test>here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href = test target="_blank">here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a class="x" href="test">here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href="javascript:void(0)">here</a>`), ShouldEqual, "click ")
So(HTML2Text(`click <a href="JaVaScRiPt:alert(1)">here</a>`), ShouldEqual, "click ")
So(HTML2Text(`click <a href="javascript:alert(1)">here</a>`), ShouldEqual, "click ")
So(HTML2Text(`click <a href="test"><span>here</span> or here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href="http://bit.ly/2n4wXRs">news</a>`), ShouldEqual, "click http://bit.ly/2n4wXRs")
So(HTML2Text(`<a rel="mw:WikiLink" href="/wiki/yet#English" title="yet">yet</a>, <a rel="mw:WikiLink" href="/wiki/not_yet#English" title="not yet">not yet</a>`), ShouldEqual, "/wiki/yet#English, /wiki/not_yet#English")
// with inner text
So(HTML2TextWithOptions(`click <a href="test">here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <A hRef="test">here</A>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href='test'>here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href=test>here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href =test>here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href = test>here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href = test>here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href = test target="_blank">here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a class="x" href="test">here</a>`, WithLinksInnerText()), ShouldEqual, "click here <test>")
So(HTML2TextWithOptions(`click <a href="ents/'x'">here</a>`, WithLinksInnerText()), ShouldEqual, "click here <ents/'x'>")
So(HTML2TextWithOptions(`click <a href="javascript:void(0)">here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`click <a href="JaVaScRiPt:alert(1)">here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`click <a href="javascript:alert(1)">here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`click <a href="test"><span>here</span> or here</a>`, WithLinksInnerText()), ShouldEqual, "click here or here <test>")
So(HTML2TextWithOptions(`click <a href="http://bit.ly/2n4wXRs">news</a>`, WithLinksInnerText()), ShouldEqual, "click news <http://bit.ly/2n4wXRs>")
So(HTML2TextWithOptions(`<a rel="mw:WikiLink" href="/wiki/yet#English" title="yet">yet</a>, <a rel="mw:WikiLink" href="/wiki/not_yet#English" title="not yet">not yet</a>`, WithLinksInnerText()), ShouldEqual, "yet </wiki/yet#English>, not yet </wiki/not_yet#English>")
So(HTML2TextWithOptions(`click <a href="one">here<a href="two"> or</a><span> here</span></a>`, WithLinksInnerText()), ShouldEqual, "click here or <two> here <one>")
})
Convey("Inlines", func() {
So(HTML2Text(`strong <strong>text</strong>`), ShouldEqual, "strong text")
So(HTML2Text(`some <div id="a" class="b">div</div>`), ShouldEqual, "some div")
})
Convey("Line breaks and spaces", func() {
So(HTML2Text("should ignore more spaces"), ShouldEqual, "should ignore more spaces")
So(HTML2Text("should \nignore \r\nnew lines"), ShouldEqual, "should ignore new lines")
So(HTML2Text("a\nb\nc"), ShouldEqual, "a b c")
So(HTML2Text(`two<br>line<br/>breaks`), ShouldEqual, "two\r\nline\r\nbreaks")
So(HTML2Text(`<p>two</p><p>paragraphs</p>`), ShouldEqual, "two\r\n\r\nparagraphs")
})
Convey("Headings", func() {
So(HTML2Text("<h1>First</h1>main text"), ShouldEqual, "First\r\n\r\nmain text")
So(HTML2Text("First<h2>Second</h2>next section"), ShouldEqual, "First\r\n\r\nSecond\r\n\r\nnext section")
So(HTML2Text("<h2>Second</h2>next section"), ShouldEqual, "Second\r\n\r\nnext section")
So(HTML2Text("Second<h3>Third</h3>next section"), ShouldEqual, "Second\r\n\r\nThird\r\n\r\nnext section")
So(HTML2Text("<h3>Third</h3>next section"), ShouldEqual, "Third\r\n\r\nnext section")
So(HTML2Text("Third<h4>Fourth</h4>next section"), ShouldEqual, "Third\r\n\r\nFourth\r\n\r\nnext section")
So(HTML2Text("<h4>Fourth</h4>next section"), ShouldEqual, "Fourth\r\n\r\nnext section")
So(HTML2Text("Fourth<h5>Fifth</h5>next section"), ShouldEqual, "Fourth\r\n\r\nFifth\r\n\r\nnext section")
So(HTML2Text("<h5>Fifth</h5>next section"), ShouldEqual, "Fifth\r\n\r\nnext section")
So(HTML2Text("Fifth<h6>Sixth</h6>next section"), ShouldEqual, "Fifth\r\n\r\nSixth\r\n\r\nnext section")
So(HTML2Text("<h6>Sixth</h6>next section"), ShouldEqual, "Sixth\r\n\r\nnext section")
So(HTML2Text("<h7>Not Header</h7>next section"), ShouldEqual, "Not Headernext section")
})
Convey("HTML entities", func() {
So(HTMLEntitiesToText("collapsible  and explicit spaces"), ShouldEqual, "collapsible and explicit spaces")
So(HTML2Text(`two spaces`), ShouldEqual, "two spaces")
So(HTML2Text(`© 2017 K3A`), ShouldEqual, "© 2017 K3A")
So(HTML2Text("<printtag>"), ShouldEqual, "<printtag>")
So(HTML2Text(`would you pay in ¢, £, ¥ or €?`),
ShouldEqual, "would you pay in ¢, £, ¥ or €?")
So(HTML2Text(`Tom & Jerry is not an entity`), ShouldEqual, "Tom & Jerry is not an entity")
So(HTML2Text(`this &neither; as you see`), ShouldEqual, "this &neither; as you see")
So(HTML2Text(`fish & chips`), ShouldEqual, "fish & chips")
So(HTML2Text(`"I'm sorry, Dave. I'm afraid I can't do that." – HAL, 2001: A Space Odyssey`), ShouldEqual, "\"I'm sorry, Dave. I'm afraid I can't do that.\" – HAL, 2001: A Space Odyssey")
So(HTML2Text(`Google ®`), ShouldEqual, "Google ®")
So(HTML2Text(`⁌ decimal and hex entities supported ⁍`), ShouldEqual, "⁌ decimal and hex entities supported ⁍")
})
Convey("Large Entity", func() {
So(HTMLEntitiesToText("&abcdefghij;"), ShouldEqual, "&abcdefghij;")
})
Convey("Numeric HTML Entities", func() {
So(HTMLEntitiesToText("'single quotes' and 츝"), ShouldEqual, "'single quotes' and 츝")
So(HTML2Text(`a null� character is ignored`), ShouldEqual, "a null character is ignored")
So(HTML2Text(`a control character is ignored`), ShouldEqual, "a control character is ignored")
So(HTML2Text(`space entities  collapsed correctly`), ShouldEqual, "space entities collapsed correctly")
})
Convey("Entities in suppressed content", func() {
for _, text := range []string{
`<html><head><title>&A</title></head><body>visible</body></html>`,
`<head><title>  </title></head>visible`,
`<script>var text = "&A";</script>visible`,
`<style>p::before { content: "&A"; }</style>visible`,
`<head><style>p::before { content: "A"; }</style><title>&</title></head>visible`,
} {
So(HTML2Text(text), ShouldEqual, "visible")
So(HTML2TextWithOptions(text, WithLinksInnerText()), ShouldEqual, "visible")
}
So(HTML2Text(`before<script>var text = "&A";</script>after`), ShouldEqual, "beforeafter")
So(HTML2Text(`before<style>p::before { content: "&A"; }</style>after`), ShouldEqual, "beforeafter")
})
Convey("Entities in anchor text", func() {
So(HTML2Text(`<a href="/one">Tom & Jerry A</a>`), ShouldEqual, "/one")
So(HTML2Text(`<a href="/one?x=1&y=2">Tom & Jerry</a>`), ShouldEqual, "/one?x=1&y=2")
So(HTML2Text(`<a>Tom & Jerry</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="javascript:void(0)">A&</a>`), ShouldEqual, "")
So(HTML2TextWithOptions(`<a href="/one">Tom & Jerry A</a>`, WithLinksInnerText()), ShouldEqual, "Tom & Jerry A </one>")
So(HTML2TextWithOptions(`<a href="/one?x=1&y=2">Tom & Jerry</a>`, WithLinksInnerText()), ShouldEqual, "Tom & Jerry </one?x=1&y=2>")
So(HTML2TextWithOptions(`<a>Tom & Jerry</a>`, WithLinksInnerText()), ShouldEqual, "Tom & Jerry")
So(HTML2TextWithOptions(`<a href="javascript:void(0)">A&</a>`, WithLinksInnerText()), ShouldEqual, "A&")
So(HTML2Text(`before & <a href="/one">A</a> after &`), ShouldEqual, "before & /one after &")
})
Convey("HTML Entities in hrefs", func() {
So(HTML2TextWithOptions(`click <a href="./filenamewith spaces">here</a>`, WithLinksInnerText()), ShouldEqual, "click here <./filenamewith spaces>")
So(HTML2TextWithOptions(`click <a href="ents/control	char">here</a>`, WithLinksInnerText()), ShouldEqual, "click here <ents/controlchar>")
So(HTML2Text(`<a href="http://example.com/`+"\r\nBcc: victim@example.com"+`">link</a>`), ShouldEqual, "http://example.com/Bcc: victim@example.com")
So(HTML2Text(`<a href="http://example.com">link</a>`), ShouldEqual, "http://example.com")
})
Convey("Full HTML structure", func() {
So(HTML2Text(``), ShouldEqual, "")
So(HTML2Text("Bad chars\x1b ignored"), ShouldEqual, "Bad chars ignored")
So(HTML2Text(`<html><head><title>Good</title></head><body>x</body>`), ShouldEqual, "x")
So(HTML2Text(`<html><head href="foo"><title>Good</title></head><body>x</body>`), ShouldEqual, "x")
So(HTML2Text(`<htMl><hEad><titLe>Good</Title></head><boDy>x</Body>`), ShouldEqual, "x")
So(HTML2Text(`we are not <script type="javascript"></script>interested in scripts`),
ShouldEqual, "we are not interested in scripts")
})
Convey("Switching Unix and Windows line breaks (original behavior)", func() {
SetUnixLbr(true)
So(HTML2Text(`two<br>line<br/>breaks`), ShouldEqual, "two\nline\nbreaks")
So(HTML2Text(`<p>two</p><p>paragraphs</p>`), ShouldEqual, "two\n\nparagraphs")
SetUnixLbr(false)
So(HTML2Text(`two<br>line<br/>breaks`), ShouldEqual, "two\r\nline\r\nbreaks")
So(HTML2Text(`<p>two</p><p>paragraphs</p>`), ShouldEqual, "two\r\n\r\nparagraphs")
})
Convey("Switching Unix and Windows line breaks (new options)", func() {
So(HTML2TextWithOptions(`two<br>line<br/>breaks`, WithUnixLineBreaks()), ShouldEqual, "two\nline\nbreaks")
So(HTML2TextWithOptions(`<p>two</p><p>paragraphs</p>`, WithUnixLineBreaks()), ShouldEqual, "two\n\nparagraphs")
So(HTML2TextWithOptions(`two<br>line<br/>breaks`), ShouldEqual, "two\r\nline\r\nbreaks")
So(HTML2TextWithOptions(`<p>two</p><p>paragraphs</p>`), ShouldEqual, "two\r\n\r\nparagraphs")
})
Convey("List items separated by new lines (original behavior)", func() {
So(HTML2Text(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
})
Convey("List items having a dash pre-fixed", func() {
So(HTML2TextWithOptions(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`, WithListSupport()), ShouldEqual, "list of items\r\n - One\r\n - Two\r\n - Three\r\n")
So(HTML2TextWithOptions(`list of items<ol><li>One</li><li>Two</li><li>Three</li></ol>`, WithListSupport()), ShouldEqual, "list of items\r\n - One\r\n - Two\r\n - Three\r\n")
})
Convey("Custom HTML Tags", func() {
So(HTML2Text(`<aa>hello</aa>`), ShouldEqual, "hello")
So(HTML2Text(`<aa >hello</aa>`), ShouldEqual, "hello")
So(HTML2Text(`<aa x="1">hello</aa>`), ShouldEqual, "hello")
})
Convey("Tags with attributes", func() {
So(HTML2Text(`list of items<ul><li class="menu-item">One</li><li class="menu-item">Two</li><li class="menu-item">Three</li></ul>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
So(HTML2Text(`list of items<ol><li class="menu-item">One</li><li class="menu-item">Two</li><li class="menu-item">Three</li></ol>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree\r\n")
So(HTML2Text(`<p class="content">content</p><div id="status">is ok</div>`), ShouldEqual, "content\r\n\r\nis ok")
})
Convey("HTML comments", func() {
So(HTML2Text(`<!-- comment > secret_internal_note -->visible`), ShouldEqual, "visible")
So(HTML2Text(`before<!-- comment -->after`), ShouldEqual, "beforeafter")
So(HTML2Text(`<!-- full comment -->`), ShouldEqual, "")
So(HTML2Text(`<!-- comment <!-- nested? -->end`), ShouldEqual, "end")
So(HTML2Text(`<div>text<!-- comment -->more</div>`), ShouldEqual, "textmore")
So(HTML2Text(`start<!-- never ended`), ShouldEqual, "start")
})
Convey("Keep spaces as they are", func() {
So(HTML2TextWithOptions("should not ignore spaces", WithKeepSpaces()), ShouldEqual, "should not ignore spaces")
})
Convey("URL scheme validation and dangerous schemes blocked by default", func() {
Convey("Garbage and unsafe URLs are ignored", func() {
So(HTML2Text(`<a href=":">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav	ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="http://example.com/ok">click</a>`), ShouldEqual, "http://example.com/ok")
So(HTML2Text(`<a href="http://example.com/\x1b[2J\x1b[H">click</a>`), ShouldEqual, "http://example.com//x1b[2J/x1b[H")
})
Convey("Dangerous schemes blocked by default", func() {
So(HTML2Text(`<a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="DATA:text/html,test">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="vbscript:msgbox(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="VBSCRIPT:msgbox(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="file:///etc/passwd">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="FILE:///etc/passwd">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="blob:http://example.com/uuid">click</a>`), ShouldEqual, "")
})
Convey("URLs with unterminated entities are parsed", func() {
So(HTML2Text(`<a href="javascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="javascript:alert(1)">click</a>`), ShouldEqual, "")
})
Convey("WithLinksInnerText suppresses dangerous schemes but keeps inner text", func() {
So(HTML2TextWithOptions(`<a href="data:text/html,test">click here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`<a href="vbscript:msgbox(1)">click here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`<a href="file:///etc/passwd">click here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
})
Convey("Safe schemes allowed by default", func() {
So(HTML2Text(`<a href="http://example.com">site</a>`), ShouldEqual, "http://example.com")
So(HTML2Text(`<a href="https://example.com">site</a>`), ShouldEqual, "https://example.com")
So(HTML2Text(`<a href="mailto:test@example.com">email</a>`), ShouldEqual, "mailto:test@example.com")
So(HTML2Text(`<a href="tel:+33639981234">phone</a>`), ShouldEqual, "tel:+33639981234")
So(HTML2TextWithOptions(`Send <a href="sms:+33639981234">SMS</a> for more info`, WithLinksInnerText()), ShouldEqual, "Send SMS <sms:+33639981234> for more info")
})
Convey("Relative URLs allowed by default", func() {
So(HTML2Text(`<a href="/path/to/page">page</a>`), ShouldEqual, "/path/to/page")
So(HTML2Text(`<a href="relative/path">page</a>`), ShouldEqual, "relative/path")
So(HTML2Text(`<a href="#section">anchor</a>`), ShouldEqual, "#section")
})
Convey("Configured custom schemes allowed", func() {
So(HTML2TextWithOptions(`<a href="ourapp://link">open app</a>`, WithAllowedURLSchemes([]string{"ourapp"})), ShouldEqual, "ourapp://link")
})
})
Convey("URLs with whitespaces and zero-width characters are ignored", func() {
Convey("Non-breaking spaces ignored: \\u00A0 ( ), \\u202F, \\u2007", func() {
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href=" javascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
})
Convey("Zero-width spaces ignored: \\u200B (​), \\u200C, \\u200D, \\u2060, \\uFEFF", func() {
So(HTML2Text(`<a href="jav​ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav​ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav‌ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav‌ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav‍ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav‍ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav⁠ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="jav⁠ascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="javascript:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="javascript:alert(1)">click</a>`), ShouldEqual, "")
})
Convey("Other Unicode spaces ignored: \\u1680, \\u2000–\\u200A, \\u3000", func() {
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="java script:alert(1)">click</a>`), ShouldEqual, "")
})
Convey("Whitespace or zero-width before colon is invalid", func() {
So(HTML2Text(`<a href="javascript :alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="javascript :alert(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="javascript​:alert(1)">click</a>`), ShouldEqual, "")
})
Convey("Dangerous schemes obfuscated with unicode spaces / zero-width characters are ignored", func() {
So(HTML2Text(`<a href="d ata:text/html,abc">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="v​bscript:msgbox(1)">click</a>`), ShouldEqual, "")
So(HTML2Text(`<a href="file:///etc/passwd">click</a>`), ShouldEqual, "")
})
Convey("WithLinksInnerText retains inner text when bad href is suppressed", func() {
So(HTML2TextWithOptions(`<a href="java script:alert(1)">click here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`<a href="jav​ascript:alert(1)">click here</a>`, WithLinksInnerText()), ShouldEqual, "click here")
So(HTML2TextWithOptions(`<a href="jav
ascript:alert(1)">click</a>`, WithLinksInnerText()), ShouldEqual, "click")
})
})
Convey("Unmatched closing tags (negative depth flaw fix)", func() {
Convey("Unmatched closing tags do not suppress subsequent text", func() {
So(HTML2Text(`</head>Hello world`), ShouldEqual, "Hello world")
So(HTML2Text(`</a>Hello world`), ShouldEqual, "Hello world")
})
Convey("Unmatched closing tags followed by bad tags do not leak contents", func() {
So(HTML2Text(`</script><script>sensitive_token</script>Hello world`), ShouldEqual, "Hello world")
So(HTML2Text(`</head><head><title>sensitive title</title></head>Hello world`), ShouldEqual, "Hello world")
})
})
Convey("Anchor tag state desynchronization in WithLinksInnerText mode", func() {
Convey("Anchors without href do not drop subsequent text", func() {
So(HTML2TextWithOptions(`<a id="top"></a>Hello world`, WithLinksInnerText()), ShouldEqual, "Hello world")
So(HTML2TextWithOptions(`<a></a>Hello world`, WithLinksInnerText()), ShouldEqual, "Hello world")
})
Convey("Inner text of anchors without href is retained", func() {
So(HTML2TextWithOptions(`<a id="top">Anchor</a> Hello world`, WithLinksInnerText()), ShouldEqual, "Anchor Hello world")
So(HTML2TextWithOptions(`<a>Anchor</a> Hello world`, WithLinksInnerText()), ShouldEqual, "Anchor Hello world")
})
Convey("Inner text is retained for mix of anchors with and without href", func() {
So(HTML2TextWithOptions(`<a id="section-1">Section 1</a> <a href="http://example.com">Link</a> and <a name="section-2">Section 2</a>`, WithLinksInnerText()), ShouldEqual, "Section 1 Link <http://example.com> and Section 2")
})
Convey("Unclosed anchors without href do not drop subsequent text", func() {
So(HTML2TextWithOptions(`<a id="top">Hello world`, WithLinksInnerText()), ShouldEqual, "Hello world")
})
Convey("Bad tags remain suppressed when anchors are present", func() {
So(HTML2TextWithOptions(`<head><a id="foo"></a><title>sensitive title</title></head>Hello world`, WithLinksInnerText()), ShouldEqual, "Hello world")
})
Convey("Unmatched closing </a> in WithLinksInnerText mode won't suppress subsequent text", func() {
So(HTML2TextWithOptions(`</a>Hello world`, WithLinksInnerText()), ShouldEqual, "Hello world")
})
})
Convey("Recognize HTML whitespace after tag names", func() {
for _, separator := range []string{" ", "\t", "\n", "\r", "\f"} {
for _, tc := range []struct{ input, want string }{
{"<script" + separator + `type="text/javascript">hidden</script>visible`, "visible"},
{"<style" + separator + `type="text/css">hidden</style>visible`, "visible"},
{"<a" + separator + `href="https://example.com">label</a> after`, "https://example.com after"},
{"before<br" + separator + `class="break">after`, "before\nafter"},
} {
So(HTML2TextWithOptions(tc.input, WithUnixLineBreaks()), ShouldEqual, tc.want)
}
}
So(HTML2Text(`<script/x>alert("LEAKED_SECRET_SCRIPT")</script>`), ShouldEqual, "")
So(HTML2Text(`<a data-href="https://attacker.com" href="https://legit.com">click</a>`), ShouldEqual, "https://legit.com")
})
Convey("Mismatched closing tags do not break content suppression", func() {
Convey("Single mismatched closing tags of different types", func() {
So(HTML2Text(`<head><title>Public</title></a><script>api_key = "AIzaSyD-CONFIDENTIAL-KEY";</script></head>body`), ShouldEqual, "body")
So(HTML2Text(`<script></style>var secret = 42;</script>visible`), ShouldEqual, "visible")
So(HTML2Text(`<style></head>.hidden { color: red; }</style>visible`), ShouldEqual, "visible")
So(HTML2Text(`<head></script><title>hidden</title></head>visible`), ShouldEqual, "visible")
})
Convey("Multiple extraneous closing tags inside a single bad block", func() {
So(HTML2Text(`<head></a></style></script><title>still hidden</title></head>visible`), ShouldEqual, "visible")
})
Convey("WithLinksInnerText: mismatched </head> inside <a> does not pop stack", func() {
So(HTML2TextWithOptions(`<a href="http://x.com"></head>click</a> visible`, WithLinksInnerText()), ShouldEqual, "click <http://x.com> visible")
})
})
Convey("Tag boundary protection in quoted attribute strings", func() {
Convey("Style + single and double-quoted attr with <", func() {
So(HTML2Text(`<style title="<">hidden</style>`), ShouldEqual, "")
So(HTML2Text(`<style title='<'>hidden</style>`), ShouldEqual, "")
})
Convey("> inside quoted attribute does not close tag", func() {
So(HTML2Text(`<style data-x="foo>bar">hidden</style>`), ShouldEqual, "")
So(HTML2Text(`<script data-y="1>2">hidden</script>`), ShouldEqual, "")
})
Convey("Single quote inside double-quoted attr must not toggle quote state", func() {
So(HTML2Text(`<style title="it's safe">hidden</style>`), ShouldEqual, "")
})
Convey("Double quote inside single-quoted attr must not toggle quote state", func() {
So(HTML2Text(`<style title='it "is safe"'>hidden</style>`), ShouldEqual, "")
})
Convey("Multiple < inside attribute values", func() {
So(HTML2Text(`<style data-x="<<<">hidden</style>`), ShouldEqual, "")
So(HTML2Text(`<style data-x="<a<b<c">hidden</style>`), ShouldEqual, "")
})
Convey("Both < and > inside attribute values (both guarded)", func() {
So(HTML2Text(`<style data-test="<b>">hidden</style>`), ShouldEqual, "")
})
Convey("<a> tags with attributes parsed correctly when < in href", func() {
So(HTML2Text(`<a href="http://example.com?a=1<b>c">link</a>`), ShouldEqual, "http://example.com?a=1<b>c")
})
Convey("Normal text works", func() {
So(HTML2Text(`"Quotes" in 'normal' text works as expected`), ShouldEqual, `"Quotes" in 'normal' text works as expected`)
})
})
Convey("Fix: Anchor tags in suppressed blocks won't produce output", func() {
So(HTML2Text(`<head><a href="http://secret.internal/token=XYZ">link</a></head>visible`), ShouldEqual, "visible")
So(HTML2Text(`<script>var a='<a href="https://auth.internal/admin?secret=123">link</a>';</script>visible`), ShouldEqual, "visible")
So(HTML2TextWithOptions(`<head><a href="http://secret.internal">link</a></head>visible`, WithLinksInnerText()), ShouldEqual, "visible")
})
})
}