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
|
local msg
context("Selectors test", function()
local rspamd_task = require "rspamd_task"
local logger = require "rspamd_logger"
local lua_selectors = require "lua_selectors"
local lua_maps = require "lua_maps"
local test_helper = require "rspamd_test_helper"
local lua_util = require "lua_util"
local cfg = rspamd_config
local task
test_helper.init_url_parser()
lua_selectors.maps.test_map = lua_maps.map_add_from_ucl({
'key value',
'key1 value1',
'key3 value1',
}, 'hash', 'test selectors maps')
before(function()
local res
res,task = rspamd_task.load_from_string(msg, cfg)
task:set_from_ip("198.172.22.91")
task:set_user("cool user name")
task:set_helo("hello mail")
task:set_request_header("hdr1", "value1")
task:process_message()
task:get_mempool():set_variable("int_var", 1)
task:get_mempool():set_variable("str_var", "str 1")
task:cache_set('cachevar1', 'hello\x00world')
task:cache_set('cachevar2', {'hello', 'world'})
if not res then
assert_true(false, "failed to load message")
end
end)
local function check_selector_plain(selector_string)
local sels = lua_selectors.create_selector_closure_fn(nil, cfg, selector_string, nil,
function(_, res, _) return res end)
local elts = sels(task)
return elts
end
local function check_selector_kv(selector_string)
local sels = lua_selectors.create_selector_closure_fn(nil, cfg, selector_string, nil,
lua_selectors.kv_table_from_pairs)
local elts = sels(task)
return elts
end
local cases_plain = {
["ip"] = {
selector = "ip",
expect = {"198.172.22.91"}
},
["header Subject"] = {
selector = "header(Subject)",
expect = {"Second, lower-cased header subject"}
},
["header Subject lower"] = {
selector = "header(Subject).lower",
expect = {"second, lower-cased header subject"}
},
["header Subject lower_utf8"] = {
selector = "header(Subject).lower_utf8",
expect = {"second, lower-cased header subject"}
},
["header full Subject lower"] = {
selector = "header(Subject, 'full').lower",
expect = {{"second, lower-cased header subject", "test subject"}}
},
["header full strong Subject"] = {
selector = "header(Subject, 'full,strong')",
expect = {{"Test subject"}}
},
["header full strong lower-cased Subject"] = {
selector = "header(subject, 'full,strong')",
expect = {{"Second, lower-cased header subject"}}
},
["digest"] = {
selector = "digest",
expect = {"1ac109c58a7d0f5f532100ac14e9f4d9"}
},
["user"] = {
selector = "user",
expect = {"cool user name"}
},
["from"] = {
selector = "from",
expect = {"whoknows@nowhere.com"}
},
["rcpts"] = {
selector = "rcpts",
expect = {{"nobody@example.com", "no-one@example.com"}}
},
["1st rcpts"] = {
selector = "rcpts.nth(1)",
expect = {"nobody@example.com"}
},
["lower rcpts"] = {
selector = "rcpts.lower.first",
expect = {"nobody@example.com"}
},
["first rcpts"] = {
selector = "rcpts.first",
expect = {"nobody@example.com"}
},
["first addr rcpts"] = {
selector = "rcpts:addr.first",
expect = {"nobody@example.com"}
},
["rcpts_uniq_domains"] = {
selector = "rcpts:domain.uniq",
expect = {{"example.com"}}
},
["rcpts_sorted"] = {
selector = "rcpts:addr.sort",
expect = {{"nobody@example.com", "no-one@example.com"}}
},
["to"] = {
selector = "to",
expect = {"nobody@example.com"}},
["attachments"] = {
selector = "attachments",
expect = {{"ce112d07c52ae649f9646f3d0b5aaab5d4834836d771c032d1a75059d31fed84f38e00c0b205918f6d354934c2055d33d19d045f783a62561f467728ebcf0160",
"ce112d07c52ae649f9646f3d0b5aaab5d4834836d771c032d1a75059d31fed84f38e00c0b205918f6d354934c2055d33d19d045f783a62561f467728ebcf0160"
}}
},
["attachments blake2 base32"] = {
selector = "attachments('base32', 'blake2')",
expect = {{"qqr41dwakt3uwhucxmxsypjiifi8er3gzqhyc3r48fw1ij9dp8b8x8nyyscmoe6tpmp1r4eafezguezurazo87ecs48cw5bfm9udyob",
"qqr41dwakt3uwhucxmxsypjiifi8er3gzqhyc3r48fw1ij9dp8b8x8nyyscmoe6tpmp1r4eafezguezurazo87ecs48cw5bfm9udyob"
}}
},
["attachments blake2 base64"] = {
selector = "attachments('base64', 'blake2')",
expect = {{"zhEtB8Uq5kn5ZG89C1qqtdSDSDbXccAy0adQWdMf7YTzjgDAsgWRj201STTCBV0z0Z0EX3g6YlYfRnco688BYA==",
"zhEtB8Uq5kn5ZG89C1qqtdSDSDbXccAy0adQWdMf7YTzjgDAsgWRj201STTCBV0z0Z0EX3g6YlYfRnco688BYA=="
}}
},
["attachments blake2 rfc base32"] = {
selector = "attachments('rbase32', 'blake2')",
expect = {{"ZYIS2B6FFLTET6LEN46QWWVKWXKIGSBW25Y4AMWRU5IFTUY75WCPHDQAYCZALEMPNU2USNGCAVOTHUM5ARPXQOTCKYPUM5ZI5PHQCYA",
"ZYIS2B6FFLTET6LEN46QWWVKWXKIGSBW25Y4AMWRU5IFTUY75WCPHDQAYCZALEMPNU2USNGCAVOTHUM5ARPXQOTCKYPUM5ZI5PHQCYA"
}}
},
["attachments md5 rfc base32"] = {
selector = "attachments('rbase32', 'md5')",
expect = {{"LYXF2IMILRFFO4LLTDTM66MKEA",
"LYXF2IMILRFFO4LLTDTM66MKEA"
}}
},
["attachments id"] = {
selector = "attachments.id",
expect = {""}},
["files"] = {
selector = "files",
expect = {{"f.zip", "f2.zip"}}},
["helo"] = {
selector = "helo",
expect = {"hello mail"}},
["received ip"] = {
selector = "received:by_hostname.filter_string_nils",
expect = {{"server1.chat-met-vreemden.nl", "server2.chat-met-vreemden.nl"}}},
["received by hostname last"] = {
selector = "received:by_hostname.filter_string_nils.last",
expect = {"server2.chat-met-vreemden.nl"}
},
["received by hostname first"] = {
selector = "received:by_hostname.filter_string_nils.first",
expect = {"server1.chat-met-vreemden.nl"}
},
["urls"] = {
selector = "urls",
expect = {{"http://subdomain.example.net"}}},
["emails"] = {
selector = "emails",
expect = {{"test@example.net"}}},
["specific_urls"] = {
selector = "specific_urls({limit = 1})",
expect = {{"http://subdomain.example.net"}}},
["specific_urls + emails"] = {
selector = "specific_urls({need_emails = true, limit = 2})",
expect = {{"test@example.net", "http://subdomain.example.net"}}},
-- Broken test as order depends on the hash function internally
--["specific_urls + emails limit"] = {
-- selector = "specific_urls({need_emails = true, limit = 1})",
-- expect = {{"test@example.net"}}},
["pool_var str, default type"] = {
selector = [[pool_var("str_var")]],
expect = {"str 1"}},
["pool_var str"] = {
selector = [[pool_var("str_var", 'string')]],
expect = {"str 1"}},
["pool_var double"] = {
selector = [[pool_var("int_var", 'double')]],
expect = {"1"}},
["time"] = {
selector = "time",
expect = {"1537364211"}},
-- ["request_header"] = {
-- selector = "request_header(hdr1)",
-- expect = {"value1"}},
["get_host"] = {
selector = "urls:get_host",
expect = {{"subdomain.example.net"}}},
["get_tld_method"] = {
selector = "urls:get_tld",
expect = {{"example.net"}}},
["get_tld_transform"] = {
selector = "urls:get_host.get_tld",
expect = {{"example.net"}}},
["transformation regexp"] = {
selector = "urls:get_tld.regexp('\\.([\\w]+)$')",
expect = {{{".net", "net"}}}},
["transformation id"] = {
selector = "urls:get_tld.id",
expect = {''}},
["transformation id arg"] = {
selector = "urls:get_tld.id('1')",
expect = {'1'}},
["transformation id args"] = {
selector = "urls:get_tld.id('1', '2', '3')",
expect = {{'1', '2', '3'}}},
["transformation in"] = {
selector = "time(message, '!%w').in(2,3,4)",
expect = {'3'}},
["transformation in id"] = {
selector = "time(message, '!%w').in(2,3,4).id",
expect = {''}},
["transformation not in"] = {
selector = "time(message, '!%w').not_in(1,6,7)",
expect = {'3'}},
["transformation in not id"] = {
selector = "time(message, '!%w').not_in(1,6,7).id",
expect = {''}},
["transformation in not id 1"] = {
selector = "time(message, '!%w').not_in(1,6,7).id(1)",
expect = {'1'}},
["transformation take"] = {
selector = "rcpts.take_n(1).lower",
expect = {{'nobody@example.com'}}},
["transformation take 2"] = {
selector = "rcpts.take_n(2).lower",
expect = {{'nobody@example.com', 'no-one@example.com'}}},
["transformation take 3"] = {
selector = "rcpts.take_n(3).lower",
expect = {{'nobody@example.com', 'no-one@example.com'}}},
["transformation nth"] = {
selector = "rcpts.nth(1).lower",
expect = {'nobody@example.com'}},
["transformation nth 2"] = {
selector = "rcpts.nth(2).lower",
expect = {'no-one@example.com'}},
["transformation last"] = {
selector = "rcpts.last.lower",
expect = {'no-one@example.com'}},
["transformation substring"] = {
selector = "header(Subject, strong).substring(6)",
expect = {'subject'}},
["transformation substring 2"] = {
selector = "header(Subject, strong).substring(6, 7)",
expect = {'su'}},
["transformation substring -4"] = {
selector = "header(Subject, strong).substring(-4)",
expect = {'ject'}
},
["map filter"] = {
selector = "id('key').filter_map(test_map)",
expect = {'key'}
},
["map except"] = {
selector = "list('key', 'key1', 'key2', 'key3', 'key4').except_map(test_map)",
expect = {{'key2', 'key4'}}
},
["map apply"] = {
selector = "id('key').apply_map(test_map)",
expect = {'value'}
},
["map filter list"] = {
selector = "list('key', 'key1', 'key2').filter_map(test_map)",
expect = {{'key', 'key1'}}
},
["map apply list"] = {
selector = "list('key', 'key1', 'key2', 'key3').apply_map(test_map)",
expect = {{'value', 'value1', 'value1'}}
},
["map apply list uniq"] = {
selector = "list('key', 'key1', 'key2', 'key3').apply_map(test_map).uniq",
expect = {{'value1', 'value'}}
},
["words"] = {
selector = "words('norm')",
expect = {{'hello', 'world', 'mail', 'me'}}
},
["words_full"] = {
selector = "words('full'):2",
expect = {{'hello', 'world', '', 'mail', 'me'}}
},
["header X-Test first"] = {
selector = "header(X-Test, full).first",
expect = {"1"}
},
["header X-Test last"] = {
selector = "header(X-Test, full).last",
expect = {"3"}
},
["header lower digest substring"] = {
selector = "header('Subject').lower.digest('hex').substring(1, 16)",
expect = {"736ad5f50fc95d73"}
},
["header gsub"] = {
selector = "header('Subject'):gsub('a', 'b')",
expect = {"Second, lower-cbsed hebder subject"}
},
["header regexp first"] = {
selector = "header('Subject').regexp('.*').first",
expect = {"Second, lower-cased header subject"}
},
["task cache string"] = {
selector = "task_cache('cachevar1')",
expect = {"hello\x00world"}
},
["task cache table"] = {
selector = "task_cache('cachevar2')",
expect = {{"hello", "world"}}
},
}
for case_name, case in lua_util.spairs(cases_plain) do
test("plain case " .. case_name, function()
local elts = check_selector_plain(case.selector)
assert_not_nil(elts)
assert_rspamd_table_eq_sorted({actual = elts, expect = case.expect})
end)
end
local cases_kv = {
["ip"] = {
selector = "id('ip');ip",
expect = { ip = "198.172.22.91" }
},
["ip+words"] = {
selector = "id('ip');ip;id('words');words('full'):2",
expect = { ip = "198.172.22.91", words = {'hello', 'world', '', 'mail', 'me'} }
},
}
for case_name, case in lua_util.spairs(cases_kv) do
test("kv case " .. case_name, function()
local elts = check_selector_kv(case.selector)
assert_not_nil(elts)
assert_rspamd_table_eq_sorted({actual = elts, expect = case.expect})
end)
end
end)
--[=========[ ******************* message ******************* ]=========]
msg = [[
Received: from ca-18-193-131.service1.infuturo.it ([151.18.193.131] helo=User)
by server1.chat-met-vreemden.nl with esmtpa (Exim 4.76)
(envelope-from <upwest201diana@outlook.com>)
id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200
Received: from ca-18-193-131.service2.infuturo.it ([151.18.193.132] helo=User)
by server2.chat-met-vreemden.nl with esmtpa (Exim 4.76)
(envelope-from <upwest201diana@outlook.com>)
id 1ZC1sl-0006b4-TU; Mon, 06 Jul 2015 10:36:08 +0200
From: <whoknows@nowhere.com>
To: <nobody@example.com>, <no-one@example.com>
Date: Wed, 19 Sep 2018 14:36:51 +0100 (BST)
subject: Second, lower-cased header subject
Subject: Test subject
X-Test: 1
X-Test: 2
X-Test: 3
Content-Type: multipart/alternative;
boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Hello world
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: text/html; charset="utf-8"
<html><body>
<a href="http://subdomain.example.net">http://subdomain.example.net</a>
<a href="mailto:test@example.net">mail me</a>
</html>
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: application/zip; name=f.zip
Content-Disposition: attachment; size=166; filename=f.zip
Content-Transfer-Encoding: base64
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==
--_000_6be055295eab48a5af7ad4022f33e2d0_
Content-Type: application/zip; name=f.zip
Content-Disposition: attachment; size=166; filename=f2.zip
Content-Transfer-Encoding: base64
UEsDBAoAAAAAAINe6kgAAAAAAAAAAAAAAAAIABwAZmFrZS5leGVVVAkAA8YaglfGGoJXdXgLAAEE
6AMAAAToAwAAUEsBAh4DCgAAAAAAg17qSAAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAALSBAAAAAGZh
a2UuZXhlVVQFAAPGGoJXdXgLAAEE6AMAAAToAwAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==
]]
|