Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/IMAP/ImapMessageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,18 @@ private function decodeSubject(Horde_Imap_Client_Data_Envelope $envelope): strin
}

private function parseHeaders(Horde_Imap_Client_Data_Fetch $fetch): void {
/** @var resource $headersStream */
$headersStream = $fetch->getHeaderText('0', Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
$parsedHeaders = Horde_Mime_Headers::parseHeaders($headersStream);
fclose($headersStream);
// Prefer the targeted HEADER.FIELDS fetch used by the sync path (BODY.PEEK[HEADER.FIELDS (...)]).
// Fall back to the full header text when loadBody=true re-fetches with headerText().
$parsedHeaders = $fetch->getHeaders('syncFields', Horde_Imap_Client_Data_Fetch::HEADER_PARSE);
if ($parsedHeaders === null) {
/** @var resource $headersStream */
$headersStream = $fetch->getHeaderText('0', Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
if ($headersStream === null) {
return;
}
$parsedHeaders = Horde_Mime_Headers::parseHeaders($headersStream);
fclose($headersStream);
}

$references = $parsedHeaders->getHeader('references');
if ($references !== null) {
Expand Down
13 changes: 9 additions & 4 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,16 @@ public function findByIds(Horde_Imap_Client_Base $client,
$query->flags();
$query->uid();
$query->imapDate();
$query->headerText(
$query->headers(
'syncFields',
[
'cache' => true,
'peek' => true,
]
'references',
'disposition-notification-to',
'dkim-signature',
'list-unsubscribe',
'list-unsubscribe-post',
],
['peek' => true],
);

if (is_array($ids)) {
Expand Down
63 changes: 63 additions & 0 deletions tests/Integration/IMAP/ImapMessageFetcherIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

namespace OCA\Mail\Tests\Integration\IMAP;

use Horde_Imap_Client_Ids;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\SmimeCertificate;
use OCA\Mail\Db\SmimeCertificateMapper;
use OCA\Mail\IMAP\ImapMessageFetcherFactory;
use OCA\Mail\IMAP\MessageMapper as ImapMessageMapper;
use OCA\Mail\Tests\Integration\Framework\ImapTest;
use OCA\Mail\Tests\Integration\Framework\ImapTestAccount;
use OCA\Mail\Tests\Integration\TestCase;
Expand Down Expand Up @@ -187,4 +189,65 @@ public function testFetchMessageWithOpaqueSignedMessage(): void {
$this->assertTrue($message->isSigned());
$this->assertTrue($message->isSignatureValid());
}

/**
* Verifies that all five header fields parsed during the loadBody=false sync path
* are correctly extracted. This is the regression guard for switching from
* BODY.PEEK[HEADER] (full headers) to BODY.PEEK[HEADER.FIELDS (...)] (selective).
*/
public function testSyncPathParsesAllRequiredHeaderFields(): void {
// Append raw bytes directly to avoid Horde_Mime_Mail rewriting the headers
$rawMime = implode("\r\n", [
'From: sender@test.invalid',
'To: recipient@test.invalid',
'Subject: Header fields test',
'Message-ID: <header-fields-test@test.invalid>',
'References: <parent@test.invalid>',
'Disposition-Notification-To: sender@test.invalid',
'DKIM-Signature: v=1; a=rsa-sha256; d=test.invalid; s=default;',
' h=from:to:subject;',
' bh=47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=;',
' b=ZXhhbXBsZXNpZ25hdHVyZQ==',
'List-Unsubscribe: <https://test.invalid/unsubscribe>',
'List-Unsubscribe-Post: List-Unsubscribe=One-Click',
'Content-Type: text/plain; charset=utf-8',
'',
'Test body',
]);

$appendClient = $this->getClient(null);
$uid = $appendClient->append('INBOX', [['data' => $rawMime]])->ids[0];
$appendClient->logout();

/** @var ImapMessageMapper $mapper */
$mapper = Server::get(ImapMessageMapper::class);
$fetchClient = $this->getClient($this->account);
try {
$messages = $mapper->findByIds(
$fetchClient,
'INBOX',
new Horde_Imap_Client_Ids([$uid]),
$this->account->getUserId(),
);
} finally {
$fetchClient->logout();
}

$this->assertCount(1, $messages);
$message = $messages[0];

// disposition-notification-to
$this->assertSame('sender@test.invalid', $message->getDispositionNotificationTo());

// dkim-signature (no public getter — check via serialization)
$this->assertTrue($message->jsonSerialize()['hasDkimSignature']);

// list-unsubscribe + list-unsubscribe-post
$this->assertSame('https://test.invalid/unsubscribe', $message->getUnsubscribeUrl());
$this->assertTrue($message->isOneClickUnsubscribe());

// references — flows through toDbMessage() which parses IDs from the raw value
$dbMessage = $message->toDbMessage(1, $this->account);
$this->assertStringContainsString('parent@test.invalid', $dbMessage->getReferences() ?? '');
}
}
Loading