From 4ae3fe9af874de5e040762f3070eb74d5b1e7db0 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 6 May 2018 16:54:01 +0100 Subject: [PATCH] [Fix] Fix base64 folding in Lua API Issue: #2225 --- src/lua/lua_util.c | 29 +++++++++++++++++++++++++++-- src/plugins/lua/dmarc.lua | 5 ++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 50fa6d397..6c200d1be 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -50,7 +50,7 @@ LUA_FUNCTION_DEF (util, load_rspamd_config); */ LUA_FUNCTION_DEF (util, config_from_ucl); /*** - * @function util.encode_base64(input[, str_len]) + * @function util.encode_base64(input[, str_len, [newlines_type]]) * Encodes data in base64 breaking lines if needed * @param {text or string} input input data * @param {number} str_len optional size of lines or 0 if split is not needed @@ -756,6 +756,7 @@ lua_util_encode_base64 (lua_State *L) gchar *out; gsize inlen, outlen; guint str_lim = 0; + gboolean fold = FALSE; if (lua_type (L, 1) == LUA_TSTRING) { s = luaL_checklstring (L, 1, &inlen); @@ -771,13 +772,37 @@ lua_util_encode_base64 (lua_State *L) if (lua_gettop (L) > 1) { str_lim = luaL_checknumber (L, 2); + + fold = !!(str_lim > 0); } if (s == NULL) { lua_pushnil (L); } else { - out = rspamd_encode_base64 (s, inlen, str_lim, &outlen); + + if (fold) { + out = rspamd_encode_base64 (s, inlen, str_lim, &outlen); + } + else { + enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; + + if (lua_type (L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring (L, 3); + + if (g_ascii_strcasecmp (how_str, "cr") == 0) { + how = RSPAMD_TASK_NEWLINES_CR; + } + else if (g_ascii_strcasecmp (how_str, "lf") == 0) { + how = RSPAMD_TASK_NEWLINES_LF; + } + else if (g_ascii_strcasecmp (how_str, "crlf") != 0) { + return luaL_error (L, "invalid newline style: %s", how_str); + } + } + + out = rspamd_encode_base64_fold (s, inlen, str_lim, &outlen, how); + } if (out != NULL) { t = lua_newuserdata (L, sizeof (*t)); diff --git a/src/plugins/lua/dmarc.lua b/src/plugins/lua/dmarc.lua index 652526d69..58e48bd01 100644 --- a/src/plugins/lua/dmarc.lua +++ b/src/plugins/lua/dmarc.lua @@ -744,7 +744,10 @@ if opts['reporting'] == true then for k in pairs(reporting_addr) do table.insert(tmp_addr, k) end - local encoded = rspamd_util.encode_base64(table.concat({xmlf('header'), xmlf('entries'), xmlf('footer')}), 78) + local encoded = rspamd_util.encode_base64(table.concat( + {xmlf('header'), + xmlf('entries'), + xmlf('footer')}), 78) local function mail_cb(err, data, conn) local function no_error(merr, mdata, wantcode) wantcode = wantcode or '2' -- 2.39.5