diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-02-26 09:28:50 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-02-26 09:28:50 +0000 |
commit | e21acf536049c8543d6faeea22a401da3e9519bd (patch) | |
tree | 5353c3a07711003c039e9a89850cfa2848a15832 | |
parent | 2e9c160032c1a3be1b4ef1bcce58d7d1c860bf52 (diff) | |
parent | 33c00d45023cdfd0cc1f53f149a1fe2e4c8ebc4c (diff) | |
download | rspamd-e21acf536049c8543d6faeea22a401da3e9519bd.tar.gz rspamd-e21acf536049c8543d6faeea22a401da3e9519bd.zip |
Merge branch 'vstakhov-another-lupa-fix' into rspamd-3.8
-rw-r--r-- | lualib/lua_util.lua | 50 | ||||
-rw-r--r-- | lualib/rspamadm/dmarc_report.lua | 22 |
2 files changed, 59 insertions, 13 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 3df12bdf6..a2b3862e3 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -199,43 +199,81 @@ local function enrich_template_with_globals(env) return newenv end --[[[ --- @function lua_util.jinja_template(text, env[, skip_global_env]) +-- @function lua_util.jinja_template(text, env[, skip_global_env][, is_orig][, custom_filters]) -- Replaces values in a text template according to jinja2 syntax -- @param {string} text text containing variables -- @param {table} replacements key/value pairs for replacements -- @param {boolean} skip_global_env don't export Rspamd superglobals -- @param {boolean} is_orig use the original lupa configuration with `{{` for variables +-- @param {table} custom_filters custom filters to use (or nil if not needed) -- @return {string} string containing replaced values -- @example -- lua_util.jinja_template("HELLO {=FOO=} {=BAR=}!", {['FOO'] = 'LUA', ['BAR'] = 'WORLD'}) -- "HELLO LUA WORLD!" --]] -exports.jinja_template = function(text, env, skip_global_env, is_orig) +exports.jinja_template = function(text, env, skip_global_env, is_orig, custom_filters) + local lupa_to_use = is_orig and lupa_orig or lupa if not skip_global_env then env = enrich_template_with_globals(env) end - return is_orig and lupa_orig.expand(text, env) or lupa.expand(text, env) + local orig_filters = {} + if type(custom_filters) == 'table' then + for k, v in pairs(custom_filters) do + orig_filters[k] = lupa_to_use.filters[k] + lupa_to_use.filters[k] = v + end + end + + local result = lupa_to_use.expand(text, env) + + -- Restore custom filters + if type(custom_filters) == 'table' then + for k, _ in pairs(custom_filters) do + lupa_to_use.filters[k] = orig_filters[k] + end + end + + return result end --[[[ --- @function lua_util.jinja_file(filename, env[, skip_global_env]) +-- @function lua_util.jinja_file(filename, env[, skip_global_env][, is_orig][, custom_filters]) -- Replaces values in a text template according to jinja2 syntax -- @param {string} filename name of file to expand -- @param {table} replacements key/value pairs for replacements -- @param {boolean} skip_global_env don't export Rspamd superglobals -- @param {boolean} is_orig use the original lupa configuration with `{{` for variables +-- @param {table} custom_filters custom filters to use (or nil if not needed) -- @return {string} string containing replaced values -- @example -- lua_util.jinja_template("HELLO {=FOO=} {=BAR=}!", {['FOO'] = 'LUA', ['BAR'] = 'WORLD'}) -- "HELLO LUA WORLD!" --]] -exports.jinja_template_file = function(filename, env, skip_global_env, is_orig) +exports.jinja_template_file = function(filename, env, skip_global_env, is_orig, custom_filters) + local lupa_to_use = is_orig and lupa_orig or lupa if not skip_global_env then env = enrich_template_with_globals(env) end - return is_orig and lupa_orig.expand_file(filename, env) or lupa.expand_file(filename, env) + local orig_filters = {} + if type(custom_filters) == 'table' then + for k, v in pairs(custom_filters) do + orig_filters[k] = lupa_to_use.filters[k] + lupa_to_use.filters[k] = v + end + end + + local result = lupa_to_use.expand_file(filename, env) + + -- Restore custom filters + if type(custom_filters) == 'table' then + for k, _ in pairs(custom_filters) do + lupa_to_use.filters[k] = orig_filters[k] + end + end + + return result end exports.remove_email_aliases = function(email_addr) diff --git a/lualib/rspamadm/dmarc_report.lua b/lualib/rspamadm/dmarc_report.lua index 42c801eed..a54e12d67 100644 --- a/lualib/rspamadm/dmarc_report.lua +++ b/lualib/rspamadm/dmarc_report.lua @@ -19,7 +19,6 @@ local lua_util = require "lua_util" local logger = require "rspamd_logger" local lua_redis = require "lua_redis" local dmarc_common = require "plugins/dmarc" -local lupa = require "lupa" local rspamd_mempool = require "rspamd_mempool" local rspamd_url = require "rspamd_url" local rspamd_text = require "rspamd_text" @@ -176,8 +175,6 @@ local function escape_xml(input) return '' end --- Enable xml escaping in lupa templates -lupa.filters.escape_xml = escape_xml -- Creates report XML header local function report_header(reporting_domain, report_start, report_end, domain_policy) @@ -211,7 +208,10 @@ local function report_header(reporting_domain, report_start, report_end, domain_ report_end = report_end, domain_policy = domain_policy, reporting_domain = reporting_domain, - }, true) + }, true, false, + { + escape_xml = escape_xml + }) end -- Generate xml entry for a preprocessed redis row @@ -248,7 +248,10 @@ local function entry_to_xml(data) </auth_results> </record> ]] - return lua_util.jinja_template(xml_template, { data = data }, true) + return lua_util.jinja_template(xml_template, { data = data }, true, + false, { + escape_xml = escape_xml + }) end -- Process a report entry stored in Redis splitting it to a lua table @@ -534,10 +537,15 @@ local function prepare_report(opts, start_time, end_time, rep_key) message_id = rspamd_util.random_hex(16) .. '@' .. report_settings.msgid_from, report_start = start_time, report_end = end_time - }, true) + }, true, + false, { + escape_xml = escape_xml + }) local rfooter = lua_util.jinja_template(report_footer, { uuid = uuid, - }, true) + }, true, false, { + escape_xml = escape_xml + }) local message = rspamd_text.fromtable { (rhead:gsub("\n", "\r\n")), rspamd_util.encode_base64(rspamd_util.gzip_compress(xml_to_compress), 73), |