| 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 lua_maps = require "lua_maps" |
| 7 |
local test_helper = require "rspamd_test_helper" |
| 8 |
local lua_util = require "lua_util" |
| 9 |
local cfg = rspamd_config |
| 10 |
local task |
| 11 |
|
| 12 |
test_helper.init_url_parser() |
| 13 |
|
| 14 |
lua_selectors.maps.test_map = lua_maps.map_add_from_ucl({ |
| 15 |
'key value', |
| 16 |
'key1 value1', |
| 17 |
'key3 value1', |
| 18 |
}, 'hash', 'test selectors maps') |
| 19 |
|
| 20 |
before(function() |
| 21 |
local res |
| 22 |
res,task = rspamd_task.load_from_string(msg, cfg) |
| 23 |
task:set_from_ip("198.172.22.91") |
| 24 |
task:set_user("cool user name") |
| 25 |
task:set_helo("hello mail") |
| 26 |
task:set_request_header("hdr1", "value1") |
| 27 |
task:process_message() |
| 28 |
task:get_mempool():set_variable("int_var", 1) |
| 29 |
task:get_mempool():set_variable("str_var", "str 1") |
| 30 |
task:cache_set('cachevar1', 'hello\x00world') |
| 31 |
task:cache_set('cachevar2', {'hello', 'world'}) |
| 32 |
if not res then |
| 33 |
assert_true(false, "failed to load message") |
| 34 |
end |
| 35 |
end) |
| 36 |
|
| 37 |
local function check_selector_plain(selector_string) |
| 38 |
local sels = lua_selectors.create_selector_closure_fn(nil, cfg, selector_string, nil, |
| 39 |
function(_, res, _) return res end) |
| 40 |
local elts = sels(task) |
| 41 |
return elts |
| 42 |
end |
| 43 |
|
| 44 |
local function check_selector_kv(selector_string) |
| 45 |
local sels = lua_selectors.create_selector_closure_fn(nil, cfg, selector_string, nil, |
| 46 |
lua_selectors.kv_table_from_pairs) |
| 47 |
local elts = sels(task) |
| 48 |
return elts |
| 49 |
end |
| 50 |
|
| 51 |
local cases_plain = { |
| 52 |
["ip"] = { |
| 53 |
selector = "ip", |
| 54 |
expect = {"198.172.22.91"} |
| 55 |
}, |
| 56 |
|
| 57 |
["header Subject"] = { |
| 58 |
selector = "header(Subject)", |
| 59 |
expect = {"Second, lower-cased header subject"} |
| 60 |
}, |
| 61 |
|
| 62 |
["header Subject lower"] = { |
| 63 |
selector = "header(Subject).lower", |
| 64 |
expect = {"second, lower-cased header subject"} |
| 65 |
}, |
| 66 |
|
| 67 |
["header Subject lower_utf8"] = { |
| 68 |
selector = "header(Subject).lower_utf8", |
| 69 |
expect = {"second, lower-cased header subject"} |
| 70 |
}, |
| 71 |
|
| 72 |
["header full Subject lower"] = { |
| 73 |
selector = "header(Subject, 'full').lower", |
| 74 |
expect = {{"second, lower-cased header subject", "test subject"}} |
| 75 |
}, |
| 76 |
|
| 77 |
["header full strong Subject"] = { |
| 78 |
selector = "header(Subject, 'full,strong')", |
| 79 |
expect = {{"Test subject"}} |
| 80 |
}, |
| 81 |
|
| 82 |
["header full strong lower-cased Subject"] = { |
| 83 |
selector = "header(subject, 'full,strong')", |
| 84 |
expect = {{"Second, lower-cased header subject"}} |
| 85 |
}, |
| 86 |
|
| 87 |
["digest"] = { |
| 88 |
selector = "digest", |
| 89 |
expect = {"1ac109c58a7d0f5f532100ac14e9f4d9"} |
| 90 |
}, |
| 91 |
|
| 92 |
["user"] = { |
| 93 |
selector = "user", |
| 94 |
expect = {"cool user name"} |
| 95 |
}, |
| 96 |
|
| 97 |
["from"] = { |
| 98 |
selector = "from", |
| 99 |
expect = {"[email protected]"} |
| 100 |
}, |
| 101 |
|
| 102 |
["rcpts"] = { |
| 103 |
selector = "rcpts", |
| 104 |
expect = {{"[email protected]", "[email protected]"}} |
| 105 |
}, |
| 106 |
|
| 107 |
["1st rcpts"] = { |
| 108 |
selector = "rcpts.nth(1)", |
| 109 |
expect = {"[email protected]"} |
| 110 |
}, |
| 111 |
|
| 112 |
["lower rcpts"] = { |
| 113 |
selector = "rcpts.lower.first", |
| 114 |
expect = {"[email protected]"} |
| 115 |
}, |
| 116 |
|
| 117 |
["first rcpts"] = { |
| 118 |
selector = "rcpts.first", |
| 119 |
expect = {"[email protected]"} |
| 120 |
}, |
| 121 |
|
| 122 |
["first addr rcpts"] = { |
| 123 |
selector = "rcpts:addr.first", |
| 124 |
expect = {"[email protected]"} |
| 125 |
}, |
| 126 |
|
| 127 |
["rcpts_uniq_domains"] = { |
| 128 |
selector = "rcpts:domain.uniq", |
| 129 |
expect = {{"example.com"}} |
| 130 |
}, |
| 131 |
|
| 132 |
["rcpts_sorted"] = { |
| 133 |
selector = "rcpts:addr.sort", |
| 134 |
expect = {{"[email protected]", "[email protected]"}} |
| 135 |
}, |
| 136 |
|
| 137 |
["to"] = { |
| 138 |
selector = "to", |
| 139 |
expect = {"[email protected]"}}, |
| 140 |
|
| 141 |
["attachments"] = { |
| 142 |
selector = "attachments", |
| 143 |
expect = {{"ce112d07c52ae649f9646f3d0b5aaab5d4834836d771c032d1a75059d31fed84f38e00c0b205918f6d354934c2055d33d19d045f783a62561f467728ebcf0160", |
| 144 |
"ce112d07c52ae649f9646f3d0b5aaab5d4834836d771c032d1a75059d31fed84f38e00c0b205918f6d354934c2055d33d19d045f783a62561f467728ebcf0160" |
| 145 |
}} |
| 146 |
}, |
| 147 |
|
| 148 |
["attachments blake2 base32"] = { |
| 149 |
selector = "attachments('base32', 'blake2')", |
| 150 |
expect = {{"qqr41dwakt3uwhucxmxsypjiifi8er3gzqhyc3r48fw1ij9dp8b8x8nyyscmoe6tpmp1r4eafezguezurazo87ecs48cw5bfm9udyob", |
| 151 |
"qqr41dwakt3uwhucxmxsypjiifi8er3gzqhyc3r48fw1ij9dp8b8x8nyyscmoe6tpmp1r4eafezguezurazo87ecs48cw5bfm9udyob" |
| 152 |
}} |
| 153 |
}, |
| 154 |
|
| 155 |
["attachments blake2 base64"] = { |
| 156 |
selector = "attachments('base64', 'blake2')", |
| 157 |
expect = {{"zhEtB8Uq5kn5ZG89C1qqtdSDSDbXccAy0adQWdMf7YTzjgDAsgWRj201STTCBV0z0Z0EX3g6YlYfRnco688BYA==", |
| 158 |
"zhEtB8Uq5kn5ZG89C1qqtdSDSDbXccAy0adQWdMf7YTzjgDAsgWRj201STTCBV0z0Z0EX3g6YlYfRnco688BYA==" |
| 159 |
}} |
| 160 |
}, |
| 161 |
|
| 162 |
["attachments blake2 rfc base32"] = { |
| 163 |
selector = "attachments('rbase32', 'blake2')", |
| 164 |
expect = {{"ZYIS2B6FFLTET6LEN46QWWVKWXKIGSBW25Y4AMWRU5IFTUY75WCPHDQAYCZALEMPNU2USNGCAVOTHUM5ARPXQOTCKYPUM5ZI5PHQCYA", |
| 165 |
"ZYIS2B6FFLTET6LEN46QWWVKWXKIGSBW25Y4AMWRU5IFTUY75WCPHDQAYCZALEMPNU2USNGCAVOTHUM5ARPXQOTCKYPUM5ZI5PHQCYA" |
| 166 |
}} |
| 167 |
}, |
| 168 |
|
| 169 |
["attachments md5 rfc base32"] = { |
| 170 |
selector = "attachments('rbase32', 'md5')", |
| 171 |
expect = {{"LYXF2IMILRFFO4LLTDTM66MKEA", |
| 172 |
"LYXF2IMILRFFO4LLTDTM66MKEA" |
| 173 |
}} |
| 174 |
}, |
| 175 |
|
| 176 |
["attachments id"] = { |
| 177 |
selector = "attachments.id", |
| 178 |
expect = {""}}, |
| 179 |
|
| 180 |
["files"] = { |
| 181 |
selector = "files", |
| 182 |
expect = {{"f.zip", "f2.zip"}}}, |
| 183 |
|
| 184 |
["helo"] = { |
| 185 |
selector = "helo", |
| 186 |
expect = {"hello mail"}}, |
| 187 |
|
| 188 |
["received ip"] = { |
| 189 |
selector = "received:by_hostname.filter_string_nils", |
| 190 |
expect = {{"server1.chat-met-vreemden.nl", "server2.chat-met-vreemden.nl"}}}, |
| 191 |
|
| 192 |
["received by hostname last"] = { |
| 193 |
selector = "received:by_hostname.filter_string_nils.last", |
| 194 |
expect = {"server2.chat-met-vreemden.nl"} |
| 195 |
}, |
| 196 |
|
| 197 |
["received by hostname first"] = { |
| 198 |
selector = "received:by_hostname.filter_string_nils.first", |
| 199 |
expect = {"server1.chat-met-vreemden.nl"} |
| 200 |
}, |
| 201 |
|
| 202 |
["urls"] = { |
| 203 |
selector = "urls", |
| 204 |
expect = {{"http://subdomain.example.net"}}}, |
| 205 |
|
| 206 |
["emails"] = { |
| 207 |
selector = "emails", |
| 208 |
expect = {{"[email protected]"}}}, |
| 209 |
|
| 210 |
["specific_urls"] = { |
| 211 |
selector = "specific_urls({limit = 1})", |
| 212 |
expect = {{"http://subdomain.example.net"}}}, |
| 213 |
|
| 214 |
["specific_urls + emails"] = { |
| 215 |
selector = "specific_urls({need_emails = true, limit = 2})", |
| 216 |
expect = {{"[email protected]", "http://subdomain.example.net"}}}, |
| 217 |
|
| 218 |
-- Broken test as order depends on the hash function internally |
| 219 |
--["specific_urls + emails limit"] = { |
| 220 |
-- selector = "specific_urls({need_emails = true, limit = 1})", |
| 221 |
-- expect = {{"[email protected]"}}}, |
| 222 |
|
| 223 |
["pool_var str, default type"] = { |
| 224 |
selector = [[pool_var("str_var")]], |
| 225 |
expect = {"str 1"}}, |
| 226 |
|
| 227 |
["pool_var str"] = { |
| 228 |
selector = [[pool_var("str_var", 'string')]], |
| 229 |
expect = {"str 1"}}, |
| 230 |
|
| 231 |
["pool_var double"] = { |
| 232 |
selector = [[pool_var("int_var", 'double')]], |
| 233 |
expect = {tostring(1.0)}}, |
| 234 |
|
| 235 |
["time"] = { |
| 236 |
selector = "time", |
| 237 |
expect = {tostring(1537364211.0)}}, |
| 238 |
|
| 239 |
-- ["request_header"] = { |
| 240 |
-- selector = "request_header(hdr1)", |
| 241 |
-- expect = {"value1"}}, |
| 242 |
|
| 243 |
["get_host"] = { |
| 244 |
selector = "urls:get_host", |
| 245 |
expect = {{"subdomain.example.net"}}}, |
| 246 |
|
| 247 |
["get_tld_method"] = { |
| 248 |
selector = "urls:get_tld", |
| 249 |
expect = {{"example.net"}}}, |
| 250 |
["get_tld_transform"] = { |
| 251 |
selector = "urls:get_host.get_tld", |
| 252 |
expect = {{"example.net"}}}, |
| 253 |
|
| 254 |
["transformation regexp"] = { |
| 255 |
selector = "urls:get_tld.regexp('\\.([\\w]+)$')", |
| 256 |
expect = {{{".net", "net"}}}}, |
| 257 |
|
| 258 |
["transformation id"] = { |
| 259 |
selector = "urls:get_tld.id", |
| 260 |
expect = {''}}, |
| 261 |
|
| 262 |
["transformation id arg"] = { |
| 263 |
selector = "urls:get_tld.id('1')", |
| 264 |
expect = {'1'}}, |
| 265 |
|
| 266 |
["transformation id args"] = { |
| 267 |
selector = "urls:get_tld.id('1', '2', '3')", |
| 268 |
expect = {{'1', '2', '3'}}}, |
| 269 |
|
| 270 |
["transformation in"] = { |
| 271 |
selector = "time(message, '!%w').in(2,3,4)", |
| 272 |
expect = {'3'}}, |
| 273 |
|
| 274 |
["transformation in id"] = { |
| 275 |
selector = "time(message, '!%w').in(2,3,4).id", |
| 276 |
expect = {''}}, |
| 277 |
|
| 278 |
["transformation not in"] = { |
| 279 |
selector = "time(message, '!%w').not_in(1,6,7)", |
| 280 |
expect = {'3'}}, |
| 281 |
|
| 282 |
["transformation in not id"] = { |
| 283 |
selector = "time(message, '!%w').not_in(1,6,7).id", |
| 284 |
expect = {''}}, |
| 285 |
|
| 286 |
["transformation in not id 1"] = { |
| 287 |
selector = "time(message, '!%w').not_in(1,6,7).id(1)", |
| 288 |
expect = {'1'}}, |
| 289 |
|
| 290 |
["transformation take"] = { |
| 291 |
selector = "rcpts.take_n(1).lower", |
| 292 |
expect = {{'[email protected]'}}}, |
| 293 |
|
| 294 |
["transformation take 2"] = { |
| 295 |
selector = "rcpts.take_n(2).lower", |
| 296 |
expect = {{'[email protected]', '[email protected]'}}}, |
| 297 |
|
| 298 |
["transformation take 3"] = { |
| 299 |
selector = "rcpts.take_n(3).lower", |
| 300 |
expect = {{'[email protected]', '[email protected]'}}}, |
| 301 |
|
| 302 |
["transformation nth"] = { |
| 303 |
selector = "rcpts.nth(1).lower", |
| 304 |
expect = {'[email protected]'}}, |
| 305 |
|
| 306 |
["transformation nth 2"] = { |
| 307 |
selector = "rcpts.nth(2).lower", |
| 308 |
expect = {'[email protected]'}}, |
| 309 |
|
| 310 |
["transformation last"] = { |
| 311 |
selector = "rcpts.last.lower", |
| 312 |
expect = {'[email protected]'}}, |
| 313 |
|
| 314 |
["transformation substring"] = { |
| 315 |
selector = "header(Subject, strong).substring(6)", |
| 316 |
expect = {'subject'}}, |
| 317 |
|
| 318 |
["transformation substring 2"] = { |
| 319 |
selector = "header(Subject, strong).substring(6, 7)", |
| 320 |
expect = {'su'}}, |
| 321 |
|
| 322 |
["transformation substring -4"] = { |
| 323 |
selector = "header(Subject, strong).substring(-4)", |
| 324 |
expect = {'ject'} |
| 325 |
}, |
| 326 |
["map filter"] = { |
| 327 |
selector = "id('key').filter_map(test_map)", |
| 328 |
expect = {'key'} |
| 329 |
}, |
| 330 |
["map except"] = { |
| 331 |
selector = "list('key', 'key1', 'key2', 'key3', 'key4').except_map(test_map)", |
| 332 |
expect = {{'key2', 'key4'}} |
| 333 |
}, |
| 334 |
["map apply"] = { |
| 335 |
selector = "id('key').apply_map(test_map)", |
| 336 |
expect = {'value'} |
| 337 |
}, |
| 338 |
["map filter list"] = { |
| 339 |
selector = "list('key', 'key1', 'key2').filter_map(test_map)", |
| 340 |
expect = {{'key', 'key1'}} |
| 341 |
}, |
| 342 |
["map apply list"] = { |
| 343 |
selector = "list('key', 'key1', 'key2', 'key3').apply_map(test_map)", |
| 344 |
expect = {{'value', 'value1', 'value1'}} |
| 345 |
}, |
| 346 |
["map apply list uniq"] = { |
| 347 |
selector = "list('key', 'key1', 'key2', 'key3').apply_map(test_map).uniq", |
| 348 |
expect = {{'value1', 'value'}} |
| 349 |
}, |
| 350 |
["words"] = { |
| 351 |
selector = "words('norm')", |
| 352 |
expect = {{'hello', 'world', 'mail', 'me'}} |
| 353 |
}, |
| 354 |
["words_full"] = { |
| 355 |
selector = "words('full'):2", |
| 356 |
expect = {{'hello', 'world', '', 'mail', 'me'}} |
| 357 |
}, |
| 358 |
["header X-Test first"] = { |
| 359 |
selector = "header(X-Test, full).first", |
| 360 |
expect = {"1"} |
| 361 |
}, |
| 362 |
["header X-Test last"] = { |
| 363 |
selector = "header(X-Test, full).last", |
| 364 |
expect = {"3"} |
| 365 |
}, |
| 366 |
["header lower digest substring"] = { |
| 367 |
selector = "header('Subject').lower.digest('hex').substring(1, 16)", |
| 368 |
expect = {"736ad5f50fc95d73"} |
| 369 |
}, |
| 370 |
["header gsub"] = { |
| 371 |
selector = "header('Subject'):gsub('a', 'b')", |
| 372 |
expect = {"Second, lower-cbsed hebder subject"} |
| 373 |
}, |
| 374 |
["header regexp first"] = { |
| 375 |
selector = "header('Subject').regexp('.*').first", |
| 376 |
expect = {"Second, lower-cased header subject"} |
| 377 |
}, |
| 378 |
|
| 379 |
["task cache string"] = { |
| 380 |
selector = "task_cache('cachevar1')", |
| 381 |
expect = {"hello\x00world"} |
| 382 |
}, |
| 383 |
["task cache table"] = { |
| 384 |
selector = "task_cache('cachevar2')", |
| 385 |
expect = {{"hello", "world"}} |
| 386 |
}, |
| 387 |
} |
| 388 |
|
| 389 |
for case_name, case in lua_util.spairs(cases_plain) do |
| 390 |
test("plain case " .. case_name, function() |
| 391 |
local elts = check_selector_plain(case.selector) |
| 392 |
assert_not_nil(elts) |
| 393 |
assert_rspamd_table_eq_sorted({actual = elts, expect = case.expect}) |
| 394 |
end) |
| 395 |
end |
| 396 |
|
| 397 |
local cases_kv = { |
| 398 |
["ip"] = { |
| 399 |
selector = "id('ip');ip", |
| 400 |
expect = { ip = "198.172.22.91" } |
| 401 |
}, |
| 402 |
["ip+words"] = { |
| 403 |
selector = "id('ip');ip;id('words');words('full'):2", |
| 404 |
expect = { ip = "198.172.22.91", words = {'hello', 'world', '', 'mail', 'me'} } |
| 405 |
}, |
| 406 |
} |
| 407 |
for case_name, case in lua_util.spairs(cases_kv) do |
| 408 |
test("kv case " .. case_name, function() |
| 409 |
local elts = check_selector_kv(case.selector) |
| 410 |
assert_not_nil(elts) |
| 411 |
assert_rspamd_table_eq_sorted({actual = elts, expect = case.expect}) |
| 412 |
end) |
| 413 |
end |
| 414 |
end) |
| 415 |
|
| 416 |
|
| 417 |
--[=========[ ******************* message ******************* ]=========] |
| 418 |
msg = [[ |
| 419 |
Received: from ca-18-193-131.service1.infuturo.it ([151.18.193.131] helo=User) |
| 420 |
by server1.chat-met-vreemden.nl with esmtpa (Exim 4.76) |
| 421 |
(envelope-from <[email protected]>) |
| 422 |
id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200 |
| 423 |
Received: from ca-18-193-131.service2.infuturo.it ([151.18.193.132] helo=User) |
| 424 |
by server2.chat-met-vreemden.nl with esmtpa (Exim 4.76) |
| 425 |
(envelope-from <[email protected]>) |
| 426 |
id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200 |
| 427 |
From: <[email protected]> |
| 428 |
To: <[email protected]>, <[email protected]> |
| 429 |
Date: Wed, 19 Sep 2018 14:36:51 +0100 (BST) |
| 430 |
subject: Second, lower-cased header subject |
| 431 |
Subject: Test subject |
| 432 |
X-Test: 1 |
| 433 |
X-Test: 2 |
| 434 |
X-Test: 3 |
| 435 |
Content-Type: multipart/alternative; |
| 436 |
boundary="_000_6be055295eab48a5af7ad4022f33e2d0_" |
| 437 |
|
| 438 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 439 |
Content-Type: text/plain; charset="utf-8" |
| 440 |
Content-Transfer-Encoding: 7bit |
| 441 |
|
| 442 |
Hello world |
| 443 |
|
| 444 |
|
| 445 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 446 |
Content-Type: text/html; charset="utf-8" |
| 447 |
|
| 448 |
<html><body> |
| 449 |
<a href="http://subdomain.example.net">http://subdomain.example.net</a> |
| 450 |
<a href="mailto:[email protected]">mail me</a> |
| 451 |
</html> |
| 452 |
|
| 453 |
|
| 454 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 455 |
Content-Type: application/zip; name=f.zip |
| 456 |
Content-Disposition: attachment; size=166; filename=f.zip |
| 457 |
Content-Transfer-Encoding: base64 |
| 458 |
|
| 459 |
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE |
| 460 |
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh |
| 461 |
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA== |
| 462 |
|
| 463 |
|
| 464 |
--_000_6be055295eab48a5af7ad4022f33e2d0_ |
| 465 |
Content-Type: application/zip; name=f.zip |
| 466 |
Content-Disposition: attachment; size=166; filename=f2.zip |
| 467 |
Content-Transfer-Encoding: base64 |
| 468 |
|
| 469 |
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE |
| 470 |
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh |
| 471 |
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA== |
| 472 |
]] |