]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Fix broken ip_within in mapstats: parse CIDR and use apply_mask return value
authorVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 10 Feb 2026 20:47:10 +0000 (20:47 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 10 Feb 2026 20:47:10 +0000 (20:47 +0000)
rspamd_ip.from_string rejects '/' in CIDR notation, so strip the mask
before parsing. Also apply_mask returns a new IP object rather than
modifying in place, so capture the return values.

lualib/rspamadm/mapstats.lua

index 983809ab99a58dbbc49de512a096cdaa1807cd9f..ee3a6100b9f5ee26ea50bb1aba748a1a9a51d0db 100644 (file)
@@ -186,21 +186,26 @@ local function get_map(symbol_cfg, map_file)
 end
 
 local function ip_within(ip_obj, cidr_str)
-  local cidr_ip = rspamd_ip.from_string(cidr_str)
-  if not cidr_ip or not cidr_ip:is_valid() then
-    return false
-  end
-
-  -- Extract mask from CIDR notation
-  local _, mask_str = cidr_str:match('^(.+)/(%d+)$')
-  if mask_str then
+  -- Extract mask from CIDR notation before parsing
+  local ip_part, mask_str = cidr_str:match('^(.+)/(%d+)$')
+  if ip_part then
+    local cidr_ip = rspamd_ip.from_string(ip_part)
+    if not cidr_ip or not cidr_ip:is_valid() then
+      return false
+    end
     local mask = tonumber(mask_str)
-    local ip_masked = ip_obj:copy()
-    local cidr_masked = cidr_ip:copy()
-    ip_masked:apply_mask(mask)
-    cidr_masked:apply_mask(mask)
+    -- apply_mask returns a new IP object with the mask applied
+    local ip_masked = ip_obj:apply_mask(mask)
+    local cidr_masked = cidr_ip:apply_mask(mask)
+    if not ip_masked or not cidr_masked then
+      return false
+    end
     return tostring(ip_masked) == tostring(cidr_masked)
   else
+    local cidr_ip = rspamd_ip.from_string(cidr_str)
+    if not cidr_ip or not cidr_ip:is_valid() then
+      return false
+    end
     return tostring(ip_obj) == tostring(cidr_ip)
   end
 end