commit 82d8d98: [Project] Fix urls injection for ics files

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Nov 25 13:28:12 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-11-25 13:26:17 +0000
URL: https://github.com/rspamd/rspamd/commit/82d8d983b1f1a2f53daea13325690651086f2c82

[Project] Fix urls injection for ics files

---
 lualib/lua_content/ical.lua | 35 +++++++++++++++++++++++++++--------
 lualib/lua_content/init.lua | 10 +++++-----
 2 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/lualib/lua_content/ical.lua b/lualib/lua_content/ical.lua
index c19723614..8052f04b9 100644
--- a/lualib/lua_content/ical.lua
+++ b/lualib/lua_content/ical.lua
@@ -17,18 +17,37 @@ limitations under the License.
 local l = require 'lpeg'
 local rspamd_text = require "rspamd_text"
 
-local wsp = l.P" "
-local crlf = l.P"\r"^-1 * l.P"\n"
-local eol = (crlf * #crlf) + (crlf - (crlf^-1 * wsp))
-local name = l.C((l.P(1) - (l.P":"))^1) / function(v) return (v:gsub("[\n\r]+%s","")) end
-local value = l.C((l.P(1) - eol)^0) / function(v) return (v:gsub("[\n\r]+%s","")) end
-local elt = name * ":" * wsp^0 * value * eol
+local ical_grammar
+
+local function gen_grammar()
+  if not ical_grammar then
+    local wsp = l.P" "
+    local crlf = l.P"\r"^-1 * l.P"\n"
+    local eol = (crlf * #crlf) + (crlf - (crlf^-1 * wsp))
+    local name = l.C((l.P(1) - (l.P":"))^1) / function(v) return (v:gsub("[\n\r]+%s","")) end
+    local value = l.C((l.P(1) - eol)^0) / function(v) return (v:gsub("[\n\r]+%s","")) end
+    ical_grammar = name * ":" * wsp^0 * value * eol
+  end
+
+  return ical_grammar
+end
 
 local exports = {}
 
-local function process_ical(input, _, _)
+local function process_ical(input, _, task)
   local control={n='\n', r='\r'}
-  local escaper = l.Ct((elt / function(_,b) return (b:gsub("\\(.)", control)) end)^1)
+  local rspamd_url = require "rspamd_url"
+  local escaper = l.Ct((gen_grammar() / function(_, value)
+    value = value:gsub("\\(.)", control)
+    local local_urls = rspamd_url.all(task:get_mempool(), value)
+
+    if local_urls and #local_urls > 0 then
+      for _,u in ipairs(local_urls) do
+        task:inject_url(u)
+      end
+    end
+    return value
+  end)^1)
 
   local values = escaper:match(input)
 
diff --git a/lualib/lua_content/init.lua b/lualib/lua_content/init.lua
index 994d613f4..06d509e00 100644
--- a/lualib/lua_content/init.lua
+++ b/lualib/lua_content/init.lua
@@ -51,7 +51,7 @@ local function init()
   end
 end
 
-exports.maybe_process_mime_part = function(part, log_obj)
+exports.maybe_process_mime_part = function(part, task)
   if not modules_by_mime_type then
     init()
   end
@@ -70,17 +70,17 @@ exports.maybe_process_mime_part = function(part, log_obj)
   end
 
   if pair then
-    lua_util.debugm(N, log_obj, "found known content of type %s: %s",
+    lua_util.debugm(N, task, "found known content of type %s: %s",
         mt, pair[1])
 
-    local data = pair[2].module.process(part:get_content(), part, log_obj)
+    local data = pair[2].module.process(part:get_content(), part, task)
 
     if data then
-      lua_util.debugm(N, log_obj, "extracted content from %s: %s type",
+      lua_util.debugm(N, task, "extracted content from %s: %s type",
           pair[1], type(data))
       part:set_specific(data)
     else
-      lua_util.debugm(N, log_obj, "failed to extract anything from %s",
+      lua_util.debugm(N, task, "failed to extract anything from %s",
           pair[1])
     end
   end


More information about the Commits mailing list