1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/loader/safe_browsing_resource_throttle.h"
#include <iterator>
#include <utility>
#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "components/safe_browsing/base_ui_manager.h"
#include "components/safe_browsing_db/util.h"
#include "components/safe_browsing_db/v4_feature_list.h"
#include "components/safe_browsing_db/v4_local_database_manager.h"
#include "components/security_interstitials/content/unsafe_resource.h"
#include "components/subresource_filter/content/browser/content_subresource_filter_driver_factory.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/url_request.h"
using safe_browsing::BaseUIManager;
namespace {
// Destroys the prerender contents associated with the web_contents, if any.
void DestroyPrerenderContents(
const content::ResourceRequestInfo::WebContentsGetter&
web_contents_getter) {
content::WebContents* web_contents = web_contents_getter.Run();
if (web_contents) {
prerender::PrerenderContents* prerender_contents =
prerender::PrerenderContents::FromWebContents(web_contents);
if (prerender_contents)
prerender_contents->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING);
}
}
} // namespace
// static
SafeBrowsingResourceThrottle* SafeBrowsingResourceThrottle::MaybeCreate(
net::URLRequest* request,
content::ResourceType resource_type,
safe_browsing::SafeBrowsingService* sb_service) {
if (sb_service->database_manager()->IsSupported()) {
return new SafeBrowsingResourceThrottle(request, resource_type, sb_service);
}
return nullptr;
}
SafeBrowsingResourceThrottle::SafeBrowsingResourceThrottle(
const net::URLRequest* request,
content::ResourceType resource_type,
safe_browsing::SafeBrowsingService* sb_service)
: safe_browsing::BaseResourceThrottle(
request,
resource_type,
safe_browsing::V4FeatureList::IsV4HybridEnabled()
? sb_service->v4_local_database_manager()
: sb_service->database_manager(),
sb_service->ui_manager()) {}
SafeBrowsingResourceThrottle::~SafeBrowsingResourceThrottle() {}
const char* SafeBrowsingResourceThrottle::GetNameForLogging() const {
return "SafeBrowsingResourceThrottle";
}
void SafeBrowsingResourceThrottle::MaybeDestroyPrerenderContents(
const content::ResourceRequestInfo* info) {
// Destroy the prefetch with FINAL_STATUS_SAFEBROSWING.
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&DestroyPrerenderContents,
info->GetWebContentsGetterForRequest()));
}
void SafeBrowsingResourceThrottle::StartDisplayingBlockingPageHelper(
security_interstitials::UnsafeResource resource) {
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&SafeBrowsingResourceThrottle::StartDisplayingBlockingPage,
AsWeakPtr(), ui_manager(), resource));
}
// Static
void SafeBrowsingResourceThrottle::StartDisplayingBlockingPage(
const base::WeakPtr<safe_browsing::BaseResourceThrottle>& throttle,
scoped_refptr<BaseUIManager> ui_manager,
const security_interstitials::UnsafeResource& resource) {
content::WebContents* web_contents = resource.web_contents_getter.Run();
if (web_contents) {
// Once activated, the subresource filter will filter subresources, but is
// triggered when the main frame document matches Safe Browsing blacklists.
if (!resource.is_subresource) {
using subresource_filter::ContentSubresourceFilterDriverFactory;
ContentSubresourceFilterDriverFactory* driver_factory =
ContentSubresourceFilterDriverFactory::FromWebContents(web_contents);
DCHECK(driver_factory);
// For a redirect chain of A -> B -> C, the subresource filter expects C
// as the resource URL and [A, B] as redirect URLs.
std::vector<GURL> redirect_parent_urls;
if (!resource.redirect_urls.empty()) {
redirect_parent_urls.push_back(resource.original_url);
redirect_parent_urls.insert(redirect_parent_urls.end(),
resource.redirect_urls.begin(),
std::prev(resource.redirect_urls.end()));
}
driver_factory->OnMainResourceMatchedSafeBrowsingBlacklist(
resource.url, redirect_parent_urls, resource.threat_type,
resource.threat_metadata.threat_pattern_type);
}
prerender::PrerenderContents* prerender_contents =
prerender::PrerenderContents::FromWebContents(web_contents);
if (prerender_contents) {
prerender_contents->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING);
} else {
ui_manager->DisplayBlockingPage(resource);
return;
}
}
// Tab is gone or it's being prerendered.
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&SafeBrowsingResourceThrottle::Cancel, throttle));
}
|