From 50a289b681c9f6c0bcf2fbd8920777333dd11b31 Mon Sep 17 00:00:00 2001
From: Giovanni Bechis <gbechis@apache.org>
Date: Wed, 3 Sep 2025 09:30:17 +0000
Subject: [PATCH] limit CNAME cache to 10 entries by default and make it
 configurable increase speed processing when there are a huge number of uris

git-svn-id: https://svn.apache.org/repos/asf/spamassassin/trunk@1928199 13f79535-47bb-0310-9956-ffa450edef68
---
 lib/Mail/SpamAssassin/Conf.pm         | 13 +++++++++++++
 lib/Mail/SpamAssassin/PerMsgStatus.pm |  3 ++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/lib/Mail/SpamAssassin/Conf.pm b/lib/Mail/SpamAssassin/Conf.pm
index 6661240763..20734421fb 100644
--- a/lib/Mail/SpamAssassin/Conf.pm
+++ b/lib/Mail/SpamAssassin/Conf.pm
@@ -2108,6 +2108,19 @@ dns_block_rule query blockage will last this many seconds.
     type => $CONF_TYPE_NUMERIC,
   });
 
+=item dns_max_cname_cache n   (default: 10 entries)
+
+When we find CNAMEs of uris, limit the cache to 10 entries
+and stop processing CNAMEs to limit resource usage.
+
+=cut
+
+  push (@cmds, {
+    setting => 'dns_max_cname_cache',
+    default => 10,
+    type => $CONF_TYPE_NUMERIC,
+  });
+
 =back
 
 =head2 LEARNING OPTIONS
diff --git a/lib/Mail/SpamAssassin/PerMsgStatus.pm b/lib/Mail/SpamAssassin/PerMsgStatus.pm
index 16b2dda527..f39b1b6e3f 100644
--- a/lib/Mail/SpamAssassin/PerMsgStatus.pm
+++ b/lib/Mail/SpamAssassin/PerMsgStatus.pm
@@ -2909,7 +2909,8 @@ sub add_uri_detail_list {
       $hosts{$host} = $domain;
     }
 
-    if($self->is_dns_available()) {
+    my $dns_max_cname_cache = $self->{main}->{conf}->{dns_max_cname_cache};
+    if($self->is_dns_available() and (not defined $self->{dns_cname_cache} or scalar %{$self->{dns_cname_cache}} < $dns_max_cname_cache)) {
       # XXX we cannot call bgsend_and_start_lookup,
       # otherwise get_uri_detail_list() might not
       # return domains extracted from CNAME dns queries
