File: proxy_policy_handler.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 (393 lines) | stat: -rw-r--r-- 15,195 bytes parent folder | download | duplicates (6)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2013 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/proxy_config/proxy_policy_handler.h"

#include <stddef.h>

#include <array>

#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "components/policy/core/browser/configuration_policy_handler.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/proxy_settings_constants.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/strings/grit/components_strings.h"

namespace proxy_config {

namespace {

using policy::kProxyPacMandatory;
using policy::PolicyErrorMap;
using policy::PolicyErrorPath;
using policy::PolicyMap;
using policy::key::kProxyBypassList;
using policy::key::kProxyMode;
using policy::key::kProxyPacUrl;
using policy::key::kProxyServer;
using policy::key::kProxyServerMode;
using policy::key::kProxySettings;

// This is used to check whether for a given ProxyMode value, the ProxyPacUrl,
// the ProxyBypassList and the ProxyServer policies are allowed to be specified.
// |error_message_id| is the message id of the localized error message to show
// when the policies are not specified as allowed. Each value of ProxyMode
// has a ProxyModeValidationEntry in the |kProxyModeValidationMap| below.
struct ProxyModeValidationEntry {
  const char* mode_value;
  bool pac_url_allowed;
  bool pac_mandatory_allowed;
  bool bypass_list_allowed;
  bool server_allowed;
  int error_message_id;
};

// List of entries determining which proxy policies can be specified, depending
// on the ProxyMode.
constexpr auto kProxyModeValidationMap =
    std::to_array<ProxyModeValidationEntry>({
        {ProxyPrefs::kDirectProxyModeName, false, false, false, false,
         IDS_POLICY_PROXY_MODE_DISABLED_ERROR},
        {ProxyPrefs::kAutoDetectProxyModeName, false, false, false, false,
         IDS_POLICY_PROXY_MODE_AUTO_DETECT_ERROR},
        {ProxyPrefs::kPacScriptProxyModeName, true, true, false, false,
         IDS_POLICY_PROXY_MODE_PAC_URL_ERROR},
        {ProxyPrefs::kFixedServersProxyModeName, false, false, true, true,
         IDS_POLICY_PROXY_MODE_FIXED_SERVERS_ERROR},
        {ProxyPrefs::kSystemProxyModeName, false, false, false, false,
         IDS_POLICY_PROXY_MODE_SYSTEM_ERROR},
    });

// Cannot be constexpr because the values of the strings are defined in an
// automatically generated .cc file.
const char* const kDeprecatedProxyPolicies[] = {
    kProxyMode, kProxyServerMode, kProxyServer, kProxyPacUrl, kProxyBypassList,
};

const base::Value* GetProxyPolicyValue(const base::Value* value,
                                       const char* policy_name) {
  if (!value) {
    return nullptr;
  }
  const base::Value::Dict* settings = value->GetIfDict();
  if (!settings) {
    return nullptr;
  }

  const base::Value* policy_value = settings->Find(policy_name);
  if (!policy_value || policy_value->is_none()) {
    return nullptr;
  }
  const std::string* tmp = policy_value->GetIfString();
  if (tmp && tmp->empty()) {
    return nullptr;
  }
  return policy_value;
}

// Converts the deprecated ProxyServerMode policy value to a ProxyMode value
// and places the result in |mode_value|. Returns whether the conversion
// succeeded.
bool CheckProxyModeAndServerMode(const base::Value* proxy_settings,
                                 PolicyErrorMap* errors,
                                 std::string* mode_value) {
  const base::Value* mode = GetProxyPolicyValue(proxy_settings, kProxyMode);
  const base::Value* server = GetProxyPolicyValue(proxy_settings, kProxyServer);
  const base::Value* server_mode =
      GetProxyPolicyValue(proxy_settings, kProxyServerMode);
  const base::Value* pac_url =
      GetProxyPolicyValue(proxy_settings, kProxyPacUrl);

  // If there's a server mode, convert it into a mode.
  // When both are specified, the mode takes precedence.
  if (mode) {
    if (server_mode) {
      errors->AddError(kProxySettings, IDS_POLICY_OVERRIDDEN, kProxyMode,
                       PolicyErrorPath{kProxyServerMode});
    }
    if (!mode->is_string()) {
      errors->AddError(kProxySettings, IDS_POLICY_TYPE_ERROR,
                       base::Value::GetTypeName(base::Value::Type::BOOLEAN),
                       PolicyErrorPath{kProxyMode});
      return false;
    }
    *mode_value = mode->GetString();

    ProxyPrefs::ProxyMode proxy_mode;
    if (!ProxyPrefs::StringToProxyMode(*mode_value, &proxy_mode)) {
      errors->AddError(kProxySettings, IDS_POLICY_INVALID_PROXY_MODE_ERROR,
                       PolicyErrorPath{kProxyMode});
      return false;
    }

    if (proxy_mode == ProxyPrefs::MODE_PAC_SCRIPT && !pac_url) {
      errors->AddError(kProxySettings, IDS_POLICY_NOT_SPECIFIED_ERROR,
                       PolicyErrorPath{kProxyPacUrl});
      return false;
    }
    if (proxy_mode == ProxyPrefs::MODE_FIXED_SERVERS && !server) {
      errors->AddError(kProxySettings, IDS_POLICY_NOT_SPECIFIED_ERROR,
                       PolicyErrorPath{kProxyServer});
      return false;
    }
  } else if (server_mode) {
    if (!server_mode->is_int()) {
      errors->AddError(kProxySettings, IDS_POLICY_TYPE_ERROR,
                       base::Value::GetTypeName(base::Value::Type::INTEGER),
                       PolicyErrorPath{kProxyServerMode});
      return false;
    }

    switch (server_mode->GetInt()) {
      case ProxyPolicyHandler::PROXY_SERVER_MODE:
        *mode_value = ProxyPrefs::kDirectProxyModeName;
        break;
      case ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE:
        *mode_value = ProxyPrefs::kAutoDetectProxyModeName;
        break;
      case ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE:
        if (server && pac_url) {
          int message_id = IDS_POLICY_PROXY_BOTH_SPECIFIED_ERROR;
          errors->AddError(kProxySettings, message_id,
                           PolicyErrorPath{kProxyServer});
          errors->AddError(kProxySettings, message_id,
                           PolicyErrorPath{kProxyPacUrl});
          return false;
        }
        if (!server && !pac_url) {
          int message_id = IDS_POLICY_PROXY_NEITHER_SPECIFIED_ERROR;
          errors->AddError(kProxySettings, message_id,
                           PolicyErrorPath{kProxyServer});
          errors->AddError(kProxySettings, message_id,
                           PolicyErrorPath{kProxyPacUrl});
          return false;
        }
        *mode_value = pac_url ? ProxyPrefs::kPacScriptProxyModeName
                              : ProxyPrefs::kFixedServersProxyModeName;
        break;
      case ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE:
        *mode_value = ProxyPrefs::kSystemProxyModeName;
        break;
      default:
        errors->AddError(kProxySettings, IDS_POLICY_OUT_OF_RANGE_ERROR,
                         base::NumberToString(server_mode->GetInt()),
                         PolicyErrorPath{kProxyServerMode});
        return false;
    }
  }
  return true;
}

// Maps the separate deprecated policies for proxy settings into a single
// Dictionary policy. This allows to keep the logic of merging policies from
// different sources simple, as all separate proxy policies should be considered
// as a single whole during merging. Returns proxy_settings value.
base::Value RemapProxyPolicies(const PolicyMap& policies) {
  // The highest (level, scope) pair for an existing proxy policy is determined
  // first, and then only policies with those exact attributes are merged.
  PolicyMap::Entry current_priority;  // Defaults to the lowest priority.
  policy::PolicySource inherited_source =
      policy::POLICY_SOURCE_ENTERPRISE_DEFAULT;
  base::Value::Dict proxy_settings;
  for (auto* policy : kDeprecatedProxyPolicies) {
    const PolicyMap::Entry* entry = policies.Get(policy);
    if (!entry)
      continue;
    if (policies.EntryHasHigherPriority(*entry, current_priority)) {
      current_priority = entry->DeepCopy();
      if (entry->source > inherited_source)  // Higher priority?
        inherited_source = entry->source;
    }
    // If two priorities are the same.
    if (!policies.EntryHasHigherPriority(*entry, current_priority) &&
        !policies.EntryHasHigherPriority(current_priority, *entry)) {
      // |value_unsafe| is used due to multiple policy types being handled.
      proxy_settings.Set(policy, entry->value_unsafe()->Clone());
    }
  }
  // Sets the new |proxy_settings| if kProxySettings isn't set yet, or if the
  // new priority is higher.
  const PolicyMap::Entry* existing = policies.Get(kProxySettings);
  if (!proxy_settings.empty() &&
      (!existing ||
       policies.EntryHasHigherPriority(current_priority, *existing))) {
    return base::Value(std::move(proxy_settings));
  } else if (existing && existing->value(base::Value::Type::DICT)) {
    return existing->value(base::Value::Type::DICT)->Clone();
  }
  return base::Value();
}

}  // namespace

// The proxy policies have the peculiarity that they are loaded from individual
// policies, but the providers then expose them through a unified
// DictionaryValue.

ProxyPolicyHandler::ProxyPolicyHandler() = default;

ProxyPolicyHandler::~ProxyPolicyHandler() = default;

bool ProxyPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
                                             PolicyErrorMap* errors) {
  base::Value proxy_settings = RemapProxyPolicies(policies);
  const base::Value* mode = GetProxyPolicyValue(&proxy_settings, kProxyMode);
  const base::Value* server =
      GetProxyPolicyValue(&proxy_settings, kProxyServer);
  const base::Value* server_mode =
      GetProxyPolicyValue(&proxy_settings, kProxyServerMode);
  const base::Value* pac_url =
      GetProxyPolicyValue(&proxy_settings, kProxyPacUrl);
  const base::Value* pac_mandatory =
      GetProxyPolicyValue(&proxy_settings, kProxyPacMandatory);
  const base::Value* bypass_list =
      GetProxyPolicyValue(&proxy_settings, kProxyBypassList);

  if ((server || pac_url || bypass_list) && !(mode || server_mode)) {
    errors->AddError(kProxySettings, IDS_POLICY_NOT_SPECIFIED_ERROR,
                     PolicyErrorPath{kProxyMode});
    return false;
  }

  std::string mode_value;
  if (!CheckProxyModeAndServerMode(&proxy_settings, errors, &mode_value))
    return false;

  // If neither ProxyMode nor ProxyServerMode are specified, mode_value will be
  // empty and the proxy shouldn't be configured at all.
  if (mode_value.empty())
    return true;

  bool is_valid_mode = false;
  for (size_t i = 0; i != std::size(kProxyModeValidationMap); ++i) {
    const ProxyModeValidationEntry& entry = kProxyModeValidationMap[i];
    if (entry.mode_value != mode_value)
      continue;

    is_valid_mode = true;

    if (!entry.pac_url_allowed && pac_url) {
      errors->AddError(kProxySettings, entry.error_message_id,
                       PolicyErrorPath{kProxyPacUrl});
    }
    if (!entry.pac_mandatory_allowed && pac_mandatory) {
      errors->AddError(kProxySettings, entry.error_message_id,
                       PolicyErrorPath{kProxyPacMandatory});
    }
    if (!entry.bypass_list_allowed && bypass_list) {
      errors->AddError(kProxySettings, entry.error_message_id,
                       PolicyErrorPath{kProxyBypassList});
    }
    if (!entry.server_allowed && server) {
      errors->AddError(kProxySettings, entry.error_message_id,
                       PolicyErrorPath{kProxyServer});
    }

    if ((!entry.pac_url_allowed && pac_url) ||
        (!entry.pac_mandatory_allowed && pac_mandatory) ||
        (!entry.bypass_list_allowed && bypass_list) ||
        (!entry.server_allowed && server)) {
      return false;
    }
  }

  if (!is_valid_mode) {
    errors->AddError(kProxySettings, IDS_POLICY_OUT_OF_RANGE_ERROR, mode_value,
                     PolicyErrorPath{mode ? kProxyMode : kProxyServerMode});
    return false;
  }
  return true;
}

void ProxyPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
                                             PrefValueMap* prefs) {
  base::Value proxy_settings = RemapProxyPolicies(policies);
  const base::Value* mode = GetProxyPolicyValue(&proxy_settings, kProxyMode);
  const base::Value* server =
      GetProxyPolicyValue(&proxy_settings, kProxyServer);
  const base::Value* server_mode =
      GetProxyPolicyValue(&proxy_settings, kProxyServerMode);
  const base::Value* pac_url =
      GetProxyPolicyValue(&proxy_settings, kProxyPacUrl);
  const base::Value* pac_mandatory =
      GetProxyPolicyValue(&proxy_settings, kProxyPacMandatory);
  const base::Value* bypass_list =
      GetProxyPolicyValue(&proxy_settings, kProxyBypassList);

  ProxyPrefs::ProxyMode proxy_mode;
  if (mode) {
    CHECK(mode->is_string());
    CHECK(ProxyPrefs::StringToProxyMode(mode->GetString(), &proxy_mode));
  } else if (server_mode) {
    switch (server_mode->GetInt()) {
      case PROXY_SERVER_MODE:
        proxy_mode = ProxyPrefs::MODE_DIRECT;
        break;
      case PROXY_AUTO_DETECT_PROXY_SERVER_MODE:
        proxy_mode = ProxyPrefs::MODE_AUTO_DETECT;
        break;
      case PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE:
        proxy_mode = ProxyPrefs::MODE_FIXED_SERVERS;
        if (pac_url)
          proxy_mode = ProxyPrefs::MODE_PAC_SCRIPT;
        break;
      case PROXY_USE_SYSTEM_PROXY_SERVER_MODE:
        proxy_mode = ProxyPrefs::MODE_SYSTEM;
        break;
      default:
        NOTREACHED();
    }
  } else {
    return;
  }

  auto set_proxy_pref_value = [&prefs](base::Value::Dict dict) {
    prefs->SetValue(prefs::kProxy, base::Value(std::move(dict)));
  };

  switch (proxy_mode) {
    case ProxyPrefs::MODE_DIRECT:
      set_proxy_pref_value(ProxyConfigDictionary::CreateDirect());
      break;
    case ProxyPrefs::MODE_AUTO_DETECT:
      set_proxy_pref_value(ProxyConfigDictionary::CreateAutoDetect());
      break;
    case ProxyPrefs::MODE_PAC_SCRIPT: {
      if (pac_url && pac_url->is_string()) {
        bool mandatory =
            pac_mandatory && pac_mandatory->GetIfBool().value_or(false);
        set_proxy_pref_value(ProxyConfigDictionary::CreatePacScript(
            pac_url->GetString(), mandatory));
      } else {
        NOTREACHED();
      }
      break;
    }
    case ProxyPrefs::MODE_FIXED_SERVERS: {
      if (server->is_string()) {
        set_proxy_pref_value(ProxyConfigDictionary::CreateFixedServers(
            server->GetString(), bypass_list && bypass_list->is_string()
                                     ? bypass_list->GetString()
                                     : std::string()));
      }
      break;
    }
    case ProxyPrefs::MODE_SYSTEM:
      set_proxy_pref_value(ProxyConfigDictionary::CreateSystem());
      break;
    case ProxyPrefs::kModeCount:
      NOTREACHED();
  }
}

}  // namespace proxy_config