[pmg-devel] [PATCH pmg-api v2 1/2] fix #4811: rule db: test regex validity on save

Stoiko Ivanov s.ivanov at proxmox.com
Fri Dec 15 17:44:19 CET 2023


Thanks for the patch, this definitely improves UX!

one tiny nit in-line (I'll gladly change that upon applying directly, but
just don't want to miss anything):

On Fri, Jun 30, 2023 at 10:27:47AM +0200, Dominik Csapak wrote:
> and warn only when it's an invalid regex on execution, because users may
> have previously had such rules. Otherwise, pmg-smtp-filter will restart
> every time it encounters such a rule.
> 
> do so for every rule type that uses a regex to match
> 
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
> changes from v1:
> ..snip..
> diff --git a/src/PMG/RuleDB/MatchArchiveFilename.pm b/src/PMG/RuleDB/MatchArchiveFilename.pm
> index 2ef3543..5b1cb6d 100644
> --- a/src/PMG/RuleDB/MatchArchiveFilename.pm
> +++ b/src/PMG/RuleDB/MatchArchiveFilename.pm
> @@ -25,6 +25,13 @@ sub parse_entity {
>  
>      my $res;
>  
> +    # test regex for validity
> +    eval { "" =~ m|^$self->{fname}$|i; };
> +    if (my $err = $@) {
> +	warn "invalid regex: $err\n";
> +	return $res;
> +    }
> +
>      if (my $id = $entity->head->mime_attr('x-proxmox-tmp-aid')) {
>  	chomp $id;
>  
> ..snip..
> diff --git a/src/PMG/RuleDB/WhoRegex.pm b/src/PMG/RuleDB/WhoRegex.pm
> index 5c13604..1db6418 100644
> --- a/src/PMG/RuleDB/WhoRegex.pm
> +++ b/src/PMG/RuleDB/WhoRegex.pm
> @@ -60,6 +60,11 @@ sub save {
>      defined($self->{address}) || die "undefined address: ERROR";
>  
>      my $adr = $self->{address};
> +
> +    # test regex for validity
> +    eval { "" =~ /^$adr$/i; };
> +    die "invalid regex: $@\n" if $@;
> +
>      $adr =~ s/\\/\\\\/g;
>      $adr = encode('UTF-8', $adr);
>  
> @@ -100,7 +105,12 @@ sub who_match {
>  
>      my $t = $self->address;
>  
> -    return $addr =~ m/^$t$/i;
> +    my $res = '';
we use the result as condition in an if (pmg-smtp-filter apply_rules),
I'd rather leave it as undef instead of '' - for consistency with the
other objects (see diff-context above) - as I asked myself a bit too long
why this needs to be an '' here instead of undef..
or am I missing something?


> +    eval {
> +	$res = $addr =~ m/^$t$/i;
> +    };
> +    warn "invalid regex: $@\n" if $@;
> +    return $res;
>  }
>  
>  sub address { 
> -- 
> 2.30.2
> 
> 
> 
> _______________________________________________
> pmg-devel mailing list
> pmg-devel at lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
> 
> 




More information about the pmg-devel mailing list