Skip to content

Commit

Permalink
fix(phishing): NPEs
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Nov 19, 2024
1 parent 23ecae1 commit f895038
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions lib/Service/PhishingDetection/PhishingDetectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\Mail\Service\PhishingDetection;

use Horde_Mime_Headers;
use Horde_Mime_Headers_Element_Address;
use OCA\Mail\AddressList;
use OCA\Mail\PhishingDetectionList;

Expand All @@ -31,20 +32,37 @@ public function __construct(

public function checkHeadersForPhishing(Horde_Mime_Headers $headers, bool $hasHtmlMessage, string $htmlMessage = ''): array {
$list = new PhishingDetectionList();
/** @psalm-suppress UndefinedMethod */
$fromFN = AddressList::fromHorde($headers->getHeader('From')->getAddressList(true))->first()->getLabel();
/** @psalm-suppress UndefinedMethod */
$fromEmail = AddressList::fromHorde($headers->getHeader('From')->getAddressList(true))->first()->getEmail();
/** @psalm-suppress UndefinedMethod */
$replyToEmailHeader = $headers->getHeader('Reply-To')?->getAddressList(true);
$replyToEmail = isset($replyToEmailHeader)? AddressList::fromHorde($replyToEmailHeader)->first()->getEmail() : null ;
$fromHeader = $headers->getHeader('From');
if (!($fromHeader instanceof Horde_Mime_Headers_Element_Address)) {
return $list->jsonSerialize();

Check warning on line 37 in lib/Service/PhishingDetection/PhishingDetectionService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/PhishingDetection/PhishingDetectionService.php#L37

Added line #L37 was not covered by tests
}
$sender = AddressList::fromHorde($fromHeader->getAddressList(true))->first();
if ($sender === null) {
return $list->jsonSerialize();

Check warning on line 41 in lib/Service/PhishingDetection/PhishingDetectionService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/PhishingDetection/PhishingDetectionService.php#L41

Added line #L41 was not covered by tests
}
$fromFN = $sender->getLabel();
$fromEmail = $sender->getEmail();
$replyToHeader = $headers->getHeader('Reply-To');
if ($fromHeader instanceof Horde_Mime_Headers_Element_Address) {
$replyToEmailHeader = $replyToHeader->getAddressList(true);

Check failure on line 47 in lib/Service/PhishingDetection/PhishingDetectionService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedMethod

lib/Service/PhishingDetection/PhishingDetectionService.php:47:42: UndefinedMethod: Method Horde_Mime_Headers_Element::getAddressList does not exist (see https://psalm.dev/022)
$replyToEmail = AddressList::fromHorde($replyToEmailHeader)->first()?->getEmail();
} else {
$replyToEmail = null;

Check warning on line 50 in lib/Service/PhishingDetection/PhishingDetectionService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/PhishingDetection/PhishingDetectionService.php#L50

Added line #L50 was not covered by tests
}
$date = $headers->getHeader('Date')->__get('value');
/** @psalm-suppress UndefinedMethod */
$customEmail = AddressList::fromHorde($headers->getHeader('From')->getAddressList(true))->first()->getCustomEmail();
$list->addCheck($this->replyToCheck->run($fromEmail, $replyToEmail));
$list->addCheck($this->contactCheck->run($fromFN, $fromEmail));
$list->addCheck($this->dateCheck->run($date));
$list->addCheck($this->customEmailCheck->run($fromEmail, $customEmail));
$customEmail = $sender->getCustomEmail();
if ($fromEmail !== null) {
$list->addCheck($this->replyToCheck->run($fromEmail, $replyToEmail));
}
if ($fromFN !== null) {
$list->addCheck($this->contactCheck->run($fromFN, $fromEmail));
}
if (is_string($date)) {
$list->addCheck($this->dateCheck->run($date));
}
if ($fromEmail !== null) {
$list->addCheck($this->customEmailCheck->run($fromEmail, $customEmail));
}
if ($hasHtmlMessage) {
$list->addCheck($this->linkCheck->run($htmlMessage));
}
Expand Down

0 comments on commit f895038

Please sign in to comment.