Skip to content

Commit

Permalink
Fixed small bug with domain matching
Browse files Browse the repository at this point in the history
- accounts.youtube.com won't be matched by s.youtube.com anymore
- fixes ||s.youtube.com^ AdBlock rule matching anything from
  https://accounts.youtube.com
  • Loading branch information
nowrep committed Sep 1, 2012
1 parent 575b211 commit b442492
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
25 changes: 20 additions & 5 deletions src/lib/adblock/adblockrule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do
bool matched = false;

if (m_useDomainMatch) {
matched = domain.endsWith(m_matchString);
matched = _matchDomain(domain, m_matchString);
}
else if (m_useEndsMatch) {
matched = encodedUrl.endsWith(m_matchString, m_caseSensitivity);
Expand Down Expand Up @@ -270,28 +270,28 @@ bool AdBlockRule::matchDomain(const QString &domain) const

if (m_blockedDomains.isEmpty()) {
foreach(const QString & d, m_allowedDomains) {
if (domain.endsWith(d)) {
if (_matchDomain(domain, d)) {
return true;
}
}
}
else if (m_allowedDomains.isEmpty()) {
foreach(const QString & d, m_blockedDomains) {
if (domain.endsWith(d)) {
if (_matchDomain(domain, d)) {
return false;
}
}
return true;
}
else {
foreach(const QString & d, m_blockedDomains) {
if (domain.endsWith(d)) {
if (_matchDomain(domain, d)) {
return false;
}
}

foreach(const QString & d, m_allowedDomains) {
if (domain.endsWith(d)) {
if (_matchDomain(domain, d)) {
return true;
}
}
Expand Down Expand Up @@ -520,3 +520,18 @@ void AdBlockRule::parseDomains(const QString &domains, const QChar &separator)

m_domainRestricted = (!m_blockedDomains.isEmpty() || !m_allowedDomains.isEmpty());
}

bool AdBlockRule::_matchDomain(const QString &domain, const QString &filter) const
{
if (!domain.endsWith(filter)) {
return false;
}

int index = domain.indexOf(filter);

if (index == 0 || filter[0] == '.') {
return true;
}

return domain[index - 1] == '.';
}
2 changes: 2 additions & 0 deletions src/lib/adblock/adblockrule.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class AdBlockRule
void parseFilter();
void parseDomains(const QString &domains, const QChar &separator);

bool _matchDomain(const QString &domain, const QString &filter) const;

AdBlockSubscription* m_subscription;
QString m_filter;

Expand Down
21 changes: 18 additions & 3 deletions src/lib/cookies/cookiejar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@

//#define COOKIE_DEBUG

bool containsDomain(QString string, QString domain)
bool _matchDomain(const QString &domain, const QString &filter)
{
if (!domain.endsWith(filter)) {
return false;
}

int index = domain.indexOf(filter);

if (index == 0 || filter[0] == '.') {
return true;
}

return domain[index - 1] == '.';
}

bool containsDomain(QString string, const QString &domain)
{
if (string.isEmpty()) {
// Some cookies have empty domain() ... bug?
Expand All @@ -36,7 +51,7 @@ bool containsDomain(QString string, QString domain)
string = string.mid(1);
}

return domain.endsWith(string);
return _matchDomain(domain, string);
}

int listContainsDomain(const QStringList &list, const QString &domain)
Expand All @@ -46,7 +61,7 @@ int listContainsDomain(const QStringList &list, const QString &domain)
}

foreach(const QString & d, list) {
if (domain.endsWith(d)) {
if (_matchDomain(domain, d)) {
return 1;
}
}
Expand Down

0 comments on commit b442492

Please sign in to comment.