1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Rules useful for running a mailing list mirror. We want to:
5 # * ensure Received: headers are really from the list mail server
6 # users expect. This is to prevent malicious users from
7 # injecting spam into mirrors without going through the expected
9 # * flag messages where the mailing list is Bcc:-ed since it is
10 # common for spam to have wrong or non-existent To:/Cc: headers.
12 package PublicInbox::SaPlugin::ListMirror;
15 use base qw(Mail::SpamAssassin::Plugin);
17 # constructor: register the eval rules
19 my ($class, $mail) = @_;
22 $class = ref($class) || $class;
23 my $self = $class->SUPER::new($mail);
25 $mail->{conf}->{list_mirror_check} = [];
26 $self->register_eval_rule('check_list_mirror_received');
27 $self->register_eval_rule('check_list_mirror_bcc');
28 $self->set_config($mail->{conf});
32 sub check_list_mirror_received {
33 my ($self, $pms) = @_;
34 my $recvd = $pms->get('Received') || '';
35 $recvd =~ s/\n.*\z//s;
37 foreach my $cfg (@{$pms->{conf}->{list_mirror_check}}) {
38 my ($hdr, $hval, $host_re, $addr_re) = @$cfg;
39 my $v = $pms->get($hdr) or next;
42 return 1 if $recvd !~ $host_re;
48 sub check_list_mirror_bcc {
49 my ($self, $pms) = @_;
50 my $tocc = $pms->get('ToCc');
52 foreach my $cfg (@{$pms->{conf}->{list_mirror_check}}) {
53 my ($hdr, $hval, $host_re, $addr_re) = @$cfg;
54 defined $addr_re or next;
55 my $v = $pms->get($hdr) or next;
58 return 1 if !$tocc || $tocc !~ $addr_re;
64 # list_mirror HEADER HEADER_VALUE HOSTNAME_GLOB [LIST_ADDRESS]
65 # list_mirror X-Mailing-List git@vger.kernel.org *.kernel.org
66 # list_mirror List-Id <foo.example.org> *.example.org foo@example.org
67 sub config_list_mirror {
68 my ($self, $key, $value, $line) = @_;
71 return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE;
73 my ($hdr, $hval, $host_glob, @extra) = split(/\s+/, $value);
74 my $addr = shift @extra;
78 return $Mail::SpamAssassin::Conf::INVALID_VALUE;
79 $addr = join('|', map { quotemeta } split(/,/, $addr));
80 $addr = qr/\b$addr\b/i;
83 @extra and return $Mail::SpamAssassin::Conf::INVALID_VALUE;
86 return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE;
88 my %patmap = ('*' => '\S+', '?' => '.', '[' => '[', ']' => ']');
89 $host_glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
90 my $host_re = qr/\A\s*from\s+$host_glob(?:\s|$)/si;
92 push @{$self->{list_mirror_check}}, [ $hdr, $hval, $host_re, $addr ];
96 my ($self, $conf) = @_;
99 setting => 'list_mirror',
101 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
102 code => *config_list_mirror,
104 $conf->{parser}->register_commands(\@cmds);