Debian Codegraph @

test/lua/unit/task.lua

raw
1 context("Task processing", function()
2 local fun = require("fun")
3 local rspamd_task = require("rspamd_task")
4
5 test("Process a simple task", function()
6 --local cfg = rspamd_util.config_from_ucl(config)
7 --assert_not_nil(cfg)
8
9 local msg = [[
10 From: <>
11 To: <[email protected]>
12 Subject: test
13 Content-Type: text/plain
14
15 Test.
16 ]]
17 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
18 assert_true(res, "failed to load message")
19 task:process_message()
20 task:destroy()
21 end)
22
23 local hdrs = [[
24 From: <>
25 To: <[email protected]>
26 Subject: test
27 ]]
28 local mpart = [[
29 Content-Type: multipart/mixed; boundary=XXX
30 ]]
31 local body = [[
32 Content-Type: text/html
33 Content-Transfer-Encoding: quoted-printable
34
35 <html>
36 <body>
37 =0DAttached is your new documents.
38 <br>
39 <a href=3D"http://evil.com/Information/">http:=
40 //example.com/privacy/XXX/YYY_April_25_2019.doc</a>
41 <br>
42 <br>
43 <br>
44 Thank you,
45 <br>
46 <b>Haloclaims.co</b>
47 </body></html>
48 ]]
49 test("Process mime nesting: simple", function()
50 local msg = hdrs .. body
51 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
52 assert_true(res, "failed to load message")
53 task:process_message()
54 assert_rspamd_table_eq_sorted({
55 actual = fun.totable(fun.map(function(u)
56 return u:get_host()
57 end, task:get_urls())),
58 expect = {
59 'evil.com', 'example.com'
60 }
61 })
62 task:destroy()
63 end)
64 test("Process mime nesting: multipart", function()
65 local msg = table.concat {
66 hdrs, mpart, '\n', '--XXX\n', body, '\n--XXX--\n'
67 }
68 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
69 assert_true(res, "failed to load message")
70 task:process_message()
71 assert_rspamd_table_eq_sorted({
72 actual = fun.totable(fun.map(function(u)
73 return u:get_host()
74 end, task:get_urls())),
75
76 expect = {
77 'evil.com', 'example.com'
78 }
79 })
80 task:destroy()
81 end)
82 test("Process mime nesting: multipart, broken", function()
83 local msg = table.concat {
84 hdrs, mpart, '\n', '--XXX\n', 'garbadge\n', '\n--XXX--\n', '--XXX\n', body
85 }
86 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
87 assert_true(res, "failed to load message")
88 task:process_message()
89 assert_rspamd_table_eq_sorted({
90 actual = fun.totable(fun.map(function(u)
91 return u:get_host()
92 end, task:get_urls())),
93
94 expect = {
95 'evil.com', 'example.com'
96 }
97 })
98
99 task:destroy()
100 end)
101 test("Process mime nesting: message", function()
102 local msg = table.concat {
103 hdrs, 'Content-Type: message/rfc822\n', '\n', hdrs, body
104 }
105 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
106 assert_true(res, "failed to load message")
107 task:process_message()
108 assert_rspamd_table_eq_sorted({
109 actual = fun.totable(fun.map(function(u)
110 return u:get_host()
111 end, task:get_urls())),
112
113 expect = {
114 'evil.com', 'example.com'
115 }
116 })
117
118 task:destroy()
119 end)
120 test("Process mime nesting: message in multipart", function()
121 local msg = table.concat {
122 hdrs, mpart, '\n',
123 '--XXX\n',
124 'Content-Type: message/rfc822\n', '\n', hdrs, body,
125 '\n--XXX--\n',
126 }
127
128 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
129 assert_true(res, "failed to load message")
130 task:process_message()
131 assert_rspamd_table_eq_sorted({
132 actual = fun.totable(fun.map(function(u)
133 return u:get_host()
134 end, task:get_urls())),
135
136 expect = {
137 'evil.com', 'example.com'
138 }
139 })
140
141 task:destroy()
142 end)
143 test("Process mime nesting: multipart message in multipart", function()
144 local msg = table.concat {
145 hdrs, mpart, '\n',
146 '--XXX\n',
147 'Content-Type: message/rfc822\n', '\n', hdrs, mpart, '\n',
148
149 '--XXX\n',
150 body,
151 '\n--XXX--\n',
152
153 '\n--XXX--\n',
154 }
155 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
156 assert_true(res, "failed to load message")
157 task:process_message()
158 assert_rspamd_table_eq_sorted({
159 actual = fun.totable(fun.map(function(u)
160 return u:get_host()
161 end, task:get_urls())),
162
163 expect = {
164 'evil.com', 'example.com'
165 }
166 })
167
168 task:destroy()
169 end)
170
171 test("Part URLs are not deduplicated across MIME parts", function()
172 local msg = table.concat {
173 hdrs,
174 'Content-Type: multipart/alternative; boundary=XXX\n',
175 '\n',
176 '--XXX\n',
177 'Content-Type: text/plain\n',
178 '\n',
179 'Visit <http://example.com/a> and <http://example.com/b>\n',
180 '\n',
181 '--XXX\n',
182 'Content-Type: text/html\n',
183 '\n',
184 '<html><body>' ..
185 '<a href="http://example.com/a">A</a>' ..
186 '<a href="http://example.com/b">B</a>' ..
187 '</body></html>\n',
188 '\n',
189 '--XXX--\n',
190 }
191 local res, task = rspamd_task.load_from_string(msg, rspamd_config)
192 assert_true(res, "failed to load message")
193 task:process_message()
194
195 local parts = task:get_parts()
196 assert_true(#parts >= 2, "should have at least two MIME parts")
197
198 local function uniq_urls(part)
199 local seen = {}
200
201 return fun.totable(fun.filter(function(v)
202 if seen[v] then
203 return false
204 end
205
206 seen[v] = true
207 return true
208 end, fun.map(function(u)
209 return u:get_host() .. '/' .. u:get_path()
210 end, part:get_urls())))
211 end
212
213 assert_rspamd_table_eq_sorted({
214 actual = uniq_urls(parts[#parts - 1]),
215 expect = {
216 'example.com/a', 'example.com/b'
217 }
218 })
219
220 assert_rspamd_table_eq_sorted({
221 actual = uniq_urls(parts[#parts]),
222 expect = {
223 'example.com/a', 'example.com/b'
224 }
225 })
226
227 task:destroy()
228 end)
229 end)