File: proxy_config_service_impl.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (322 lines) | stat: -rw-r--r-- 13,375 bytes parent folder | download | duplicates (8)
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
// 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 "chromeos/ash/components/network/proxy/proxy_config_service_impl.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "chromeos/ash/components/network/network_profile.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/onc/network_onc_utils.h"
#include "chromeos/ash/components/network/proxy/proxy_config_handler.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/constants/pref_names.h"
#include "components/onc/onc_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/proxy_config/proxy_prefs.h"
#include "components/user_manager/user_manager.h"

namespace ash {

namespace {

// Writes the proxy config of |network| to |proxy_config|.  Set |onc_source| to
// the source of this configuration. Returns false if no proxy was configured
// for this network.
bool GetNetworkProxyConfig(const PrefService* profile_prefs,
                           const PrefService* local_state_prefs,
                           const NetworkState& network,
                           net::ProxyConfigWithAnnotation* proxy_config,
                           ::onc::ONCSource* onc_source) {
  std::unique_ptr<ProxyConfigDictionary> proxy_dict =
      proxy_config::GetProxyConfigForNetwork(
          profile_prefs, local_state_prefs, network,
          NetworkHandler::Get()->network_profile_handler(), onc_source);
  if (!proxy_dict)
    return false;
  return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
                                                           proxy_config);
}

// This code is responsible for determining whether proxies should be disabled
// for captive portal signin windows.
bool ProxiesDisabledForCaptivePortalSignin(const PrefService* profile_prefs) {
  // Prior to login only the signin Profile can be used with no special
  // behavior.
  if (!profile_prefs) {
    return false;
  }
  // The kCaptivePortalSignin pref identifies signin windows. If this is not
  // a signin window, do not disable proxies here.
  const PrefService::Preference* const captive_portal_pref =
      profile_prefs->FindPreference(chromeos::prefs::kCaptivePortalSignin);
  if (!captive_portal_pref || !captive_portal_pref->GetValue()->GetBool()) {
    return false;
  }
  // If the CaptivePortalAuthenticationIgnoresProxy pref is set to false by
  // policy, proxies should not be disabled for captive portal signin.
  const PrefService::Preference* const ignore_proxy_pref =
      profile_prefs->FindPreference(
          chromeos::prefs::kCaptivePortalAuthenticationIgnoresProxy);
  if (ignore_proxy_pref && !ignore_proxy_pref->GetValue()->GetBool()) {
    return false;
  }
  // Otherwise, disable proxies for captive portal signin.
  return true;
}

}  // namespace

ProxyConfigServiceImpl::ProxyConfigServiceImpl(
    PrefService* profile_prefs,
    PrefService* local_state_prefs,
    scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
    : PrefProxyConfigTrackerImpl(
          profile_prefs ? profile_prefs : local_state_prefs,
          io_task_runner),
      profile_prefs_(profile_prefs),
      local_state_prefs_(local_state_prefs) {
  const base::RepeatingClosure proxy_change_callback = base::BindRepeating(
      &ProxyConfigServiceImpl::OnProxyPrefChanged, base::Unretained(this));

  if (profile_prefs) {
    profile_pref_registrar_.Init(profile_prefs);
    profile_pref_registrar_.Add(::onc::prefs::kOpenNetworkConfiguration,
                                proxy_change_callback);
    profile_pref_registrar_.Add(::proxy_config::prefs::kUseSharedProxies,
                                proxy_change_callback);
    profile_pref_registrar_.Add(chromeos::prefs::kCaptivePortalSignin,
                                proxy_change_callback);
  }
  local_state_pref_registrar_.Init(local_state_prefs);
  local_state_pref_registrar_.Add(::onc::prefs::kDeviceOpenNetworkConfiguration,
                                  proxy_change_callback);

  if (NetworkHandler::IsInitialized()) {  // null in unit tests.
    // Register for changes to the default network.
    NetworkStateHandler* state_handler =
        NetworkHandler::Get()->network_state_handler();
    network_state_handler_observer_.Observe(state_handler);
    DefaultNetworkChanged(state_handler->DefaultNetwork());
  }
}

ProxyConfigServiceImpl::~ProxyConfigServiceImpl() = default;

void ProxyConfigServiceImpl::OnProxyConfigChanged(
    ProxyPrefs::ConfigState config_state,
    const net::ProxyConfigWithAnnotation& config) {
  VLOG(1) << "Got prefs change: "
          << ProxyPrefs::ConfigStateToDebugString(config_state)
          << ", mode=" << static_cast<int>(config.value().proxy_rules().type);
  DetermineEffectiveConfigFromDefaultNetwork();
}

void ProxyConfigServiceImpl::OnProxyPrefChanged() {
  DetermineEffectiveConfigFromDefaultNetwork();
}

void ProxyConfigServiceImpl::DefaultNetworkChanged(
    const NetworkState* new_network) {
  std::string new_network_path;
  if (new_network)
    new_network_path = new_network->path();

  VLOG(1) << "DefaultNetworkChanged to '" << new_network_path << "'.";
  VLOG_IF(1, new_network) << "New network: name=" << new_network->name()
                          << ", profile=" << new_network->profile_path();

  // Even if the default network is the same, its proxy config (e.g. if private
  // version of network replaces the shared version after login), or
  // use-shared-proxies setting (e.g. after login) may have changed, so
  // re-determine effective proxy config, and activate if different.
  DetermineEffectiveConfigFromDefaultNetwork();
}

void ProxyConfigServiceImpl::OnShuttingDown() {
  // Ownership of this class is complicated. Stop observing NetworkStateHandler
  // when the class shuts down.
  network_state_handler_observer_.Reset();
}

// static
bool ProxyConfigServiceImpl::IgnoreProxy(const PrefService* profile_prefs,
                                         const std::string network_profile_path,
                                         ::onc::ONCSource onc_source) {
  if (!profile_prefs) {
    // If the profile preference are not available, this must be the object
    // associated to local state used for system requests or login-profile. Make
    // sure that proxies are enabled.
    VLOG(1) << "Use proxy for system requests and sign-in screen.";
    return false;
  }

  if (network_profile_path.empty())
    return true;

  const NetworkProfile* profile =
      NetworkHandler::Get()->network_profile_handler()->GetProfileForPath(
          network_profile_path);
  if (!profile) {
    VLOG(1) << "Unknown profile_path '" << network_profile_path
            << "'. Ignoring proxy.";
    return true;
  }
  if (profile->type() == NetworkProfile::TYPE_USER) {
    VLOG(1) << "Respect proxy of not-shared networks.";
    return false;
  }
  if (onc_source == ::onc::ONC_SOURCE_USER_POLICY) {
    // Note that this case can occur if the network is shared (e.g. ethernet)
    // but the proxy is determined by user policy.
    // See https://crbug.com/454966 .
    VLOG(1) << "Respect proxy from user policy although network is shared.";
    return false;
  }
  if (onc_source == ::onc::ONC_SOURCE_DEVICE_POLICY) {
    const user_manager::User* primary_user =
        user_manager::UserManager::Get()->GetPrimaryUser();
    if (!primary_user || primary_user->IsAffiliated()) {
      VLOG(1) << "Respecting proxy for network, as the primary user belongs to "
              << "the domain the device is enrolled to.";
      return false;
    }
  }

  // This network is shared and not managed by the user's domain.
  bool use_shared_proxies =
      profile_prefs->GetBoolean(::proxy_config::prefs::kUseSharedProxies);
  VLOG(1) << "Use proxy of shared network: " << use_shared_proxies;
  return !use_shared_proxies;
}

// static
std::unique_ptr<ProxyConfigDictionary>
ProxyConfigServiceImpl::GetActiveProxyConfigDictionary(
    const PrefService* profile_prefs,
    const PrefService* local_state_prefs) {
  if (ProxiesDisabledForCaptivePortalSignin(profile_prefs)) {
    return nullptr;
  }

  // Apply Pref Proxy configuration if available.
  net::ProxyConfigWithAnnotation pref_proxy_config;
  ProxyPrefs::ConfigState pref_state =
      PrefProxyConfigTrackerImpl::ReadPrefConfig(profile_prefs,
                                                 &pref_proxy_config);
  if (PrefProxyConfigTrackerImpl::PrefPrecedes(pref_state)) {
    const PrefService::Preference* const pref =
        profile_prefs->FindPreference(::proxy_config::prefs::kProxy);
    DCHECK(pref->GetValue() && pref->GetValue()->is_dict());
    return std::make_unique<ProxyConfigDictionary>(
        pref->GetValue()->GetDict().Clone());
  }

  const NetworkState* network =
      NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
  // No connected network.
  if (!network) {
    return nullptr;
  }

  // Apply network proxy configuration.
  ::onc::ONCSource onc_source;
  std::unique_ptr<ProxyConfigDictionary> proxy_config =
      proxy_config::GetProxyConfigForNetwork(
          profile_prefs, local_state_prefs, *network,
          NetworkHandler::Get()->network_profile_handler(), &onc_source);
  if (!ProxyConfigServiceImpl::IgnoreProxy(
          profile_prefs, network->profile_path(), onc_source)) {
    return proxy_config;
  }

  return std::make_unique<ProxyConfigDictionary>(
      ProxyConfigDictionary::CreateDirect());
}

void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() {
  if (!NetworkHandler::IsInitialized()) {
    return;
  }

  if (ProxiesDisabledForCaptivePortalSignin(profile_prefs_)) {
    return;
  }

  NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
  const NetworkState* network = handler->DefaultNetwork();

  // Get prefs proxy config if available.
  net::ProxyConfigWithAnnotation pref_config;
  ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config);

  // Get network proxy config if available.
  net::ProxyConfigWithAnnotation network_config;
  net::ProxyConfigService::ConfigAvailability network_availability =
      net::ProxyConfigService::CONFIG_UNSET;
  bool ignore_proxy = true;
  if (network) {
    ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE;
    const bool network_proxy_configured = GetNetworkProxyConfig(
        prefs(), local_state_prefs_, *network, &network_config, &onc_source);
    ignore_proxy =
        IgnoreProxy(profile_prefs_, network->profile_path(), onc_source);

    // If network is shared but use-shared-proxies is off, use direct mode.
    if (ignore_proxy) {
      network_config = net::ProxyConfigWithAnnotation::CreateDirect();
      network_availability = net::ProxyConfigService::CONFIG_VALID;
    } else if (network_proxy_configured) {
      // Network is private or shared with user using shared proxies.
      VLOG(1) << this << ": using proxy of network " << network->path();
      network_availability = net::ProxyConfigService::CONFIG_VALID;
    }
  }

  // Determine effective proxy config, either from prefs or network.
  ProxyPrefs::ConfigState effective_config_state;
  net::ProxyConfigWithAnnotation effective_config;
  GetEffectiveProxyConfig(pref_state, pref_config, network_availability,
                          network_config, ignore_proxy, &effective_config_state,
                          &effective_config);

  // If effective config is from system (i.e. network), it's considered a
  // special kind of prefs that ranks below policy/extension but above
  // others, so bump it up to CONFIG_OTHER_PRECEDE to force its precedence
  // when PrefProxyConfigTrackerImpl pushes it to ChromeProxyConfigService.
  if (effective_config_state == ProxyPrefs::CONFIG_SYSTEM)
    effective_config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE;
  // If config is manual, add rule to bypass local host.
  if (effective_config.value().proxy_rules().type !=
      net::ProxyConfig::ProxyRules::Type::EMPTY) {
    net::ProxyConfig proxy_config = effective_config.value();
    // TODO(https://crbug.com/902418): Is this rule still needed?
    proxy_config.proxy_rules()
        .bypass_rules.PrependRuleToBypassSimpleHostnames();
    effective_config = net::ProxyConfigWithAnnotation(
        proxy_config, effective_config.traffic_annotation());
  }
  PrefProxyConfigTrackerImpl::OnProxyConfigChanged(effective_config_state,
                                                   effective_config);
  if (VLOG_IS_ON(1)) {
    base::Value config_dict = effective_config.value().ToValue();
    VLOG(1) << this << ": Proxy changed: "
            << ProxyPrefs::ConfigStateToDebugString(effective_config_state)
            << ", " << config_dict;
  }
}

}  // namespace ash