commit 231ab5e: [Minor] Add starting mark to patterns with offset 0

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Sep 24 08:07:07 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-09-24 09:06:12 +0100
URL: https://github.com/rspamd/rspamd/commit/231ab5e95dbe16bfcc9696b22536ab70d995c418 (HEAD -> master)

[Minor] Add starting mark to patterns with offset 0

---
 lualib/lua_magic/init.lua     | 19 +++++++++++++++++--
 lualib/lua_magic/patterns.lua | 14 +++++++-------
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/lualib/lua_magic/init.lua b/lualib/lua_magic/init.lua
index 0784b4a77..2fb848707 100644
--- a/lualib/lua_magic/init.lua
+++ b/lualib/lua_magic/init.lua
@@ -62,8 +62,14 @@ local function process_patterns(log_obj)
         short_patterns[#short_patterns + 1] = {
           str, match, pattern
         }
-        lua_util.debugm(N, log_obj, 'add short pattern %s for ext %s',
-            str, pattern.ext)
+        if str:sub(1, 1) == '^' then
+          lua_util.debugm(N, log_obj, 'add head pattern %s for ext %s',
+              str, pattern.ext)
+        else
+          lua_util.debugm(N, log_obj, 'add short pattern %s for ext %s',
+              str, pattern.ext)
+        end
+
 
         if max_short_offset < match.position then
           max_short_offset = match.position
@@ -94,6 +100,12 @@ local function process_patterns(log_obj)
         if match.string then
           if match.relative_position and not match.position then
             match.position = match.relative_position + #match.string
+
+            if match.relative_position == 0 then
+              if match.string:sub(1, 1) ~= '^' then
+                match.string = '^' .. match.string
+              end
+            end
           end
           add_processed(match.string, match, pattern)
         elseif match.hex then
@@ -107,6 +119,9 @@ local function process_patterns(log_obj)
           if match.relative_position and not match.position then
             match.position = match.relative_position + #match.hex / 2
           end
+          if match.relative_position == 0 then
+            table.insert(hex_table, 1, '^')
+          end
           add_processed(table.concat(hex_table), match, pattern)
         end
       end
diff --git a/lualib/lua_magic/patterns.lua b/lualib/lua_magic/patterns.lua
index 31267de31..d9604b714 100644
--- a/lualib/lua_magic/patterns.lua
+++ b/lualib/lua_magic/patterns.lua
@@ -26,17 +26,17 @@ local patterns = {
     -- These are alternatives
     matches = {
       {
-        string = [[%PDF-\d]],
+        string = [[^%PDF-\d]],
         position = 6, -- must be end of the match, as that's how hyperscan works (or use relative_position)
         weight = 60,
       },
       {
-        string = [[\012%PDF-\d]],
+        string = [[^\012%PDF-\d]],
         position = 7,
         weight = 60,
       },
       {
-        string = [[%FDF-\d]],
+        string = [[^%FDF-\d]],
         position = 6,
         weight = 60,
       },
@@ -55,7 +55,7 @@ local patterns = {
   rtf = {
     matches = {
       {
-        string = [[{\\rt]],
+        string = [[^{\\rt]],
         position = 4,
         weight = 60,
       }
@@ -181,7 +181,7 @@ local patterns = {
   bz2 = {
     matches = {
       {
-        string = "BZ[h0]",
+        string = "^BZ[h0]",
         position = 3,
         weight = 60,
       },
@@ -215,7 +215,7 @@ local patterns = {
   zst = {
     matches = {
       {
-        string = [[[\x{22}-\x{40}]\x{B5}\x{2F}\x{FD}]],
+        string = [[^[\x{22}-\x{40}]\x{B5}\x{2F}\x{FD}]],
         position = 4,
         weight = 60,
       },
@@ -374,7 +374,7 @@ local patterns = {
   dwg = {
     matches = {
       {
-        string = 'AC10[12][2-9]',
+        string = '^AC10[12][2-9]',
         position = 6,
         weight = 60,
       }


More information about the Commits mailing list