File: html.lua

package info (click to toggle)
rspamd 3.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,056 kB
  • sloc: ansic: 243,746; cpp: 105,657; javascript: 29,539; asm: 2,512; perl: 2,440; pascal: 1,625; python: 1,274; sql: 313; sh: 281; makefile: 140; xml: 74
file content (493 lines) | stat: -rw-r--r-- 14,122 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
context("HTML processing", function()
  local rspamd_util = require("rspamd_util")
  local cases = {
    -- Entities
    { [[<html><body>.&#102;&#105;&#114;&#101;&#98;&#97;&#115;&#101;&#97;&#112;&#112;.&#99;&#111;&#109;</body></html>]],
      [[.firebaseapp.com]] },
    { [[
<?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE html
   PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
     <title>
       Wikibooks
     </title>
   </head>
   <body>
     <p>
       Hello,          world!

     </p>
   </body>
 </html>]], 'Hello, world!\n' },
    { [[
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <style><!--
- -a -a -a -- --- -
  --></head>
  <body>
    <!-- page content -->
    Hello, world!
  </body>
</html>
      ]], 'Hello, world!' },
    { [[
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
    <!-- page content -->
    Hello, world!<br>test</br><br>content</hr>more content<br>
    <div>
      content inside div
    </div>
  </body>
</html>
      ]], 'Hello, world!\ntest\ncontentmore content\ncontent inside div\n' },
    { [[
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
    <!-- tabular content -->
    <table>
      content
    </table>
    <table>
      <tr>
        <th>heada</th>
        <th>headb</th>
      </tr>
      <tr>
        <td>data1</td>
        <td>data2</td>
      </tr>
    </table>

  </body>
</html>
      ]], 'content\nheada headb\ndata1 data2\n' },
    { [[
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
    <!-- escape content -->
    a&nbsp;b a &gt; b a &lt; b a &amp; b &apos;a &quot;a&quot;
  </body>
</html>
      ]], 'a b a > b a < b a & b \'a "a"' },
  }

  for i, c in ipairs(cases) do
    test("Extract text from HTML " .. tostring(i), function()
      local t = rspamd_util.parse_html(c[1])

      assert_not_nil(t)
      assert_equal(c[2], tostring(t), string.format("'%s' doesn't match with '%s'",
        c[2], t))
    end)
  end

  -- Test cases for new HTML tag API methods
  local function parse_html_and_extract_tags(html_content, pool)
    local rspamd_parsers = require("rspamd_parsers")

    local parsed = rspamd_parsers.parse_html_content(html_content, pool)
    local tags = {}

    if parsed then
      parsed:foreach_tag("any", function(tag, content_length, is_leaf)
        table.insert(tags, tag)
        return false
      end)
    end

    return parsed, tags
  end

  test("HTML tag get_all_attributes basic test", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[<div class="test-class" id="test-id" style="color: red;" width="100">content</div>]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)
    assert_true(#tags > 0)

    -- Find the div tag
    local div_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "div" then
        div_tag = tag
        break
      end
    end

    assert_not_nil(div_tag)

    local attrs = div_tag:get_all_attributes()
    assert_not_nil(attrs)

    -- Check that we have the expected attributes
    assert_equal("test-class", attrs["class"])
    assert_equal("test-id", attrs["id"])
    assert_equal("color: red;", attrs["style"])
    assert_equal("100", attrs["width"])

    pool:destroy()
  end)

  test("HTML tag has_attribute test", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[<img src="test.jpg" width="100" height="50" alt="Test image" hidden />]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local img_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "img" then
        img_tag = tag
        break
      end
    end

    assert_not_nil(img_tag)

    -- Test existing attributes
    assert_true(img_tag:has_attribute("src"))
    assert_true(img_tag:has_attribute("width"))
    assert_true(img_tag:has_attribute("height"))
    assert_true(img_tag:has_attribute("alt"))
    assert_true(img_tag:has_attribute("hidden"))

    -- Test non-existing attributes
    assert_false(img_tag:has_attribute("nonexistent"))
    assert_false(img_tag:has_attribute("class"))
    assert_false(img_tag:has_attribute(""))

    pool:destroy()
  end)

  test("HTML tag get_numeric_attribute test", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[<div width="200" height="150" font-size="14" opacity="0.8" tabindex="5">content</div>]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local div_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "div" then
        div_tag = tag
        break
      end
    end

    assert_not_nil(div_tag)

    -- Test numeric attributes
    assert_equal(200, div_tag:get_numeric_attribute("width"))
    assert_equal(150, div_tag:get_numeric_attribute("height"))
    assert_equal(14, div_tag:get_numeric_attribute("font-size"))

    -- Test opacity with floating-point tolerance
    local opacity = div_tag:get_numeric_attribute("opacity")
    assert_not_nil(opacity)
    assert_true(math.abs(opacity - 0.8) < 0.01, string.format("Expected opacity ~0.8, got %f", opacity))

    assert_equal(5, div_tag:get_numeric_attribute("tabindex"))

    -- Test non-numeric attributes
    assert_nil(div_tag:get_numeric_attribute("nonexistent"))

    pool:destroy()
  end)

  test("HTML tag get_unknown_attributes test", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[<div class="known" data-track="analytics" unknown-attr="test-value" custom-id="12345">content</div>]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local div_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "div" then
        div_tag = tag
        break
      end
    end

    assert_not_nil(div_tag)

    local unknown_attrs = div_tag:get_unknown_attributes()
    assert_not_nil(unknown_attrs)

    -- Should include unknown attributes but not known ones like "class"
    assert_not_nil(unknown_attrs["unknown-attr"])
    assert_equal("test-value", unknown_attrs["unknown-attr"])
    assert_not_nil(unknown_attrs["custom-id"])
    assert_equal("12345", unknown_attrs["custom-id"])

    -- data-track should be recognized as a known attribute now
    -- but if not, it would appear in unknown attributes

    pool:destroy()
  end)

  test("HTML tag get_children test", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[
      <div id="parent">
        <p>First child</p>
        <span>Second child</span>
        <img src="test.jpg" />
      </div>
    ]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local parent_div = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "div" and tag:has_attribute("id") and tag:get_attribute("id") == "parent" then
        parent_div = tag
        break
      end
    end

    assert_not_nil(parent_div)

    local children = parent_div:get_children()
    assert_not_nil(children)
    assert_equal(3, #children)

    -- Check child types
    local child_types = {}
    for _, child in ipairs(children) do
      table.insert(child_types, child:get_type())
    end

    -- Should contain p, span, and img
    local child_types_str = table.concat(child_types, ",")
    assert_true(child_types_str:find("p") ~= nil)
    assert_true(child_types_str:find("span") ~= nil)
    assert_true(child_types_str:find("img") ~= nil)

    pool:destroy()
  end)

  test("HTML tag get_attribute vs get_all_attributes consistency", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[<a href="https://example.com" class="link" target="_blank" title="Example Link">Link</a>]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local a_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "a" then
        a_tag = tag
        break
      end
    end

    assert_not_nil(a_tag)

    local all_attrs = a_tag:get_all_attributes()

    -- Test that individual get_attribute calls match get_all_attributes
    for attr_name, attr_value in pairs(all_attrs) do
      assert_equal(attr_value, a_tag:get_attribute(attr_name),
        string.format("Attribute '%s' mismatch: get_attribute='%s', get_all_attributes='%s'",
          attr_name, a_tag:get_attribute(attr_name) or "nil", attr_value))
    end

    -- Test specific expected attributes
    assert_equal("https://example.com", a_tag:get_attribute("href"))
    assert_equal("link", a_tag:get_attribute("class"))
    assert_equal("_blank", a_tag:get_attribute("target"))
    assert_equal("Example Link", a_tag:get_attribute("title"))

    pool:destroy()
  end)



  test("HTML tag attribute edge cases", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[<div class="" hidden style=" " width="0" height="abc">content</div>]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local div_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "div" then
        div_tag = tag
        break
      end
    end

    assert_not_nil(div_tag)

    -- Test empty attribute value
    assert_true(div_tag:has_attribute("class"))
    assert_equal("", div_tag:get_attribute("class"))

    -- Test boolean attribute (hidden)
    assert_true(div_tag:has_attribute("hidden"))

    -- Test whitespace-only attribute
    assert_true(div_tag:has_attribute("style"))
    assert_equal(" ", div_tag:get_attribute("style"))

    -- Test numeric attributes with edge cases
    assert_equal(0, div_tag:get_numeric_attribute("width"))
    assert_nil(div_tag:get_numeric_attribute("height")) -- "abc" is not numeric

    -- Test non-existent attribute
    assert_false(div_tag:has_attribute("nonexistent"))
    assert_nil(div_tag:get_attribute("nonexistent"))
    assert_nil(div_tag:get_numeric_attribute("nonexistent"))

    pool:destroy()
  end)

  test("HTML tag complex nested structure", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html = [[
      <table cellpadding="5" cellspacing="2" border="1">
        <tr>
          <td align="center" valign="top" width="100">
            <img src="image1.jpg" width="80" height="60" alt="Image 1" />
          </td>
          <td align="left" valign="middle">
            <p font-size="12">Text content</p>
          </td>
        </tr>
      </table>
    ]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    -- Find table tag
    local table_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "table" then
        table_tag = tag
        break
      end
    end

    assert_not_nil(table_tag)

    -- Test table attributes
    assert_equal(5, table_tag:get_numeric_attribute("cellpadding"))
    assert_equal(2, table_tag:get_numeric_attribute("cellspacing"))
    assert_equal("1", table_tag:get_attribute("border"))

    -- Test that table has children
    local children = table_tag:get_children()
    assert_not_nil(children)
    assert_true(#children > 0)

    -- Find img tag
    local img_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "img" then
        img_tag = tag
        break
      end
    end

    assert_not_nil(img_tag)

    -- Test img attributes
    assert_equal("image1.jpg", img_tag:get_attribute("src"))
    assert_equal(80, img_tag:get_numeric_attribute("width"))
    assert_equal(60, img_tag:get_numeric_attribute("height"))
    assert_equal("Image 1", img_tag:get_attribute("alt"))

    pool:destroy()
  end)

  test("HTML tag with mixed known and unknown attributes", function()
    local rspamd_mempool = require("rspamd_mempool")
    local pool = rspamd_mempool.create()

    local html =
    [[<div class="container" data-analytics="track" custom-attr="value" style="color: blue;" unknown123="test">content</div>]]
    local parsed, tags = parse_html_and_extract_tags(html, pool)

    assert_not_nil(parsed)

    local div_tag = nil
    for _, tag in ipairs(tags) do
      if tag:get_type() == "div" then
        div_tag = tag
        break
      end
    end

    assert_not_nil(div_tag)

    local all_attrs = div_tag:get_all_attributes()
    local unknown_attrs = div_tag:get_unknown_attributes()

    -- All attributes should include both known and unknown
    assert_not_nil(all_attrs["class"])       -- known
    assert_not_nil(all_attrs["style"])       -- known
    assert_not_nil(all_attrs["custom-attr"]) -- unknown
    assert_not_nil(all_attrs["unknown123"])  -- unknown

    -- Unknown attributes should only include unrecognized ones
    assert_nil(unknown_attrs["class"])           -- known, shouldn't be here
    assert_nil(unknown_attrs["style"])           -- known, shouldn't be here
    assert_not_nil(unknown_attrs["custom-attr"]) -- unknown, should be here
    assert_not_nil(unknown_attrs["unknown123"])  -- unknown, should be here

    assert_equal("value", unknown_attrs["custom-attr"])
    assert_equal("test", unknown_attrs["unknown123"])

    pool:destroy()
  end)
end)