]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] maybe_obfuscate_string changes
authorAnton Yuzhaninov <citrin+git@citrin.ru>
Mon, 1 Jul 2019 13:39:23 +0000 (14:39 +0100)
committerAnton Yuzhaninov <citrin+git@citrin.ru>
Mon, 1 Jul 2019 13:39:23 +0000 (14:39 +0100)
1. Return empty string as is (to save space).
2. Don't add ':' if prefix is empty.

lualib/lua_util.lua

index b5fbf78f0f93a82b2e0b7f84506a9268b64e28a4..d1d43564d0640a50b776010c19e77e6ef33a6aa1 100644 (file)
@@ -937,7 +937,8 @@ 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
@@ -948,20 +949,24 @@ end
 
 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 ''
 
+    local strip_len = settings[prefix .. '_privacy_length']
     if strip_len then
-      subject = privacy_prefix .. ':' ..
-          subject_hash:hex():sub(1, strip_len)
+      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