Mail-DKIM2
view release on metacpan or search on metacpan
t/full-chain.t view on Meta::CPAN
},
{
# brong-final.eml differs from brong-mm3.eml by a pile of prepended
# MTA trace/spam headers (Received, ARC-*, Authentication-Results,
# Received-SPF, X-*) plus a "[test5] " tag hand-added to the front
# of the Subject header's value. Under -09 §4, every one of
# those prepended headers is unsigned (the last holdout,
# Received-SPF, moved to unsigned when the `received-` prefix rule
# landed) -- without the Subject tag, this hop's header+body hash
# would be bit-identical to hop 4's, add_mi() would correctly
# decline to add a new Message-Instance (see MI.pm's verify()
# short-circuit, exercised deliberately by hop 6 below), and this
# "final delivery" hop would silently collapse into a duplicate of
# hop 6's "unchanged re-sign" scenario. The Subject tag keeps this
# hop exercising a genuinely CHANGED (not added/removed) header --
# coverage no other hop in this chain provides. Do not remove it.
name => 'final delivery',
file => 'tests/emails/brong-final.eml',
domain => 'test5.dkim2.com',
selector => 'sel2',
mailfrom => 'forwarder@test5.dkim2.com',
rcptto => ['brong@test1.dkim2.com'],
},
);
# Strip all existing MI/DKIM2 headers from the base files
sub strip_msg {
my $msg = shift;
$msg->header_raw_set('Message-Instance');
$msg->header_raw_set('DKIM2-Signature');
$msg->header_raw_set('MailVersion');
$msg->header_raw_set('Mail-Version');
return $msg;
}
sub add_mi {
my ($msg, $prev_msg) = @_;
# Skip if the message already has an MI that matches current content
if ( Mail::DKIM2::MessageInstance->verify($msg) ) {
return undef;
}
my $mi = $prev_msg
? Mail::DKIM2::MessageInstance->calculate($msg, $prev_msg)
: Mail::DKIM2::MessageInstance->calculate($msg);
my $folded = fold_header("Message-Instance: " . $mi->as_string());
$folded =~ s/^Message-Instance: //;
$msg->header_raw_prepend('Message-Instance', $folded);
return $mi;
}
# Fixed timestamp for reproducible output
my $TIMESTAMP = 1740000000;
sub sign_msg {
my ($msg, $hop) = @_;
my $signer = Mail::DKIM2::Signer->new(
Domain => $hop->{domain},
Selector => $hop->{selector},
Key => DKIM2TestKeys::private_key($hop->{domain}, $hop->{selector}),
MailFrom => $hop->{mailfrom},
RcptTo => $hop->{rcptto},
Timestamp => $TIMESTAMP,
);
$signer->PRINT($msg->as_string());
$signer->CLOSE;
my $header = $signer->as_string();
$header =~ s{^DKIM2-Signature:\s*}{};
$msg->header_raw_prepend('DKIM2-Signature', $header);
return $signer;
}
sub verify_msg {
my ($msg) = @_;
my $verifier = Mail::DKIM2::Verifier->new();
$verifier->set_pubkey_callback(DKIM2TestKeys::pubkey_callback());
$verifier->skip_timestamp_check(1); # test emails have fixed timestamps
$verifier->PRINT($msg->as_string());
$verifier->CLOSE;
return $verifier;
}
# ============================================================
# Phase 1: Build the full chain, verifying at each step
# ============================================================
diag("=== Building chain ===");
my $out_dir = 'tests/expected';
path($out_dir)->mkpath;
my $current_msg;
for my $i (0..$#hops) {
my $hop = $hops[$i];
my $label = "hop " . ($i + 1) . " ($hop->{name})";
# Load the raw message for this hop
my $raw = path($hop->{file})->slurp;
my $msg = Email::MIME->new($raw);
strip_msg($msg);
if ($i == 0) {
# First hop: calculate v=1 MI (no previous message)
my $mi = add_mi($msg);
ok($mi->get_tag('m') == 1, "$label: MI version is 1");
} else {
# Graft MI and DKIM2-Sig headers from the accumulated chain
# onto this new message version
my @mi_headers = $current_msg->header_raw('Message-Instance');
my @dk2_headers = $current_msg->header_raw('DKIM2-Signature');
# Prepend in reverse order (bottom-up, so first prepended = bottom)
for my $h (reverse @dk2_headers) {
$msg->header_raw_prepend('DKIM2-Signature', $h);
}
for my $h (reverse @mi_headers) {
$msg->header_raw_prepend('Message-Instance', $h);
}
( run in 0.778 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )