Mail-DKIM2

 view release on metacpan or  search on metacpan

t/gate-null-top.t  view on Meta::CPAN

#!/usr/bin/perl
# Mail::DKIM2::Gate and null body Recipes (0.29, -09 contract E2). A
# signature with m=k covers instances 1..k, so an instance is uncovered when
# its m= is above the highest m= of every valid signature: a null THIS hop
# is introducing, which is always signed (it PERMERRORs downstream anyway,
# but our header changes are signed). A null that the upstream domain
# declared and signed fails upstream verification and is refused.
use strict;
use warnings;
use Test::More;
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/../lib";
use Email::MIME;
use Mail::DKIM2::Gate;
use Mail::DKIM2::MessageInstance;
use Mail::DKIM2::Signer;
use DKIM2TestKeys;

my $EOL = "\015\012";
my $PLAIN = join($EOL,
    'MIME-Version: 1.0',
    'Message-Id: <post@test1.dkim2.com>',
    'Date: Thu, 10 Sep 2026 15:47:11 +1000',
    'From: Author <author@test1.dkim2.com>',
    'To: list@test2.dkim2.com',
    'Subject: a post',
    'Content-Type: text/plain',
    '',
    'hello list',
    '');

sub sign_as {
    my ($msg, $dom, $mf, $rt) = @_;
    my $s = Mail::DKIM2::Signer->new(
        Domain => $dom, Selector => 'sel1',
        Key => DKIM2TestKeys::private_key($dom, 'sel1'),
        MailFrom => $mf, RcptTo => [$rt], Timestamp => time());
    $s->PRINT($msg); $s->CLOSE;
    return $s->as_string . $EOL . $msg;
}

# i=1/m=1 by test1, then the list test2 tags the subject and rewrites the
# body, recording m=2 with a null body Recipe.  m=2 is unsigned here.
sub null_list_post {
    my (%o) = @_;
    my $mi = Mail::DKIM2::MessageInstance->calculate(Email::MIME->new($PLAIN));
    my $signed = sign_as("Message-Instance: " . $mi->as_string . $EOL . $PLAIN,
        'test1.dkim2.com', 'author@test1.dkim2.com', 'list@test2.dkim2.com');
    my $mod = $signed;
    $mod =~ s/^Subject: /Subject: [list] /m;
    $mod =~ s/^To: .*$/To: tampered\@example.net/m if $o{forge};
    $mod .= "--$EOL" . "rewritten$EOL";
    my $mi2 = Mail::DKIM2::MessageInstance->calculate(
        Email::MIME->new($mod), Email::MIME->new($signed));
    $mi2->set_null_body_recipe;
    if ($o{forge}) {
        my $rh = $mi2->{bits}{rh};
        delete $rh->{$_} for grep { lc($_) eq 'to' } keys %$rh;
    }
    return "Message-Instance: " . $mi2->as_string . $EOL . $mod;
}

# ... and the list domain signs its own m=2 (i=2, m=2) to the forwarder.
sub signed_null_list_post {
    return sign_as(null_list_post(@_), 'test2.dkim2.com',
        'list-bounces@test2.dkim2.com', 'subscriber@test3.dkim2.com');
}

# An ordinary hop on top of $msg: another Subject tag and a footer, recorded as
# a new UNSIGNED Message-Instance with real Recipes.
sub ordinary_on_top {
    my ($msg) = @_;
    my $mod = $msg;
    $mod =~ s/^Subject: /Subject: [fwd] /m;
    $mod .= "footer$EOL";
    my $mi = Mail::DKIM2::MessageInstance->calculate(
        Email::MIME->new($mod), Email::MIME->new($msg));
    return "Message-Instance: " . $mi->as_string . $EOL . $mod;
}

my %cb = (PubkeyCallback => DKIM2TestKeys::pubkey_callback());

{
    my $msg = null_list_post();
    my $g = Mail::DKIM2::Gate->check($msg, %cb);
    ok($g->{ok}, 'unsigned null top: signed (0.29: our own null is always signed)')
        or diag($g->{message});
    is($g->{reason}, undef, 'unsigned null top: no refusal');
    is($g->{top_null}, 1, 'unsigned null top: top_null');
    is($g->{top_signed}, 0, 'unsigned null top: top_signed is 0 (i=1 covers only m=1)');
    my @warn;
    {
        local $SIG{__WARN__} = sub { push @warn, @_ };
        $g = Mail::DKIM2::Gate->check($msg, %cb, AllowNullBodyRecipe => 0);
        is(scalar @warn, 0, 'AllowNullBodyRecipe => 0 (as hm passes it) does not warn');
        Mail::DKIM2::Gate->check($msg, %cb, AllowNullBodyRecipe => 1);



( run in 1.271 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )