]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Lua_util: Add maybe_obfuscate_subject utility
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 12 Feb 2019 16:12:13 +0000 (16:12 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 12 Feb 2019 16:12:13 +0000 (16:12 +0000)
lualib/lua_util.lua

index 34bafcc4ba323bb91e4ba4de85b5b7dcf81394da..8d7f2bd69392273513561a184c0ccc73e746dc77 100644 (file)
@@ -876,5 +876,35 @@ exports.get_task_verdict = function(task)
   return 'uncertain'
 end
 
+---[[[
+-- @function lua_util.maybe_obfuscate_subject(subject, settings)
+-- Obfuscate subject if enabled in settings. Also checks utf8 validity.
+-- Supported settings:
+-- * subject_privacy = false - subject privacy is off
+-- * subject_privacy_alg = 'blake2' - default hash-algorithm to obfuscate subject
+-- * subject_privacy_prefix = 'obf' - prefix to show it's obfuscated
+-- * subject_privacy_length = 16 - cut the length of the hash
+-- @return obfuscated or validated subject
+--]]
+
+exports.maybe_obfuscate_subject = function(subject, settings)
+  local hash = require 'rspamd_cryptobox_hash'
+  if subject and not rspamd_util.is_valid_utf8(subject) then
+    subject = '???'
+  elseif settings.subject_privacy then
+    local hash_alg = settings.subject_privacy_alg or 'blake2'
+    local subject_hash = hash.create_specific(hash_alg, subject)
+
+    if settings.subject_privacy_length then
+      subject = (settings.subject_privacy_prefix or 'obf') .. ':' ..
+          subject_hash:hex():sub(1, settings.subject_privacy_length)
+    else
+      subject = (settings.subject_privacy_prefix or '') .. ':' ..
+          subject_hash:hex()
+    end
+  end
+
+  return subject
+end
 
 return exports