Skip to content

Commit

Permalink
fixup! Feat: trusted sender phishing check
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Jun 6, 2024
1 parent 21a2013 commit 7111b87
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 11 deletions.
53 changes: 53 additions & 0 deletions lib/Service/PhishingDetection/TrustedCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/*
* @copyright 2024 Hamza Mahjoubi <hamza.mahjoubi221@proton.me>
*
* @author 2024 Hamza Mahjoubi <hamza.mahjoubi221@proton.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Service\PhishingDetection;

use OCA\Mail\Contracts\ITrustedSenderService;
use OCA\Mail\PhishingDetectionResult;
use OCP\IL10N;

class TrustedCheck {
protected IL10N $l10n;
private ITrustedSenderService $trustedSenderService;


public function __construct(ITrustedSenderService $trustedSenderService, IL10N $l10n) {
$this->l10n = $l10n;
$this->trustedSenderService = $trustedSenderService;
}

private function run(string $uid, string $email) :PhishingDetectionResult {
$domain = explode('@', $email)[1];
$trusted = $this->trustedSenderService->isTrusted($uid, $email) || $this->trustedSenderService->isTrusted($uid, $domain);

if(!$trusted) {
return new PhishingDetectionResult(PhishingDetectionResult::TRUSTED_CHECK, true, $this->l10n->t('Sender email: %1$s is not trusted', [$email]));
}

return new PhishingDetectionResult(PhishingDetectionResult::TRUSTED_CHECK, false);
}

}
36 changes: 28 additions & 8 deletions lib/Service/TrustedSenderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,55 @@ public function __construct(TrustedSenderMapper $mapper) {
$this->mapper = $mapper;
}

public function isTrusted(string $uid, string $email): bool {
/**
* @param string $uid User ID
* @param string $sender Email address or domain
*
* @return bool
*/

public function isTrusted(string $uid, string $sender): bool {

Check failure on line 46 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

ParamNameMismatch

lib/Service/TrustedSenderService.php:46:48: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::isTrusted has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::isTrusted (see https://psalm.dev/230)

Check failure on line 46 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

ParamNameMismatch

lib/Service/TrustedSenderService.php:46:48: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::isTrusted has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::isTrusted (see https://psalm.dev/230)

Check failure on line 46 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

ParamNameMismatch

lib/Service/TrustedSenderService.php:46:48: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::isTrusted has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::isTrusted (see https://psalm.dev/230)

Check failure on line 46 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

ParamNameMismatch

lib/Service/TrustedSenderService.php:46:48: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::isTrusted has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::isTrusted (see https://psalm.dev/230)
return $this->mapper->exists(
$uid,
$email
$sender
);
}

public function trust(string $uid, string $email, string $type, ?bool $trust = true): void {
if ($trust && $this->isTrusted($uid, $email)) {
/**
* @param string $uid User ID
* @param string $sender Email address or domain
* @param string $type individual|domain
* @param bool|null $trust
*
* @return void
*/

public function trust(string $uid, string $sender, string $type, ?bool $trust = true): void {

Check failure on line 62 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

ParamNameMismatch

lib/Service/TrustedSenderService.php:62:44: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::trust has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::trust (see https://psalm.dev/230)

Check failure on line 62 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

ParamNameMismatch

lib/Service/TrustedSenderService.php:62:44: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::trust has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::trust (see https://psalm.dev/230)

Check failure on line 62 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

ParamNameMismatch

lib/Service/TrustedSenderService.php:62:44: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::trust has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::trust (see https://psalm.dev/230)

Check failure on line 62 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

ParamNameMismatch

lib/Service/TrustedSenderService.php:62:44: ParamNameMismatch: Argument 2 of OCA\Mail\Service\TrustedSenderService::trust has wrong name $sender, expecting $email as defined by OCA\Mail\Contracts\ITrustedSenderService::trust (see https://psalm.dev/230)
if ($trust && $this->isTrusted($uid, $sender)) {
// Nothing to do
return;
}

if ($trust) {
$this->mapper->create(
$uid,
$email,
$sender,
$type
);
} else {
$this->mapper->remove(
$uid,
$email,
$sender,
$type
);
}
}

/**
* @param string $uid User ID
*
* @return array Trusted senders

Check failure on line 85 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

LessSpecificImplementedReturnType

lib/Service/TrustedSenderService.php:85:13: LessSpecificImplementedReturnType: The inherited return type 'array<array-key, OCA\Mail\Db\TrustedSender>' for OCA\Mail\Contracts\ITrustedSenderService::getTrusted is more specific than the implemented return type for OCA\Mail\Service\TrustedSenderService::gettrusted 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 85 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

LessSpecificImplementedReturnType

lib/Service/TrustedSenderService.php:85:13: LessSpecificImplementedReturnType: The inherited return type 'array<array-key, OCA\Mail\Db\TrustedSender>' for OCA\Mail\Contracts\ITrustedSenderService::getTrusted is more specific than the implemented return type for OCA\Mail\Service\TrustedSenderService::gettrusted 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 85 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

LessSpecificImplementedReturnType

lib/Service/TrustedSenderService.php:85:13: LessSpecificImplementedReturnType: The inherited return type 'array<array-key, OCA\Mail\Db\TrustedSender>' for OCA\Mail\Contracts\ITrustedSenderService::getTrusted is more specific than the implemented return type for OCA\Mail\Service\TrustedSenderService::gettrusted 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 85 in lib/Service/TrustedSenderService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

LessSpecificImplementedReturnType

lib/Service/TrustedSenderService.php:85:13: LessSpecificImplementedReturnType: The inherited return type 'array<array-key, OCA\Mail\Db\TrustedSender>' for OCA\Mail\Contracts\ITrustedSenderService::getTrusted is more specific than the implemented return type for OCA\Mail\Service\TrustedSenderService::gettrusted 'array<array-key, mixed>' (see https://psalm.dev/166)
*/
public function getTrusted(string $uid): array {
return $this->mapper->findAll($uid);
}
}
}
73 changes: 70 additions & 3 deletions src/components/TrustedSenders.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<template>
<template>

Check failure on line 22 in src/components/TrustedSenders.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Expected "\t" character, but found " " character
<div>
<div v-for="sender in sortedSenders"
:key="sender.email">
Expand All @@ -33,29 +33,56 @@
</ButtonVue>
</div>
<span v-if="!sortedSenders.length"> {{ t('mail', 'No senders are trusted at the moment.') }}</span>
<ButtonVue type="primary"
@click="openModal = true">
<template #icon>
<IconAdd :size="20" />
</template>
{{ t('mail', 'Add trusted sender') }}
</ButtonVue>
<NcModal v-if="openModal"
:container="null"
:name="t('mail', 'Add trusted sender')"
@close="openModal = false">
<div class="content">
<h2>{{ t('mail', 'Add trusted sender') }}</h2>
<NcTextField class="input" :label="t('mail', 'Sender')" :value.sync="sender" />
<ButtonVue type="primary"
:disabled="sender.length === 0"
@click="trustSender">
{{ t('mail', 'Add') }}
</ButtonVue>
</div>
</NcModal>
</div>
</template>

<script>
import { fetchTrustedSenders, trustSender } from '../service/TrustedSenderService.js'
import { NcButton as ButtonVue } from '@nextcloud/vue'
import { NcButton as ButtonVue, NcModal, NcTextField } from '@nextcloud/vue'
import prop from 'lodash/fp/prop.js'
import sortBy from 'lodash/fp/sortBy.js'
import logger from '../logger.js'
import { showError } from '@nextcloud/dialogs'
import IconAdd from 'vue-material-design-icons/Plus.vue'
const sortByEmail = sortBy(prop('email'))
export default {
name: 'TrustedSenders',
components: {
ButtonVue,
IconAdd,
NcModal,
NcTextField,
},
data() {
return {
list: [],
openModal: false,
sender: '',
}
},
computed: {
Expand Down Expand Up @@ -87,6 +114,40 @@ export default {
this.list.push(sender)
}
},
async trustSender() {
const type = this.checkType()
try {
await trustSender(
this.sender,
type,
true,
).then(async () => {
this.list = await fetchTrustedSenders()
this.sender = ''
this.openModal = false
})
} catch (error) {
logger.error(`Could not trust sender ${this.sender}`, {
error,
})
showError(t('mail', 'Could not trust sender {sender}', {
sende: this.sender,
}))
}
},
checkType() {
const parts = this.sender.split('@')
if (parts.length !== 2) {
return 'domain'
}
// remove '@'' from domain if added by mistake
if (parts[0].length === 0) {
this.sender = parts[1]
return 'domain'
}
return 'individual'
},
senderType(type) {
switch (type) {
case 'individual':
Expand All @@ -104,4 +165,10 @@ export default {
.button-vue:deep() {
display: inline-block !important;
}
</style>
.content{
margin: 50px;
}
.input{
margin-bottom: 10px;
}
</style>

Check failure on line 174 in src/components/TrustedSenders.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Newline required at end of file but not found

0 comments on commit 7111b87

Please sign in to comment.