summaryrefslogtreecommitdiff
path: root/chromium/components/password_manager/core/browser/password_scripts_fetcher_impl.cc
blob: 4ad6464b3fe2e4e73005d223ceb9319cf638cb0a (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
// Copyright 2020 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/password_manager/core/browser/password_scripts_fetcher_impl.h"

#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/json/json_reader.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace {

constexpr int kCacheTimeoutInMinutes = 5;
constexpr int kFetchTimeoutInSeconds = 3;

constexpr int kMaxDownloadSizeInBytes = 512 * 1024;

using ParsingResult =
    password_manager::PasswordScriptsFetcherImpl::ParsingResult;

// Extracts the domains and min Chrome's version for which password changes are
// supported and adds them to |supported_domains|.
// |script_config| is the dictionary passed for a domain, representig the
// configuration data of one password change script. For example, the fetched
// JSON might look like this:
// {
//   'example.com': {
//     'domains': [ 'https://www.example.com', 'https://m.example.com' ],
//     'min_version': '86'
//   }
// }
// In this case |script_config| would represent the dictionary value
// of 'example.com'.
// Returns a set of warnings.
// The function tries to be lax about errors and prefers to skip them
// with warnings rather than bail the parsing. This is for forward
// compatibility.
base::flat_set<ParsingResult> ParseDomainSpecificParamaters(
    const base::Value& script_config,
    base::flat_map<url::Origin, base::Version>& supported_domains) {
  if (!script_config.is_dict())
    return {ParsingResult::kInvalidJson};

  const base::Value* supported_domains_list =
      script_config.FindListKey("domains");
  if (!supported_domains_list || !supported_domains_list->is_list())
    return {ParsingResult::kInvalidJson};

  base::flat_set<ParsingResult> warnings;

  const std::string* min_version = script_config.FindStringKey("min_version");
  base::Version version;
  if (!min_version) {
    return {ParsingResult::kInvalidJson};
  } else {
    version = base::Version(*min_version);
    if (!version.IsValid()) {
      return {ParsingResult::kInvalidJson};
    }
  }

  for (const base::Value& domain :
       supported_domains_list->GetListDeprecated()) {
    if (!domain.is_string()) {
      warnings.insert(ParsingResult::kInvalidJson);
      continue;
    }

    GURL url(domain.GetString());
    if (!url.is_valid()) {
      warnings.insert(ParsingResult::kInvalidUrl);
      continue;
    }
    supported_domains.insert(std::make_pair(url::Origin::Create(url), version));
  }

  return warnings;
}

}  // namespace

namespace password_manager {

constexpr char kDefaultChangePasswordScriptsListUrl[] =
    "https://www.gstatic.com/chrome/duplex/change_password_scripts.json";

constexpr base::FeatureParam<std::string> kScriptsListUrlParam{
    &features::kPasswordScriptsFetching, "custom_list_url",
    kDefaultChangePasswordScriptsListUrl};

PasswordScriptsFetcherImpl::PasswordScriptsFetcherImpl(
    const base::Version& version,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
    : PasswordScriptsFetcherImpl(version,
                                 std::move(url_loader_factory),
                                 kScriptsListUrlParam.Get()) {}

PasswordScriptsFetcherImpl::PasswordScriptsFetcherImpl(
    const base::Version& version,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
    std::string scripts_list_url)
    : version_(version),
      scripts_list_url_(std::move(scripts_list_url)),
      url_loader_factory_(std::move(url_loader_factory)) {}

PasswordScriptsFetcherImpl::~PasswordScriptsFetcherImpl() = default;

void PasswordScriptsFetcherImpl::PrewarmCache() {
  if (IsCacheStale())
    StartFetch();
}

void PasswordScriptsFetcherImpl::RefreshScriptsIfNecessary(
    base::OnceClosure fetch_finished_callback) {
  CacheState state = IsCacheStale()
                         ? (url_loader_ ? CacheState::kWaiting
                                        : (last_fetch_timestamp_.is_null()
                                               ? CacheState::kNeverSet
                                               : CacheState::kStale))
                         : CacheState::kReady;
  base::UmaHistogramEnumeration(
      "PasswordManager.PasswordScriptsFetcher.CacheState", state);

  if (state == CacheState::kReady)
    std::move(fetch_finished_callback).Run();
  else
    fetch_finished_callbacks_.emplace_back(std::move(fetch_finished_callback));

  switch (state) {
    case CacheState::kReady:
    case CacheState::kWaiting:
      // No new fetching.
      break;
    case CacheState::kNeverSet:
    case CacheState::kStale:
      StartFetch();
      break;
  }
}

void PasswordScriptsFetcherImpl::FetchScriptAvailability(
    const url::Origin& origin,
    ResponseCallback callback) {
  if (IsCacheStale()) {
    pending_callbacks_.emplace_back(
        std::make_pair(origin, std::move(callback)));
    StartFetch();
    return;
  }

  RunResponseCallback(origin, std::move(callback));
}

bool PasswordScriptsFetcherImpl::IsScriptAvailable(
    const url::Origin& origin) const {
  const auto& it = password_change_domains_.find(origin);
  if (it == password_change_domains_.end()) {
    return false;
  }
  return version_ >= it->second;
}

void PasswordScriptsFetcherImpl::StartFetch() {
  static const base::TimeDelta kFetchTimeout(
      base::Seconds(kFetchTimeoutInSeconds));
  if (url_loader_)
    return;
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = GURL(scripts_list_url_);
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("gstatic_change_password_scripts",
                                          R"(
        semantics {
          sender: "Password Manager"
          description:
            "A JSON file hosted by gstatic containing a map of password change"
            "scripts to optional parameters for those scripts."
          trigger:
            "When the user visits chrome://settings/passwords/check or "
            "makes Safety Check in settings or sees a leak warning."
          data:
            "The request body is empty. No user data is included."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "The user can enable or disable automatic password leak checks in "
            "Chrome's security settings. The feature is enabled by default."
          policy_exception_justification:
            "TODO(crbug.com/1231780): Add this field."
        })");
  url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
                                                 traffic_annotation);
  url_loader_->SetTimeoutDuration(kFetchTimeout);
  url_loader_->DownloadToString(
      url_loader_factory_.get(),
      base::BindOnce(&PasswordScriptsFetcherImpl::OnFetchComplete,
                     base::Unretained(this), base::TimeTicks::Now()),
      kMaxDownloadSizeInBytes);
}

void PasswordScriptsFetcherImpl::OnFetchComplete(
    base::TimeTicks request_start_timestamp,
    std::unique_ptr<std::string> response_body) {
  base::UmaHistogramTimes("PasswordManager.PasswordScriptsFetcher.ResponseTime",
                          base::TimeTicks::Now() - request_start_timestamp);
  bool report_http_response_code =
      (url_loader_->NetError() == net::OK ||
       url_loader_->NetError() == net::ERR_HTTP_RESPONSE_CODE_FAILURE) &&
      url_loader_->ResponseInfo() && url_loader_->ResponseInfo()->headers;
  base::UmaHistogramSparse(
      "PasswordManager.PasswordScriptsFetcher.HttpResponseAndNetErrorCode",
      report_http_response_code
          ? url_loader_->ResponseInfo()->headers->response_code()
          : url_loader_->NetError());
  url_loader_.reset();
  last_fetch_timestamp_ = base::TimeTicks::Now();

  base::flat_set<ParsingResult> parsing_warnings =
      ParseResponse(std::move(response_body));
  if (parsing_warnings.empty())
    parsing_warnings.insert(ParsingResult::kOk);
  for (ParsingResult warning : parsing_warnings) {
    base::UmaHistogramEnumeration(
        "PasswordManager.PasswordScriptsFetcher.ParsingResult", warning);
  }

  for (auto& callback : std::exchange(fetch_finished_callbacks_, {}))
    std::move(callback).Run();
  for (auto& callback : std::exchange(pending_callbacks_, {}))
    RunResponseCallback(std::move(callback.first), std::move(callback.second));
}

base::flat_set<ParsingResult> PasswordScriptsFetcherImpl::ParseResponse(
    std::unique_ptr<std::string> response_body) {
  password_change_domains_.clear();

  if (!response_body)
    return {ParsingResult::kNoResponse};

  base::JSONReader::ValueWithError data =
      base::JSONReader::ReadAndReturnValueWithError(*response_body);

  if (data.value == absl::nullopt) {
    DVLOG(1) << "Parse error: " << data.error_message;
    return {ParsingResult::kInvalidJson};
  }
  if (!data.value->is_dict())
    return {ParsingResult::kInvalidJson};

  base::flat_set<ParsingResult> warnings;
  for (const auto script_it : data.value->DictItems()) {
    // |script_it.first| is an identifier (normally, a domain name, e.g.
    // example.com) that we don't care about.
    // |script_it.second| provides domain-specific parameters.
    base::flat_set<ParsingResult> warnings_for_script =
        ParseDomainSpecificParamaters(script_it.second,
                                      password_change_domains_);
    warnings.insert(warnings_for_script.begin(), warnings_for_script.end());
  }
  return warnings;
}

bool PasswordScriptsFetcherImpl::IsCacheStale() const {
  static const base::TimeDelta kCacheTimeout(
      base::Minutes(kCacheTimeoutInMinutes));
  return last_fetch_timestamp_.is_null() ||
         base::TimeTicks::Now() - last_fetch_timestamp_ >= kCacheTimeout;
}

void PasswordScriptsFetcherImpl::RunResponseCallback(
    url::Origin origin,
    ResponseCallback callback) {
  DCHECK(!url_loader_);     // Fetching is not running.
  DCHECK(!IsCacheStale());  // Cache is ready.
  std::move(callback).Run(IsScriptAvailable(origin));
}

}  // namespace password_manager