aboutsummaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-07-01 17:45:15 +0100
committerGitHub <noreply@github.com>2019-07-01 17:45:15 +0100
commit51235f510cd7d55448f50501c3a5549b8aaedc06 (patch)
treeac84f624363b2dd8f04216ef775faabb3e610b3e /lualib
parent9ba5158ae4d8226f86a646a4c9bbf7dbb0b7d298 (diff)
parentbab9c14b048bee2b33b8f2aa9d5ce4215f0413f7 (diff)
downloadrspamd-51235f510cd7d55448f50501c3a5549b8aaedc06.tar.gz
rspamd-51235f510cd7d55448f50501c3a5549b8aaedc06.zip
Merge pull request #2956 from citrin/obfuscate_string
More usable maybe_obfuscate_string
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_util.lua25
1 files changed, 15 insertions, 10 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index b5fbf78f0..e8dc57429 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -937,31 +937,36 @@ end
---[[[
-- @function lua_util.maybe_obfuscate_string(subject, settings, prefix)
--- Obfuscate string if enabled in settings. Also checks utf8 validity.
+-- Obfuscate string if enabled in settings. Also checks utf8 validity - if
+-- string is not valid utf8 then '???' is returned. Empty string returned as is.
-- Supported settings:
-- * <prefix>_privacy = false - subject privacy is off
-- * <prefix>_privacy_alg = 'blake2' - default hash-algorithm to obfuscate subject
-- * <prefix>_privacy_prefix = 'obf' - prefix to show it's obfuscated
--- * <prefix>_privacy_length = 16 - cut the length of the hash
+-- * <prefix>_privacy_length = 16 - cut the length of the hash; if 0 or fasle full hash is returned
-- @return obfuscated or validated subject
--]]
exports.maybe_obfuscate_string = function(subject, settings, prefix)
local hash = require 'rspamd_cryptobox_hash'
- if subject and not rspamd_util.is_valid_utf8(subject) then
+ if not subject or subject == '' then
+ return subject
+ elseif not rspamd_util.is_valid_utf8(subject) then
subject = '???'
elseif settings[prefix .. '_privacy'] then
local hash_alg = settings[prefix .. '_privacy_alg'] or 'blake2'
local subject_hash = hash.create_specific(hash_alg, subject)
- local strip_len = settings[prefix .. '_privacy_length']
- local privacy_prefix = settings[prefix .. '_privacy_prefix'] or ''
- if strip_len then
- subject = privacy_prefix .. ':' ..
- subject_hash:hex():sub(1, strip_len)
+ local strip_len = settings[prefix .. '_privacy_length']
+ if strip_len and strip_len > 0 then
+ subject = subject_hash:hex():sub(1, strip_len)
else
- subject = privacy_prefix .. ':' ..
- subject_hash:hex()
+ subject = subject_hash:hex()
+ end
+
+ local privacy_prefix = settings[prefix .. '_privacy_prefix']
+ if privacy_prefix and #privacy_prefix > 0 then
+ subject = privacy_prefix .. ':' .. subject
end
end