diff options
-rw-r--r-- | lualib/lua_util.lua | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 34bafcc4b..8d7f2bd69 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -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 |