Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup SignExternalMessage #415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/usr/sbin/MailScanner
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,9 @@ sub WorkForHours {
# Encapsulate the messages into message/rfc822 attachments as needed
$batch->Encapsulate();

# Sign all external messages
$batch->SignExternalMessage();

# Sign all the uninfected messages
$batch->SignUninfected();

Expand Down
28 changes: 16 additions & 12 deletions common/usr/share/MailScanner/perl/MailScanner/Message.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4958,7 +4958,7 @@ sub SignWarningMessage {
# https://github.com/MailScanner/v5/issues/375
# Sign the body of the message with a text or html warning message
# alerting users that message was from an external source
# Return 0 if nothing was signed, true if it signed something.
# Set bodymodifed and externalsigned upon signing
sub SignExternalMessage {
my $this = shift;
my $top = shift;
Expand Down Expand Up @@ -4995,12 +4995,24 @@ sub SignExternalMessage {
MailScanner::Log::DebugLog("Debug: Adding external html for message %s", $this->{id});
$warning = $this->ReadExternalWarning('inlineexternalhtml');
#$warning = quotemeta $warning; # Must leave HTML tags alone!
my $htmltagfound = 0;
foreach $line (@body) {
# html tags can have extra attributes. In a case where the <html> tag
# has attributes and is closed on a subsequent line, the warning will
# actually be in the tag, but it's malformed in any case because it
# precedes any <head> and <body> tags and clients seem to render it OK.
$line =~ s/\<html( [^>]*)?(\>|$)/$&$warning/i;
if ( $line =~ /\<html( [^>]*)?(\>|$)/ ) {
$htmltagfound = 1;
}
}

# Just sign if no html tag present
$io->print($warning)
unless $htmltagfound == 1;
foreach $line (@body) {
# if at <html> tag, sign here
$line =~ s/<html( [^>]*)?(\>|$)/$&$warning/i
unless $htmltagfound == 0;
$io->print($line);
}
} else {
Expand All @@ -5015,7 +5027,8 @@ sub SignExternalMessage {
MailScanner::Log::DebugLog("Debug: Exiting SignExternalMessage for message %s", $this->{id});

# We signed something
return 1;
$this->{bodymodified} = 1;
$this->{externalsigned} = 1;
}

# Read the appropriate warning message to sign the top of cleaned messages.
Expand Down Expand Up @@ -5864,15 +5877,6 @@ sub DeliverModifiedBody {
return;
}

# https://github.com/MailScanner/v5/issues/375
# Sign the top of the message body with a text/html externalwarning if they want.
if (MailScanner::Config::Value('externalwarning',$this) =~ /1/ &&
!$this->{externalsigned}) {
MailScanner::Log::DebugLog("Debug: Adding external warning to message %s body", $this->{id});
$this->SignExternalMessage($this->{entity});
$this->{externalsigned} = 1;
}

# Prune the entity tree to remove all undef values
#PruneEntityTree($this->{entity},$this->{entity2file},$this->{file2entity});
PruneEntityTree($entity,$this->{entity2file},$this->{file2entity});
Expand Down
16 changes: 16 additions & 0 deletions common/usr/share/MailScanner/perl/MailScanner/MessageBatch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,22 @@ sub QuarantineModifiedBody {
}
}

# Sign all external messages
sub SignExternalMessage {
my $this = shift;

my ($id, $message);

while(($id, $message) = each %{$this->{messages}}) {
next if $message->{deleted} || $message->{dontdeliver};
if (MailScanner::Config::Value('externalwarning',$this) =~ /1/ &&
!$this->{externalsigned}) {
MailScanner::Log::NoticeLog("Message is external, prepending warning for %s", $id);
$message->SignExternalMessage($message->{entity});
}
}
}

# Sign all the messages that were clean with a tag line saying
# (ideally) that MailScanner is wonderful :-)
sub SignUninfected {
Expand Down