summaryrefslogtreecommitdiff
path: root/chromium/components/subresource_filter/content/browser/content_subresource_filter_driver_factory.cc
blob: 7ba5c8223658a6982f4c36f09003b373a05efffa (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2016 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 "components/subresource_filter/content/browser/content_subresource_filter_driver_factory.h"

#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "base/time/time.h"
#include "components/subresource_filter/content/browser/content_activation_list_utils.h"
#include "components/subresource_filter/content/browser/subresource_filter_client.h"
#include "components/subresource_filter/content/common/subresource_filter_messages.h"
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
#include "components/subresource_filter/core/common/activation_list.h"
#include "components/subresource_filter/core/common/activation_state.h"
#include "components/subresource_filter/core/common/time_measurements.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "ipc/ipc_message_macros.h"
#include "net/base/net_errors.h"
#include "url/gurl.h"

namespace subresource_filter {

namespace {

const char kWebContentsUserDataKey[] =
    "web_contents_subresource_filter_driver_factory";

std::string DistillURLToHostAndPath(const GURL& url) {
  return url.host() + url.path();
}

// Returns true with a probability given by |performance_measurement_rate| if
// ThreadTicks is supported, otherwise returns false.
bool ShouldMeasurePerformanceForPageLoad(double performance_measurement_rate) {
  if (!base::ThreadTicks::IsSupported())
    return false;
  return performance_measurement_rate == 1 ||
         (performance_measurement_rate > 0 &&
          base::RandDouble() < performance_measurement_rate);
}

// Records histograms about the length of redirect chains, and about the pattern
// of whether each URL in the chain matched the activation list.
#define REPORT_REDIRECT_PATTERN_FOR_SUFFIX(suffix, hits_pattern, chain_size)   \
  do {                                                                         \
    UMA_HISTOGRAM_ENUMERATION(                                                 \
        "SubresourceFilter.PageLoad.RedirectChainMatchPattern." suffix,        \
        hits_pattern, 0x10);                                                   \
    UMA_HISTOGRAM_COUNTS(                                                      \
        "SubresourceFilter.PageLoad.RedirectChainLength." suffix, chain_size); \
  } while (0)

}  // namespace

// static
void ContentSubresourceFilterDriverFactory::CreateForWebContents(
    content::WebContents* web_contents,
    std::unique_ptr<SubresourceFilterClient> client) {
  if (FromWebContents(web_contents))
    return;
  web_contents->SetUserData(kWebContentsUserDataKey,
                            new ContentSubresourceFilterDriverFactory(
                                web_contents, std::move(client)));
}

// static
ContentSubresourceFilterDriverFactory*
ContentSubresourceFilterDriverFactory::FromWebContents(
    content::WebContents* web_contents) {
  return static_cast<ContentSubresourceFilterDriverFactory*>(
      web_contents->GetUserData(kWebContentsUserDataKey));
}

// static
bool ContentSubresourceFilterDriverFactory::NavigationIsPageReload(
    const GURL& url,
    const content::Referrer& referrer,
    ui::PageTransition transition) {
  return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD) ||
         // Some pages 'reload' from JavaScript by navigating to themselves.
         url == referrer.url;
}

ContentSubresourceFilterDriverFactory::ContentSubresourceFilterDriverFactory(
    content::WebContents* web_contents,
    std::unique_ptr<SubresourceFilterClient> client)
    : content::WebContentsObserver(web_contents),
      configuration_(GetActiveConfiguration()),
      client_(std::move(client)),
      throttle_manager_(
          base::MakeUnique<ContentSubresourceFilterThrottleManager>(
              this,
              client_->GetRulesetDealer(),
              web_contents)),
      activation_level_(ActivationLevel::DISABLED),
      activation_decision_(ActivationDecision::UNKNOWN),
      measure_performance_(false) {}

ContentSubresourceFilterDriverFactory::
    ~ContentSubresourceFilterDriverFactory() {}

void ContentSubresourceFilterDriverFactory::OnDocumentLoadStatistics(
    const DocumentLoadStatistics& statistics) {
  // Note: Chances of overflow are negligible.
  aggregated_document_statistics_.num_loads_total += statistics.num_loads_total;
  aggregated_document_statistics_.num_loads_evaluated +=
      statistics.num_loads_evaluated;
  aggregated_document_statistics_.num_loads_matching_rules +=
      statistics.num_loads_matching_rules;
  aggregated_document_statistics_.num_loads_disallowed +=
      statistics.num_loads_disallowed;

  aggregated_document_statistics_.evaluation_total_wall_duration +=
      statistics.evaluation_total_wall_duration;
  aggregated_document_statistics_.evaluation_total_cpu_duration +=
      statistics.evaluation_total_cpu_duration;
}

bool ContentSubresourceFilterDriverFactory::IsWhitelisted(
    const GURL& url) const {
  return whitelisted_hosts_.find(url.host()) != whitelisted_hosts_.end() ||
         client_->IsWhitelistedByContentSettings(url);
}

void ContentSubresourceFilterDriverFactory::
    OnMainResourceMatchedSafeBrowsingBlacklist(
        const GURL& url,
        const std::vector<GURL>& redirect_urls,
        safe_browsing::SBThreatType threat_type,
        safe_browsing::ThreatPatternType threat_type_metadata) {
  AddActivationListMatch(
      url, GetListForThreatTypeAndMetadata(threat_type, threat_type_metadata));
}

void ContentSubresourceFilterDriverFactory::AddHostOfURLToWhitelistSet(
    const GURL& url) {
  if (url.has_host() && url.SchemeIsHTTPOrHTTPS())
    whitelisted_hosts_.insert(url.host());
}

ContentSubresourceFilterDriverFactory::ActivationDecision
ContentSubresourceFilterDriverFactory::ComputeActivationDecisionForMainFrameURL(
    const GURL& url) const {
  if (configuration_.activation_level == ActivationLevel::DISABLED)
    return ActivationDecision::ACTIVATION_DISABLED;

  if (configuration_.activation_scope == ActivationScope::NO_SITES)
    return ActivationDecision::ACTIVATION_DISABLED;

  if (!url.SchemeIsHTTPOrHTTPS())
    return ActivationDecision::UNSUPPORTED_SCHEME;
  if (IsWhitelisted(url))
    return ActivationDecision::URL_WHITELISTED;

  switch (configuration_.activation_scope) {
    case ActivationScope::ALL_SITES:
      return ActivationDecision::ACTIVATED;
    case ActivationScope::ACTIVATION_LIST: {
      // The logic to ensure only http/https URLs are activated lives in
      // AddActivationListMatch to ensure the activation list only has relevant
      // entries.
      DCHECK(url.SchemeIsHTTPOrHTTPS() ||
             !DidURLMatchActivationList(url, configuration_.activation_list));
      bool should_activate =
          DidURLMatchActivationList(url, configuration_.activation_list);
      if (configuration_.activation_list ==
          ActivationList::PHISHING_INTERSTITIAL) {
        // Handling special case, where activation on the phishing sites also
        // mean the activation on the sites with social engineering metadata.
        should_activate |= DidURLMatchActivationList(
            url, ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL);
      }
      return should_activate ? ActivationDecision::ACTIVATED
                             : ActivationDecision::ACTIVATION_LIST_NOT_MATCHED;
    }
    default:
      return ActivationDecision::ACTIVATION_DISABLED;
  }
}

void ContentSubresourceFilterDriverFactory::OnReloadRequested() {
  UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumReloads", true);
  const GURL& whitelist_url = web_contents()->GetLastCommittedURL();

  // Only whitelist via content settings when using the experimental UI,
  // otherwise could get into a situation where content settings cannot be
  // adjusted.
  if (base::FeatureList::IsEnabled(
          subresource_filter::kSafeBrowsingSubresourceFilterExperimentalUI)) {
    client_->WhitelistByContentSettings(whitelist_url);
  } else {
    AddHostOfURLToWhitelistSet(whitelist_url);
  }
  web_contents()->GetController().Reload(content::ReloadType::NORMAL, true);
}

void ContentSubresourceFilterDriverFactory::WillProcessResponse(
    content::NavigationHandle* navigation_handle) {
  DCHECK(!navigation_handle->IsSameDocument());
  if (!navigation_handle->IsInMainFrame() ||
      navigation_handle->GetNetErrorCode() != net::OK) {
    return;
  }

  const GURL& url = navigation_handle->GetURL();
  const content::Referrer& referrer = navigation_handle->GetReferrer();
  ui::PageTransition transition = navigation_handle->GetPageTransition();

  RecordRedirectChainMatchPattern();

  if (configuration_.should_whitelist_site_on_reload &&
      NavigationIsPageReload(url, referrer, transition)) {
    // Whitelist this host for the current as well as subsequent navigations.
    AddHostOfURLToWhitelistSet(url);
  }

  activation_decision_ = ComputeActivationDecisionForMainFrameURL(url);
  DCHECK(activation_decision_ != ActivationDecision::UNKNOWN);
  if (activation_decision_ != ActivationDecision::ACTIVATED) {
    ResetActivationState();
    return;
  }

  activation_level_ = configuration_.activation_level;
  measure_performance_ = activation_level_ != ActivationLevel::DISABLED &&
                         ShouldMeasurePerformanceForPageLoad(
                             configuration_.performance_measurement_rate);
  ActivationState state = ActivationState(activation_level_);
  state.measure_performance = measure_performance_;
  throttle_manager_->NotifyPageActivationComputed(navigation_handle, state);
}

void ContentSubresourceFilterDriverFactory::OnFirstSubresourceLoadDisallowed() {
  if (configuration_.should_suppress_notifications)
    return;

  client_->ToggleNotificationVisibility(activation_level_ ==
                                        ActivationLevel::ENABLED);
}

bool ContentSubresourceFilterDriverFactory::ShouldSuppressActivation(
    content::NavigationHandle* navigation_handle) {
  // Never suppress subframe navigations.
  return navigation_handle->IsInMainFrame() &&
         IsWhitelisted(navigation_handle->GetURL());
}

void ContentSubresourceFilterDriverFactory::ResetActivationState() {
  navigation_chain_.clear();
  activation_list_matches_.clear();
  activation_level_ = ActivationLevel::DISABLED;
  measure_performance_ = false;
  aggregated_document_statistics_ = DocumentLoadStatistics();
}

void ContentSubresourceFilterDriverFactory::DidStartNavigation(
    content::NavigationHandle* navigation_handle) {
  if (navigation_handle->IsInMainFrame() &&
      !navigation_handle->IsSameDocument()) {
    activation_decision_ = ActivationDecision::UNKNOWN;
    ResetActivationState();
    navigation_chain_.push_back(navigation_handle->GetURL());
    client_->ToggleNotificationVisibility(false);
  }
}

void ContentSubresourceFilterDriverFactory::DidRedirectNavigation(
    content::NavigationHandle* navigation_handle) {
  DCHECK(!navigation_handle->IsSameDocument());
  if (navigation_handle->IsInMainFrame())
    navigation_chain_.push_back(navigation_handle->GetURL());
}

void ContentSubresourceFilterDriverFactory::DidFinishLoad(
    content::RenderFrameHost* render_frame_host,
    const GURL& validated_url) {
  if (render_frame_host->GetParent())
    return;

  if (activation_level_ != ActivationLevel::DISABLED) {
    UMA_HISTOGRAM_COUNTS_1000(
        "SubresourceFilter.PageLoad.NumSubresourceLoads.Total",
        aggregated_document_statistics_.num_loads_total);
    UMA_HISTOGRAM_COUNTS_1000(
        "SubresourceFilter.PageLoad.NumSubresourceLoads.Evaluated",
        aggregated_document_statistics_.num_loads_evaluated);
    UMA_HISTOGRAM_COUNTS_1000(
        "SubresourceFilter.PageLoad.NumSubresourceLoads.MatchedRules",
        aggregated_document_statistics_.num_loads_matching_rules);
    UMA_HISTOGRAM_COUNTS_1000(
        "SubresourceFilter.PageLoad.NumSubresourceLoads.Disallowed",
        aggregated_document_statistics_.num_loads_disallowed);
  }

  if (measure_performance_) {
    DCHECK(activation_level_ != ActivationLevel::DISABLED);
    UMA_HISTOGRAM_CUSTOM_MICRO_TIMES(
        "SubresourceFilter.PageLoad.SubresourceEvaluation.TotalWallDuration",
        aggregated_document_statistics_.evaluation_total_wall_duration,
        base::TimeDelta::FromMicroseconds(1), base::TimeDelta::FromSeconds(10),
        50);
    UMA_HISTOGRAM_CUSTOM_MICRO_TIMES(
        "SubresourceFilter.PageLoad.SubresourceEvaluation.TotalCPUDuration",
        aggregated_document_statistics_.evaluation_total_cpu_duration,
        base::TimeDelta::FromMicroseconds(1), base::TimeDelta::FromSeconds(10),
        50);
  } else {
    DCHECK(aggregated_document_statistics_.evaluation_total_wall_duration
               .is_zero());
    DCHECK(aggregated_document_statistics_.evaluation_total_cpu_duration
               .is_zero());
  }
}

bool ContentSubresourceFilterDriverFactory::OnMessageReceived(
    const IPC::Message& message,
    content::RenderFrameHost* render_frame_host) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ContentSubresourceFilterDriverFactory, message)
    IPC_MESSAGE_HANDLER(SubresourceFilterHostMsg_DocumentLoadStatistics,
                        OnDocumentLoadStatistics)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

bool ContentSubresourceFilterDriverFactory::DidURLMatchActivationList(
    const GURL& url,
    ActivationList activation_list) const {
  auto match_types =
      activation_list_matches_.find(DistillURLToHostAndPath(url));
  return match_types != activation_list_matches_.end() &&
         match_types->second.find(activation_list) != match_types->second.end();
}

void ContentSubresourceFilterDriverFactory::AddActivationListMatch(
    const GURL& url,
    ActivationList match_type) {
  if (match_type == ActivationList::NONE)
    return;
  if (url.has_host() && url.SchemeIsHTTPOrHTTPS())
    activation_list_matches_[DistillURLToHostAndPath(url)].insert(match_type);
}

int ContentSubresourceFilterDriverFactory::CalculateHitPatternForActivationList(
    ActivationList activation_list) const {
  int hits_pattern = 0;
  const int kInitialURLHitMask = 0x4;
  const int kRedirectURLHitMask = 0x2;
  const int kFinalURLHitMask = 0x1;

  if (navigation_chain_.size() > 1) {
    if (DidURLMatchActivationList(navigation_chain_.back(), activation_list))
      hits_pattern |= kFinalURLHitMask;
    if (DidURLMatchActivationList(navigation_chain_.front(), activation_list))
      hits_pattern |= kInitialURLHitMask;

    // Examine redirects.
    for (size_t i = 1; i < navigation_chain_.size() - 1; ++i) {
      if (DidURLMatchActivationList(navigation_chain_[i], activation_list)) {
        hits_pattern |= kRedirectURLHitMask;
        break;
      }
    }
  } else {
    if (navigation_chain_.size() &&
        DidURLMatchActivationList(navigation_chain_.front(), activation_list)) {
      hits_pattern = 0x8;  // One url hit.
    }
  }
  return hits_pattern;
}

void ContentSubresourceFilterDriverFactory::RecordRedirectChainMatchPattern()
    const {
  RecordRedirectChainMatchPatternForList(
      ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL);
  RecordRedirectChainMatchPatternForList(ActivationList::PHISHING_INTERSTITIAL);
  RecordRedirectChainMatchPatternForList(ActivationList::SUBRESOURCE_FILTER);
}

void ContentSubresourceFilterDriverFactory::
    RecordRedirectChainMatchPatternForList(
        ActivationList activation_list) const {
  int hits_pattern = CalculateHitPatternForActivationList(activation_list);
  if (!hits_pattern)
    return;
  size_t chain_size = navigation_chain_.size();
  switch (activation_list) {
    case ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL:
      REPORT_REDIRECT_PATTERN_FOR_SUFFIX("SocialEngineeringAdsInterstitial",
                                         hits_pattern, chain_size);
      break;
    case ActivationList::PHISHING_INTERSTITIAL:
      REPORT_REDIRECT_PATTERN_FOR_SUFFIX("PhishingInterstital", hits_pattern,
                                         chain_size);
      break;
    case ActivationList::SUBRESOURCE_FILTER:
      REPORT_REDIRECT_PATTERN_FOR_SUFFIX("SubresourceFilterOnly", hits_pattern,
                                         chain_size);
      break;
    default:
      NOTREACHED();
      break;
  }
}

}  // namespace subresource_filter