#!perl
use Cassandane::Tiny;
use Cassandane::Util::Slurp;

# Header values a received message or the client controls must not be
# able to start a new header field in the MDN that goes out.
sub test_mdn_send_header_injection
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $using = [
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:mail',
        'urn:ietf:params:jmap:submission',
        'urn:ietf:params:jmap:mdn',
    ];

    my $disposition = {
        actionMode => 'manual-action',
        sendingMode => 'MDN-sent-manually',
        type => 'displayed',
    };

    # An MDN goes out with a null return path, which fakesmtpd refuses
    $self->{instance}->set_smtpd({ from => [250, 'ok'] });

    my $deliver = sub {
        my ($subject, @extra) = @_;
        my $msg = $self->make_message($subject,
            extra_headers => [
                ['Disposition-Notification-To', 'sender@example.org'],
                @extra,
            ],
            body => 'read me',
        ) || die;

        my $want = $msg->get_header('Message-ID') =~ s/^<(.*)>$/$1/r;
        my $res = $jmap->CallMethods([
            ['Email/query', {}, 'q'],
            ['Email/get', {
                '#ids' => { resultOf => 'q', name => 'Email/query', path => '/ids' },
                properties => ['messageId'],
            }, 'g'],
        ], $using);
        my ($email) = grep { $_->{messageId}[0] eq $want }
            @{$res->[1][1]{list}};
        return $email->{id};
    };

    my $send_mdn = sub {
        my ($emailId, %props) = @_;
        my $res = $jmap->CallMethods([
            ['MDN/send', {
                send => { k1 => {
                    forEmailId => $emailId,
                    disposition => $disposition,
                    %props,
                } },
            }, 'R1'],
        ], $using);
        return $res->[0][1];
    };

    # Every MDN sent so far, as one string
    my $all_sent = sub {
        my $dir = $self->{instance}{basedir} . '/smtpd';
        return join "", map { slurp_file($_) } glob("$dir/message_*.smtp");
    };

    xlog $self, "an encoded CRLF in the original Subject stays in Subject";
    my $emailId = $deliver->('=?us-ascii?Q?hi=0D=0AX-Injected-Subject:_true?=');
    my $sent = $send_mdn->($emailId);
    $self->assert_not_null($sent->{sent}{k1});
    my $out = $all_sent->();
    $self->assert_does_not_match(qr/^X-Injected/m, $out);
    $self->assert_matches(qr/^Subject: Return Receipt \(displayed\) for\r?\n\t\S/m,
                          $out);

    xlog $self, "an encoded CRLF in Original-Recipient is not decoded";
    my $encoded = '=?us-ascii?Q?rfc822;_u@example.com=0D=0AX-Injected-Rcpt:_true?=';
    $emailId = $deliver->('encoded rcpt', ['Original-Recipient', $encoded]);
    $sent = $send_mdn->($emailId);
    $self->assert_str_equals($encoded, $sent->{sent}{k1}{originalRecipient});
    $self->assert_does_not_match(qr/^X-Injected/m, $all_sent->());

    xlog $self, "a folded Original-Recipient is unfolded";
    $emailId = $deliver->('folded rcpt',
        ['Original-Recipient', "rfc822; u\@example.com\r\n X-Injected-Fold: true"]);
    $sent = $send_mdn->($emailId);
    $self->assert_str_equals('rfc822; u@example.com X-Injected-Fold: true',
                             $sent->{sent}{k1}{originalRecipient});
    $self->assert_does_not_match(qr/^X-Injected/m, $all_sent->());

    xlog $self, "a CRLF in reportingUA is rejected";
    $emailId = $deliver->('reporting ua');
    $sent = $send_mdn->($emailId, reportingUA => "ua\r\nX-Injected-UA: true");
    $self->assert_deep_equals(
        { k1 => { type => 'invalidProperties', properties => ['reportingUA'] } },
        $sent->{notSent},
    );
}
