user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] examples/unsubscribe.milter: limit scope of munging
Date: Sat, 25 Nov 2023 01:52:25 +0000	[thread overview]
Message-ID: <20231125015225.4052893-1-e@80x24.org> (raw)

We don't want the milter to munge List-Unsubscribe headers from
external (incoming) mlmmj lists, only lists hosted on the server
running unsubscribe.milter.

Adding support for an allow_domains file should've been enough,
but this further restricts the milter to only operating on Postfix
connections from localhost.
---
 examples/unsubscribe.milter | 38 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/examples/unsubscribe.milter b/examples/unsubscribe.milter
index 216b0ddd..8c682012 100644
--- a/examples/unsubscribe.milter
+++ b/examples/unsubscribe.milter
@@ -27,6 +27,28 @@ my $crypt = Crypt::CBC->new(-key => $key,
 			-cipher => 'Blowfish');
 $fh = $iv = $key = undef;
 
+my $allow_domains = '/etc/unsubscribe-milter.allow_domains';
+my $ALLOW_DOMAINS;
+if (open my $fh, '<', $allow_domains) {
+	local $/ = "\n";
+	chomp(my @l = <$fh>);
+	die "close: $!" unless eof($fh) && close($fh);
+	my %l = map { lc($_) => 1 } @l;
+	$ALLOW_DOMAINS = \%l;
+} else {
+	warn <<EOM;
+W: open $allow_domains: $! (all domains allowed)
+W: all mlmmj-looking messages will have List-Unsubscribe added,
+W: this is probably not what you want.
+EOM
+}
+
+# only allow users hitting SMTP server locally:
+# Is a config file necessary?  Regexps are ugly for IP addresses
+# but Net::Patricia (or similar) seems like overkill.  Ugly it is:
+my @ALLOW_ADDR = (qr/\A::1\z/, qr/\A127\./);
+my $ALLOW_ADDR = join('|', @ALLOW_ADDR);
+
 my %cbs;
 $cbs{connect} = sub {
 	my ($ctx) = @_;
@@ -88,10 +110,24 @@ $cbs{eom} = sub {
 	eval {
 		my $priv = $ctx->getpriv;
 		$ctx->setpriv({ header => {}, envrcpt => {} });
-		my @rcpt = keys %{$priv->{envrcpt}};
+
+		# XXX my postfix (3.5.18-0+deb11u1) + Sendmail::PMilter
+		# instance doesn't seem to get {client_addr}, but
+		# {daemon_addr} seems to make sense since I only want it
+		# to apply to users connecting to postfix locally:
+		if ($ALLOW_ADDR) {
+			my $x = $ctx->getsymval('{daemon_addr}');
+			return SMFIS_CONTINUE if $x && $x !~ /$ALLOW_ADDR/;
+		}
 
 		# one recipient, one unique HTTP(S) URL
+		my @rcpt = keys %{$priv->{envrcpt}};
 		return SMFIS_CONTINUE if @rcpt != 1;
+		if ($ALLOW_DOMAINS) {
+			my $addr = $ctx->getsymval('{mail_addr}');
+			my (undef, $d) = split /\@/, $addr;
+			return SMFIS_CONTINUE if !$ALLOW_DOMAINS->{$d};
+		}
 		return SMFIS_CONTINUE if archive_addr(lc($rcpt[0]));
 
 		my $unsub = $priv->{header}->{'list-unsubscribe'} || [];

                 reply	other threads:[~2023-11-25  1:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231125015225.4052893-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).