Mail-DKIM2

 view release on metacpan or  search on metacpan

t/mi-null-header-history.t  view on Meta::CPAN

    my $m3 = ordinary_hop(forge_history($m1));
    my ($ok, $why) = $MI->chain_verifies($m3);
    ok(!$ok, 'hidden To change in null m=2 header Recipe, ordinary m=3 above, is caught');
    like($why // '', qr/^FAIL: Message-Instance m=1 header hash sha256 mismatch$/, 'reason names m=1 header hash') or diag $why;
}

# Empty-body chain: no body at all, header-only Recipe at m=2.
my $empty = join($EOL, 'From: a@example.com', 'To: list@example.org',
    'Subject: hello', 'Message-ID: <x@example.com>', '', '');
my $e1 = with_mi($MI->calculate($empty), $empty);
{
    my ($ok, $why) = $MI->chain_verifies(ordinary_hop($e1));
    ok($ok, 'empty-body chain, header-only m=2 verifies') or diag $why;
    ($ok, $why) = $MI->chain_verifies(ordinary_hop($e1, 1));
    ok(!$ok, 'empty-body chain, hidden To change fails');
    like($why // '', qr/^FAIL: Message-Instance m=1 header hash sha256 mismatch$/, 'empty-body: reason names m=1 header hash') or diag $why;
}

# Recipe structure is checked even where the body is gone: m=2 has a body
# Recipe with descending copy ranges, m=3 a null one.
sub malformed_body_below_null {
    my ($prev) = @_;
    my $cur2 = $prev . "footer$EOL";
    my $mi2 = $MI->calculate($cur2, $prev);
    $mi2->{bits}{rb} = [[2, 2], [1, 1]];
    return list_hop(with_mi($mi2, $cur2), 'list');
}

{
    my ($ok, $why, $detail) = $MI->chain_verifies(malformed_body_below_null($m1));
    ok(!$ok, 'malformed body Recipe below a null body Recipe is caught');
    is($why, 'PERMERROR: Message-Instance m=2 recipe b[1] is invalid',
        'reason names the out-of-order step (-08 D6)');
}

# The structure-only check below a null has no line count: a bad range is
# the step's error (-08 D6), and must not warn.
for my $range ([0, 2], [3, 1]) {
    my @warn;
    local $SIG{__WARN__} = sub { push @warn, @_ };
    my $cur2 = $m1 . "footer$EOL";
    my $mi2 = $MI->calculate($cur2, $m1);
    $mi2->{bits}{rb} = [$range];
    my ($ok, $why, $detail) = $MI->chain_verifies(list_hop(with_mi($mi2, $cur2), 'list'));
    my $r = "[@$range]";
    ok(!$ok, "body Recipe $r below a null is caught");
    is($why, 'PERMERROR: Message-Instance m=2 recipe b[0] is invalid', "$r: reason names the step");
    is_deeply(\@warn, [], "$r: no warnings") or diag @warn;
}

use lib "$FindBin::Bin/lib";
use Mail::DKIM2::Signer;
use Mail::DKIM2::Verifier;
use DKIM2TestKeys;

my $signed_m1;
sub sign_i1 {
    my ($msg) = @_;
    my $s = Mail::DKIM2::Signer->new(
        Domain => 'test1.dkim2.com', Selector => 'rsa1024',
        Key => DKIM2TestKeys::private_key('test1.dkim2.com', 'rsa1024'),
        MailFrom => 'a@test1.dkim2.com', RcptTo => ['list@test2.dkim2.com'],
        Timestamp => 1740000000);
    $s->PRINT($msg); $s->CLOSE;
    return $s->as_string . $EOL . $msg;
}

sub verifier_result {
    my ($msg) = @_;
    my $v = Mail::DKIM2::Verifier->new;
    $v->allow_unsigned_mi(1);
    $v->skip_timestamp_check(1);
    $v->set_pubkey_callback(DKIM2TestKeys::pubkey_callback());
    $v->PRINT($msg); $v->CLOSE;
    return $v->result_detail;
}

{
    my $signed = sign_i1($m1);
    my $m2 = list_hop($signed, 'list');
    like(verifier_result($m2), qr/^pass/, 'Verifier: null body over signed m=1 passes');

    $signed_m1 = $signed;
    my $forged = forge_history($signed);
    like(verifier_result($forged), qr/^fail \(FAIL: Message-Instance m=1 header hash sha256 mismatch\)$/, 'Verifier: tampered history below null fails on m=1');

    like(verifier_result(bad_header_recipe_below_null($signed)), qr/^permerror \(PERMERROR: Message-Instance m=2 contains invalid JSON\)$/,
        'Verifier: header Recipe that does not apply below null fails');
}

{
    my $v = verifier_result(malformed_body_below_null($signed_m1));
    unlike($v, qr/^pass/, 'Verifier: malformed body Recipe below null is not a pass');
}

done_testing;



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