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

Fix exception type matching

Fixes matching of type if more than one exception type is listed.
This commit is contained in:
Allan Sandfeld Jensen 2021-05-23 12:56:52 +02:00 committed by David Rosca
parent 832b40027c
commit e6450ba8f9
2 changed files with 92 additions and 156 deletions

View File

@ -225,55 +225,9 @@ bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QS
return false; return false;
} }
// Check object restrictions // Check type restrictions
if (hasOption(ObjectOption) && !matchObject(request)) { if (((m_exceptions | m_options) & TypeOptions) && !matchType(request))
return false; return false;
}
// Check subdocument restriction
if (hasOption(SubdocumentOption) && !matchSubdocument(request)) {
return false;
}
// Check xmlhttprequest restriction
if (hasOption(XMLHttpRequestOption) && !matchXmlHttpRequest(request)) {
return false;
}
// Check image restriction
if (hasOption(ImageOption) && !matchImage(request)) {
return false;
}
// Check script restriction
if (hasOption(ScriptOption) && !matchScript(request)) {
return false;
}
// Check stylesheet restriction
if (hasOption(StyleSheetOption) && !matchStyleSheet(request)) {
return false;
}
// Check object-subrequest restriction
if (hasOption(ObjectSubrequestOption) && !matchObjectSubrequest(request)) {
return false;
}
// Check ping restriction
if (hasOption(PingOption) && !matchPing(request)) {
return false;
}
// Check media restriction
if (hasOption(MediaOption) && !matchMedia(request)) {
return false;
}
// Check font restriction
if (hasOption(FontOption) && !matchFont(request)) {
return false;
}
} }
return matched; return matched;
@ -332,88 +286,60 @@ bool AdBlockRule::matchThirdParty(const QWebEngineUrlRequestInfo &request) const
return hasException(ThirdPartyOption) ? !match : match; return hasException(ThirdPartyOption) ? !match : match;
} }
bool AdBlockRule::matchObject(const QWebEngineUrlRequestInfo &request) const bool AdBlockRule::matchType(const QWebEngineUrlRequestInfo &request) const
{ {
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeObject; RuleOption type;
switch (request.resourceType()) {
return hasException(ObjectOption) ? !match : match; case QWebEngineUrlRequestInfo::ResourceTypeMainFrame:
} type = DocumentOption;
break;
bool AdBlockRule::matchSubdocument(const QWebEngineUrlRequestInfo &request) const case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
{ type = SubdocumentOption;
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSubFrame; break;
case QWebEngineUrlRequestInfo::ResourceTypeStylesheet:
return hasException(SubdocumentOption) ? !match : match; type = StyleSheetOption;
} break;
case QWebEngineUrlRequestInfo::ResourceTypeScript:
bool AdBlockRule::matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const type = ScriptOption;
{ break;
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeXhr; case QWebEngineUrlRequestInfo::ResourceTypeImage:
type = ImageOption;
return hasException(XMLHttpRequestOption) ? !match : match; break;
} case QWebEngineUrlRequestInfo::ResourceTypeFontResource:
type = FontOption;
bool AdBlockRule::matchImage(const QWebEngineUrlRequestInfo &request) const break;
{ case QWebEngineUrlRequestInfo::ResourceTypeObject:
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage; type = ObjectOption;
break;
return hasException(ImageOption) ? !match : match; case QWebEngineUrlRequestInfo::ResourceTypeMedia:
} type = MediaOption;
break;
bool AdBlockRule::matchScript(const QWebEngineUrlRequestInfo &request) const case QWebEngineUrlRequestInfo::ResourceTypeXhr:
{ type = XMLHttpRequestOption;
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeScript; break;
case QWebEngineUrlRequestInfo::ResourceTypePing:
return hasException(ScriptOption) ? !match : match; type = PingOption;
} break;
case QWebEngineUrlRequestInfo::ResourceTypePluginResource:
bool AdBlockRule::matchStyleSheet(const QWebEngineUrlRequestInfo &request) const type = ObjectSubrequestOption;
{ break;
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeStylesheet; case QWebEngineUrlRequestInfo::ResourceTypeSubResource:
case QWebEngineUrlRequestInfo::ResourceTypeWorker:
return hasException(StyleSheetOption) ? !match : match; case QWebEngineUrlRequestInfo::ResourceTypeSharedWorker:
} case QWebEngineUrlRequestInfo::ResourceTypePrefetch:
case QWebEngineUrlRequestInfo::ResourceTypeFavicon:
bool AdBlockRule::matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const case QWebEngineUrlRequestInfo::ResourceTypeServiceWorker:
{ case QWebEngineUrlRequestInfo::ResourceTypeCspReport:
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypePluginResource; case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadMainFrame:
case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadSubFrame:
return hasException(ObjectSubrequestOption) ? !match : match; case QWebEngineUrlRequestInfo::ResourceTypeUnknown:
} default:
type = OtherOption;
bool AdBlockRule::matchPing(const QWebEngineUrlRequestInfo &request) const break;
{ }
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypePing; if (!m_exceptions)
return m_options.testFlag(type);
return hasException(PingOption) ? !match : match; return !m_exceptions.testFlag(type);
}
bool AdBlockRule::matchMedia(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMedia;
return hasException(MediaOption) ? !match : match;
}
bool AdBlockRule::matchFont(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFontResource;
return hasException(FontOption) ? !match : match;
}
bool AdBlockRule::matchOther(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFontResource
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSubResource
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeWorker
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSharedWorker
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypePrefetch
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFavicon
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeServiceWorker
|| request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeUnknown;
return hasException(MediaOption) ? !match : match;
} }
void AdBlockRule::parseFilter() void AdBlockRule::parseFilter()
@ -529,6 +455,11 @@ void AdBlockRule::parseFilter()
setException(OtherOption, option.startsWith(QL1C('~'))); setException(OtherOption, option.startsWith(QL1C('~')));
++handledOptions; ++handledOptions;
} }
else if (option == QL1S("popup")) {
// doesn't do anything yet
setOption(PopupOption);
++handledOptions;
}
else if (option == QL1S("document") && m_isException) { else if (option == QL1S("document") && m_isException) {
setOption(DocumentOption); setOption(DocumentOption);
++handledOptions; ++handledOptions;

View File

@ -95,17 +95,8 @@ public:
bool matchDomain(const QString &domain) const; bool matchDomain(const QString &domain) const;
bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const; bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const;
bool matchObject(const QWebEngineUrlRequestInfo &request) const;
bool matchSubdocument(const QWebEngineUrlRequestInfo &request) const; bool matchType(const QWebEngineUrlRequestInfo &request) const;
bool matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const;
bool matchImage(const QWebEngineUrlRequestInfo &request) const;
bool matchScript(const QWebEngineUrlRequestInfo &request) const;
bool matchStyleSheet(const QWebEngineUrlRequestInfo &request) const;
bool matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const;
bool matchPing(const QWebEngineUrlRequestInfo &request) const;
bool matchMedia(const QWebEngineUrlRequestInfo &request) const;
bool matchFont(const QWebEngineUrlRequestInfo &request) const;
bool matchOther(const QWebEngineUrlRequestInfo &request) const;
protected: protected:
bool stringMatch(const QString &domain, const QString &encodedUrl) const; bool stringMatch(const QString &domain, const QString &encodedUrl) const;
@ -125,24 +116,38 @@ private:
}; };
enum RuleOption { enum RuleOption {
NoOption = 0, NoOption = 0,
DomainRestrictedOption = 1, DomainRestrictedOption = 1,
ThirdPartyOption = 2, ThirdPartyOption = 1 << 1,
ObjectOption = 4,
SubdocumentOption = 8, ObjectOption = 1 << 2,
XMLHttpRequestOption = 16, SubdocumentOption = 1 << 3,
ImageOption = 32, XMLHttpRequestOption = 1 << 4,
ScriptOption = 64, ImageOption = 1 << 5,
StyleSheetOption = 128, ScriptOption = 1 << 6,
ObjectSubrequestOption = 256, StyleSheetOption = 1 << 7,
PingOption = 512, ObjectSubrequestOption = 1 << 8,
MediaOption = 1024, PingOption = 1 << 9,
FontOption = 2048, MediaOption = 1 << 10,
OtherOption = 4096, FontOption = 1 << 11,
OtherOption = 1 << 12,
TypeOptions = ObjectOption
| SubdocumentOption
| XMLHttpRequestOption
| ImageOption
| ScriptOption
| StyleSheetOption
| ObjectSubrequestOption
| PingOption
| MediaOption
| FontOption
| OtherOption,
PopupOption = 1 << 13,
// Exception only options // Exception only options
DocumentOption = 8192, DocumentOption = 1 << 20,
ElementHideOption = 16384 ElementHideOption = 1 << 21,
}; };
Q_DECLARE_FLAGS(RuleOptions, RuleOption) Q_DECLARE_FLAGS(RuleOptions, RuleOption)