Debian Codegraph @

test/lua/unit/get_html_urls.lua

raw
1 context("HTML URL extraction", function()
2 local rspamd_task = require("rspamd_task")
3 local logger = require("rspamd_logger")
4
5 test("Basic URL extraction from simple HTML", function()
6 local msg = [[
7 From: [email protected]
8 To: [email protected]
9 Subject: test
10 Content-Type: text/html
11
12 <html>
13 <body>
14 <a href="http://example.com/test">Click here</a>
15 </body>
16 </html>
17 ]]
18 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
19 assert_true(res, "failed to load message")
20
21 task:process_message()
22
23 local urls = task:get_html_urls()
24
25 assert_not_nil(urls, "should extract URLs")
26
27 -- Check structure
28 local found_url = false
29 for part_id, url_list in pairs(urls) do
30 assert_true(type(url_list) == "table", "URL list should be a table")
31 for i, url_info in ipairs(url_list) do
32 assert_not_nil(url_info.url, "should have url field")
33 assert_not_nil(url_info.attr, "should have attr field")
34 assert_not_nil(url_info.tag, "should have tag field")
35
36 if url_info.url == "http://example.com/test" then
37 assert_equal(url_info.attr, "href", "should be href attribute")
38 assert_equal(url_info.tag, "a", "should be <a> tag")
39 found_url = true
40 end
41 end
42 end
43
44 assert_true(found_url, "should find the expected URL")
45
46 task:destroy()
47 end)
48
49 test("Multiple URLs in same HTML part", function()
50 local msg = [[
51 From: [email protected]
52 To: [email protected]
53 Subject: test
54 Content-Type: text/html
55
56 <html>
57 <body>
58 <a href="http://example.com/link1">Link 1</a>
59 <a href="http://example.com/link2">Link 2</a>
60 <img src="http://example.com/image.jpg">
61 </body>
62 </html>
63 ]]
64 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
65 assert_true(res, "failed to load message")
66
67 task:process_message()
68
69 local urls = task:get_html_urls()
70
71 assert_not_nil(urls, "should extract URLs")
72
73 -- Count URLs
74 local url_count = 0
75 local found_urls = {}
76 for part_id, url_list in pairs(urls) do
77 for i, url_info in ipairs(url_list) do
78 url_count = url_count + 1
79 found_urls[url_info.url] = url_info
80 end
81 end
82
83 assert_equal(url_count, 3, "should have found 3 URLs")
84 assert_not_nil(found_urls["http://example.com/link1"], "should find link1")
85 assert_not_nil(found_urls["http://example.com/link2"], "should find link2")
86 assert_not_nil(found_urls["http://example.com/image.jpg"], "should find image")
87
88 -- Check attributes
89 assert_equal(found_urls["http://example.com/link1"].attr, "href")
90 assert_equal(found_urls["http://example.com/link1"].tag, "a")
91 assert_equal(found_urls["http://example.com/image.jpg"].attr, "src")
92 assert_equal(found_urls["http://example.com/image.jpg"].tag, "img")
93
94 task:destroy()
95 end)
96
97 test("Non-HTML parts return nil", function()
98 local msg = [[
99 From: [email protected]
100 To: [email protected]
101 Subject: test
102 Content-Type: text/plain
103
104 This is plain text with http://example.com/test
105 ]]
106 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
107 assert_true(res, "failed to load message")
108
109 task:process_message()
110
111 local urls = task:get_html_urls()
112
113 -- Should return nil for plain text
114 assert_nil(urls, "should return nil for non-HTML parts")
115
116 task:destroy()
117 end)
118
119 test("Empty HTML returns nil", function()
120 local msg = [[
121 From: [email protected]
122 To: [email protected]
123 Subject: test
124 Content-Type: text/html
125
126 ]]
127 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
128 assert_true(res, "failed to load message")
129
130 task:process_message()
131
132 local urls = task:get_html_urls()
133
134 -- Should return nil for empty HTML
135 assert_nil(urls, "should return nil for empty HTML")
136
137 task:destroy()
138 end)
139
140 test("HTML without URLs returns nil", function()
141 local msg = [[
142 From: [email protected]
143 To: [email protected]
144 Subject: test
145 Content-Type: text/html
146
147 <html>
148 <body>
149 <p>Just some text without any links</p>
150 </body>
151 </html>
152 ]]
153 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
154 assert_true(res, "failed to load message")
155
156 task:process_message()
157
158 local urls = task:get_html_urls()
159
160 -- Should return nil when no URLs found
161 assert_nil(urls, "should return nil when no URLs found")
162
163 task:destroy()
164 end)
165
166 test("Data URI scheme is skipped", function()
167 local msg = [[
168 From: [email protected]
169 To: [email protected]
170 Subject: test
171 Content-Type: text/html
172
173 <html>
174 <body>
175 <img src="data:image/png;base64,iVBORw0KG==">
176 <a href="http://example.com/test">Real link</a>
177 </body>
178 </html>
179 ]]
180 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
181 assert_true(res, "failed to load message")
182
183 task:process_message()
184
185 local urls = task:get_html_urls()
186
187 assert_not_nil(urls, "should extract non-data URLs")
188
189 -- Check that data: URIs are skipped
190 local found_data_uri = false
191 local found_http_url = false
192 for part_id, url_list in pairs(urls) do
193 for i, url_info in ipairs(url_list) do
194 if url_info.url:find("^data:", 1, false) then
195 found_data_uri = true
196 end
197 if url_info.url == "http://example.com/test" then
198 found_http_url = true
199 end
200 end
201 end
202
203 assert_false(found_data_uri, "data: URIs should be skipped")
204 assert_true(found_http_url, "should have found the http URL")
205
206 task:destroy()
207 end)
208
209 test("CID scheme is skipped", function()
210 local msg = [[
211 From: [email protected]
212 To: [email protected]
213 Subject: test
214 Content-Type: text/html
215
216 <html>
217 <body>
218 <img src="cid:[email protected]">
219 <a href="http://example.com/test">Real link</a>
220 </body>
221 </html>
222 ]]
223 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
224 assert_true(res, "failed to load message")
225
226 task:process_message()
227
228 local urls = task:get_html_urls()
229
230 assert_not_nil(urls, "should extract non-cid URLs")
231
232 -- Check that cid: URIs are skipped
233 local found_cid_uri = false
234 local found_http_url = false
235 for part_id, url_list in pairs(urls) do
236 for i, url_info in ipairs(url_list) do
237 if url_info.url:find("^cid:", 1, false) then
238 found_cid_uri = true
239 end
240 if url_info.url == "http://example.com/test" then
241 found_http_url = true
242 end
243 end
244 end
245
246 assert_false(found_cid_uri, "cid: URIs should be skipped")
247 assert_true(found_http_url, "should have found the http URL")
248
249 task:destroy()
250 end)
251
252 test("Multipart message with multiple HTML parts", function()
253 local msg = [[
254 From: [email protected]
255 To: [email protected]
256 Subject: test
257 Content-Type: multipart/alternative; boundary="boundary123"
258
259 --boundary123
260 Content-Type: text/plain
261
262 Plain text part
263
264 --boundary123
265 Content-Type: text/html
266
267 <html><body><a href="http://example.com/part1">Part 1</a></body></html>
268
269 --boundary123
270 Content-Type: text/html
271
272 <html><body><a href="http://example.com/part2">Part 2</a></body></html>
273
274 --boundary123--
275 ]]
276 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
277 assert_true(res, "failed to load message")
278
279 task:process_message()
280
281 local urls = task:get_html_urls()
282
283 assert_not_nil(urls, "should extract URLs from multipart HTML")
284
285 -- Should have processed at least one HTML part
286 local part_count = 0
287 local total_urls = 0
288 for part_id, url_list in pairs(urls) do
289 part_count = part_count + 1
290 total_urls = total_urls + #url_list
291 end
292
293 assert_true(part_count >= 1, "should have URLs from at least one HTML part")
294 assert_true(total_urls >= 1, "should have found at least one URL")
295
296 task:destroy()
297 end)
298
299 test("URL with special characters", function()
300 local msg = [[
301 From: [email protected]
302 To: [email protected]
303 Subject: test
304 Content-Type: text/html
305
306 <html>
307 <body>
308 <a href="http://example.com/path?param=value&other=123#anchor">Link</a>
309 </body>
310 </html>
311 ]]
312 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
313 assert_true(res, "failed to load message")
314
315 task:process_message()
316
317 local urls = task:get_html_urls()
318
319 assert_not_nil(urls, "should handle URLs with special chars")
320
321 local found_url = false
322 for part_id, url_list in pairs(urls) do
323 for i, url_info in ipairs(url_list) do
324 if url_info.url:find("example.com/path", 1, true) then
325 found_url = true
326 -- URL should contain the query parameters
327 assert_true(url_info.url:find("param=value", 1, true) ~= nil,
328 "should preserve query parameters")
329 end
330 end
331 end
332
333 assert_true(found_url, "should have found the URL with special chars")
334
335 task:destroy()
336 end)
337
338 end)