File: get_html_urls.lua

package info (click to toggle)
rspamd 3.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 35,064 kB
  • sloc: ansic: 247,728; cpp: 107,741; javascript: 31,385; perl: 3,089; asm: 2,512; pascal: 1,625; python: 1,510; sh: 589; sql: 313; makefile: 195; xml: 74
file content (338 lines) | stat: -rw-r--r-- 8,712 bytes parent folder | download
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
context("HTML URL extraction", function()
  local rspamd_task = require("rspamd_task")
  local logger = require("rspamd_logger")

  test("Basic URL extraction from simple HTML", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

<html>
<body>
<a href="http://example.com/test">Click here</a>
</body>
</html>
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    assert_not_nil(urls, "should extract URLs")

    -- Check structure
    local found_url = false
    for part_id, url_list in pairs(urls) do
      assert_true(type(url_list) == "table", "URL list should be a table")
      for i, url_info in ipairs(url_list) do
        assert_not_nil(url_info.url, "should have url field")
        assert_not_nil(url_info.attr, "should have attr field")
        assert_not_nil(url_info.tag, "should have tag field")

        if url_info.url == "http://example.com/test" then
          assert_equal(url_info.attr, "href", "should be href attribute")
          assert_equal(url_info.tag, "a", "should be <a> tag")
          found_url = true
        end
      end
    end

    assert_true(found_url, "should find the expected URL")

    task:destroy()
  end)

  test("Multiple URLs in same HTML part", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

<html>
<body>
<a href="http://example.com/link1">Link 1</a>
<a href="http://example.com/link2">Link 2</a>
<img src="http://example.com/image.jpg">
</body>
</html>
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    assert_not_nil(urls, "should extract URLs")

    -- Count URLs
    local url_count = 0
    local found_urls = {}
    for part_id, url_list in pairs(urls) do
      for i, url_info in ipairs(url_list) do
        url_count = url_count + 1
        found_urls[url_info.url] = url_info
      end
    end

    assert_equal(url_count, 3, "should have found 3 URLs")
    assert_not_nil(found_urls["http://example.com/link1"], "should find link1")
    assert_not_nil(found_urls["http://example.com/link2"], "should find link2")
    assert_not_nil(found_urls["http://example.com/image.jpg"], "should find image")

    -- Check attributes
    assert_equal(found_urls["http://example.com/link1"].attr, "href")
    assert_equal(found_urls["http://example.com/link1"].tag, "a")
    assert_equal(found_urls["http://example.com/image.jpg"].attr, "src")
    assert_equal(found_urls["http://example.com/image.jpg"].tag, "img")

    task:destroy()
  end)

  test("Non-HTML parts return nil", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/plain

This is plain text with http://example.com/test
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    -- Should return nil for plain text
    assert_nil(urls, "should return nil for non-HTML parts")

    task:destroy()
  end)

  test("Empty HTML returns nil", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    -- Should return nil for empty HTML
    assert_nil(urls, "should return nil for empty HTML")

    task:destroy()
  end)

  test("HTML without URLs returns nil", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

<html>
<body>
<p>Just some text without any links</p>
</body>
</html>
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    -- Should return nil when no URLs found
    assert_nil(urls, "should return nil when no URLs found")

    task:destroy()
  end)

  test("Data URI scheme is skipped", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

<html>
<body>
<img src="data:image/png;base64,iVBORw0KG==">
<a href="http://example.com/test">Real link</a>
</body>
</html>
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    assert_not_nil(urls, "should extract non-data URLs")

    -- Check that data: URIs are skipped
    local found_data_uri = false
    local found_http_url = false
    for part_id, url_list in pairs(urls) do
      for i, url_info in ipairs(url_list) do
        if url_info.url:find("^data:", 1, false) then
          found_data_uri = true
        end
        if url_info.url == "http://example.com/test" then
          found_http_url = true
        end
      end
    end

    assert_false(found_data_uri, "data: URIs should be skipped")
    assert_true(found_http_url, "should have found the http URL")

    task:destroy()
  end)

  test("CID scheme is skipped", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

<html>
<body>
<img src="cid:image001@example.com">
<a href="http://example.com/test">Real link</a>
</body>
</html>
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    assert_not_nil(urls, "should extract non-cid URLs")

    -- Check that cid: URIs are skipped
    local found_cid_uri = false
    local found_http_url = false
    for part_id, url_list in pairs(urls) do
      for i, url_info in ipairs(url_list) do
        if url_info.url:find("^cid:", 1, false) then
          found_cid_uri = true
        end
        if url_info.url == "http://example.com/test" then
          found_http_url = true
        end
      end
    end

    assert_false(found_cid_uri, "cid: URIs should be skipped")
    assert_true(found_http_url, "should have found the http URL")

    task:destroy()
  end)

  test("Multipart message with multiple HTML parts", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: multipart/alternative; boundary="boundary123"

--boundary123
Content-Type: text/plain

Plain text part

--boundary123
Content-Type: text/html

<html><body><a href="http://example.com/part1">Part 1</a></body></html>

--boundary123
Content-Type: text/html

<html><body><a href="http://example.com/part2">Part 2</a></body></html>

--boundary123--
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    assert_not_nil(urls, "should extract URLs from multipart HTML")

    -- Should have processed at least one HTML part
    local part_count = 0
    local total_urls = 0
    for part_id, url_list in pairs(urls) do
      part_count = part_count + 1
      total_urls = total_urls + #url_list
    end

    assert_true(part_count >= 1, "should have URLs from at least one HTML part")
    assert_true(total_urls >= 1, "should have found at least one URL")

    task:destroy()
  end)

  test("URL with special characters", function()
    local msg = [[
From: test@example.com
To: nobody@example.com
Subject: test
Content-Type: text/html

<html>
<body>
<a href="http://example.com/path?param=value&other=123#anchor">Link</a>
</body>
</html>
]]
    local res, task = rspamd_task.load_from_string(msg, rspamd_config)
    assert_true(res, "failed to load message")

    task:process_message()

    local urls = task:get_html_urls()

    assert_not_nil(urls, "should handle URLs with special chars")

    local found_url = false
    for part_id, url_list in pairs(urls) do
      for i, url_info in ipairs(url_list) do
        if url_info.url:find("example.com/path", 1, true) then
          found_url = true
          -- URL should contain the query parameters
          assert_true(url_info.url:find("param=value", 1, true) ~= nil,
                     "should preserve query parameters")
        end
      end
    end

    assert_true(found_url, "should have found the URL with special chars")

    task:destroy()
  end)

end)