File: standard_management_policy_provider.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 (282 lines) | stat: -rw-r--r-- 9,572 bytes parent folder | download | duplicates (3)
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
// 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 "chrome/browser/extensions/standard_management_policy_provider.h"

#include <string>

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/managed_installation_mode.h"
#include "chrome/browser/extensions/manifest_v2_experiment_manager.h"
#include "chrome/grit/generated_resources.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest.h"
#include "extensions/strings/grit/extensions_strings.h"
#include "ui/base/l10n/l10n_util.h"

namespace extensions {

namespace {

// Returns whether the extension can be modified under admin policy or not, and
// fills |error| with corresponding error message if necessary.
bool AdminPolicyIsModifiable(const Extension* source_extension,
                             const Extension* extension,
                             std::u16string* error) {
  // Component and force installed extensions can enable/disable all other
  // extensions including force installed ones (but component are off limits).
  const bool component_or_force_installed =
      source_extension &&
      (Manifest::IsComponentLocation(source_extension->location()) ||
       Manifest::IsPolicyLocation(source_extension->location()));

  // We also specifically disallow the Webstore to modify force installed
  // extensions even though it is a component extension, because it doesn't
  // need this capability and it can open up interesting attacks if it's
  // leveraged via bookmarklets or devtools.
  // TODO(crbug.com/40239460): This protection should be expanded by also
  // blocking bookmarklets on the Webstore Origin through checks on the Blink
  // side.
  const bool is_webstore_hosted_app =
      source_extension && source_extension->id() == extensions::kWebStoreAppId;

  bool is_modifiable = true;

  if (Manifest::IsComponentLocation(extension->location()))
    is_modifiable = false;
  if ((!component_or_force_installed || is_webstore_hosted_app) &&
      Manifest::IsPolicyLocation(extension->location())) {
    is_modifiable = false;
  }

  if (is_modifiable)
    return true;

  if (error) {
    *error = l10n_util::GetStringFUTF16(
        IDS_EXTENSION_CANT_MODIFY_POLICY_REQUIRED,
        base::UTF8ToUTF16(extension->name()));
  }

  return false;
}

}  // namespace

StandardManagementPolicyProvider::StandardManagementPolicyProvider(
    ExtensionManagement* settings,
    Profile* profile)
    : profile_(profile), settings_(settings) {}

StandardManagementPolicyProvider::~StandardManagementPolicyProvider() = default;

std::string
    StandardManagementPolicyProvider::GetDebugPolicyProviderName() const {
#if DCHECK_IS_ON()
  return "extension management policy controlled settings";
#else
  base::ImmediateCrash();
#endif
}

bool StandardManagementPolicyProvider::UserMayLoad(
    const Extension* extension,
    std::u16string* error) const {
  if (Manifest::IsComponentLocation(extension->location())) {
    return true;
  }

  // Shared modules are always allowed too: they only contain resources that
  // are used by other extensions. The extension that depends on the shared
  // module may be filtered by policy.
  if (extension->is_shared_module())
    return true;

  // Check whether the extension type is allowed.
  //
  // If you get a compile error here saying that the type you added is not
  // handled by the switch statement below, please consider whether enterprise
  // policy should be able to disallow extensions of the new type. If so, add
  // a branch to the second block and add a line to the definition of
  // kAllowedTypesMap in extension_management_constants.h.
  switch (extension->GetType()) {
    case Manifest::TYPE_UNKNOWN:
      break;
    case Manifest::TYPE_EXTENSION:
    case Manifest::TYPE_THEME:
    case Manifest::TYPE_USER_SCRIPT:
    case Manifest::TYPE_HOSTED_APP:
    case Manifest::TYPE_LEGACY_PACKAGED_APP:
    case Manifest::TYPE_PLATFORM_APP:
    case Manifest::TYPE_SHARED_MODULE:
    case Manifest::TYPE_LOGIN_SCREEN_EXTENSION:
    case Manifest::TYPE_CHROMEOS_SYSTEM_EXTENSION: {
      if (!settings_->IsAllowedManifestType(extension->GetType(),
                                            extension->id()))
        return ReturnLoadError(extension, error);
      break;
    }
    case Manifest::NUM_LOAD_TYPES:
      NOTREACHED();
  }

  ManagedInstallationMode installation_mode =
      settings_->GetInstallationMode(extension);
  if (installation_mode == ManagedInstallationMode::kBlocked ||
      installation_mode == ManagedInstallationMode::kRemoved) {
    return ReturnLoadError(extension, error);
  }

  if (!settings_->IsAllowedManifestVersion(extension)) {
    if (error) {
      *error = l10n_util::GetStringFUTF16(
          IDS_EXTENSION_MANIFEST_VERSION_NOT_SUPPORTED,
          base::UTF8ToUTF16(extension->name()));
    }
    return false;
  }

  return true;
}

bool StandardManagementPolicyProvider::UserMayInstall(
    const Extension* extension,
    std::u16string* error) const {
  ManagedInstallationMode installation_mode =
      settings_->GetInstallationMode(extension);

  // Force-installed extensions cannot be overwritten manually.
  if (!Manifest::IsPolicyLocation(extension->location()) &&
      installation_mode == ManagedInstallationMode::kForced) {
    return ReturnLoadError(extension, error);
  }

  // Check if the extension would be force-disabled once it's installed. If it
  // would, block the new installation.
  auto* mv2_experiment_manager = ManifestV2ExperimentManager::Get(profile_);
  if (mv2_experiment_manager &&
      mv2_experiment_manager->ShouldBlockExtensionEnable(*extension)) {
    *error =
        l10n_util::GetStringUTF16(IDS_EXTENSIONS_CANT_INSTALL_MV2_EXTENSION);
    return false;
  }

  return UserMayLoad(extension, error);
}

bool StandardManagementPolicyProvider::UserMayModifySettings(
    const Extension* extension,
    std::u16string* error) const {
  return AdminPolicyIsModifiable(nullptr, extension, error);
}

bool StandardManagementPolicyProvider::ExtensionMayModifySettings(
    const Extension* source_extension,
    const Extension* extension,
    std::u16string* error) const {
  return AdminPolicyIsModifiable(source_extension, extension, error);
}

bool StandardManagementPolicyProvider::MustRemainEnabled(
    const Extension* extension,
    std::u16string* error) const {
  return !AdminPolicyIsModifiable(nullptr, extension, error);
}

bool StandardManagementPolicyProvider::MustRemainDisabled(
    const Extension* extension,
    disable_reason::DisableReason* reason) const {
  std::string required_version;
  if (!settings_->CheckMinimumVersion(extension, &required_version)) {
    if (reason) {
      *reason = disable_reason::DISABLE_UPDATE_REQUIRED_BY_POLICY;
    }
    return true;
  }

  if (!settings_->IsAllowedByUnpublishedAvailabilityPolicy(extension)) {
    if (reason) {
      *reason = disable_reason::DISABLE_PUBLISHED_IN_STORE_REQUIRED_BY_POLICY;
    }
    return true;
  }

  if (!settings_->IsAllowedByUnpackedDeveloperModePolicy(*extension)) {
    if (reason) {
      *reason = disable_reason::DISABLE_UNSUPPORTED_DEVELOPER_EXTENSION;
    }
    return true;
  }

  if (settings_->ShouldBlockForceInstalledOffstoreExtension(*extension)) {
    if (reason) {
      *reason = disable_reason::DISABLE_NOT_VERIFIED;
    }
    return true;
  }

  // Note: `mv2_experiment_manager` may be null for certain types of profiles
  // (such as the sign-in profile). We can ignore this check in this case, since
  // users can't install extensions in these profiles.
  auto* mv2_experiment_manager = ManifestV2ExperimentManager::Get(profile_);
  if (mv2_experiment_manager &&
      mv2_experiment_manager->ShouldBlockExtensionEnable(*extension)) {
    if (reason) {
      *reason = disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION;
    }

    return true;
  }

  return false;
}

bool StandardManagementPolicyProvider::MustRemainInstalled(
    const Extension* extension,
    std::u16string* error) const {
  ManagedInstallationMode mode = settings_->GetInstallationMode(extension);
  // Disallow removing of recommended extension, to avoid re-install it
  // again while policy is reload. But disabling of recommended extension is
  // allowed.
  if (mode == ManagedInstallationMode::kForced ||
      mode == ManagedInstallationMode::kRecommended) {
    if (error) {
      *error = l10n_util::GetStringFUTF16(
          IDS_EXTENSION_CANT_UNINSTALL_POLICY_REQUIRED,
          base::UTF8ToUTF16(extension->name()));
    }
    return true;
  }
  return false;
}

bool StandardManagementPolicyProvider::ShouldForceUninstall(
    const Extension* extension,
    std::u16string* error) const {
  if (UserMayLoad(extension, error))
    return false;
  if (settings_->GetInstallationMode(extension) ==
      ManagedInstallationMode::kRemoved) {
    return true;
  }
  return false;
}

bool StandardManagementPolicyProvider::ReturnLoadError(
    const extensions::Extension* extension,
    std::u16string* error) const {
  if (error) {
    *error = l10n_util::GetStringFUTF16(
        IDS_EXTENSION_CANT_INSTALL_POLICY_BLOCKED,
        base::UTF8ToUTF16(extension->name()),
        base::UTF8ToUTF16(extension->id()),
        base::UTF8ToUTF16(settings_->BlockedInstallMessage(extension->id())));
  }
  return false;
}

}  // namespace extensions