From 978f87970bab21475efb4e8922d85af0b6446172 Mon Sep 17 00:00:00 2001 From: Anton Yuzhaninov Date: Mon, 1 Jul 2019 14:39:23 +0100 Subject: [PATCH] [Minor] maybe_obfuscate_string changes 1. Return empty string as is (to save space). 2. Don't add ':' if prefix is empty. --- lualib/lua_util.lua | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index b5fbf78f0f..d1d43564d0 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -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: -- * _privacy = false - subject privacy is off -- * _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 -- 2.47.3