Skip to content

Commit

Permalink
fix: phishing detection fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Dec 11, 2024
1 parent c0f17db commit 89edd80
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/Service/PhishingDetection/DateCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Mail\Service\PhishingDetection;

use DateException;
use OCA\Mail\PhishingDetectionResult;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IL10N;
Expand All @@ -24,7 +25,11 @@ public function __construct(IL10N $l10n, ITimeFactory $timeFactory) {

public function run(string $date): PhishingDetectionResult {
$now = $this->timeFactory->getDateTime('now');
$dt = $this->timeFactory->getDateTime($date);
try {
$dt = $this->timeFactory->getDateTime($date);
} catch (DateException $e) {
return new PhishingDetectionResult(PhishingDetectionResult::DATE_CHECK, false);
}
if ($dt > $now) {
return new PhishingDetectionResult(PhishingDetectionResult::DATE_CHECK, true, $this->l10n->t('Sent date is in the future'));
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Service/PhishingDetection/LinkCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public function run(string $htmlMessage) : PhishingDetectionResult {
if ($href === '') {
continue;
}
// handle links that are wrapped in brackets, quotes, etc.
$firstChar = $linkText[0];
$lastChar = $linkText[strlen($linkText) - 1];

if (!ctype_alpha($firstChar) && !ctype_alpha($lastChar)) {
$linkText = substr($linkText, 1, -1);
}

$zippedArray[] = [
'href' => $href,
'linkText' => $linkText
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Service/Phishing/DateCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,23 @@ public function testInTheFuture(): void {
$this->assertTrue($result->isPhishing());
}

public function testInvalidDate(): void {

$this->time->expects($this->exactly(2))
->method('getDateTime')
->willReturnCallback(function ($argument): DateTime {
static $callCount = 0;
$callCount++;

if ($callCount === 1) {
return new \DateTime('now');
}
throw new \DateException();
});

$result = $this->service->run('invalid date');

$this->assertFalse($result->isPhishing());
}

}
8 changes: 8 additions & 0 deletions tests/Unit/Service/Phishing/LinkCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public function testCompleteAddressPass(): void {
$this->assertFalse($result->isPhishing());
}

public function testAddressInParenthesessPass(): void {
$htmlMessage = '<html><body><a href="https://nextcloud.com/">(https://nextcloud.com/)</a></body></html>';

$result = $this->service->run($htmlMessage);

$this->assertFalse($result->isPhishing());
}

public function testCompleteAddressFail(): void {
$htmlMessage = '<html><body><a href="https://nextcloud.com/">https://google.com/</a></p></body></html>';

Expand Down

0 comments on commit 89edd80

Please sign in to comment.