aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/lua/force_actions.lua
blob: b73ab9d88df0ed09696dc7dd43df4bb9da69a25b (plain) (blame)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
--[[
Copyright (c) 2017-2026, Vsevolod Stakhov <vsevolod@rspamd.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--

-- A plugin that forces actions

if confighelp then
  return
end

local E = {}
local N = 'force_actions'
local selector_cache = {}

local fun = require "fun"
local lua_util = require "lua_util"
local rspamd_cryptobox_hash = require "rspamd_cryptobox_hash"
local rspamd_expression = require "rspamd_expression"
local rspamd_logger = require "rspamd_logger"
local lua_selectors = require "lua_selectors"

-- Composites are resolved after normal filters, so a rule referencing one must
-- be a postfilter. Only the composites registered before this module is
-- configured are seen here, and a composite with a postfilter atom is resolved
-- after all postfilters, so a rule still cannot use it. A rule is also starved
-- by an earlier pre-result without `process_all`, as that suppresses composites.
local function has_composite_atom(atoms)
  for _, a in ipairs(atoms) do
    -- get_symbol_flags returns an array of flag names, not a keyed map
    for _, fl in ipairs(rspamd_config:get_symbol_flags(a) or {}) do
      if fl == 'composite' then
        return true
      end
    end
  end
  return false
end

-- Passthrough priority band from src/libmime/scan_result.h:
-- 0 = low, 1 = normal (default), 2 = high, 3 = critical (used by the core for
-- broken messages). The C side stores it as unsigned int, so a negative value
-- would wrap around and outrank everything.
local min_priority, max_priority = 0, 3
local named_priorities = {
  low = 0,
  normal = 1,
  high = 2,
  critical = 3,
}
-- What set_pre_result uses for a rule without a priority
local default_priority = named_priorities.normal
-- Per task: the action a matching rule left to the score (see gen_cb)
local kept_cache_key = 'force_actions_kept'

local function add_flag(flags, flag)
  if flags == '' then
    return flag
  elseif flags:find(flag, 1, true) then
    return flags
  end
  return flags .. ',' .. flag
end

-- Options of the FORCE_ACTION_* symbol
local function rule_options(action, priority)
  if priority then
    return { action, string.format('priority:%s', priority) }
  end
  return { action }
end

-- The SMTP reply of the pre-result that selects the action, if it is this one
local function selected_reply(task, action)
  local _, pre_action, pre_message, _, pre_flags = task:has_pre_result(true)
  if pre_action == action and not (pre_flags or ''):find('no_smtp_message', 1, true) then
    return pre_message
  end
end

local function check_priority(name, priority)
  if priority == nil then
    return nil
  end

  if type(priority) == 'string' then
    local named = named_priorities[string.lower(priority)]

    if named then
      return named
    end

    -- UCL may hand us a quoted number
    local as_number = tonumber(priority)

    if not as_number then
      rspamd_logger.warnx(rspamd_config,
          'force_actions rule %1: unknown priority name "%2", expected one of low/normal/high/critical; ignored',
          name, priority)
      return nil
    end

    priority = as_number
  end

  if type(priority) ~= 'number' then
    rspamd_logger.warnx(rspamd_config, 'force_actions rule %1: priority must be a number or a name, got %2; ignored',
        name, type(priority))
    return nil
  end

  -- NaN is the only number that does not compare equal to itself
  if priority ~= priority then
    rspamd_logger.warnx(rspamd_config, 'force_actions rule %1: priority is NaN; ignored', name)
    return nil
  end

  -- LuaJIT also reads quoted "inf" as a number, while Lua 5.3+ does not
  if priority == math.huge or priority == -math.huge then
    rspamd_logger.warnx(rspamd_config, 'force_actions rule %1: priority is not finite; ignored', name)
    return nil
  end

  local adjusted = math.floor(priority)

  if adjusted == 0 then
    -- LuaJIT keeps the sign of -0.0
    adjusted = 0
  end

  if adjusted ~= priority then
    rspamd_logger.warnx(rspamd_config, 'force_actions rule %1: priority %2 is not an integer, adjusted to %3',
        name, priority, adjusted)
  end

  if adjusted < min_priority or adjusted > max_priority then
    local clamped = math.max(min_priority, math.min(max_priority, adjusted))
    rspamd_logger.warnx(rspamd_config,
        'force_actions rule %1: priority %2 is out of the allowed range [%3..%4], adjusted to %5',
        name, adjusted, min_priority, max_priority, clamped)
    adjusted = clamped
  end

  return adjusted
end

-- Params table fields:
-- expr, act, pool, message, subject, raction, honor, limit, flags, priority,
-- least; outranked and overridable are set once all rules are registered
local function gen_cb(params)
  params.flags = params.flags or ''

  local function parse_atom(str)
    local atom = table.concat(fun.totable(fun.take_while(function(c)
      if string.find(', \t()><+!|&\n', c, 1, true) then
        return false
      end
      return true
    end, fun.iter(str))), '')
    return atom
  end

  local function process_atom(atom, task)
    local f_ret = task:has_symbol(atom)
    if f_ret then
      f_ret = math.abs(task:get_symbol(atom)[1].score)
      if f_ret < 0.001 then
        -- Adjust some low score to distinguish from pure zero
        f_ret = 0.001
      end
      return f_ret
    end
    return 0
  end

  local e, err = rspamd_expression.create(params.expr, { parse_atom, process_atom }, params.pool)
  if err then
    rspamd_logger.errx(rspamd_config, 'Couldnt create expression [%1]: %2', params.expr, err)
    return
  end

  return function(task)

    local function process_message_selectors(repl, selector_expr)
      -- create/reuse selector to extract value for this placeholder
      local selector = selector_cache[selector_expr]
      if not selector then
        selector_cache[selector_expr] = lua_selectors.create_selector_closure(rspamd_config, selector_expr, '', true)
        selector = selector_cache[selector_expr]
        if not selector then
          rspamd_logger.errx(task, 'could not create selector [%1]', selector_expr)
          return "((could not create selector))"
        end
      end
      local extracted = selector(task)
      if extracted then
        if type(extracted) == 'table' then
          extracted = table.concat(extracted, ',')
        end
      else
        rspamd_logger.errx(task, 'could not extract value with selector [%1]', selector_expr)
        extracted = '((error extracting value))'
      end
      return extracted
    end

    local cact = task:get_metric_action()
    if params.honor and params.honor[cact] then
      return false
    elseif params.raction and not params.raction[cact] then
      return false
    end

    -- Forcing the selected action changes nothing, while recording it would
    -- make the verdict passthrough. A rule with its own priority records it
    -- anyway to outrank the results set later, but the require/honor_action
    -- rules run last, so there is nothing left for them to outrank.
    local already_selected = not params.message and not params.subject
        and params.act and cact == params.act
    local keep_only = already_selected and (not params.priority or params.raction or params.honor)
    if keep_only and not params.overridable then
      return false
    end

    local ret = e:process(task)
    lua_util.debugm(N, task, "expression %s returned %s", params.expr, ret)
    if (not params.limit and ret > 0) or (ret > (params.limit or 0)) then
      local rank = params.priority or default_priority
      local kept = task:cache_get(kept_cache_key)

      if keep_only then
        -- A rule below this one may still match and change the action, so
        -- remember that the action is wanted at this priority, unless a rule
        -- above did so for the action that is still selected
        if not kept or kept.action ~= cact or rank >= kept.rank then
          task:cache_set(kept_cache_key, { action = params.act, rank = rank, priority = params.priority,
                                           flags = add_flag(params.flags, 'process_all'),
                                           symbol = params.symbol })
        end
        return false
      end

      -- A least result only raises the action, as before, whatever the priority
      if kept and not kept.recorded and not params.least and kept.action == cact
          and kept.action ~= params.act and rank < kept.rank then
        -- This rule would override the action kept by a rule above it
        kept.recorded = true
        task:set_pre_result { action = kept.action, message = selected_reply(task, kept.action), module = N,
                              flags = kept.flags, priority = kept.priority }
        task:insert_result(kept.symbol, 1.0, rule_options(kept.action, kept.priority))
      end

      if params.subject then
        task:set_metric_subject(params.subject)
      end

      local flags = params.flags
      -- Do not cut the scan short when competing for the selected action, or
      -- when a rule with a higher priority may still match
      if already_selected or params.outranked then
        flags = add_flag(flags, 'process_all')
      end

      local message
      if type(params.message) == 'string' then
        -- process selector expressions in the message
        message = string.gsub(params.message, '(${(.-)})', process_message_selectors)
      elseif already_selected then
        -- This result may outrank the one for the same action, keep its reply
        message = selected_reply(task, params.act)
      end

      -- A least rule without priority stays below the least results of the
      -- other modules (e.g. DMARC), as before
      local priority = params.priority
      if not priority and params.least then
        priority = named_priorities.low
      end

      task:set_pre_result { action = params.act, message = message, module = N, flags = flags,
                            priority = priority }

      return true, rule_options(params.act, params.priority)
    end

  end, e:atoms()

end

local function configure_module()
  local opts = rspamd_config:get_all_opt(N)
  if not opts then
    return false
  end
  if type(opts.actions) == 'table' then
    rspamd_logger.warnx(rspamd_config, 'Processing legacy config')
    -- Stable order, as for the rules below; it decides between the actions
    -- matching at once
    local action_names = {}
    for action in pairs(opts.actions) do
      table.insert(action_names, action)
    end
    table.sort(action_names)

    for _, action in ipairs(action_names) do
      local expressions = opts.actions[action]
      if type(expressions) == 'table' then
        for _, expr in ipairs(expressions) do
          local message, subject
          if type(expr) == 'table' then
            subject = expr[3]
            message = expr[2]
            expr = expr[1]
          else
            message = (opts.messages or E)[expr]
          end
          if type(expr) == 'string' then
            -- expr, act, pool, message, subject, raction, honor, limit, flags
            local cb, atoms = gen_cb { expr = expr,
                                       act = action,
                                       pool = rspamd_config:get_mempool(),
                                       message = message,
                                       subject = subject }
            if cb and atoms then
              local h = rspamd_cryptobox_hash.create()
              h:update(expr)
              local name = 'FORCE_ACTION_' .. string.upper(string.sub(h:hex(), 1, 12))
              local composite_dep = has_composite_atom(atoms)
              local t = {
                name = name,
                callback = cb,
                flags = 'empty',
                group = N,
              }
              if composite_dep then
                -- Right after the composites, see the rules below
                t.type = 'postfilter'
                t.priority = lua_util.symbols_priorities.low
              else
                t.type = 'normal'
              end
              rspamd_config:register_symbol(t)
              if t.type == 'normal' then
                for _, a in ipairs(atoms) do
                  rspamd_config:register_dependency(name, a)
                end
                rspamd_logger.infox(rspamd_config, 'Registered symbol %1 <%2> with dependencies [%3]',
                    name, expr, table.concat(atoms, ','))
              else
                rspamd_logger.infox(rspamd_config,
                    'Registered symbol %1 <%2> as postfilter (expression uses composite symbol(s))', name, expr)
              end
            end
          end
        end
      end
    end
  elseif type(opts.rules) == 'table' then
    -- Register in a stable order: pairs() over a UCL object is arbitrary, and
    -- registration order leaks into the symcache ordering heuristics.
    local rule_names = {}
    for name in pairs(opts.rules) do
      table.insert(rule_names, name)
    end
    table.sort(rule_names)

    local priorities = {}
    for _, name in ipairs(rule_names) do
      priorities[name] = check_priority(name, opts.rules[name].priority)
    end
    -- The require/honor_action rules share one postfilter level, which runs in
    -- the registration order: the higher priorities have to see the action
    -- before the lower ones change it
    local function late_rank(name)
      local sett = opts.rules[name]
      if sett.require_action or sett.honor_action then
        return priorities[name] or (sett.least and named_priorities.low or default_priority)
      end
      return -1
    end
    table.sort(rule_names, function(a, b)
      local ra, rb = late_rank(a), late_rank(b)
      if ra ~= rb then
        return ra > rb
      end
      return a < b
    end)

    local registered = {}
    for _, name in ipairs(rule_names) do
      local sett = opts.rules[name]
      local action = sett.action
      local expr = sett.expression

      if action and expr then
        local flags = {}
        if sett.least then
          table.insert(flags, "least")
        end
        if sett.process_all then
          table.insert(flags, "process_all")
        end
        local raction = lua_util.list_to_hash(sett.require_action)
        local honor = lua_util.list_to_hash(sett.honor_action)
        local priority = priorities[name]
        if priority and sett.least then
          rspamd_logger.warnx(rspamd_config,
              'force_actions rule %1: `least` is set, so `priority` cannot outrank a non-least rule ' ..
              '(any non-least result always wins); it only orders this rule against other `least` rules',
              name)
        end
        local params = { expr = expr,
                         act = action,
                         pool = rspamd_config:get_mempool(),
                         message = sett.message,
                         subject = sett.subject,
                         raction = raction,
                         honor = honor,
                         limit = sett.limit,
                         priority = priority,
                         least = sett.least,
                         flags = table.concat(flags, ',') }
        local cb, atoms = gen_cb(params)
        if cb and atoms then
          local composite_dep = has_composite_atom(atoms)
          local t = {}
          if raction or honor then
            -- Acts on the action selected by everything else, so runs late
            t.type = 'postfilter'
            t.priority = lua_util.symbols_priorities.high
          elseif composite_dep then
            -- Right after the composites: before the postfilters acting on the
            -- selected action (e.g. greylisting) and the require/honor_action rules
            t.type = 'postfilter'
            t.priority = lua_util.symbols_priorities.low
          else
            t.type = 'normal'
            if not sett.least then
              t.augmentations = { 'passthrough', 'important' }
            end
          end
          t.name = 'FORCE_ACTION_' .. name
          params.symbol = t.name
          t.callback = cb
          t.flags = 'empty, ignore_passthrough'
          t.group = N
          rspamd_config:register_symbol(t)
          table.insert(registered, { params = params, main = t.type == 'normal' })

          local effective_priority = priority or (sett.least and named_priorities.low or default_priority)

          if t.type == 'normal' then
            for _, a in ipairs(atoms) do
              rspamd_config:register_dependency(t.name, a)
            end
            rspamd_logger.infox(rspamd_config,
                'Registered symbol %1 <%2> with dependencies [%3]; action %4, passthrough priority %5',
                t.name, expr, table.concat(atoms, ','), action, effective_priority)
          elseif composite_dep and not (raction or honor) then
            rspamd_logger.infox(rspamd_config,
                'Registered symbol %1 <%2> as postfilter (expression uses composite symbol(s)); ' ..
                'action %3, passthrough priority %4', t.name, expr, action, effective_priority)
          else
            rspamd_logger.infox(rspamd_config,
                'Registered symbol %1 <%2> as postfilter; action %3, passthrough priority %4',
                t.name, expr, action, effective_priority)
          end
        end
      end
    end

    for _, r in ipairs(registered) do
      local p = r.params
      local rank = p.priority or default_priority
      for _, other in ipairs(registered) do
        local o = other.params
        local other_rank = o.priority or default_priority
        -- A match without `process_all` stops the symbols that have not run
        -- yet, composites included, so a rule above it might never match (a
        -- least one cannot outrank it anyway). The postfilter rules run once
        -- all of their atoms are known, so they cannot be starved that way.
        if r.main and not o.least and other_rank > rank then
          p.outranked = true
        end
        -- A rule below this one could still change the selected action (a
        -- least one only raises it, whatever the priorities)
        if not p.least and not o.least and other_rank < rank and o.act ~= p.act then
          p.overridable = true
        end
      end
    end
  end
end

configure_module()