1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

AdBlockRule: Fix handling domain and third-party restrictions

Domain and third-party restrictions should be applied on page url,
not url of request.

This requires https://codereview.qt-project.org/#/c/127159/
This commit is contained in:
David Rosca 2015-10-09 13:16:24 +02:00
parent b891d92736
commit 258d78a247

View File

@ -55,7 +55,6 @@
#include <QWebEnginePage>
#include <QWebEngineUrlRequestInfo>
#if QTWEBENGINE_DISABLED
static QString toSecondLevelDomain(const QUrl &url)
{
const QString topLevelDomain = url.topLevelDomain();
@ -77,7 +76,6 @@ static QString toSecondLevelDomain(const QUrl &url)
return domain + topLevelDomain;
}
#endif
AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscription)
: m_subscription(subscription)
@ -220,16 +218,14 @@ bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QS
if (matched) {
// Check domain restrictions
if (hasOption(DomainRestrictedOption) && !matchDomain(domain)) {
if (hasOption(DomainRestrictedOption) && !matchDomain(request.firstPartyUrl().host())) {
return false;
}
#if QTWEBENGINE_DISABLED
// Check third-party restriction
if (hasOption(ThirdPartyOption) && !matchThirdParty(request)) {
return false;
}
#endif
// Check object restrictions
if (hasOption(ObjectOption) && !matchObject(request)) {
@ -314,24 +310,13 @@ bool AdBlockRule::matchDomain(const QString &domain) const
bool AdBlockRule::matchThirdParty(const QWebEngineUrlRequestInfo &request) const
{
#if QTWEBENGINE_DISABLED
const QString referer = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()).toString();
if (referer.isEmpty()) {
return false;
}
// Third-party matching should be performed on second-level domains
const QString refererHost = toSecondLevelDomain(QUrl(referer));
const QString host = toSecondLevelDomain(request.url());
const QString firstPartyHost = toSecondLevelDomain(request.firstPartyUrl());
const QString host = toSecondLevelDomain(request.requestUrl());
bool match = refererHost != host;
bool match = firstPartyHost != host;
return hasException(ThirdPartyOption) ? !match : match;
#else
Q_UNUSED(request)
return false;
#endif
}
bool AdBlockRule::matchObject(const QWebEngineUrlRequestInfo &request) const
@ -436,13 +421,11 @@ void AdBlockRule::parseFilter()
m_caseSensitivity = Qt::CaseSensitive;
++handledOptions;
}
#if QTWEBENGINE_DISABLED
else if (option.endsWith(QL1S("third-party"))) {
setOption(ThirdPartyOption);
setException(ThirdPartyOption, option.startsWith(QL1C('~')));
++handledOptions;
}
#endif
else if (option.endsWith(QL1S("object"))) {
setOption(ObjectOption);
setException(ObjectOption, option.startsWith(QL1C('~')));