From 8c846b889a7c4e7fcf75f41c5242f3c9bcdecc7b Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sun, 14 Jul 2024 11:27:16 +0200 Subject: [PATCH] Fix cookies filtering with SiteSettings Signed-off-by: Juraj Oravec --- src/lib/cookies/cookiejar.cpp | 17 ++++++++++-- src/lib/other/sitesettingsmanager.cpp | 39 +++++++++++++++++++++++++++ src/lib/other/sitesettingsmanager.h | 3 +++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/lib/cookies/cookiejar.cpp b/src/lib/cookies/cookiejar.cpp index 055e649dd..8c507d639 100644 --- a/src/lib/cookies/cookiejar.cpp +++ b/src/lib/cookies/cookiejar.cpp @@ -29,7 +29,7 @@ #include #include -//#define COOKIE_DEBUG +// #define COOKIE_DEBUG CookieJar::CookieJar(QObject* parent) : QObject(parent) @@ -182,7 +182,20 @@ bool CookieJar::rejectCookie(const QString &domain, const QNetworkCookie &cookie { Q_UNUSED(domain) - auto result = mApp->siteSettingsManager()->getPermission(SiteSettingsManager::poAllowCookies, cookieDomain); + SiteSettingsManager::Permission result = SiteSettingsManager::Default; + auto results = mApp->siteSettingsManager()->getPermissionsLike(SiteSettingsManager::poAllowCookies, cookieDomain); + for (auto it = results.begin(); it != results.end(); ++it) { + QString host = QUrl(it.key()).host(); + + if (matchDomain(cookieDomain, host)) { + result = it.value(); +#ifdef COOKIE_DEBUG + qDebug() << "Cookie domain" << cookieDomain << "matched to" << it.key(); +#endif + break; + } + } + if (result == SiteSettingsManager::Default) { result = mApp->siteSettingsManager()->getDefaultPermission(SiteSettingsManager::poAllowCookies); } diff --git a/src/lib/other/sitesettingsmanager.cpp b/src/lib/other/sitesettingsmanager.cpp index e324c84ef..b1d132d85 100644 --- a/src/lib/other/sitesettingsmanager.cpp +++ b/src/lib/other/sitesettingsmanager.cpp @@ -226,6 +226,45 @@ SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEng return getPermission(webAttributeToSqlColumn(attribute), adjustUrl(url)); } +QMap SiteSettingsManager::getPermissionsLike(const QString& column, const QString& host) +{ + QMap domainMatch; + + if (column.isEmpty()) { + domainMatch.insert(host, Deny); + return domainMatch; + } + if (host.isEmpty()) { + domainMatch.insert(host, Default); + return domainMatch; + } + + auto searchHost = host; + + if (searchHost.startsWith(QLatin1Char('.'))) { + searchHost.remove(0, 1); + } + + QSqlQuery query(SqlDatabase::instance()->database()); + query.prepare(QSL("SELECT server, %1 FROM %2 WHERE server LIKE ?").arg(column, sqlTable())); + query.addBindValue(QSL("%%%1").arg(searchHost)); + query.exec(); + + while (query.next()) { + Permission allow_option = intToPermission(query.value(column).toInt()); + QString server_option = query.value(QSL("server")).toString(); + + domainMatch.insert(server_option, allow_option); + } + + return domainMatch; +} + +QMap SiteSettingsManager::getPermissionsLike(const SiteSettingsManager::PageOptions option, const QString& host) +{ + return getPermissionsLike(optionToSqlColumn(option), host); +} + SiteSettingsManager::Permission SiteSettingsManager::getDefaultPermission(const SiteSettingsManager::PageOptions option) { switch (option) { diff --git a/src/lib/other/sitesettingsmanager.h b/src/lib/other/sitesettingsmanager.h index 6a6ebd1af..69d44b57f 100644 --- a/src/lib/other/sitesettingsmanager.h +++ b/src/lib/other/sitesettingsmanager.h @@ -92,6 +92,9 @@ public: Permission getPermission(const QWebEnginePage::Feature feature, const QUrl &url); Permission getPermission(const QWebEngineSettings::WebAttribute attribute, const QUrl &url); + QMap getPermissionsLike(const QString &column, const QString &host); + QMap getPermissionsLike(const SiteSettingsManager::PageOptions option, const QString &host); + void setOption(const QString &column, const QUrl &url, const int value); void setOption(const PageOptions option, const QUrl &url, const int value); void setOption(const QWebEnginePage::Feature &feature, const QUrl &url, const Permission value);