File: supervised_user_utils.cc

package info (click to toggle)
chromium 146.0.7680.153-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,057,156 kB
  • sloc: cpp: 36,426,539; ansic: 7,626,206; javascript: 3,599,825; python: 1,658,592; xml: 842,302; asm: 722,011; pascal: 186,153; sh: 88,976; perl: 88,684; objc: 79,984; sql: 60,492; cs: 42,470; fortran: 24,101; makefile: 21,141; tcl: 15,277; php: 14,022; yacc: 9,154; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (232 lines) | stat: -rw-r--r-- 9,245 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/supervised_user/core/browser/supervised_user_utils.h"

#include <optional>
#include <string>
#include <variant>
#include <vector>

#include "base/base64.h"
#include "base/base64url.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/strings/grit/components_strings.h"
#include "components/supervised_user/core/browser/proto/parent_access_callback.pb.h"
#include "components/supervised_user/core/browser/proto/transaction_data.pb.h"
#include "components/supervised_user/core/browser/supervised_user_log_record.h"
#include "components/supervised_user/core/common/features.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "components/url_formatter/url_formatter.h"
#include "components/url_matcher/url_util.h"
#include "net/base/url_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

namespace supervised_user {

namespace {

// Url query param returned from the PACP widget with an encoded parent approval
// result.
constexpr char kParentAccessResultQueryParameter[] = "result";

// Url that contains the approval result in PACP parent approval requests.
constexpr char kPacpOriginUrlHost[] = "families.google.com";

#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || \
    BUILDFLAG(IS_WIN)
// Returns the text that will be shown as the PACP widget subtitle, containing
// information about the blocked hostname and the blocking reason.
std::string GetBlockingReasonSubtitle(
    const std::u16string& blocked_hostname,
    supervised_user::FilteringBehaviorReason filtering_reason) {
  int message_id = 0;
  switch (filtering_reason) {
    case FilteringBehaviorReason::DEFAULT:
      message_id = IDS_PARENT_WEBSITE_APPROVAL_BLOCK_ALL_URL;
      break;
    case FilteringBehaviorReason::ASYNC_CHECKER:
      message_id = IDS_PARENT_WEBSITE_APPROVAL_SAFE_SITES_URL;
      break;
    case FilteringBehaviorReason::MANUAL:
      message_id = IDS_PARENT_WEBSITE_APPROVAL_MANUAL_URL;
      break;
    case FilteringBehaviorReason::FILTER_DISABLED:
      NOTREACHED() << "No blocking reason when the filter is disabled";
  }
  return l10n_util::GetStringFUTF8(message_id, blocked_hostname);
}

// Returns a base64-encoded `TransactionData` proto message that encapsulates
// the blocked url so that PACP can consume it.
std::string GetBase64EncodedInTransactionalDataForPayload(
    const std::u16string& blocked_hostname,
    supervised_user::FilteringBehaviorReason filtering_reason) {
  static constexpr char kPacpUrlPayloadMessageType[] =
      "type.googleapis.com/"
      "kids.platform.parentaccess.ui.common.proto.LocalApprovalPayload";

  CHECK(!blocked_hostname.empty());
  kids::platform::parentaccess::proto::LocalApprovalPayload approval_url;
  approval_url.set_url_approval_context(
      GetBlockingReasonSubtitle(blocked_hostname, filtering_reason));

  kids::platform::parentaccess::proto::TransactionData transaction_data;
  transaction_data.mutable_payload()->set_value(
      approval_url.SerializeAsString());
  transaction_data.mutable_payload()->set_type_url(kPacpUrlPayloadMessageType);
  std::string base_64_url_encoded_data;
  base::Base64UrlEncode(transaction_data.SerializeAsString(),
                        base::Base64UrlEncodePolicy::INCLUDE_PADDING,
                        &base_64_url_encoded_data);
  CHECK_GT(base_64_url_encoded_data.length(), 0UL);
  return base_64_url_encoded_data;
}
#endif  // BUILDFLAG(IS_IOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) ||
        // BUILDFLAG(IS_WIN)
}  // namespace

ParentAccessCallbackParsedResult::ParentAccessCallbackParsedResult(
    ParentAccessWidgetError error)
    : result_(error) {}
ParentAccessCallbackParsedResult::ParentAccessCallbackParsedResult(
    kids::platform::parentaccess::client::proto::ParentAccessCallback callback)
    : result_(callback) {}
ParentAccessCallbackParsedResult::~ParentAccessCallbackParsedResult() = default;

std::optional<ParentAccessWidgetError>
ParentAccessCallbackParsedResult::GetError() const {
  if (std::holds_alternative<ParentAccessWidgetError>(result_)) {
    return std::get<ParentAccessWidgetError>(result_);
  }
  return std::nullopt;
}

std::optional<kids::platform::parentaccess::client::proto::ParentAccessCallback>
ParentAccessCallbackParsedResult::GetCallback() const {
  if (std::holds_alternative<
          kids::platform::parentaccess::client::proto::ParentAccessCallback>(
          result_)) {
    return std::get<
        kids::platform::parentaccess::client::proto::ParentAccessCallback>(
        result_);
  }
  return std::nullopt;
}

// static
ParentAccessCallbackParsedResult
ParentAccessCallbackParsedResult::ParseParentAccessCallbackResult(
    const std::string& encoded_parent_access_callback_proto) {
  std::string decoded_parent_access_callback;
  if (!base::Base64UrlDecode(encoded_parent_access_callback_proto,
                             base::Base64UrlDecodePolicy::IGNORE_PADDING,
                             &decoded_parent_access_callback)) {
    LOG(ERROR) << "ParentAccessHandler::ParentAccessResult: Error decoding "
                  "parent_access_result from base64";
    return ParentAccessCallbackParsedResult(
        ParentAccessWidgetError::kDecodingError);
  }

  kids::platform::parentaccess::client::proto::ParentAccessCallback
      parent_access_callback;
  if (!parent_access_callback.ParseFromString(decoded_parent_access_callback)) {
    LOG(ERROR) << "ParentAccessHandler::ParentAccessResult: Error parsing "
                  "decoded_parent_access_result to proto";
    return ParentAccessCallbackParsedResult(
        ParentAccessWidgetError::kParsingError);
  }

  return ParentAccessCallbackParsedResult(parent_access_callback);
}

std::optional<std::string> MaybeGetPacpResultFromUrl(const GURL& url) {
  std::string result_value;
  bool contains_result_query_param = net::GetValueForKeyInQuery(
      url,
      /*search_key=*/kParentAccessResultQueryParameter,
      /*out_value=*/&result_value);

  if (!url.GetHost().starts_with(kPacpOriginUrlHost) ||
      !contains_result_query_param) {
    return std::nullopt;
  }
  return result_value;
}

std::string FamilyRoleToString(kidsmanagement::FamilyRole role) {
  switch (role) {
    case kidsmanagement::CHILD:
      return "child";
    case kidsmanagement::MEMBER:
      return "member";
    case kidsmanagement::PARENT:
      return "parent";
    case kidsmanagement::HEAD_OF_HOUSEHOLD:
      return "family_manager";
    default:
      // Keep the previous semantics - other values were not allowed.
      NOTREACHED();
  }
}

GURL NormalizeUrl(const GURL& url) {
  GURL effective_url = url_matcher::util::GetEmbeddedURL(url);
  if (!effective_url.is_valid()) {
    effective_url = url;
  }
  return url_matcher::util::Normalize(effective_url);
}

#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || \
    BUILDFLAG(IS_WIN)
GURL GetParentAccessURL(
    const std::string& locale,
    const GURL& blocked_url,
    supervised_user::FilteringBehaviorReason filtering_reason) {
  // Parent Access widget constants, used in the local web approval
  // flow.
  // URL hosting the Parent Access widget.
  static constexpr char kBaseUrl[] = "https://families.google.com/parentaccess";
  // URL to which the Parent Access widget redirects on approval.
  static constexpr char kContinueUrl[] = "https://families.google.com";

  // Caller Ids for Desktop and iOS platforms.
#if BUILDFLAG(IS_IOS)
  static constexpr char kCallerId[] = "qSTnVRdQ";
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  static constexpr char kCallerId[] = "clwAA5XJ";
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)

  GURL url(kBaseUrl);
  GURL::Replacements replacements;
  std::string query = base::StrCat(
      {"callerid=", kCallerId, "&hl=", locale, "&continue=", kContinueUrl});
  if (base::FeatureList::IsEnabled(
          kLocalWebApprovalsWidgetSupportsUrlPayload) &&
      !blocked_url.GetHost().empty()) {
    // Prepare blocked URL hostname for user-friendly display, including
    // internationalized domain name (IDN) conversion if necessary.
    std::u16string blocked_hostname = url_formatter::FormatUrl(
        blocked_url,
        url_formatter::kFormatUrlOmitHTTP | url_formatter::kFormatUrlOmitHTTPS |
            url_formatter::kFormatUrlOmitDefaults,
        base::UnescapeRule::SPACES, nullptr, nullptr, nullptr);
    query += base::StrCat(
        {"&transaction-data=", GetBase64EncodedInTransactionalDataForPayload(
                                   blocked_hostname, filtering_reason)});
  }
  replacements.SetQueryStr(query);
  return url.ReplaceComponents(replacements);
}
#endif  // BUILDFLAG(IS_IOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) ||
        // BUILDFLAG(IS_WIN)
}  // namespace supervised_user