Debian Codegraph @

lualib/plugins/ratelimit.lua

raw
1 --[[
2 Copyright (c) 2024, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 local rspamd_logger = require "rspamd_logger"
18 local lua_util = require "lua_util"
19 local T = require "lua_shape.core"
20
21 local exports = {}
22
23 local limit_parser
24 local function parse_string_limit(lim, no_error)
25 local function parse_time_suffix(s)
26 if s == 's' then
27 return 1
28 elseif s == 'm' then
29 return 60
30 elseif s == 'h' then
31 return 3600
32 elseif s == 'd' then
33 return 86400
34 end
35 end
36 local function parse_num_suffix(s)
37 if s == '' then
38 return 1
39 elseif s == 'k' then
40 return 1000
41 elseif s == 'm' then
42 return 1000000
43 elseif s == 'g' then
44 return 1000000000
45 end
46 end
47 local lpeg = require "lpeg"
48
49 if not limit_parser then
50 local digit = lpeg.R("09")
51 limit_parser = {}
52 limit_parser.integer = (lpeg.S("+-") ^ -1) *
53 (digit ^ 1)
54 limit_parser.fractional = (lpeg.P(".")) *
55 (digit ^ 1)
56 limit_parser.number = (limit_parser.integer *
57 (limit_parser.fractional ^ -1)) +
58 (lpeg.S("+-") * limit_parser.fractional)
59 limit_parser.time = lpeg.Cf(lpeg.Cc(1) *
60 (limit_parser.number / tonumber) *
61 ((lpeg.S("smhd") / parse_time_suffix) ^ -1),
62 function(acc, val)
63 return acc * val
64 end)
65 limit_parser.suffixed_number = lpeg.Cf(lpeg.Cc(1) *
66 (limit_parser.number / tonumber) *
67 ((lpeg.S("kmg") / parse_num_suffix) ^ -1),
68 function(acc, val)
69 return acc * val
70 end)
71 limit_parser.limit = lpeg.Ct(limit_parser.suffixed_number *
72 (lpeg.S(" ") ^ 0) * lpeg.S("/") * (lpeg.S(" ") ^ 0) *
73 limit_parser.time)
74 end
75 local t = lpeg.match(limit_parser.limit, lim)
76
77 if t and t[1] and t[2] and t[2] ~= 0 then
78 return t[2], t[1]
79 end
80
81 if not no_error then
82 rspamd_logger.errx(rspamd_config, 'bad limit: %s', lim)
83 end
84
85 return nil
86 end
87
88 local function str_to_rate(str)
89 local divider, divisor = parse_string_limit(str, false)
90
91 if not divisor then
92 rspamd_logger.errx(rspamd_config, 'bad rate string: %s', str)
93
94 return nil
95 end
96
97 return divisor / divider
98 end
99
100 local bucket_schema = T.table({
101 burst = T.one_of({
102 T.number(),
103 T.transform(T.string(), lua_util.dehumanize_number)
104 }):doc({ summary = "Burst size (number of messages)" }),
105 rate = T.one_of({
106 T.number(),
107 T.transform(T.string(), str_to_rate)
108 }):doc({ summary = "Rate limit (messages per time unit)" }),
109 skip_recipients = T.boolean():optional():doc({ summary = "Skip per-recipient limits" }),
110 symbol = T.string():optional():doc({ summary = "Custom symbol name" }),
111 message = T.string():optional():doc({ summary = "Custom reject message" }),
112 skip_soft_reject = T.boolean():optional():doc({ summary = "Skip soft reject" }),
113 ham_factor_rate = T.number():optional():doc({ summary = "Bucket-specific ham_factor_rate"}),
114 spam_factor_rate = T.number():optional():doc({ summary = "Bucket-specific spam_factor_rate"}),
115 ham_factor_burst = T.number():optional():doc({ summary = "Bucket-specific ham_factor_burst"}),
116 spam_factor_burst = T.number():optional():doc({ summary = "Bucket-specific spam_factor_burst"}),
117 max_rate_mult = T.number():optional():doc({ summary = "Bucket-specific rate multiplicator limit"}),
118 max_bucket_mult = T.number():optional():doc({ summary = "Bucket-specific bucket multiplicator limit"}),
119 }):doc({ summary = "Ratelimit bucket configuration" })
120
121 exports.parse_limit = function(name, data)
122 if type(data) == 'table' then
123 -- 2 cases here:
124 -- * old limit in format [burst, rate]
125 -- * vector of strings in Andrew's string format (removed from 1.8.2)
126 -- * proper bucket table
127 if #data == 2 and tonumber(data[1]) and tonumber(data[2]) then
128 -- Old style ratelimit
129 rspamd_logger.warnx(rspamd_config, 'old style ratelimit for %s', name)
130 if tonumber(data[1]) > 0 and tonumber(data[2]) > 0 then
131 return {
132 burst = data[1],
133 rate = data[2]
134 }
135 elseif data[1] ~= 0 then
136 rspamd_logger.warnx(rspamd_config, 'invalid numbers for %s', name)
137 else
138 rspamd_logger.infox(rspamd_config, 'disable limit %s, burst is zero', name)
139 end
140
141 return nil
142 else
143 local parsed_bucket, err = bucket_schema:transform(data)
144
145 if not parsed_bucket or err then
146 rspamd_logger.errx(rspamd_config, 'cannot parse bucket for %s: %s; original value: %s',
147 name, err, data)
148 else
149 return parsed_bucket
150 end
151 end
152 elseif type(data) == 'string' then
153 local rep_rate, burst = parse_string_limit(data)
154 rspamd_logger.warnx(rspamd_config, 'old style rate bucket config detected for %s: %s',
155 name, data)
156 if rep_rate and burst then
157 return {
158 burst = burst,
159 rate = burst / rep_rate -- reciprocal
160 }
161 end
162 end
163
164 return nil
165 end
166
167 return exports