tutanota/test/tests/api/worker/facades/MailFacadeTest.ts

390 lines
13 KiB
TypeScript
Raw Normal View History

2023-06-29 18:26:45 +02:00
import o from "@tutao/otest"
import { MailFacade, phishingMarkerValue } from "../../../../../src/api/worker/facades/lazy/MailFacade.js"
import { createMail, createMailAddress, createReportedMailFieldMarker } from "../../../../../src/api/entities/tutanota/TypeRefs.js"
2022-12-27 15:37:40 +01:00
import { MailAuthenticationStatus, ReportedMailFieldType } from "../../../../../src/api/common/TutanotaConstants.js"
import { object } from "testdouble"
import { CryptoFacade } from "../../../../../src/api/worker/crypto/CryptoFacade.js"
import { IServiceExecutor } from "../../../../../src/api/common/ServiceRequest.js"
import { EntityClient } from "../../../../../src/api/common/EntityClient.js"
import { BlobFacade } from "../../../../../src/api/worker/facades/lazy/BlobFacade.js"
2022-12-27 15:37:40 +01:00
import { UserFacade } from "../../../../../src/api/worker/facades/UserFacade"
import { NativeFileApp } from "../../../../../src/native/common/FileApp.js"
import { LoginFacade } from "../../../../../src/api/worker/facades/LoginFacade.js"
o.spec("MailFacade test", function () {
let facade: MailFacade
let userFacade: UserFacade
2022-03-09 17:43:29 +01:00
let cryptoFacade: CryptoFacade
let serviceExecutor: IServiceExecutor
let entity: EntityClient
2022-03-16 10:14:53 +01:00
let blobFacade: BlobFacade
let fileApp: NativeFileApp
let loginFacade: LoginFacade
2022-03-09 17:43:29 +01:00
o.beforeEach(function () {
userFacade = object()
2022-03-16 10:14:53 +01:00
blobFacade = object()
2022-03-09 17:43:29 +01:00
entity = object()
cryptoFacade = object()
serviceExecutor = object()
fileApp = object()
loginFacade = object()
facade = new MailFacade(userFacade, entity, cryptoFacade, serviceExecutor, blobFacade, fileApp, loginFacade)
})
o.spec("checkMailForPhishing", function () {
o("not phishing if no markers", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
address: "test@example.com",
2022-12-27 15:37:40 +01:00
}),
})
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(false)
})
o("not phishing if no matching markers", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test 2"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN, "example2.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(false)
})
o("not phishing if only from domain matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test 2"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(false)
})
o("not phishing if only subject matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN, "example2.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(false)
})
o("is phishing if subject and sender domain matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("is phishing if subject with whitespaces and sender domain matches", async function () {
const mail = createMail({
subject: "\tTest spaces \n",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Testspaces"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("is not phishing if subject and sender domain matches but not authenticated", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.SOFT_FAIL,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(false)
})
o("is phishing if subject and sender address matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_ADDRESS, "test@example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("is not phishing if subject and sender address matches but not authenticated", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.SOFT_FAIL,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_ADDRESS, "test@example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(false)
})
o("is phishing if subject and non auth sender domain matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.SOFT_FAIL,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_DOMAIN_NON_AUTH, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("is phishing if subject and non auth sender address matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.SOFT_FAIL,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.FROM_ADDRESS_NON_AUTH, "test@example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("is phishing if subject and link matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.LINK, "https://example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("is not phishing if just two links match", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.LINK, "https://example.com"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.LINK, "https://example2.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(
await facade.checkMailForPhishing(mail, [
{ href: "https://example.com", innerHTML: "link1" },
{ href: "https://example2.com", innerHTML: "link2" },
]),
).equals(false)
})
o("is phishing if subject and link domain matches", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.LINK_DOMAIN, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "link" }])).equals(true)
})
o("does not throw on invalid link", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
createReportedMailFieldMarker({
2022-12-27 15:37:40 +01:00
marker: phishingMarkerValue(ReportedMailFieldType.LINK_DOMAIN, "example.com"),
}),
])
2022-12-27 15:37:40 +01:00
o(
await facade.checkMailForPhishing(mail, [
{ href: "/example1", innerHTML: "link1" },
{ href: "example2", innerHTML: "link2" },
{ href: "http:/", innerHTML: "link3" },
]),
).equals(false)
})
o("is phishing if subject and suspicious link", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "https://evil-domain.com" }])).equals(true)
})
o("link is not suspicious if on the same domain", async function () {
const mail = createMail({
subject: "Test",
authStatus: MailAuthenticationStatus.AUTHENTICATED,
sender: createMailAddress({
name: "a",
2022-12-27 15:37:40 +01:00
address: "test@example.com",
}),
})
facade.phishingMarkersUpdateReceived([
createReportedMailFieldMarker({
marker: phishingMarkerValue(ReportedMailFieldType.SUBJECT, "Test"),
}),
])
2022-12-27 15:37:40 +01:00
o(await facade.checkMailForPhishing(mail, [{ href: "https://example.com", innerHTML: "https://example.com/test" }])).equals(false)
})
})
2022-12-27 15:37:40 +01:00
})