You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

selectors.combined.lua 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. local msg
  2. context("Selectors test", function()
  3. local rspamd_task = require "rspamd_task"
  4. local logger = require "rspamd_logger"
  5. local lua_selectors = require "lua_selectors"
  6. local test_helper = require "rspamd_test_helper"
  7. local cfg = rspamd_config
  8. local task
  9. test_helper.init_url_parser()
  10. before(function()
  11. local res
  12. res,task = rspamd_task.load_from_string(msg, cfg)
  13. task:set_from_ip("198.172.22.91")
  14. task:set_user("cool user name")
  15. task:set_helo("hello mail")
  16. task:set_request_header("hdr1", "value1")
  17. task:process_message()
  18. task:get_mempool():set_variable("int_var", 1)
  19. task:get_mempool():set_variable("str_var", "str 1")
  20. if not res then
  21. assert_true(false, "failed to load message")
  22. end
  23. end)
  24. local function check_selector(selector_string)
  25. local sels = lua_selectors.parse_selector(cfg, selector_string)
  26. local elts = lua_selectors.process_selectors(task, sels)
  27. local res = lua_selectors.combine_selectors(task, elts, ':')
  28. return res
  29. end
  30. local cases = {
  31. ["rcpts + weekend"] = {
  32. selector = "rcpts:addr.take_n(5).lower;time('message', '!%w').in(6, 7).id('weekends')",
  33. expect = {
  34. "nobody@example.com:weekends",
  35. "no-one@example.com:weekends"}},
  36. ["weekend + rcpts"] = {
  37. selector = "time('message', '!%w').in(6, 7).id('weekends');rcpts:addr.take_n(5).lower",
  38. expect = {
  39. "weekends:nobody@example.com",
  40. "weekends:no-one@example.com"}},
  41. ["id(rcpt) + rcpts + weekend"] = {
  42. selector = "id('rcpt');rcpts:addr.take_n(5).lower;time('message', '!%w').in(6, 7).id('weekends')",
  43. expect = {
  44. "rcpt:nobody@example.com:weekends",
  45. "rcpt:no-one@example.com:weekends"}},
  46. ["id(rcpt) + id(2) rcpts + weekend"] = {
  47. selector = "id('rcpt'); id(2); rcpts:addr.take_n(5).lower; time('message', '!%w').in(6, 7).id('weekends')",
  48. expect = {
  49. "rcpt:2:nobody@example.com:weekends",
  50. "rcpt:2:no-one@example.com:weekends"}},
  51. -- There are two rcpts but only one url in the message
  52. -- resulting table size is the size of the smallest table
  53. ["id(rcpt) + id(2) + rcpts and urls + weekend"] = {
  54. selector = "id('rcpt'); id(2); rcpts:addr.take_n(5).lower; id('urls'); urls:get_host; time('message', '!%w').in(6, 7).id('weekends')",
  55. expect = {
  56. "rcpt:2:nobody@example.com:urls:example.net:weekends"}},
  57. }
  58. for case_name, case in pairs(cases) do
  59. test("case " .. case_name, function()
  60. local elts = check_selector(case.selector)
  61. assert_not_nil(elts)
  62. assert_rspamd_table_eq({actual = elts, expect = case.expect})
  63. end)
  64. end
  65. end)
  66. --[=========[ ******************* message ******************* ]=========]
  67. msg = [[
  68. Received: from ca-18-193-131.service.infuturo.it ([151.18.193.131] helo=User)
  69. by server.chat-met-vreemden.nl with esmtpa (Exim 4.76)
  70. (envelope-from <upwest201diana@outlook.com>)
  71. id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200
  72. From: <whoknows@nowhere.com>
  73. To: <nobody@example.com>, <no-one@example.com>
  74. Date: Sat, 22 Sep 2018 14:36:51 +0100 (BST)
  75. subject: Second, lower-cased header subject
  76. Subject: Test subject
  77. Content-Type: multipart/alternative;
  78. boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
  79. --_000_6be055295eab48a5af7ad4022f33e2d0_
  80. Content-Type: text/plain; charset="utf-8"
  81. Content-Transfer-Encoding: base64
  82. Hello world
  83. --_000_6be055295eab48a5af7ad4022f33e2d0_
  84. Content-Type: text/html; charset="utf-8"
  85. <html><body>
  86. <a href="http://example.net">http://example.net</a>
  87. <a href="mailto:test@example.net">mail me</a>
  88. </html>
  89. --_000_6be055295eab48a5af7ad4022f33e2d0_
  90. Content-Type: application/zip; name=f.zip
  91. Content-Disposition: attachment; size=166; filename=f.zip
  92. Content-Transfer-Encoding: base64
  93. UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE
  94. 6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh
  95. a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==
  96. --_000_6be055295eab48a5af7ad4022f33e2d0_
  97. Content-Type: application/zip; name=f.zip
  98. Content-Disposition: attachment; size=166; filename=f2.zip
  99. Content-Transfer-Encoding: base64
  100. UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE
  101. 6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh
  102. a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==
  103. ]]