]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Add tool to rspamadm
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 29 Nov 2024 13:40:11 +0000 (13:40 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 29 Nov 2024 13:40:11 +0000 (13:40 +0000)
lualib/lua_mime.lua
lualib/rspamadm/mime.lua

index 167939189a4ae6caa98b1b1fa78228c9e6450406..ce14a49f3e5f6a72c9a636a5ad668ba49db92a28 100644 (file)
@@ -951,21 +951,26 @@ exports.anonymize_message = function(task, settings)
 
   -- Process headers
   local modified_headers = {}
-  for name, processor in pairs(header_processors) do
-    local hdrs = task:get_header_full(name, true)
-    if hdrs then
-      for _, hdr in ipairs(hdrs) do
-        local new_value = processor(hdr)
-        if new_value then
-          table.insert(modified_headers, {
-            name = name,
-            value = new_value
-          })
-        end
+  local function process_hdr(name, hdr)
+    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 = hdr.value,
+      })
     end
   end
 
+  task:headers_foreach(process_hdr, { full = true })
+
   -- Create new text content
   local text_content = {}
   local urls = {}
@@ -974,12 +979,14 @@ exports.anonymize_message = function(task, settings)
   -- Extract text content, URLs and emails
   local text_parts = task:get_text_parts()
   for _, part in ipairs(text_parts) do
-    if part:is_html() then
-      local words = part:get_words('norm')
-      if words then
-        text_content = words
+    if not part:get_mimepart():is_attachment() then
+      if part:is_html() then
+        local words = part:get_words('norm')
+        if words then
+          text_content = words
+        end
+        break -- Use only first HTML part
       end
-      break -- Use only first HTML part
     end
   end
 
@@ -1027,9 +1034,7 @@ exports.anonymize_message = function(task, settings)
   local new_text = table.concat(text_content, ' ')
 
   -- Create new message structure
-  local boundaries = {}
   local cur_boundary = '--XXX'
-  boundaries[1] = cur_boundary
 
   -- Add headers
   out[#out + 1] = {
index 7750c5a7868ffa32656180463f14d699e12c3941..617f57a77a4239bca0dd0e02363725af667b221f 100644 (file)
@@ -179,6 +179,13 @@ strip:option "--max-text-size"
      :convert(tonumber)
      :default(math.huge)
 
+local anonymize = parser:command "anonymize"
+                        :description "Try to remove sensitive information from a message"
+anonymize:argument "file"
+         :description "File to process"
+         :argname "<file>"
+         :args "+"
+
 local sign = parser:command "sign"
                    :description "Performs DKIM signing"
 sign:argument "file"
@@ -968,6 +975,41 @@ local function strip_handler(opts)
   end
 end
 
+local function anonymize_handler(opts)
+  load_config(opts)
+  rspamd_url.init(rspamd_config:get_tld_path())
+
+  for _, fname in ipairs(opts.file) do
+    local task = load_task(opts, fname)
+    local newline_s = newline(task)
+
+    local rewrite = lua_mime.anonymize_message(task, opts) or {}
+
+    for _, o in ipairs(rewrite.out) do
+      if type(o) == 'string' then
+        io.write(o)
+        io.write(newline_s)
+      elseif type(o) == 'table' then
+        io.flush()
+        if type(o[1]) == 'string' then
+          io.write(o[1])
+        else
+          o[1]:save_in_file(1)
+        end
+
+        if o[2] then
+          io.write(newline_s)
+        end
+      else
+        o:save_in_file(1)
+        io.write(newline_s)
+      end
+    end
+
+    task:destroy() -- No automatic dtor
+  end
+end
+
 -- Strips directories and .extensions (if present) from a filepath
 local function filename_only(filepath)
   local filename = filepath:match(".*%/([^%.]+)")
@@ -1076,6 +1118,8 @@ local function handler(args)
     sign_handler(opts)
   elseif command == 'dump' then
     dump_handler(opts)
+  elseif command == 'anonymize' then
+    anonymize_handler(opts)
   else
     parser:error('command %s is not implemented', command)
   end