File: reporting_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (307 lines) | stat: -rw-r--r-- 11,413 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/enterprise/data_controls/reporting_service.h"

#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/enterprise/data_controls/core/browser/prefs.h"
#include "components/enterprise/data_controls/core/browser/verdict.h"
#include "components/policy/core/common/policy_types.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/clipboard_types.h"
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/base/clipboard/clipboard_format_type.h"

namespace data_controls {

namespace {

// TODO(b/311679168): Move this to share logic with ContentAnalysisDelegate.
std::string GetMimeType(const ui::ClipboardFormatType& clipboard_format) {
  if (clipboard_format == ui::ClipboardFormatType::PlainTextType()) {
    return ui::kMimeTypePlainText;
  } else if (clipboard_format == ui::ClipboardFormatType::HtmlType()) {
    return ui::kMimeTypeHtml;
  } else if (clipboard_format == ui::ClipboardFormatType::SvgType()) {
    return ui::kMimeTypeSvg;
  } else if (clipboard_format == ui::ClipboardFormatType::RtfType()) {
    return ui::kMimeTypeRtf;
  } else if (clipboard_format == ui::ClipboardFormatType::PngType()) {
    return ui::kMimeTypePng;
  } else if (clipboard_format == ui::ClipboardFormatType::FilenamesType()) {
    return ui::kMimeTypeUriList;
  }
  return "";
}

GURL GetURL(const content::ClipboardEndpoint& endpoint) {
  if (!endpoint.data_transfer_endpoint() ||
      !endpoint.data_transfer_endpoint()->IsUrlType() ||
      !endpoint.data_transfer_endpoint()->GetURL()) {
    return GURL();
  }
  return *endpoint.data_transfer_endpoint()->GetURL();
}

enterprise_connectors::EventResult GetEventResult(Rule::Level level) {
  switch (level) {
    case Rule::Level::kNotSet:
    case Rule::Level::kAllow:
    case Rule::Level::kReport:
      return enterprise_connectors::EventResult::ALLOWED;
    case Rule::Level::kBlock:
      return enterprise_connectors::EventResult::BLOCKED;
    case Rule::Level::kWarn:
      return enterprise_connectors::EventResult::WARNED;
  }
}

bool PolicyAppliedAtUserScope(content::BrowserContext* browser_context,
                              const char* scope_pref) {
  CHECK(browser_context);
  CHECK(scope_pref);

  return Profile::FromBrowserContext(browser_context)
             ->GetPrefs()
             ->GetInteger(scope_pref) == policy::POLICY_SCOPE_USER;
}

}  // namespace

// -------------------------------
// ReportingService implementation
// -------------------------------

// static
enterprise_connectors::ContentMetaData::CopiedTextSource
ReportingService::GetClipboardSource(
    const content::ClipboardEndpoint& source,
    const content::ClipboardEndpoint& destination,
    const char* scope_pref) {
  CHECK(destination.browser_context());

  using SourceType = enterprise_connectors::ContentMetaData::CopiedTextSource;

  SourceType copied_text_source;
  if (!source.browser_context()) {
    // This off the record check will also include guest profile sources, but
    // since there is no way to disambiguate them with a null BrowserContext
    // INCOGNITO is selected instead of CLIPBOARD to not share the source URL in
    // such cases.
    if (source.data_transfer_endpoint() &&
        source.data_transfer_endpoint()->off_the_record()) {
      copied_text_source.set_context(SourceType::INCOGNITO);
    } else {
      copied_text_source.set_context(SourceType::CLIPBOARD);
    }
  } else if (Profile::FromBrowserContext(source.browser_context())
                 ->IsIncognitoProfile()) {
    copied_text_source.set_context(SourceType::INCOGNITO);
  } else if (source.browser_context() == destination.browser_context()) {
    copied_text_source.set_context(SourceType::SAME_PROFILE);
  } else {
    copied_text_source.set_context(SourceType::OTHER_PROFILE);
  }

  switch (copied_text_source.context()) {
    case SourceType::UNSPECIFIED:
    case SourceType::INCOGNITO:
      break;
    case SourceType::CLIPBOARD:
      // If the user does something like closing the browser between the time
      // they copy and then paste, the DTE might have a URL even though the lack
      // of browser context will make it impossible to know if the `SourceType`
      // is `SAME_PROFILE` or `OTHER_PROFILE`.
      //
      // In that case, we can be conservative and perform the same check as
      // `OTHER_PROFILE`. Note that this code path is unreachable in the case of
      // an incognito source URL as that is handled in the `set_context`
      // conditions above.
      [[fallthrough]];
    case SourceType::OTHER_PROFILE:
      // Only add a source URL if the other profile is getting the policy
      // applied at the machine scope, not the user scope.
      if (PolicyAppliedAtUserScope(destination.browser_context(), scope_pref)) {
        break;
      }
      [[fallthrough]];
    case SourceType::SAME_PROFILE:
      if (source.data_transfer_endpoint() &&
          source.data_transfer_endpoint()->IsUrlType() &&
          source.data_transfer_endpoint()->GetURL()) {
        copied_text_source.set_url(
            source.data_transfer_endpoint()->GetURL()->spec());
      }
      break;
  }

  return copied_text_source;
}

// static
std::string ReportingService::GetClipboardSourceString(
    const content::ClipboardEndpoint& source,
    const content::ClipboardEndpoint& destination,
    const char* scope_pref) {
  return GetClipboardSourceString(
      GetClipboardSource(source, destination, scope_pref));
}

// static
std::string ReportingService::GetClipboardSourceString(
    const enterprise_connectors::ContentMetaData::CopiedTextSource& source) {
  if (!source.url().empty()) {
    return source.url();
  }

  switch (source.context()) {
    case enterprise_connectors::ContentMetaData::CopiedTextSource::UNSPECIFIED:
    case enterprise_connectors::ContentMetaData::CopiedTextSource::SAME_PROFILE:
      return "";
    case enterprise_connectors::ContentMetaData::CopiedTextSource::INCOGNITO:
      return "INCOGNITO";
    case enterprise_connectors::ContentMetaData::CopiedTextSource::CLIPBOARD:
      return "CLIPBOARD";
    case enterprise_connectors::ContentMetaData::CopiedTextSource::
        OTHER_PROFILE:
      return "OTHER_PROFILE";
  }
}

ReportingService::ReportingService(content::BrowserContext& browser_context)
    : profile_(*Profile::FromBrowserContext(&browser_context)) {}

ReportingService::~ReportingService() = default;

void ReportingService::ReportPaste(
    const content::ClipboardEndpoint& source,
    const content::ClipboardEndpoint& destination,
    const content::ClipboardMetadata& metadata,
    const Verdict& verdict) {
  ReportCopyOrPaste(
      source, destination, metadata, verdict,
      extensions::SafeBrowsingPrivateEventRouter::kTriggerWebContentUpload,
      GetEventResult(verdict.level()));
}

void ReportingService::ReportPasteWarningBypassed(
    const content::ClipboardEndpoint& source,
    const content::ClipboardEndpoint& destination,
    const content::ClipboardMetadata& metadata,
    const Verdict& verdict) {
  ReportCopyOrPaste(
      source, destination, metadata, verdict,
      extensions::SafeBrowsingPrivateEventRouter::kTriggerWebContentUpload,
      enterprise_connectors::EventResult::BYPASSED);
}

void ReportingService::ReportCopy(const content::ClipboardEndpoint& source,
                                  const content::ClipboardMetadata& metadata,
                                  const Verdict& verdict) {
  ReportCopyOrPaste(
      source, /*destination=*/std::nullopt, metadata, verdict,
      extensions::SafeBrowsingPrivateEventRouter::kTriggerClipboardCopy,
      GetEventResult(verdict.level()));
}

void ReportingService::ReportCopyWarningBypassed(
    const content::ClipboardEndpoint& source,
    const content::ClipboardMetadata& metadata,
    const Verdict& verdict) {
  ReportCopyOrPaste(
      source, /*destination=*/std::nullopt, metadata, verdict,
      extensions::SafeBrowsingPrivateEventRouter::kTriggerClipboardCopy,
      enterprise_connectors::EventResult::BYPASSED);
}

void ReportingService::ReportCopyOrPaste(
    const content::ClipboardEndpoint& source,
    const std::optional<content::ClipboardEndpoint>& destination,
    const content::ClipboardMetadata& metadata,
    const Verdict& verdict,
    const std::string& trigger,
    enterprise_connectors::EventResult event_result) {
  auto* router =
      extensions::SafeBrowsingPrivateEventRouterFactory::GetForProfile(
          &profile_.get());

  if (!router || verdict.triggered_rules().empty()) {
    return;
  }

  GURL url;
  std::string destination_string;
  std::string source_string;
  if (trigger ==
      extensions::SafeBrowsingPrivateEventRouter::kTriggerWebContentUpload) {
    DCHECK(destination.has_value());

    url = GetURL(*destination);
    destination_string = url.spec();
    source_string = GetClipboardSourceString(source, *destination,
                                             kDataControlsRulesScopePref);
  } else {
    DCHECK_EQ(
        trigger,
        extensions::SafeBrowsingPrivateEventRouter::kTriggerClipboardCopy);
    DCHECK(!destination.has_value());

    url = GetURL(source);
    source_string = GetURL(source).spec();
  }

  router->OnDataControlsSensitiveDataEvent(
      /*url=*/url,
      /*tab_url=*/url,
      /*source=*/source_string,
      /*destination=*/destination_string,
      /*mime_type=*/GetMimeType(metadata.format_type),
      /*trigger=*/trigger,
      /*triggered_rules=*/verdict.triggered_rules(),
      /*event_result=*/event_result,
      /*content_size=*/metadata.size.value_or(-1));
}

// --------------------------------------
// ReportingServiceFactory implementation
// --------------------------------------

ReportingServiceBase* ReportingServiceFactory::GetForBrowserContext(
    content::BrowserContext* context) {
  return static_cast<ReportingServiceBase*>(
      GetInstance()->GetServiceForBrowserContext(context, /*create=*/true));
}

// static
ReportingServiceFactory* ReportingServiceFactory::GetInstance() {
  static base::NoDestructor<ReportingServiceFactory> instance;
  return instance.get();
}

ReportingServiceFactory::ReportingServiceFactory()
    : ProfileKeyedServiceFactory(
          "DataControlsReportingService",
          // `kOriginalOnly` is used since there is no reporting done for
          // incognito profiles.
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kOriginalOnly)
              .WithGuest(ProfileSelection::kNone)
              .WithSystem(ProfileSelection::kNone)
              .WithAshInternals(ProfileSelection::kNone)
              .Build()) {
  DependsOn(extensions::SafeBrowsingPrivateEventRouterFactory::GetInstance());
}

ReportingServiceFactory::~ReportingServiceFactory() = default;

std::unique_ptr<KeyedService>
ReportingServiceFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  return base::WrapUnique(new ReportingService(*context));
}

}  // namespace data_controls