From 55f5a827a7abc2f248c2405e1977222062b88e6b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 23 Mar 2018 17:07:49 +0100 Subject: Cookie filter - Requires Qt 5.11 (now required) - New settings: filter.cookies: block.all, block.thirdParty, path --- src/webengine/cookiefilter.cpp | 30 +++++ src/webengine/cookiefilter.h | 30 +++++ src/webengine/cookieinterceptor.cpp | 34 ------ src/webengine/cookieinterceptor.h | 32 ----- src/webengine/webengineprofile.cpp | 232 ++++++++++++++++-------------------- src/webengine/webengineprofile.h | 27 ++--- 6 files changed, 169 insertions(+), 216 deletions(-) create mode 100644 src/webengine/cookiefilter.cpp create mode 100644 src/webengine/cookiefilter.h delete mode 100644 src/webengine/cookieinterceptor.cpp delete mode 100644 src/webengine/cookieinterceptor.h (limited to 'src/webengine') diff --git a/src/webengine/cookiefilter.cpp b/src/webengine/cookiefilter.cpp new file mode 100644 index 0000000..08938b1 --- /dev/null +++ b/src/webengine/cookiefilter.cpp @@ -0,0 +1,30 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "cookiefilter.h" + +CookieFilter::CookieFilter(bool shouldBlockAll, bool shouldBlockThirdParty, QObject *parent) + : QObject(parent) +{ + blockAll = shouldBlockAll; + blockThirdParty = shouldBlockThirdParty; +} + +bool CookieFilter::shouldBlock(const QWebEngineCookieStore::FilterRequest &filter) +{ + // block all check + if(blockAll) + return true; + + // block third-party cookies check + if(blockThirdParty && filter.thirdParty) + return true; + + // otherwise, don't block + return false; +} diff --git a/src/webengine/cookiefilter.h b/src/webengine/cookiefilter.h new file mode 100644 index 0000000..b537c57 --- /dev/null +++ b/src/webengine/cookiefilter.h @@ -0,0 +1,30 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_COOKIEFILTER_H +#define SMOLBOTE_COOKIEFILTER_H + +#include +#include + +class CookieFilter : public QObject +{ + Q_OBJECT + +public: + explicit CookieFilter(bool shouldBlockAll, bool shouldBlockThirdParty, QObject *parent = nullptr); + +public: + bool shouldBlock(const QWebEngineCookieStore::FilterRequest &filter); + +private: + bool blockAll = false; + bool blockThirdParty = false; +}; + +#endif //SMOLBOTE_COOKIEFILTER_H diff --git a/src/webengine/cookieinterceptor.cpp b/src/webengine/cookieinterceptor.cpp deleted file mode 100644 index e1a7d6b..0000000 --- a/src/webengine/cookieinterceptor.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "cookieinterceptor.h" -#include -#include - -CookieInterceptor::CookieInterceptor(const QString &path, QObject *parent) - : QObject(parent) -{ -} - -void CookieInterceptor::judgeCookie(const QNetworkCookie &cookie) { - QWebEngineCookieStore *store = dynamic_cast(sender()); - Q_CHECK_PTR(store); - - qDebug("Added cookie %s::%s", qUtf8Printable(cookie.domain()), qUtf8Printable(cookie.name())); - - // A session cookie is a cookie which has no expiration date, which means it should be discarded when - // the application's concept of session is over (usually, when the application exits) - if(!cookie.isSessionCookie()) { - QNetworkCookie copyCookie(cookie); - copyCookie.setExpirationDate(QDateTime::fromString("")); - qDebug("cookie is now session?: %s", copyCookie.isSessionCookie() ? "yes" : "no"); - - store->deleteCookie(cookie, cookie.domain()); - store->setCookie(copyCookie, copyCookie.domain()); - } -} diff --git a/src/webengine/cookieinterceptor.h b/src/webengine/cookieinterceptor.h deleted file mode 100644 index da21f04..0000000 --- a/src/webengine/cookieinterceptor.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef COOKIEINTERCEPTOR_H -#define COOKIEINTERCEPTOR_H - -#include -#include - -class CookieInterceptor : public QObject -{ - Q_OBJECT - -public: - struct CookieRule { - bool isBlocking; - }; - - explicit CookieInterceptor(const QString &path, QObject *parent = nullptr); - -public slots: - void judgeCookie(const QNetworkCookie &cookie); - -private: -}; - -#endif //COOKIEINTERCEPTOR_H diff --git a/src/webengine/webengineprofile.cpp b/src/webengine/webengineprofile.cpp index e8aa0ae..263dcb5 100644 --- a/src/webengine/webengineprofile.cpp +++ b/src/webengine/webengineprofile.cpp @@ -24,7 +24,7 @@ WebEngineProfile::WebEngineProfile(QObject *parent) // Off-the-record profiles have no persistent path } -WebEngineProfile::WebEngineProfile(const QString &name, const QString &path, QObject *parent) +WebEngineProfile::WebEngineProfile(const QString &name, QObject *parent) : QWebEngineProfile(name, parent) { m_name = name; @@ -32,17 +32,9 @@ WebEngineProfile::WebEngineProfile(const QString &name, const QString &path, QOb #ifdef QT_DEBUG qDebug("Creating profile %s", qUtf8Printable(m_name)); #endif - - setPersistentStoragePath(path + "/storage"); - setCachePath(path + "/cache"); } -WebEngineProfile::~WebEngineProfile() -{ - if(shouldSaveProfile) { - saveProfile(); - } -} +WebEngineProfile::~WebEngineProfile() = default; QString WebEngineProfile::name() const { @@ -54,22 +46,112 @@ QUrl WebEngineProfile::homepage() const return m_homepage; } -void WebEngineProfile::setHomepage(const QUrl &url) -{ - m_homepage = url; -} - QUrl WebEngineProfile::newtab() const { return m_newtab; } -void WebEngineProfile::setNewtab(const QUrl &url) +void WebEngineProfile::loadProfile(const QString &path) { - m_newtab = url; + m_configPath = path; + + // check if config file exists + if(!QFileInfo::exists(m_configPath)) { +#ifdef QT_DEBUG + qDebug("No config for profile '%s': %s", qUtf8Printable(m_name), qUtf8Printable(m_configPath)); +#endif + return; + } + +#ifdef QT_DEBUG + qDebug("Reading config for profile '%s': %s", qUtf8Printable(m_name), qUtf8Printable(m_configPath)); +#endif + QSettings config(m_configPath, QSettings::IniFormat); + + m_homepage = config.value("homepage", m_homepage).toUrl(); + m_newtab = config.value("newtab", m_newtab).toUrl(); + + config.beginGroup("http"); + setHttpUserAgent(config.value("userAgent", httpUserAgent()).toString()); + setHttpAcceptLanguage(config.value("accept-lang", httpAcceptLanguage()).toString()); + { + QString cacheType = config.value("cacheType").toString(); + if(cacheType == "memory") { + setHttpCacheType(QWebEngineProfile::MemoryHttpCache); + } else if(cacheType == "disk") { + setHttpCacheType(QWebEngineProfile::DiskHttpCache); + } else if(cacheType == "disabled") { + setHttpCacheType(QWebEngineProfile::NoCache); + } + } + setHttpCacheMaximumSize(config.value("cacheSize").toInt()); + config.endGroup(); // http + + config.beginGroup("policy"); + { + QString cookies = config.value("cookies").toString(); + if(cookies == "disabled") { + setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies); + } else if(cookies == "allow") { + setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies); + } else if(cookies == "force") { + setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies); + } + } + config.endGroup(); // policy + + config.beginGroup("attributes"); + QWebEngineSettings *s = settings(); + s->setAttribute(QWebEngineSettings::AutoLoadImages, + config.value("autoLoadImages", s->testAttribute(QWebEngineSettings::AutoLoadImages)).toBool()); + s->setAttribute(QWebEngineSettings::JavascriptEnabled, + config.value("javascriptEnabled", s->testAttribute(QWebEngineSettings::JavascriptEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, + config.value("javascriptCanOpenWindows", s->testAttribute(QWebEngineSettings::JavascriptCanOpenWindows)).toBool()); + s->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, + config.value("javascriptCanAccessClipboard", s->testAttribute(QWebEngineSettings::JavascriptCanAccessClipboard)).toBool()); + s->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, + config.value("linksIncludedInFocusChain", s->testAttribute(QWebEngineSettings::LinksIncludedInFocusChain)).toBool()); + s->setAttribute(QWebEngineSettings::LocalStorageEnabled, + config.value("localStorageEnabled", s->testAttribute(QWebEngineSettings::LocalStorageEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, + config.value("localContentCanAccessRemoteUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls)).toBool()); + s->setAttribute(QWebEngineSettings::XSSAuditingEnabled, + config.value("xssAuditingEnabled", s->testAttribute(QWebEngineSettings::XSSAuditingEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, + config.value("spatialNavigationEnabled", s->testAttribute(QWebEngineSettings::SpatialNavigationEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, + config.value("localContentCanAccessFileUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls)).toBool()); + s->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled, + config.value("hyperlinkAuditingEnabled", s->testAttribute(QWebEngineSettings::HyperlinkAuditingEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, + config.value("scrollAnimatorEnabled", s->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::ErrorPageEnabled, + config.value("errorPageEnabled", s->testAttribute(QWebEngineSettings::ErrorPageEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::PluginsEnabled, + config.value("pluginsEnabled", s->testAttribute(QWebEngineSettings::PluginsEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, + config.value("fullscreenSupportEnabled", s->testAttribute(QWebEngineSettings::FullScreenSupportEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, + config.value("screenCaptureEnabled", s->testAttribute(QWebEngineSettings::ScreenCaptureEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::WebGLEnabled, + config.value("webglEnabled", s->testAttribute(QWebEngineSettings::WebGLEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled, + config.value("accelerated2dCanvasEnabled", s->testAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, + config.value("autoLoadIconsForPage", s->testAttribute(QWebEngineSettings::AutoLoadIconsForPage)).toBool()); + s->setAttribute(QWebEngineSettings::TouchIconsEnabled, + config.value("touchIconsEnabled", s->testAttribute(QWebEngineSettings::TouchIconsEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, + config.value("focusOnNavigationEnabled", s->testAttribute(QWebEngineSettings::FocusOnNavigationEnabled)).toBool()); + s->setAttribute(QWebEngineSettings::PrintElementBackgrounds, + config.value("printElementBackgrounds", s->testAttribute(QWebEngineSettings::PrintElementBackgrounds)).toBool()); + s->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, + config.value("allowRunningInsecureContent", s->testAttribute(QWebEngineSettings::AllowRunningInsecureContent)).toBool()); + config.endGroup(); // attributes } -void WebEngineProfile::saveProfile() +void WebEngineProfile::saveProfile(const QString &path) { QSettings config(persistentStoragePath() + "/profile.ini", QSettings::IniFormat); @@ -138,117 +220,3 @@ void WebEngineProfile::saveProfile() config.sync(); } - -void WebEngineProfile::setCookieInterceptor(CookieInterceptor *interceptor) -{ - connect(cookieStore(), &QWebEngineCookieStore::cookieAdded, interceptor, &CookieInterceptor::judgeCookie); - cookieStore()->loadAllCookies(); -} - -WebEngineProfile *createProfile(const QString &name, const QString &path, QObject *parent) -{ - WebEngineProfile *profile; - if(name.isEmpty()) { - profile = new WebEngineProfile(parent); - } else { - profile = new WebEngineProfile(name, path, parent); - } - - // Read profile settings - const QString profileIniPath = path + "/profile.ini"; - if(QFileInfo::exists(profileIniPath)) { - qDebug("Reading profile from [%s]", qUtf8Printable(profileIniPath)); - QSettings config(profileIniPath, QSettings::IniFormat); - - profile->setHomepage(config.value("homepage", profile->homepage()).toUrl()); - profile->setNewtab(config.value("newtab", profile->newtab()).toUrl()); - - config.beginGroup("http"); - profile->setHttpUserAgent(config.value("userAgent").toString()); - profile->setHttpAcceptLanguage(config.value("accept-lang").toString()); - { - QString cacheType = config.value("cacheType").toString(); - if(cacheType == "memory") { - profile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache); - } else if(cacheType == "disk") { - profile->setHttpCacheType(QWebEngineProfile::DiskHttpCache); - } else if(cacheType == "disabled") { - profile->setHttpCacheType(QWebEngineProfile::NoCache); - } - } - profile->setHttpCacheMaximumSize(config.value("cacheSize").toInt()); - config.endGroup(); // http - - config.beginGroup("policy"); - { - QString cookies = config.value("cookies").toString(); - if(cookies == "disabled") { - profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies); - } else if(cookies == "allow") { - profile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies); - } else if(cookies == "force") { - profile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies); - } - } - config.endGroup(); // policy - - config.beginGroup("attributes"); - QWebEngineSettings *s = profile->settings(); - s->setAttribute(QWebEngineSettings::AutoLoadImages, - config.value("autoLoadImages", s->testAttribute(QWebEngineSettings::AutoLoadImages)).toBool()); - s->setAttribute(QWebEngineSettings::JavascriptEnabled, - config.value("javascriptEnabled", s->testAttribute(QWebEngineSettings::JavascriptEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, - config.value("javascriptCanOpenWindows", s->testAttribute(QWebEngineSettings::JavascriptCanOpenWindows)).toBool()); - s->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, - config.value("javascriptCanAccessClipboard", s->testAttribute(QWebEngineSettings::JavascriptCanAccessClipboard)).toBool()); - s->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, - config.value("linksIncludedInFocusChain", s->testAttribute(QWebEngineSettings::LinksIncludedInFocusChain)).toBool()); - s->setAttribute(QWebEngineSettings::LocalStorageEnabled, - config.value("localStorageEnabled", s->testAttribute(QWebEngineSettings::LocalStorageEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, - config.value("localContentCanAccessRemoteUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls)).toBool()); - s->setAttribute(QWebEngineSettings::XSSAuditingEnabled, - config.value("xssAuditingEnabled", s->testAttribute(QWebEngineSettings::XSSAuditingEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, - config.value("spatialNavigationEnabled", s->testAttribute(QWebEngineSettings::SpatialNavigationEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, - config.value("localContentCanAccessFileUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls)).toBool()); - s->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled, - config.value("hyperlinkAuditingEnabled", s->testAttribute(QWebEngineSettings::HyperlinkAuditingEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, - config.value("scrollAnimatorEnabled", s->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::ErrorPageEnabled, - config.value("errorPageEnabled", s->testAttribute(QWebEngineSettings::ErrorPageEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::PluginsEnabled, - config.value("pluginsEnabled", s->testAttribute(QWebEngineSettings::PluginsEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, - config.value("fullscreenSupportEnabled", s->testAttribute(QWebEngineSettings::FullScreenSupportEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, - config.value("screenCaptureEnabled", s->testAttribute(QWebEngineSettings::ScreenCaptureEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::WebGLEnabled, - config.value("webglEnabled", s->testAttribute(QWebEngineSettings::WebGLEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled, - config.value("accelerated2dCanvasEnabled", s->testAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, - config.value("autoLoadIconsForPage", s->testAttribute(QWebEngineSettings::AutoLoadIconsForPage)).toBool()); - s->setAttribute(QWebEngineSettings::TouchIconsEnabled, - config.value("touchIconsEnabled", s->testAttribute(QWebEngineSettings::TouchIconsEnabled)).toBool()); -#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - s->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, - config.value("focusOnNavigationEnabled", s->testAttribute(QWebEngineSettings::FocusOnNavigationEnabled)).toBool()); - s->setAttribute(QWebEngineSettings::PrintElementBackgrounds, - config.value("printElementBackgrounds", s->testAttribute(QWebEngineSettings::PrintElementBackgrounds)).toBool()); - s->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, - config.value("allowRunningInsecureContent", s->testAttribute(QWebEngineSettings::AllowRunningInsecureContent)).toBool()); -#endif - config.endGroup(); // attributes - } -#ifdef QT_DEBUG - else { - qDebug("No config for profile %s: %s", qUtf8Printable(name), qUtf8Printable(profileIniPath)); - } -#endif - - return profile; -} diff --git a/src/webengine/webengineprofile.h b/src/webengine/webengineprofile.h index d93e079..6a0d4e2 100644 --- a/src/webengine/webengineprofile.h +++ b/src/webengine/webengineprofile.h @@ -6,44 +6,35 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef WEBENGINEPROFILE_H -#define WEBENGINEPROFILE_H +#ifndef SMOLBOTE_WEBENGINEPROFILE_H +#define SMOLBOTE_WEBENGINEPROFILE_H #include #include -#include "cookieinterceptor.h" +#include "cookiefilter.h" class WebEngineProfile : public QWebEngineProfile { Q_OBJECT public: explicit WebEngineProfile(QObject *parent = nullptr); - explicit WebEngineProfile(const QString &name, const QString &path, QObject *parent = nullptr); + explicit WebEngineProfile(const QString &name, QObject *parent = nullptr); - ~WebEngineProfile(); + ~WebEngineProfile() override; QString name() const; - QUrl homepage() const; - void setHomepage(const QUrl &url); - QUrl newtab() const; - void setNewtab(const QUrl &url); - - void setCookieInterceptor(CookieInterceptor *interceptor); - -signals: public slots: - void saveProfile(); + void loadProfile(const QString &path); + void saveProfile(const QString &path = QString()); private: + QString m_configPath; QString m_name; - bool shouldSaveProfile = false; QUrl m_homepage = QUrl("about:blank"); QUrl m_newtab = QUrl("about:blank"); }; -[[nodiscard]] WebEngineProfile *createProfile(const QString &name, const QString &path, QObject *parent); - -#endif // WEBENGINEPROFILE_H +#endif // SMOLBOTE_WEBENGINEPROFILE_H -- cgit v1.2.1