Debian Codegraph @

test/lua/unit/url_rewrite.lua

raw
1 context("HTML URL rewriting", function()
2 local rspamd_task = require("rspamd_task")
3 local logger = require("rspamd_logger")
4
5 test("Basic URL rewriting with 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 -- Rewrite URL callback
24 local function rewrite_callback(task, url)
25 if url == "http://example.com/test" then
26 return "http://safe.com/redirected"
27 end
28 return nil
29 end
30
31 local result = task:rewrite_html_urls(rewrite_callback)
32
33 assert_not_nil(result, "rewrite should return results")
34
35 -- Check that we got rewritten HTML
36 local rewritten_found = false
37 for part_id, html_text in pairs(result) do
38 local html = tostring(html_text)
39 assert_true(html:find("http://safe.com/redirected", 1, true) ~= nil,
40 "rewritten URL not found in output")
41 assert_true(html:find("http://example.com/test", 1, true) == nil,
42 "original URL should be replaced")
43 rewritten_found = true
44 end
45
46 assert_true(rewritten_found, "should have rewritten at least one part")
47
48 task:destroy()
49 end)
50
51 test("Multiple URLs in same HTML part", function()
52 local msg = [[
53 From: [email protected]
54 To: [email protected]
55 Subject: test
56 Content-Type: text/html
57
58 <html>
59 <body>
60 <a href="http://example.com/link1">Link 1</a>
61 <a href="http://example.com/link2">Link 2</a>
62 <img src="http://example.com/image.jpg">
63 </body>
64 </html>
65 ]]
66 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
67 assert_true(res, "failed to load message")
68
69 task:process_message()
70
71 local rewritten_urls = {}
72 local function rewrite_callback(task, url)
73 table.insert(rewritten_urls, url)
74 return "http://safe.com/" .. #rewritten_urls
75 end
76
77 local result = task:rewrite_html_urls(rewrite_callback)
78
79 assert_not_nil(result, "rewrite should return results")
80 assert_equal(#rewritten_urls, 3, "should have found 3 URLs")
81
82 -- Check all URLs were rewritten
83 for part_id, html_text in pairs(result) do
84 local html = tostring(html_text)
85 assert_true(html:find("http://safe.com/1", 1, true) ~= nil, "first URL not rewritten")
86 assert_true(html:find("http://safe.com/2", 1, true) ~= nil, "second URL not rewritten")
87 assert_true(html:find("http://safe.com/3", 1, true) ~= nil, "third URL not rewritten")
88 end
89
90 task:destroy()
91 end)
92
93 test("Callback returning nil (no rewrite)", function()
94 local msg = [[
95 From: [email protected]
96 To: [email protected]
97 Subject: test
98 Content-Type: text/html
99
100 <html>
101 <body>
102 <a href="http://example.com/test">Click here</a>
103 </body>
104 </html>
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 -- Callback returns nil, so no rewriting
112 local function rewrite_callback(task, url)
113 return nil
114 end
115
116 local result = task:rewrite_html_urls(rewrite_callback)
117
118 -- Should return nil when no URLs are rewritten
119 assert_nil(result, "should return nil when callback returns nil for all URLs")
120
121 task:destroy()
122 end)
123
124 test("Selective URL rewriting", function()
125 local msg = [[
126 From: [email protected]
127 To: [email protected]
128 Subject: test
129 Content-Type: text/html
130
131 <html>
132 <body>
133 <a href="http://evil.com/phish">Bad Link</a>
134 <a href="http://safe.com/ok">Good Link</a>
135 </body>
136 </html>
137 ]]
138 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
139 assert_true(res, "failed to load message")
140
141 task:process_message()
142
143 local function rewrite_callback(task, url)
144 -- Only rewrite evil.com URLs
145 if url:find("evil.com", 1, true) then
146 return "http://warning.com/blocked"
147 end
148 return nil
149 end
150
151 local result = task:rewrite_html_urls(rewrite_callback)
152
153 assert_not_nil(result, "rewrite should return results")
154
155 for part_id, html_text in pairs(result) do
156 local html = tostring(html_text)
157 assert_true(html:find("http://warning.com/blocked", 1, true) ~= nil,
158 "evil URL should be rewritten")
159 assert_true(html:find("http://safe.com/ok", 1, true) ~= nil,
160 "safe URL should remain unchanged")
161 assert_true(html:find("http://evil.com/phish", 1, true) == nil,
162 "original evil URL should be replaced")
163 end
164
165 task:destroy()
166 end)
167
168 test("Non-HTML parts are skipped", function()
169 local msg = [[
170 From: [email protected]
171 To: [email protected]
172 Subject: test
173 Content-Type: text/plain
174
175 This is plain text with http://example.com/test
176 ]]
177 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
178 assert_true(res, "failed to load message")
179
180 task:process_message()
181
182 local callback_called = false
183 local function rewrite_callback(task, url)
184 callback_called = true
185 return "http://rewritten.com/"
186 end
187
188 local result = task:rewrite_html_urls(rewrite_callback)
189
190 -- Should return nil for plain text
191 assert_nil(result, "should return nil for non-HTML parts")
192 assert_false(callback_called, "callback should not be called for plain text")
193
194 task:destroy()
195 end)
196
197 test("Quoted-printable encoded HTML", function()
198 local msg = [[
199 From: [email protected]
200 To: [email protected]
201 Subject: test
202 Content-Type: text/html
203 Content-Transfer-Encoding: quoted-printable
204
205 <html>
206 <body>
207 <a href=3D"http://example.com/test">Link</a>
208 </body>
209 </html>
210 ]]
211 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
212 assert_true(res, "failed to load message")
213
214 task:process_message()
215
216 local function rewrite_callback(task, url)
217 if url == "http://example.com/test" then
218 return "http://safe.com/redirect"
219 end
220 return nil
221 end
222
223 local result = task:rewrite_html_urls(rewrite_callback)
224
225 assert_not_nil(result, "rewrite should work on quoted-printable content")
226
227 for part_id, html_text in pairs(result) do
228 local html = tostring(html_text)
229 -- The rewritten HTML should contain the new URL
230 assert_true(html:find("safe.com", 1, true) ~= nil,
231 "rewritten URL should be in output")
232 end
233
234 task:destroy()
235 end)
236
237 test("Empty HTML", function()
238 local msg = [[
239 From: [email protected]
240 To: [email protected]
241 Subject: test
242 Content-Type: text/html
243
244 ]]
245 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
246 assert_true(res, "failed to load message")
247
248 task:process_message()
249
250 local function rewrite_callback(task, url)
251 return "http://rewritten.com/"
252 end
253
254 local result = task:rewrite_html_urls(rewrite_callback)
255
256 -- Should return nil for empty HTML
257 assert_nil(result, "should return nil for empty HTML")
258
259 task:destroy()
260 end)
261
262 test("Invalid callback type", function()
263 local msg = [[
264 From: [email protected]
265 To: [email protected]
266 Subject: test
267 Content-Type: text/html
268
269 <html><body><a href="http://test.com">test</a></body></html>
270 ]]
271 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
272 assert_true(res, "failed to load message")
273
274 task:process_message()
275
276 -- Pass a non-function
277 local success, err = pcall(function()
278 task:rewrite_html_urls("not a function")
279 end)
280
281 assert_false(success, "should fail with invalid callback")
282 assert_true(err:find("function expected", 1, true) ~= nil,
283 "error message should mention function expected")
284
285 task:destroy()
286 end)
287
288 test("Multipart message with multiple HTML parts", function()
289 local msg = [[
290 From: [email protected]
291 To: [email protected]
292 Subject: test
293 Content-Type: multipart/alternative; boundary="boundary123"
294
295 --boundary123
296 Content-Type: text/plain
297
298 Plain text part
299
300 --boundary123
301 Content-Type: text/html
302
303 <html><body><a href="http://example.com/part1">Part 1</a></body></html>
304
305 --boundary123
306 Content-Type: text/html
307
308 <html><body><a href="http://example.com/part2">Part 2</a></body></html>
309
310 --boundary123--
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_seen = {}
318 local function rewrite_callback(task, url)
319 table.insert(urls_seen, url)
320 return "http://safe.com/" .. #urls_seen
321 end
322
323 local result = task:rewrite_html_urls(rewrite_callback)
324
325 assert_not_nil(result, "should rewrite multipart HTML")
326
327 -- Should have processed both HTML parts
328 local part_count = 0
329 for part_id, html_text in pairs(result) do
330 part_count = part_count + 1
331 end
332
333 assert_true(part_count >= 1, "should have rewritten at least one HTML part")
334 assert_true(#urls_seen >= 1, "should have found at least one URL")
335
336 task:destroy()
337 end)
338
339 test("URL with special characters", function()
340 local msg = [[
341 From: [email protected]
342 To: [email protected]
343 Subject: test
344 Content-Type: text/html
345
346 <html>
347 <body>
348 <a href="http://example.com/path?param=value&other=123#anchor">Link</a>
349 </body>
350 </html>
351 ]]
352 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
353 assert_true(res, "failed to load message")
354
355 task:process_message()
356
357 local captured_url = nil
358 local function rewrite_callback(task, url)
359 captured_url = url
360 return "http://safe.com/redirect"
361 end
362
363 local result = task:rewrite_html_urls(rewrite_callback)
364
365 assert_not_nil(result, "should handle URLs with special chars")
366 assert_not_nil(captured_url, "should have captured URL")
367
368 for part_id, html_text in pairs(result) do
369 local html = tostring(html_text)
370 assert_true(html:find("http://safe.com/redirect", 1, true) ~= nil,
371 "rewritten URL should be in output")
372 end
373
374 task:destroy()
375 end)
376
377 test("Data URI scheme is skipped", function()
378 local msg = [[
379 From: [email protected]
380 To: [email protected]
381 Subject: test
382 Content-Type: text/html
383
384 <html>
385 <body>
386 <img src="data:image/png;base64,iVBORw0KG==">
387 <a href="http://example.com/test">Real link</a>
388 </body>
389 </html>
390 ]]
391 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
392 assert_true(res, "failed to load message")
393
394 task:process_message()
395
396 local urls_seen = {}
397 local function rewrite_callback(task, url)
398 table.insert(urls_seen, url)
399 return "http://safe.com/redirect"
400 end
401
402 local result = task:rewrite_html_urls(rewrite_callback)
403
404 assert_not_nil(result, "should rewrite non-data URLs")
405
406 -- Should only see the http URL, not the data: URI
407 local found_data_uri = false
408 for _, url in ipairs(urls_seen) do
409 if url:find("^data:", 1) then
410 found_data_uri = true
411 end
412 end
413
414 assert_false(found_data_uri, "data: URIs should be skipped")
415 assert_true(#urls_seen >= 1, "should have found the http URL")
416
417 task:destroy()
418 end)
419
420 test("CID scheme is skipped", function()
421 local msg = [[
422 From: [email protected]
423 To: [email protected]
424 Subject: test
425 Content-Type: text/html
426
427 <html>
428 <body>
429 <img src="cid:[email protected]">
430 <a href="http://example.com/test">Real link</a>
431 </body>
432 </html>
433 ]]
434 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
435 assert_true(res, "failed to load message")
436
437 task:process_message()
438
439 local urls_seen = {}
440 local function rewrite_callback(task, url)
441 table.insert(urls_seen, url)
442 return "http://safe.com/redirect"
443 end
444
445 local result = task:rewrite_html_urls(rewrite_callback)
446
447 assert_not_nil(result, "should rewrite non-cid URLs")
448
449 -- Should only see the http URL, not the cid: URI
450 local found_cid_uri = false
451 for _, url in ipairs(urls_seen) do
452 if url:find("^cid:", 1) then
453 found_cid_uri = true
454 end
455 end
456
457 assert_false(found_cid_uri, "cid: URIs should be skipped")
458 assert_true(#urls_seen >= 1, "should have found the http URL")
459
460 task:destroy()
461 end)
462
463 test("Edge case: bare cid: and data: schemes", function()
464 local msg = [[
465 From: [email protected]
466 To: [email protected]
467 Subject: test
468 Content-Type: text/html
469
470 <html>
471 <body>
472 <img src="cid:">
473 <img src="data:">
474 <a href="http://example.com/test">Real link</a>
475 </body>
476 </html>
477 ]]
478 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
479 assert_true(res, "failed to load message")
480
481 task:process_message()
482
483 local urls_seen = {}
484 local function rewrite_callback(task, url)
485 table.insert(urls_seen, url)
486 return "http://safe.com/redirect"
487 end
488
489 local result = task:rewrite_html_urls(rewrite_callback)
490
491 assert_not_nil(result, "should rewrite non-special scheme URLs")
492
493 -- Should only see the http URL, not bare cid: or data:
494 assert_equal(#urls_seen, 1, "should see exactly 1 URL (the http one)")
495 assert_equal(urls_seen[1], "http://example.com/test", "should see the http URL")
496
497 task:destroy()
498 end)
499
500 end)