File: protocol_handler.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (214 lines) | stat: -rw-r--r-- 7,533 bytes parent folder | download | duplicates (2)
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
// Copyright 2012 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/custom_handlers/protocol_handler.h"

#include "base/json/values_util.h"
#include "base/strings/escape.h"
#include "base/strings/utf_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_client.h"
#include "content/public/common/origin_util.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "third_party/blink/public/common/custom_handlers/protocol_handler_utils.h"
#include "third_party/blink/public/common/scheme_registry.h"
#include "third_party/blink/public/common/security/protocol_handler_security_level.h"
#include "ui/base/l10n/l10n_util.h"

using content::BrowserThread;

namespace custom_handlers {

namespace features {

// https://html.spec.whatwg.org/multipage/system-state.html#security-and-privacy
BASE_FEATURE(kStripCredentialsForExternalProtocolHandler,
             "StripCredentialsForExternalProtocolHandler",
             base::FEATURE_ENABLED_BY_DEFAULT);
}  // namespace features

ProtocolHandler::ProtocolHandler(
    const std::string& protocol,
    const GURL& url,
    base::Time last_modified,
    blink::ProtocolHandlerSecurityLevel security_level)
    : protocol_(base::ToLowerASCII(protocol)),
      url_(url),
      last_modified_(last_modified),
      security_level_(security_level) {}

ProtocolHandler::ProtocolHandler(const ProtocolHandler&) = default;
ProtocolHandler::~ProtocolHandler() = default;

ProtocolHandler ProtocolHandler::CreateProtocolHandler(
    const std::string& protocol,
    const GURL& url,
    blink::ProtocolHandlerSecurityLevel security_level) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return ProtocolHandler(protocol, url, base::Time::Now(), security_level);
}

ProtocolHandler::ProtocolHandler(
    const std::string& protocol,
    const GURL& url,
    const std::string& app_id,
    base::Time last_modified,
    blink::ProtocolHandlerSecurityLevel security_level)
    : protocol_(base::ToLowerASCII(protocol)),
      url_(url),
      web_app_id_(app_id),
      last_modified_(last_modified),
      security_level_(security_level) {}

// static
ProtocolHandler ProtocolHandler::CreateWebAppProtocolHandler(
    const std::string& protocol,
    const GURL& url,
    const std::string& app_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return ProtocolHandler(protocol, url, app_id, base::Time::Now(),
                         blink::ProtocolHandlerSecurityLevel::kStrict);
}

ProtocolHandler::ProtocolHandler() = default;

bool ProtocolHandler::IsValidDict(const base::Value::Dict& value) {
  // Note that "title" parameter is ignored.
  // The |last_modified| field is optional as it was introduced in M68.
  return value.FindString("protocol") && value.FindString("url");
}

bool ProtocolHandler::IsValid() const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  // We don't want to include URL's syntax checks because there are use cases of
  // the protocol handlers logic that require more flexibility than the one
  // specified for the registerProtocolHandler API (eg, Predefined Handlers).
  if (!blink::IsAllowedCustomHandlerURL(url_, security_level_))
    return false;

  return blink::IsValidCustomHandlerScheme(protocol_, security_level_);
}

bool ProtocolHandler::IsSameOrigin(const ProtocolHandler& handler) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return handler.url().DeprecatedGetOriginAsURL() ==
         url_.DeprecatedGetOriginAsURL();
}

const ProtocolHandler& ProtocolHandler::EmptyProtocolHandler() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  static const ProtocolHandler* const kEmpty = new ProtocolHandler();
  return *kEmpty;
}

ProtocolHandler ProtocolHandler::CreateProtocolHandler(
    const base::Value::Dict& value) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (!IsValidDict(value)) {
    return EmptyProtocolHandler();
  }
  std::string protocol, url;
  // |time| defaults to the beginning of time if it is not specified.
  base::Time time;
  blink::ProtocolHandlerSecurityLevel security_level =
      blink::ProtocolHandlerSecurityLevel::kStrict;
  if (const std::string* protocol_in = value.FindString("protocol"))
    protocol = *protocol_in;
  if (const std::string* url_in = value.FindString("url"))
    url = *url_in;
  absl::optional<base::Time> time_value =
      base::ValueToTime(value.Find("last_modified"));
  // Treat invalid times as the default value.
  if (time_value)
    time = *time_value;
  absl::optional<int> security_level_value = value.FindInt("security_level");
  if (security_level_value) {
    security_level =
        blink::ProtocolHandlerSecurityLevelFrom(*security_level_value);
  }

  if (const base::Value* app_id_val = value.Find("app_id")) {
    std::string app_id;
    if (app_id_val->is_string())
      app_id = app_id_val->GetString();
    return ProtocolHandler(protocol, GURL(url), app_id, time, security_level);
  }

  return ProtocolHandler(protocol, GURL(url), time, security_level);
}

GURL ProtocolHandler::TranslateUrl(const GURL& url) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  std::string clean_url;
  base::StringPiece url_spec(url.spec());

  // Remove credentials from the url if present, in order to mitigate the risk
  // of credential leakage
  if ((url.has_username() || url.has_password()) &&
      base::FeatureList::IsEnabled(
          features::kStripCredentialsForExternalProtocolHandler)) {
    GURL::Replacements replacements;
    replacements.ClearUsername();
    replacements.ClearPassword();
    clean_url = url.ReplaceComponents(replacements).spec();
    url_spec = clean_url;
  }

  std::string translatedUrlSpec(url_.spec());
  base::ReplaceFirstSubstringAfterOffset(
      &translatedUrlSpec, 0, "%s",
      base::EscapeQueryParamValue(url_spec, false));
  return GURL(translatedUrlSpec);
}

base::Value::Dict ProtocolHandler::Encode() const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  base::Value::Dict d;
  d.Set("protocol", protocol_);
  d.Set("url", url_.spec());
  d.Set("last_modified", base::TimeToValue(last_modified_));
  d.Set("security_level", static_cast<int>(security_level_));

  if (web_app_id_.has_value())
    d.Set("app_id", web_app_id_.value());

  return d;
}

std::u16string ProtocolHandler::GetProtocolDisplayName(
    const std::string& protocol) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (protocol == "mailto")
    return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME);
  if (protocol == "webcal")
    return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME);
  return base::UTF8ToUTF16(protocol);
}

std::u16string ProtocolHandler::GetProtocolDisplayName() const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  return GetProtocolDisplayName(protocol_);
}

#if !defined(NDEBUG)
std::string ProtocolHandler::ToString() const {
  return "{ protocol=" + protocol_ + ", url=" + url_.spec() + " }";
}
#endif

bool ProtocolHandler::operator==(const ProtocolHandler& other) const {
  return protocol_ == other.protocol_ && url_ == other.url_;
}

bool ProtocolHandler::IsEquivalent(const ProtocolHandler& other) const {
  return protocol_ == other.protocol_ && url_ == other.url_;
}

bool ProtocolHandler::operator<(const ProtocolHandler& other) const {
  return url_ < other.url_;
}

}  // namespace custom_handlers