From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 657CC98467
 for <pmg-devel@lists.proxmox.com>; Fri, 14 Apr 2023 11:15:31 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 40B7D23B43
 for <pmg-devel@lists.proxmox.com>; Fri, 14 Apr 2023 11:15:01 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pmg-devel@lists.proxmox.com>; Fri, 14 Apr 2023 11:15:00 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1BC3A41DFA
 for <pmg-devel@lists.proxmox.com>; Fri, 14 Apr 2023 11:14:59 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pmg-devel@lists.proxmox.com
Date: Fri, 14 Apr 2023 11:14:58 +0200
Message-Id: <20230414091458.1517612-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.010 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pmg-devel] [PATCH pmg-api] ruledb: match field: improve handling
 of invalid regular expressions
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 14 Apr 2023 09:15:31 -0000

by not saving them in the first place if they die during execution.
We test this by using them once on an empty string.

Since users may have saved already invalid ones, only warn if we encounter
such a regex in 'parse_entity' during execution instead of die'ing. Otherwise
pmg-smtp-filter will exit and restart, possibly leading to wrongly denying
mails (and possibly sending out NDRs) before spam checking was done.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/PMG/RuleDB/MatchField.pm | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/PMG/RuleDB/MatchField.pm b/src/PMG/RuleDB/MatchField.pm
index 2b56058..177a283 100644
--- a/src/PMG/RuleDB/MatchField.pm
+++ b/src/PMG/RuleDB/MatchField.pm
@@ -69,7 +69,13 @@ sub save {
 
     defined($self->{ogroup}) || die "undefined ogroup: ERROR";
 
-    my $new_value = "$self->{field}:$self->{field_value}";
+    my $regex = $self->{field_value};
+
+    # test regex for validity
+    eval { "" =~ /$regex/i; };
+    die "invalid regex: $@\n" if $@;
+
+    my $new_value = "$self->{field}:$regex";
     $new_value =~ s/\\/\\\\/g;
     $new_value = encode('UTF-8', $new_value);
 
@@ -111,9 +117,12 @@ sub parse_entity {
 	    my $decvalue = PMG::Utils::decode_rfc1522($value);
 	    $decvalue = PMG::Utils::try_decode_utf8($decvalue);
 
-	    if ($decvalue =~ m|$self->{field_value}|i) {
-		push @$res, $id;
-	    }
+	    eval {
+		if ($decvalue =~ m|$self->{field_value}|i) {
+		    push @$res, $id;
+		}
+	    };
+	    warn "invalid regex: $@\n" if $@;
 	}
     }
 
-- 
2.30.2