diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-12-07 16:48:55 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-12-07 16:48:55 +0000 |
commit | 14b7742ea4de1bc258bf4031da637d9caf27c23f (patch) | |
tree | 6357f7bfabafa188c9b0fedabb58a0352b3b77bb | |
parent | 53f73e02a4daf884a31a69e3f55ad1328c3bd1d3 (diff) | |
download | rspamd-14b7742ea4de1bc258bf4031da637d9caf27c23f.tar.gz rspamd-14b7742ea4de1bc258bf4031da637d9caf27c23f.zip |
[Feature] Add include/exclude logic for headers
-rw-r--r-- | lualib/lua_mime.lua | 66 | ||||
-rw-r--r-- | lualib/rspamadm/mime.lua | 8 |
2 files changed, 64 insertions, 10 deletions
diff --git a/lualib/lua_mime.lua b/lualib/lua_mime.lua index bb65dc9ba..de5c6db33 100644 --- a/lualib/lua_mime.lua +++ b/lualib/lua_mime.lua @@ -1012,22 +1012,68 @@ exports.anonymize_message = function(task, settings) end -- Process headers + local all_include = true + local all_exclude = false + + -- Convert strings list to a list of globs where possible + local function process_exceptions_list(list) + if list and #list > 0 then + for i, hdr in ipairs(list) do + local gl = rspamd_re.import_glob(hdr, 'i') + if gl then + list[i] = gl + end + end + return true + end + end + + local function maybe_match_header(hdr, list) + if not list then + return false + end + for _, expr in ipairs(list) do + if type(expr) == 'userdata' then + if expr:match(hdr) then + return true + end + else + if expr:lower() == hdr:lower() then + return true + end + end + end + return false + end + + if process_exceptions_list(settings.include_header) then + all_include = false + all_exclude = true + end + if process_exceptions_list(settings.exclude_header) then + all_exclude = true + end + local modified_headers = {} local function process_hdr(name, hdr) - local processor = header_processors[name:lower()] - if processor then - local new_value = processor(hdr) - if new_value then + local include_hdr = (all_include and not maybe_match_header(name, settings.exclude_header)) or + (all_exclude and maybe_match_header(name, settings.include_header)) + if include_hdr then + local processor = header_processors[name:lower()] + if processor then + local new_value = processor(hdr) + if new_value then + table.insert(modified_headers, { + name = name, + value = new_value + }) + end + else table.insert(modified_headers, { name = name, - value = new_value + value = hdr.value }) end - else - table.insert(modified_headers, { - name = name, - value = hdr.value, - }) end end diff --git a/lualib/rspamadm/mime.lua b/lualib/rspamadm/mime.lua index 617f57a77..f8c7fc4f7 100644 --- a/lualib/rspamadm/mime.lua +++ b/lualib/rspamadm/mime.lua @@ -185,6 +185,14 @@ anonymize:argument "file" :description "File to process" :argname "<file>" :args "+" +anonymize:option "--exclude-header -X" + :description "Exclude specific headers from anonymization" + :argname "<header>" + :count "*" +anonymize:option "--include-header -I" + :description "Include specific headers from anonymization" + :argname "<header>" + :count "*" local sign = parser:command "sign" :description "Performs DKIM signing" |