From: Soren Stoutner Date: Thu, 26 Jun 2025 18:27:28 +0000 (-0700) Subject: Block all preload requests. https://redmine.stoutner.com/issues/1195 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=1324655bd5453011c884b261382b3ceecf720add;p=PrivacyBrowserPC.git Block all preload requests. https://redmine.stoutner.com/issues/1195 --- diff --git a/src/helpers/FilterListHelper.cpp b/src/helpers/FilterListHelper.cpp index 9d8d2e2..0f81e0d 100644 --- a/src/helpers/FilterListHelper.cpp +++ b/src/helpers/FilterListHelper.cpp @@ -76,6 +76,7 @@ FilterListHelper::FilterListHelper() RESOURCE_TYPE_NAVIGATION_PRELOAD_SUB_FRAME = i18nc("Resource type preload sub frame", "Preload Sub Frame"); RESOURCE_TYPE_WEB_SOCKET = i18nc("Resource type web socket", "Web Socket"); RESOURCE_TYPE_UNKNOWN = i18nc("Resource type unknown", "Unknown"); + RESOURCE_TYPE_JSON = i18nc("Resource type JSON", "JSON"); // Populate the translated sublist strings. Translated entries cannot be public static const. MAIN_ALLOWLIST_STRING = i18nc("Main allowlist", "Main Allow List"); @@ -808,6 +809,7 @@ QString FilterListHelper::getResourceTypeString(int resourceTypeInt) const case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadMainFrame: return RESOURCE_TYPE_NAVIGATION_PRELOAD_MAIN_FRAME; case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadSubFrame: return RESOURCE_TYPE_NAVIGATION_PRELOAD_SUB_FRAME; case QWebEngineUrlRequestInfo::ResourceTypeWebSocket: return RESOURCE_TYPE_WEB_SOCKET; + case QWebEngineUrlRequestInfo::ResourceTypeJson: return RESOURCE_TYPE_JSON; default: return RESOURCE_TYPE_UNKNOWN; } } diff --git a/src/helpers/FilterListHelper.h b/src/helpers/FilterListHelper.h index 7241dea..4115ae1 100644 --- a/src/helpers/FilterListHelper.h +++ b/src/helpers/FilterListHelper.h @@ -106,6 +106,7 @@ private: QString RESOURCE_TYPE_NAVIGATION_PRELOAD_SUB_FRAME; QString RESOURCE_TYPE_WEB_SOCKET; QString RESOURCE_TYPE_UNKNOWN; + QString RESOURCE_TYPE_JSON; // The private translated sublist strings. QString MAIN_ALLOWLIST_STRING; diff --git a/src/interceptors/UrlRequestInterceptor.cpp b/src/interceptors/UrlRequestInterceptor.cpp index ccaa179..e7b7807 100644 --- a/src/interceptors/UrlRequestInterceptor.cpp +++ b/src/interceptors/UrlRequestInterceptor.cpp @@ -46,8 +46,40 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlReques requestStructPointer->urlString = urlRequestInfo.requestUrl().toString(); requestStructPointer->webPageUrlString = urlRequestInfo.firstPartyUrl().toString(); - // Check the filter lists. - bool continueProcessing = globalFilterListHelperPointer->checkFilterLists(privacyWebEngineViewPointer, urlRequestInfo, requestStructPointer); + // Create a continue processing flag. + bool continueProcessing = true; + + // Block certain resource types. + switch (urlRequestInfo.resourceType()) + { + case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadMainFrame: + case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadSubFrame: + { + // Block the request. + urlRequestInfo.block(true); + + // Mark the request struct as blocked. + requestStructPointer->dispositionInt = FilterListHelper::BLOCKED; + + // Mark the preload request as blocked by the default behavior. + requestStructPointer->filterListTitle = i18nc("Default preload blocking", "Default blocking of all preload requests."); + + // Set the continue processing flag. + continueProcessing = false; + + break; + } + + default: + { + // Do nothing. + break; + } + } + + // Check the filter lists if it hasn't already been handled. + if (continueProcessing) + continueProcessing = globalFilterListHelperPointer->checkFilterLists(privacyWebEngineViewPointer, urlRequestInfo, requestStructPointer); // Further process the request if it hasn't already been handled. if (continueProcessing) { @@ -66,6 +98,9 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlReques // Mark the ping as blocked by the default behavior. requestStructPointer->filterListTitle = i18nc("Default HTTP ping blocking", "Default blocking of all HTTP ping requests."); + // Set the continue processing flag. + continueProcessing = false; + // Display the HTTP Ping blocked dialog. Q_EMIT displayHttpPingDialog(urlRequestInfo.requestUrl().toString()); @@ -80,57 +115,62 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlReques } } - // Handle the request according to the navigation type. - switch (urlRequestInfo.navigationType()) + // Handle the request according to the navigation type if it hasn't already been handled. + if (continueProcessing) { - case QWebEngineUrlRequestInfo::NavigationTypeLink: - case QWebEngineUrlRequestInfo::NavigationTypeTyped: - case QWebEngineUrlRequestInfo::NavigationTypeFormSubmitted: - case QWebEngineUrlRequestInfo::NavigationTypeRedirect: - case QWebEngineUrlRequestInfo::NavigationTypeOther: + switch (urlRequestInfo.navigationType()) { - // Only check the hosts if the main URL is changing. - if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) + case QWebEngineUrlRequestInfo::NavigationTypeLink: + case QWebEngineUrlRequestInfo::NavigationTypeTyped: + case QWebEngineUrlRequestInfo::NavigationTypeFormSubmitted: + case QWebEngineUrlRequestInfo::NavigationTypeRedirect: + case QWebEngineUrlRequestInfo::NavigationTypeOther: { - // Get the request URL. - QUrl requestUrl = urlRequestInfo.requestUrl(); - - // Reapply the domain settings if the host is changing. - if (privacyWebEngineViewPointer->currentHost != requestUrl.host()) + // Only check the hosts if the main URL is changing. + if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { - // Apply domain settings, after which the request will be loaded. `false` indicates that history is not being navigated. - Q_EMIT applyDomainSettings(requestUrl, false); - - // Block this copy of the request. - urlRequestInfo.block(true); + // Get the request URL. + QUrl requestUrl = urlRequestInfo.requestUrl(); + + // Reapply the domain settings if the host is changing. + if (privacyWebEngineViewPointer->currentHost != requestUrl.host()) + { + // Apply domain settings, after which the request will be loaded. `false` indicates that history is not being navigated. + Q_EMIT applyDomainSettings(requestUrl, false); + + // Block this copy of the request. + urlRequestInfo.block(true); + } } - } - break; - } + break; + } - case QWebEngineUrlRequestInfo::NavigationTypeBackForward: - { - // Only check the hosts if the main URL is changing. - if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) + case QWebEngineUrlRequestInfo::NavigationTypeBackForward: { - // Get the request URL. - QUrl requestUrl = urlRequestInfo.requestUrl(); - - // Reapply the domain settings if the host is changing. - if (privacyWebEngineViewPointer->currentHost != requestUrl.host()) + // Only check the hosts if the main URL is changing. + if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { - // Apply domain settings, after which the request will be loaded. `true` indicates that history is being navigated. - Q_EMIT applyDomainSettings(requestUrl, true); + // Get the request URL. + QUrl requestUrl = urlRequestInfo.requestUrl(); + + // Reapply the domain settings if the host is changing. + if (privacyWebEngineViewPointer->currentHost != requestUrl.host()) + { + // Apply domain settings, after which the request will be loaded. `true` indicates that history is being navigated. + Q_EMIT applyDomainSettings(requestUrl, true); + } } + + break; } - break; + default: + { + // Do nothing. + break; + } } - - default: - // Do nothing. - break; } // Send the request struct to the privacy WebEngine view.