You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by he...@apache.org on 2022/05/17 05:43:58 UTC

svn commit: r1900977 - in /spamassassin/trunk: lib/Mail/SpamAssassin/Plugin/HashBL.pm t/hashbl.t

Author: hege
Date: Tue May 17 05:43:57 2022
New Revision: 1900977

URL: http://svn.apache.org/viewvc?rev=1900977&view=rev
Log:
Add sha256 option to HashBL (Bug 7993)

Modified:
    spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/HashBL.pm
    spamassassin/trunk/t/hashbl.t

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/HashBL.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/HashBL.pm?rev=1900977&r1=1900976&r2=1900977&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/HashBL.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/HashBL.pm Tue May 17 05:43:57 2022
@@ -57,6 +57,7 @@ OPTS refers to multiple generic options:
   raw      do not hash data, query as is
   md5      hash query with MD5
   sha1     hash query with SHA1
+  sha256   hash query with Base32 encoded SHA256
   case     keep case before hashing, default is to lowercase
   max=x	   maximum number of queries
   shuffle  if max exceeded, random shuffle queries before truncating to limit
@@ -168,11 +169,12 @@ use re 'taint';
 my $VERSION = 0.101;
 
 use Digest::MD5 qw(md5_hex);
-use Digest::SHA qw(sha1_hex);
+use Digest::SHA qw(sha1_hex sha256);
 
 use Mail::SpamAssassin::Plugin;
 use Mail::SpamAssassin::Constants qw(:ip);
-use Mail::SpamAssassin::Util qw(compile_regexp is_fqdn_valid reverse_ip_address);
+use Mail::SpamAssassin::Util qw(compile_regexp is_fqdn_valid reverse_ip_address
+                                base32_encode);
 
 our @ISA = qw(Mail::SpamAssassin::Plugin);
 
@@ -804,7 +806,10 @@ sub _check_hashbl_tag {
 sub _hash {
   my ($self, $opts, $value) = @_;
 
-  if ($opts->{sha1}) {
+  if ($opts->{sha256}) {
+    utf8::encode($value) if utf8::is_utf8($value); # sha256 expects bytes
+    return base32_encode(sha256($value));
+  } elsif ($opts->{sha1}) {
     utf8::encode($value) if utf8::is_utf8($value); # sha1_hex expects bytes
     return sha1_hex($value);
   } elsif ($opts->{md5}) {

Modified: spamassassin/trunk/t/hashbl.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/hashbl.t?rev=1900977&r1=1900976&r2=1900977&view=diff
==============================================================================
--- spamassassin/trunk/t/hashbl.t (original)
+++ spamassassin/trunk/t/hashbl.t Tue May 17 05:43:57 2022
@@ -9,7 +9,7 @@ plan skip_all => "Can't use Net::DNS Saf
 
 # run many times to catch some random natured failures
 my $iterations = 5;
-plan tests => 10 * $iterations;
+plan tests => 11 * $iterations;
 
 # ---------------------------------------------------------------------------
 
@@ -23,7 +23,9 @@ plan tests => 10 * $iterations;
  q{ 1.0 META_HASHBL_BTC } => '',
  q{ 1.0 META_HASHBL_URI } => '',
 );
-%anti_patterns = ();
+%anti_patterns = (
+ q{ 1.0 X_HASHBL_SHA256 } => '',
+);
 
 # Check from debug output log that nothing else than these were queried
 @valid_queries = qw(
@@ -33,6 +35,7 @@ bc9f1b35acd338b92b0659cc2111e6b661a8b2bc
 96b802967118135ef048c2bc860e7b0deb7d2333.hashbltest1.spamassassin.org
 170d83ef2dc9c2de0e65ce4461a3a375.hashbltest2.spamassassin.org
 cc205dd956d568ff8524d7fc42868500e4d7d162.hashbltest3.spamassassin.org
+jykf2a5v6asavfel3stymlmieh4e66jeroxuw52mc5xhdylnyb7a.hashbltest3.spamassassin.org
 6a42acf4133289d595e3875a9d677f810e80b7b4.hashbltest4.spamassassin.org
 5c6205960a65b1f9078f0e12dcac970aab0015eb.hashbltest4.spamassassin.org
 1234567890.hashbltest5.spamassassin.org
@@ -77,6 +80,10 @@ tstlocalrules(q{
   body     X_HASHBL_BTC eval:check_hashbl_bodyre('hashbltest3.spamassassin.org', 'sha1/max=10/shuffle', '\b([13][a-km-zA-HJ-NP-Z1-9]{25,34})\b')
   tflags   X_HASHBL_BTC net
 
+  # Not supposed to hit, @valid_queries just checks that sha256 is calculated correctly
+  body     X_HASHBL_SHA256 eval:check_hashbl_bodyre('hashbltest3.spamassassin.org', 'sha256/max=10/shuffle', '\b([13][a-km-zA-HJ-NP-Z1-9]{25,34})\b')
+  tflags   X_HASHBL_SHA256 net
+
   header   X_HASHBL_URI eval:check_hashbl_uris('hashbltest4.spamassassin.org', 'sha1', '127.0.0.2')
   tflags   X_HASHBL_URI net