diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-02-12 16:12:13 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-02-12 16:12:13 +0000 |
commit | 0044f31571c0e87152d8765861fc1472c8d279f7 (patch) | |
tree | e0e9fb3b40933f50cd037a7bb9d507c45b8eff11 /lualib | |
parent | 6aaac155fa5f8503d38616fff60320e411b971cc (diff) | |
download | rspamd-0044f31571c0e87152d8765861fc1472c8d279f7.tar.gz rspamd-0044f31571c0e87152d8765861fc1472c8d279f7.zip |
[Minor] Lua_util: Add maybe_obfuscate_subject utility
Diffstat (limited to 'lualib')
-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 |