diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py index f2a56d3c1..afb7a2a18 100644 --- a/qutebrowser/browser/adblock.py +++ b/qutebrowser/browser/adblock.py @@ -110,9 +110,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 4ea27adf3..516dd0899 100644 --- a/qutebrowser/browser/webengine/interceptor.py +++ b/qutebrowser/browser/webengine/interceptor.py @@ -70,21 +70,21 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor): resource_type, navigation_type)) url = info.requestUrl() - firstparty = info.firstPartyUrl() + first_party = info.firstPartyUrl() if ((url.scheme(), url.host(), url.path()) == ('qute', 'settings', '/set')): - if (firstparty != QUrl('qute://settings/') or + if (first_party != QUrl('qute://settings/') or info.resourceType() != QWebEngineUrlRequestInfo.ResourceTypeXhr): log.webview.warning("Blocking malicious request from {} to {}" - .format(firstparty.toDisplayString(), + .format(first_party.toDisplayString(), url.toDisplayString())) info.block(True) return # 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 0406f8bdf..df65b314d 100644 --- a/qutebrowser/browser/webkit/network/networkmanager.py +++ b/qutebrowser/browser/webkit/network/networkmanager.py @@ -376,14 +376,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. @@ -401,6 +393,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 a81a98604..67e850ea8 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -529,6 +529,7 @@ content.headers.user_agent: content.host_blocking.enabled: default: true + supports_pattern: true type: Bool desc: Enable host blocking.