From 40b2d03688ab14703fe8eae6f8d98e578e623995 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Sun, 8 Jul 2018 13:29:41 +0200 Subject: [PATCH] make the host_blocker aware of patterns Add an additional argument to is_blocked for the current url, and enable the host_blocking.enabled property to be set per domain. This allows to bypass host blocking for specific domains --- qutebrowser/browser/adblock.py | 5 +++-- qutebrowser/browser/webengine/interceptor.py | 3 ++- .../browser/webkit/network/networkmanager.py | 16 ++++++++-------- qutebrowser/config/configdata.yml | 1 + 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py index f42d1a1db..9eda07ce5 100644 --- a/qutebrowser/browser/adblock.py +++ b/qutebrowser/browser/adblock.py @@ -111,9 +111,10 @@ class HostBlocker: config.instance.changed.connect(self._update_files) - def is_blocked(self, url): + def is_blocked(self, url, first_party_url=None): """Check if the given URL (as QUrl) is blocked.""" - if not config.val.content.host_blocking.enabled: + if not config.instance.get('content.host_blocking.enabled', + url=first_party_url): return False host = url.host() return ((host in self._blocked_hosts or diff --git a/qutebrowser/browser/webengine/interceptor.py b/qutebrowser/browser/webengine/interceptor.py index 18386eed8..f14853cbe 100644 --- a/qutebrowser/browser/webengine/interceptor.py +++ b/qutebrowser/browser/webengine/interceptor.py @@ -69,9 +69,10 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor): resource_type, navigation_type)) url = info.requestUrl() + first_party = info.firstPartyUrl() # FIXME:qtwebengine only block ads for NavigationTypeOther? - if self._host_blocker.is_blocked(url): + if self._host_blocker.is_blocked(url, first_party): log.webview.info("Request to {} blocked by host blocker.".format( url.host())) info.block(True) diff --git a/qutebrowser/browser/webkit/network/networkmanager.py b/qutebrowser/browser/webkit/network/networkmanager.py index ad58cc984..df670cf87 100644 --- a/qutebrowser/browser/webkit/network/networkmanager.py +++ b/qutebrowser/browser/webkit/network/networkmanager.py @@ -383,14 +383,6 @@ class NetworkManager(QNetworkAccessManager): for header, value in shared.custom_headers(url=req.url()): req.setRawHeader(header, value) - host_blocker = objreg.get('host-blocker') - if host_blocker.is_blocked(req.url()): - log.webview.info("Request to {} blocked by host blocker.".format( - req.url().host())) - return networkreply.ErrorNetworkReply( - req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied, - self) - # There are some scenarios where we can't figure out current_url: # - There's a generic NetworkManager, e.g. for downloads # - The download was in a tab which is now closed. @@ -408,6 +400,14 @@ class NetworkManager(QNetworkAccessManager): # the webpage shutdown here. current_url = QUrl() + host_blocker = objreg.get('host-blocker') + if host_blocker.is_blocked(req.url(), current_url): + log.webview.info("Request to {} blocked by host blocker.".format( + req.url().host())) + return networkreply.ErrorNetworkReply( + req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied, + self) + if 'log-requests' in self._args.debug_flags: operation = debug.qenum_key(QNetworkAccessManager, op) operation = operation.replace('Operation', '').upper() diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index 2698c34b1..7f7cd332b 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -467,6 +467,7 @@ content.headers.user_agent: content.host_blocking.enabled: default: true + supports_pattern: true type: Bool desc: Enable host blocking.