commit d43b852: [Fix] Fix regexp selector and add flattening

Vsevolod Stakhov vsevolod at highsecure.ru
Thu May 14 12:21:29 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-05-14 13:16:06 +0100
URL: https://github.com/rspamd/rspamd/commit/d43b852a028b6c77f1641361bff3d96f0d0ef4a8 (HEAD -> master)

[Fix] Fix regexp selector and add flattening

---
 lualib/lua_selectors/transforms.lua | 18 +++++++++++++-----
 test/lua/unit/selectors.lua         |  4 ++++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/lualib/lua_selectors/transforms.lua b/lualib/lua_selectors/transforms.lua
index bbf5f510a..6f5ed7027 100644
--- a/lualib/lua_selectors/transforms.lua
+++ b/lualib/lua_selectors/transforms.lua
@@ -202,16 +202,24 @@ the second argument is optional hash type (`blake2`, `sha256`, `sha1`, `sha512`,
       local res = re:search(inp, false, true)
 
       if res then
-        if #res == 1 then
-          return res[1],'string'
+        -- Map all results in a single list
+        local flattened_table = {}
+        local function flatten_table(tbl)
+          for _, v in ipairs(tbl) do
+            if type(v) == 'table' then
+              flatten_table(v)
+            else
+              table.insert(flattened_table, v)
+            end
+          end
         end
-
-        return res,'string_list'
+        flatten_table(res)
+        return flattened_table,'string_list'
       end
 
       return nil
     end,
-    ['description'] = 'Regexp matching',
+    ['description'] = 'Regexp matching, returns all matches flattened in a single list',
     ['args_schema'] = {ts.string}
   },
   -- Returns a value if it exists in some map (or acts like a `filter` function)
diff --git a/test/lua/unit/selectors.lua b/test/lua/unit/selectors.lua
index dea80fdbf..cca1d1e5a 100644
--- a/test/lua/unit/selectors.lua
+++ b/test/lua/unit/selectors.lua
@@ -350,6 +350,10 @@ context("Selectors test", function()
       selector = "header('Subject'):gsub('a', 'b')",
       expect = {"Second, lower-cbsed hebder subject"}
     },
+    ["header regexp first"] = {
+      selector = "header('Subject').regexp('.*').first",
+      expect = {"Second, lower-cased header subject"}
+    },
   }
 
   for case_name, case in pairs(cases) do


More information about the Commits mailing list