Commit dc848949 authored by Laurent Montel's avatar Laurent Montel 😁

Make scamdetection works

parent 3a101a38
......@@ -20,10 +20,12 @@
#include "scamcheckshorturlmanager.h"
#include "scamdetectiondetailsdialog.h"
#include "settings/messageviewersettings.h"
#include "MessageViewer/ScamCheckShortUrl"
#include "webengine/webenginescript.h"
#include <KLocalizedString>
#include <QRegularExpression>
#include <QPointer>
#include <QWebEnginePage>
......@@ -93,46 +95,15 @@ void ScamDetectionWebEngine::handleScanPage(const QVariant &result)
bool foundScam = false;
d->mDetails.clear();
d->mDetails = QLatin1String("<b>") + i18n("Details:") + QLatin1String("</b><ul>");
qDebug()<<" void ScamDetectionWebEngine::handleScanPage(const QVariant &result)"<< result;
//TODO
if (foundScam) {
Q_EMIT messageMayBeAScam();
}
}
#if 0 //TODO
void ScamDetection::scanPage(QWebFrame *frame)
{
if (MessageViewer::MessageViewerSettings::self()->scamDetectionEnabled()) {
d->mDetails.clear();
d->mDetails = QLatin1String("<b>") + i18n("Details:") + QLatin1String("</b><ul>");
bool foundScam = false;
const QWebElement rootElement = frame->documentElement();
bool result = scanFrame(rootElement, d->mDetails);
if (result) {
foundScam = true;
}
foreach (QWebFrame *childFrame, frame->childFrames()) {
result = scanFrame(childFrame->documentElement(), d->mDetails);
if (result) {
foundScam = true;
}
}
if (foundScam) {
Q_EMIT messageMayBeAScam();
}
}
}
bool ScamDetection::scanFrame(const QWebElement &rootElement, QString &details)
{
bool foundScam = false;
QRegularExpression ip4regExp(QStringLiteral("\\b[0-9]{1,3}\\.[0-9]{1,3}(?:\\.[0-9]{0,3})?(?:\\.[0-9]{0,3})?"));
const QWebElementCollection allAnchor = rootElement.findAll(QStringLiteral("a"));
Q_FOREACH (const QWebElement &anchorElement, allAnchor) {
const QList<QVariant> lst = result.toList();
Q_FOREACH(const QVariant &var, lst) {
QMap<QString, QVariant> mapVariant = var.toMap();
//qDebug()<<" mapVariant"<<mapVariant;
//1) detect if title has a url and title != href
const QString href = anchorElement.attribute(QStringLiteral("href"));
const QString title = anchorElement.attribute(QStringLiteral("title"));
const QString title = mapVariant.value(QStringLiteral("title")).toString();
const QString href = mapVariant.value(QStringLiteral("src")).toString();
const QUrl url(href);
if (!title.isEmpty()) {
if (title.startsWith(QStringLiteral("http:"))
......@@ -153,7 +124,7 @@ bool ScamDetection::scanFrame(const QWebElement &rootElement, QString &details)
}
}
if (foundScam) {
details += QLatin1String("<li>") + i18n("This email contains a link which reads as '%1' in the text, but actually points to '%2'. This is often the case in scam emails to mislead the recipient", addWarningColor(title), addWarningColor(href)) + QLatin1String("</li>");
d->mDetails += QLatin1String("<li>") + i18n("This email contains a link which reads as '%1' in the text, but actually points to '%2'. This is often the case in scam emails to mislead the recipient", addWarningColor(title), addWarningColor(href)) + QLatin1String("</li>");
}
}
}
......@@ -161,18 +132,18 @@ bool ScamDetection::scanFrame(const QWebElement &rootElement, QString &details)
//2) detect if url href has ip and not server name.
const QString hostname = url.host();
if (hostname.contains(ip4regExp) && !hostname.contains(QStringLiteral("127.0.0.1"))) { //hostname
details += QLatin1String("<li>") + i18n("This email contains a link which points to a numerical IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname)) + QLatin1String("</li>");
d->mDetails += QLatin1String("<li>") + i18n("This email contains a link which points to a numerical IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname)) + QLatin1String("</li>");
foundScam = true;
} else if (hostname.contains(QLatin1Char('%'))) { //Hexa value for ip
details += QLatin1String("<li>") + i18n("This email contains a link which points to a hexadecimal IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname)) + QLatin1String("</li>");
d->mDetails += QLatin1String("<li>") + i18n("This email contains a link which points to a hexadecimal IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname)) + QLatin1String("</li>");
foundScam = true;
} else if (url.toString().contains(QStringLiteral("url?q="))) { //4) redirect url.
details += QLatin1String("<li>") + i18n("This email contains a link (%1) which has a redirection", addWarningColor(url.toString())) + QLatin1String("</li>");
d->mDetails += QLatin1String("<li>") + i18n("This email contains a link (%1) which has a redirection", addWarningColor(url.toString())) + QLatin1String("</li>");
foundScam = true;
} else if ((url.toString().count(QStringLiteral("http://")) > 1) ||
(url.toString().count(QStringLiteral("https://")) > 1)) { //5) more that 1 http in url.
if (!url.toString().contains(QStringLiteral("kmail:showAuditLog"))) {
details += QLatin1String("<li>") + i18n("This email contains a link (%1) which contains multiple http://. This is often the case in scam emails.", addWarningColor(url.toString())) + QLatin1String("</li>");
d->mDetails += QLatin1String("<li>") + i18n("This email contains a link (%1) which contains multiple http://. This is often the case in scam emails.", addWarningColor(url.toString())) + QLatin1String("</li>");
foundScam = true;
}
}
......@@ -180,21 +151,23 @@ bool ScamDetection::scanFrame(const QWebElement &rootElement, QString &details)
//Check shortUrl
if (!foundScam) {
if (ScamCheckShortUrl::isShortUrl(url)) {
details += QLatin1String("<li>") + i18n("This email contains a shorturl (%1). It can redirect to another server.", addWarningColor(url.toString())) + QLatin1String("</li>");
d->mDetails += QLatin1String("<li>") + i18n("This email contains a shorturl (%1). It can redirect to another server.", addWarningColor(url.toString())) + QLatin1String("</li>");
foundScam = true;
}
}
}
#if 0 //FIXME
//3) has form
if (rootElement.findAll(QStringLiteral("form")).count() > 0) {
details += QLatin1String("<li></b>") + i18n("Message contains form element. This is often the case in scam emails.") + QLatin1String("</b></li>");
d->mDetails += QLatin1String("<li></b>") + i18n("Message contains form element. This is often the case in scam emails.") + QLatin1String("</b></li>");
foundScam = true;
}
details += QLatin1String("</ul>");
return foundScam;
}
#endif
d->mDetails += QLatin1String("</ul>");
if (foundScam) {
Q_EMIT messageMayBeAScam();
}
}
void ScamDetectionWebEngine::showDetails()
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment