Mail-DKIM2
view release on metacpan or search on metacpan
t/header-name-wsp.t view on Meta::CPAN
#!/usr/bin/perl -w
# RFC 5322's obsolete syntax lets WSP sit between a field name and its colon
# ("From : x"), and -09 section 6.2 deletes it: the name is the name without
# it. Taking the WSP as part of the name made "From : ceo@paypal.example" a
# field called "from " -- hashed, so intact, but invisible to the From check,
# which saw only the other From. A reader that shows the first From showed
# paypal.example as authenticated.
use 5.020;
use strict;
use warnings;
use Test::More;
use lib 'lib', 't/lib';
use DKIM2TestKeys;
use Mail::DKIM2::Common qw(dkim2_canonicalize_header);
use Mail::DKIM2::MessageInstance;
use Mail::DKIM2::Signer;
use Mail::DKIM2::Verifier;
use UndoWalkVerifier;
is(dkim2_canonicalize_header("Subject : hi\r\n"), "subject:hi\r\n",
'canonical form: WSP before the colon is deleted');
is(dkim2_canonicalize_header("Subject\t: hi\r\n"), "subject:hi\r\n", ' ... tab too');
# Sign as test1.dkim2.com with its own From check off, as a signer that does
# not check (or another implementation) would.
sub signed {
my ($raw) = @_;
my $mi = Mail::DKIM2::MessageInstance->calculate($raw);
my $msg = "Message-Instance: " . $mi->as_string . "\r\n$raw";
no warnings 'redefine';
no warnings 'redefine';
local *Mail::DKIM2::Address::from_pair_error = sub { undef };
my $s = Mail::DKIM2::Signer->new(
Domain => 'test1.dkim2.com', Selector => 'sel1',
Key => DKIM2TestKeys::private_key('test1.dkim2.com', 'sel1'),
MailFrom => 'a@test1.dkim2.com', RcptTo => ['b@test2.dkim2.com'],
Timestamp => 1740000000,
);
$s->PRINT($msg); $s->CLOSE;
(my $sig = $s->as_string) =~ s/\r?\n\z//;
return "$sig\r\n$msg";
}
sub verify {
my ($msg, $stream) = @_;
my $v = ($stream ? "Mail::DKIM2::Verifier" : "UndoWalkVerifier")->new(
PubkeyCallback => DKIM2TestKeys::pubkey_callback());
$v->skip_timestamp_check(1);
$v->PRINT($msg); $v->CLOSE;
return ($v->result, $v->result_detail);
}
for my $ws (' ', "\t") {
my $label = $ws eq ' ' ? 'space' : 'tab';
my $msg = signed("From${ws}: CEO <ceo\@paypal.example>\r\n"
. "From: a\@test1.dkim2.com\r\nTo: b\@test2.dkim2.com\r\n"
. "Subject: pay\r\n\r\nbody\r\n");
for my $stream (1, 0) {
my $walk = $stream ? "plans" : "undo walk";
my ($r, $d) = verify($msg, $stream);
isnt($r, 'pass', "From${label}: is a second From ($walk)");
like($d, qr/From/, ' ... and the From check says so') or diag $d;
}
}
# The name without its WSP is the name: "Subject :" hashes as Subject, so a
# Subject written either way verifies, and either way written by a hop
# that rewrote it is the same field.
{
my $msg = signed("From: a\@test1.dkim2.com\r\nTo: b\@test2.dkim2.com\r\n"
. "Subject : hello\r\n\r\nbody\r\n");
for my $stream (1, 0) {
my $walk = $stream ? "plans" : "undo walk";
my ($r, $d) = verify($msg, $stream);
is($r, 'pass', "Subject with WSP before the colon verifies ($walk)") or diag $d;
}
(my $respelled = $msg) =~ s/^Subject : hello/Subject: hello/m;
my ($r, $d) = verify($respelled, 1);
is($r, 'pass', 'and is the same field without the WSP') or diag $d;
}
# A hop's own Recipe over such a field: the key is the field's name, as
# hashing and undo group it ("comments", not "comments "), so the chain the
# hop signs reverses -- through the undo walk and the plan alike.
use Mail::DKIM2::Common qw(parse_mime);
use Mail::DKIM2::HeaderPlan;
sub hop {
my ($prev_raw, $edit) = @_;
my $prev = parse_mime($prev_raw);
$prev->header_raw_prepend('Message-Instance', Mail::DKIM2::MessageInstance->calculate($prev)->as_string);
my $cur = parse_mime($prev->as_string);
$edit->($cur);
my $mi2 = Mail::DKIM2::MessageInstance->calculate($cur, $prev);
$cur->header_raw_prepend('Message-Instance', $mi2->as_string);
my ($h) = split /\r\n\r\n/, $cur->as_string, 2;
( run in 1.187 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )