Messagelib

hashcachemanager.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "hashcachemanager.h"
8#include "checkphishingurlutil.h"
9#include "webengineviewer_debug.h"
10#include <KConfig>
11#include <KConfigGroup>
12
13using namespace WebEngineViewer;
14
15struct HashCacheInfo {
16 HashCacheInfo() = default;
17
18 [[nodiscard]] bool isValid() const;
19 HashCacheManager::UrlStatus status = HashCacheManager::Unknown;
20 uint verifyCacheAfterThisTime = 0;
21};
22
23bool HashCacheInfo::isValid() const
24{
25 return status != HashCacheManager::Unknown;
26}
27
28class WebEngineViewer::HashCacheManagerPrivate
29{
30public:
31 HashCacheManagerPrivate()
32 {
33 load();
34 }
35
36 void clearCache();
37 void save();
38 void load();
39 void addHashStatus(const QByteArray &hash, HashCacheManager::UrlStatus status, uint cacheDuration);
40 [[nodiscard]] HashCacheManager::UrlStatus hashStatus(const QByteArray &hash);
41
42private:
43 void clear();
44
46};
47
48void HashCacheManagerPrivate::clear()
49{
50 mHashList.clear();
51}
52
53void HashCacheManagerPrivate::clearCache()
54{
55 clear();
56 save();
57}
58
59void HashCacheManagerPrivate::save()
60{
61 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
62 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("Hash"));
63
64 QList<QByteArray> lstMalware;
65 QList<double> lstMalwareDuration;
66
68 QList<double> lstOkDuration;
69
71 while (i.hasNext()) {
72 i.next();
73 switch (i.value().status) {
74 case HashCacheManager::UrlOk:
75 lstOk << i.key();
76 lstOkDuration << i.value().verifyCacheAfterThisTime;
77 break;
78 case HashCacheManager::MalWare:
79 lstMalware << i.key();
80 lstMalwareDuration << i.value().verifyCacheAfterThisTime;
81 break;
82 case HashCacheManager::Unknown:
83 break;
84 }
85 }
86 grp.writeEntry("malware", lstMalware);
87 grp.writeEntry("malwareCacheDuration", lstMalwareDuration);
88
89 grp.writeEntry("safe", lstOk);
90 grp.writeEntry("safeCacheDuration", lstOkDuration);
91 grp.sync();
92}
93
94void HashCacheManagerPrivate::load()
95{
96 clear();
97 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
98 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("Hash"));
99 QList<QByteArray> lstMalware = grp.readEntry("malware", QList<QByteArray>());
100 QList<double> lstMalwareDuration = grp.readEntry("malwareCacheDuration", QList<double>());
101
102 QList<QByteArray> lstOk = grp.readEntry("safe", QList<QByteArray>());
103 QList<double> lstOkDuration = grp.readEntry("safeCacheDuration", QList<double>());
104 if (lstMalware.count() != lstMalwareDuration.count()) {
105 qCWarning(WEBENGINEVIEWER_LOG) << "unsafe url: HashCacheManagerPrivate invalid number of data stored";
106 return;
107 }
108 if (lstOk.count() != lstOkDuration.count()) {
109 qCWarning(WEBENGINEVIEWER_LOG) << "safe url: HashCacheManagerPrivate invalid number of data stored";
110 return;
111 }
112
113 const int nb(lstOk.count());
114 for (int i = 0; i < nb; ++i) {
115 HashCacheInfo info;
116 info.status = HashCacheManager::UrlOk;
117 info.verifyCacheAfterThisTime = lstOkDuration.at(i);
118 if (info.isValid()) {
119 mHashList.insert(lstOk.at(i), info);
120 }
121 }
122
123 const int nb2(lstMalware.count());
124 for (int i = 0; i < nb2; ++i) {
125 HashCacheInfo info;
126 info.status = HashCacheManager::MalWare;
127 info.verifyCacheAfterThisTime = lstMalwareDuration.at(i);
128 if (info.isValid()) {
129 mHashList.insert(lstMalware.at(i), info);
130 }
131 }
132}
133
134HashCacheManager::UrlStatus HashCacheManagerPrivate::hashStatus(const QByteArray &hash)
135{
136 const HashCacheInfo info = mHashList.value(hash, HashCacheInfo());
137 if (info.isValid()) {
138 if (CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
139 return info.status;
140 } else {
141 return HashCacheManager::Unknown;
142 }
143 } else {
144 return HashCacheManager::Unknown;
145 }
146}
147
148void HashCacheManagerPrivate::addHashStatus(const QByteArray &hash, HashCacheManager::UrlStatus status, uint cacheDuration)
149{
150 HashCacheInfo info;
151 info.status = status;
152 info.verifyCacheAfterThisTime = cacheDuration;
153 switch (status) {
154 case HashCacheManager::UrlOk:
155 mHashList.insert(hash, info);
156 break;
157 case HashCacheManager::MalWare:
158 mHashList.insert(hash, info);
159 break;
160 case HashCacheManager::Unknown:
161 qCWarning(WEBENGINEVIEWER_LOG()) << "HashCacheManagerPrivate::addHashStatus unknown status detected!";
162 return;
163 }
164 save();
165}
166
167HashCacheManager *HashCacheManager::self()
168{
169 static HashCacheManager s_self;
170 return &s_self;
171}
172
173HashCacheManager::HashCacheManager(QObject *parent)
174 : QObject(parent)
175 , d(new HashCacheManagerPrivate)
176{
177}
178
179HashCacheManager::~HashCacheManager() = default;
180
181void HashCacheManager::clearCache()
182{
183 d->clearCache();
184}
185
186void HashCacheManager::addHashStatus(const QByteArray &hash, HashCacheManager::UrlStatus status, uint cacheDuration)
187{
188 d->addHashStatus(hash, status, cacheDuration);
189}
190
191HashCacheManager::UrlStatus HashCacheManager::hashStatus(const QByteArray &hash)
192{
193 return d->hashStatus(hash);
194}
195
196#include "moc_hashcachemanager.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
bool sync() override
The HashCacheManager class.
Q_SCRIPTABLE CaptureState status()
const_reference at(qsizetype i) const const
qsizetype count() const const
T value(qsizetype i) const const
void clear()
iterator insert(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.