Messagelib

hashcachemanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
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 
13 using namespace WebEngineViewer;
14 
15 struct HashCacheInfo {
16  HashCacheInfo() = default;
17 
18  Q_REQUIRED_RESULT bool isValid() const;
19  HashCacheManager::UrlStatus status = HashCacheManager::Unknown;
20  uint verifyCacheAfterThisTime = 0;
21 };
22 
23 bool HashCacheInfo::isValid() const
24 {
25  return status != HashCacheManager::Unknown;
26 }
27 
28 class WebEngineViewer::HashCacheManagerPrivate
29 {
30 public:
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  Q_REQUIRED_RESULT HashCacheManager::UrlStatus hashStatus(const QByteArray &hash);
41 
42 private:
43  void clear();
44 
46 };
47 
48 void HashCacheManagerPrivate::clear()
49 {
50  mHashList.clear();
51 }
52 
53 void HashCacheManagerPrivate::clearCache()
54 {
55  clear();
56  save();
57 }
58 
59 void 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 
67  QList<QByteArray> lstOk;
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 
94 void 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 
134 HashCacheManager::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 
148 void 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 
167 HashCacheManager *HashCacheManager::self()
168 {
169  static HashCacheManager s_self;
170  return &s_self;
171 }
172 
173 HashCacheManager::HashCacheManager(QObject *parent)
174  : QObject(parent)
175  , d(new HashCacheManagerPrivate)
176 {
177 }
178 
179 HashCacheManager::~HashCacheManager() = default;
180 
181 void HashCacheManager::clearCache()
182 {
183  d->clearCache();
184 }
185 
186 void HashCacheManager::addHashStatus(const QByteArray &hash, HashCacheManager::UrlStatus status, uint cacheDuration)
187 {
188  d->addHashStatus(hash, status, cacheDuration);
189 }
190 
191 HashCacheManager::UrlStatus HashCacheManager::hashStatus(const QByteArray &hash)
192 {
193  return d->hashStatus(hash);
194 }
bool sync() override
const T & at(int i) const const
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
void clear()
T value(int i) const const
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
int count(const T &value) const const
const QList< QKeySequence > & save()
KConfigGroup group(const QString &group)
bool isValid(QStringView ifopt)
The HashCacheManager class.
T readEntry(const QString &key, const T &aDefault) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.