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
|
--[=========[ ******************* message ******************* ]=========]
local msg = [[
Received: from mail0.mindspring.com (unknown [1.1.1.1])
(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by mail.example.com (Postfix) with ESMTPS id 88A0C6B332
for <example@example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)
From: <>
To: <nobody@example.com>
Subject: test
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>
lol
</html>
]]
context("Task piecewise split", function()
local rspamd_task = require "rspamd_task"
local rspamd_util = require "rspamd_util"
local rspamd_test_helper = require "rspamd_test_helper"
local lua_mime = require "lua_mime"
local ucl = require "ucl"
local rspamd_parsers = require "rspamd_parsers"
rspamd_test_helper.init_url_parser()
local cfg = rspamd_util.config_from_ucl(rspamd_test_helper.default_config(),
"INIT_URL,INIT_LIBS,INIT_SYMCACHE,INIT_VALIDATE,INIT_PRELOAD_MAPS")
test("Simple message split", function()
local res,task = rspamd_task.load_from_string(msg, cfg)
if not res or not task then
assert_true(false, "failed to load message")
end
task:set_from('smtp', rspamd_parsers.parse_mail_address("Test <test@example.com>")[1])
task:set_recipients('smtp', {
rspamd_parsers.parse_mail_address("Test1 <test1@example.com>")[1],
rspamd_parsers.parse_mail_address("Test2 <test2@example.com>")[1]
}, 'rewrite')
task:set_from_ip("198.172.22.91")
task:set_user("cool user name")
task:set_helo("hello mail")
task:process_message()
local expected_json = [[
{
"parts": [
{
"type": "multipart/alternative",
"multipart_boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
"size": 0,
"headers": []
},
{
"content": "Hello world\n\n\n",
"size": 14,
"type": "text/plain",
"boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
"detected_type": "text/plain",
"headers": [
{
"order": 0,
"raw": "Content-Type: text/plain; charset=\"utf-8\"\n",
"empty_separator": false,
"value": "text/plain; charset=\"utf-8\"",
"separator": " ",
"decoded": "text/plain; charset=\"utf-8\"",
"name": "Content-Type",
"tab_separated": false
},
{
"order": 1,
"raw": "Content-Transfer-Encoding: 7bit\n",
"empty_separator": false,
"value": "7bit",
"separator": " ",
"decoded": "7bit",
"name": "Content-Transfer-Encoding",
"tab_separated": false
}
]
},
{
"content": "<html><body>\nlol\n</html>\n",
"size": 25,
"type": "text/html",
"boundary": "_000_6be055295eab48a5af7ad4022f33e2d0_",
"detected_type": "text/html",
"headers": [
{
"order": 0,
"raw": "Content-Type: text/html; charset=\"utf-8\"\n",
"empty_separator": false,
"value": "text/html; charset=\"utf-8\"",
"separator": " ",
"decoded": "text/html; charset=\"utf-8\"",
"name": "Content-Type",
"tab_separated": false
}
]
}
],
"newlines": "lf",
"digest": "043cf1a314d0a1af95951d6aec932faf",
"envelope": {
"recipients_smtp": [
{
"addr": "test1@example.com",
"raw": "<test1@example.com>",
"flags": {
"valid": true
},
"user": "test1",
"name": "Test1",
"domain": "example.com"
},
{
"addr": "test2@example.com",
"raw": "<test2@example.com>",
"flags": {
"valid": true
},
"user": "test2",
"name": "Test2",
"domain": "example.com"
}
],
"from_smtp": {
"addr": "test@example.com",
"raw": "<test@example.com>",
"flags": {
"valid": true
},
"user": "test",
"name": "Test",
"domain": "example.com"
},
"helo": "hello mail",
"from_ip": "198.172.22.91"
},
"size": 666,
"headers": [
{
"order": 0,
"raw": "Received: from mail0.mindspring.com (unknown [1.1.1.1])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby mail.example.com (Postfix) with ESMTPS id 88A0C6B332\n\tfor <example@example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)\n",
"empty_separator": false,
"value": "from mail0.mindspring.com (unknown [1.1.1.1]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.example.com (Postfix) with ESMTPS id 88A0C6B332 for <example@example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)",
"separator": " ",
"decoded": "from mail0.mindspring.com (unknown [1.1.1.1]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.example.com (Postfix) with ESMTPS id 88A0C6B332 for <example@example.com>; Wed, 24 Nov 2021 19:05:43 +0000 (GMT)",
"name": "Received",
"tab_separated": false
},
{
"order": 1,
"raw": "From: <>\n",
"empty_separator": false,
"value": "<>",
"separator": " ",
"decoded": "<>",
"name": "From",
"tab_separated": false
},
{
"order": 2,
"raw": "To: <nobody@example.com>\n",
"empty_separator": false,
"value": "<nobody@example.com>",
"separator": " ",
"decoded": "<nobody@example.com>",
"name": "To",
"tab_separated": false
},
{
"order": 3,
"raw": "Subject: test\n",
"empty_separator": false,
"value": "test",
"separator": " ",
"decoded": "test",
"name": "Subject",
"tab_separated": false
},
{
"order": 4,
"raw": "Content-Type: multipart/alternative;\n boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"\n",
"empty_separator": false,
"value": "multipart/alternative; boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"",
"separator": " ",
"decoded": "multipart/alternative; boundary=\"_000_6be055295eab48a5af7ad4022f33e2d0_\"",
"name": "Content-Type",
"tab_separated": false
}
]
}
]]
local parser = ucl.parser()
local res = parser:parse_string(expected_json)
assert_true(res)
local expected = parser:get_object()
local ucl_object = lua_mime.message_to_ucl(task, true)
local schema = lua_mime.message_to_ucl_schema()
assert_true(schema(ucl_object))
assert_rspamd_table_eq({
actual = ucl_object,
expect = expected
})
task:destroy()
end)
end)
|