File: simple_overrides_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (220 lines) | stat: -rw-r--r-- 9,577 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
// Copyright 2022 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/ui/extensions/extensions_overrides/simple_overrides.h"

#include <string>
#include <vector>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_split.h"
#include "chrome/common/extensions/api/chrome_url_overrides.h"
#include "chrome/common/extensions/api/omnibox.h"
#include "chrome/common/extensions/api/side_panel.h"
#include "extensions/common/api/content_scripts.h"
#include "extensions/common/api/cross_origin_isolation.h"
#include "extensions/common/api/declarative_net_request.h"
#include "extensions/common/api/incognito.h"
#include "extensions/common/api/oauth2.h"
#include "extensions/common/api/requirements.h"
#include "extensions/common/api/shared_module.h"
#include "extensions/common/api/web_accessible_resources.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/features/feature_provider.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace simple_overrides {

namespace {

// If any of the following are present, the extension is not considered a
// "simple override". The union of this set and the allowed features should
// encompass every feature. This ensures that when developers add a new
// feature, they consider whether it should be allowed for "simple override"
// extensions.
const char* kDisallowedFeatures[] = {
    // Manifest constants.
    extensions::manifest_keys::kAction,
    extensions::manifest_keys::kApp,
    extensions::manifest_keys::kAutomation,
    extensions::manifest_keys::kBackground,
    extensions::manifest_keys::kBackgroundPage,
    extensions::manifest_keys::kBackgroundPersistent,
    extensions::manifest_keys::kBackgroundScripts,
    extensions::manifest_keys::kBackgroundServiceWorkerScript,
    extensions::manifest_keys::kBluetooth,
    extensions::manifest_keys::kBrowserAction,
    extensions::manifest_keys::kCommands,
    extensions::manifest_keys::kContentCapabilities,
    extensions::manifest_keys::kContentSecurityPolicy,
    extensions::manifest_keys::kConvertedFromUserScript,
    extensions::manifest_keys::kDevToolsPage,
    extensions::manifest_keys::kDisplayInLauncher,
    extensions::manifest_keys::kDisplayInNewTabPage,
    extensions::manifest_keys::kEventRules,
    extensions::manifest_keys::kExternallyConnectable,
    extensions::manifest_keys::kFileBrowserHandlers,
    extensions::manifest_keys::kFileHandlers,
    extensions::manifest_keys::kHostPermissions,
    extensions::manifest_keys::kInputComponents,
    extensions::manifest_keys::kKiosk,
    extensions::manifest_keys::kKioskAlwaysUpdate,
    extensions::manifest_keys::kKioskEnabled,
    extensions::manifest_keys::kKioskOnly,
    extensions::manifest_keys::kKioskRequiredPlatformVersion,
    extensions::manifest_keys::kKioskSecondaryApps,
    extensions::manifest_keys::kLaunch,
    extensions::manifest_keys::kLinkedAppIcons,
    extensions::manifest_keys::kMIMETypes,
    extensions::manifest_keys::kMimeTypesHandler,
    extensions::manifest_keys::kNaClModules,
    extensions::manifest_keys::kNativelyConnectable,
    extensions::manifest_keys::kOptionalHostPermissions,
    extensions::manifest_keys::kOptionalPermissions,
    extensions::manifest_keys::kPageAction,
    extensions::manifest_keys::kPermissions,
    extensions::manifest_keys::kPlatformAppBackground,
    extensions::manifest_keys::kPlatformAppContentSecurityPolicy,
    extensions::manifest_keys::kReplacementWebApp,
    extensions::manifest_keys::kSockets,
    extensions::manifest_keys::kTheme,
    extensions::manifest_keys::kTrialTokens,
    extensions::manifest_keys::kTtsEngine,
    extensions::manifest_keys::kUrlHandlers,
    extensions::manifest_keys::kUsbPrinters,
    extensions::manifest_keys::kWebview,

    // Compiled manifest keys.
    extensions::api::chrome_url_overrides::ManifestKeys::kChromeUrlOverrides,
    extensions::api::content_scripts::ManifestKeys::kContentScripts,
    extensions::api::cross_origin_isolation::ManifestKeys::
        kCrossOriginEmbedderPolicy,
    extensions::api::cross_origin_isolation::ManifestKeys::
        kCrossOriginOpenerPolicy,
    extensions::api::declarative_net_request::ManifestKeys::
        kDeclarativeNetRequest,
    extensions::api::oauth2::ManifestKeys::kOauth2,
    extensions::api::omnibox::ManifestKeys::kOmnibox,
    extensions::api::requirements::ManifestKeys::kRequirements,
    extensions::api::shared_module::ManifestKeys::kExport,
    extensions::api::shared_module::ManifestKeys::kImport,
    extensions::api::side_panel::ManifestKeys::kSidePanel,
    extensions::api::web_accessible_resources::ManifestKeys::
        kWebAccessibleResources,

    // The following features are only available on ChromeOS. However, the
    // entries in manifest_features are compiled for every OS (so that we can
    // populate availability messages if an extension requests them), so we
    // need to ensure they are included in this list.
    "action_handlers",
    "file_system_provider_capabilities",

// Unlike the keys above, chromeos_system_extension *is* only defined on
// ChromeOS.
#if BUILDFLAG(IS_CHROMEOS)
    extensions::manifest_keys::kChromeOSSystemExtension,
#endif

    // The following features have no declared constant, but are present in
    // the manifest_features file (they may be used only in a single other file,
    // and thus not exposed in a .h).
    "chrome_url_overrides.activationmessage",
    "chrome_url_overrides.keyboard",
    "oauth2.auto_approve",
    "platforms",
    "sandbox",
    "storage",
};

}  // namespace

// Ensures that all features are either specified in the allowed or disallowed
// feature lists (so that new features are guaranteed to be evaluated).
TEST(ExtensionSimpleOverridesTest,
     AllFeaturesAreAllowlistedOrInDisallowedFeatures) {
  const extensions::FeatureProvider* manifest_features =
      extensions::FeatureProvider::GetManifestFeatures();
  ASSERT_TRUE(manifest_features);

  const extensions::FeatureMap& known_features =
      manifest_features->GetAllFeatures();

  std::vector<std::string> allowlisted_features =
      simple_overrides::GetAllowlistedManifestKeysForTesting();

  std::vector<std::string> disallowed_features(std::begin(kDisallowedFeatures),
                                               std::end(kDisallowedFeatures));

  // Verify that all disallowed features are recognized and that none are in
  // both the disallowed and allowed feature sets.
  for (const auto& feature : disallowed_features) {
    EXPECT_TRUE(base::Contains(known_features, feature))
        << "Unknown feature: " << feature;
    EXPECT_FALSE(base::Contains(allowlisted_features, feature))
        << "Feature in both allowed and disallowed: " << feature;
  }

  // Verify that all allowed features are recognized and that none are in
  // both the disallowed and allowed feature sets.
  for (const auto& feature : allowlisted_features) {
    EXPECT_TRUE(base::Contains(known_features, feature))
        << "Unknown feature: " << feature;
    EXPECT_FALSE(base::Contains(disallowed_features, feature))
        << "Feature in both allowed and disallowed: " << feature;
  }

  // Verify that all known features are in either the allowed or disallowed
  // set.
  for (const auto& [key, value] : known_features) {
    // Is this expectation failing for you?
    // Review the comment in simple_overrides.cc above the allowed feature
    // list and evaluate whether your new feature belongs in the allowed or
    // disallowed features list (it should likely be disallowed).
    EXPECT_TRUE(base::Contains(disallowed_features, key) ||
                base::Contains(allowlisted_features, key))
        << "Unknown feature: " << key;
  }
}

// Verifies that an extension that contains only allowlisted features is
// considered a simple override.
TEST(ExtensionSimpleOverridesTest,
     ExtensionWithOnlyAllowlistedFeaturesIsConsideredSimple) {
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("alpha")
          .SetManifestKey("description", "a description")
          .SetManifestKey("author", "some author")
          .SetManifestKey("incognito", "split")
          .SetManifestKey("short_name", "a")
          .Build();
  EXPECT_TRUE(simple_overrides::IsSimpleOverrideExtension(*extension));
}

// Verifies that an extension that contains any non-allowlisted features is
// not considered a simple override.
TEST(ExtensionSimpleOverridesTest,
     ExtensionWithPermissionsIsNotConsideredSimple) {
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("alpha").AddAPIPermission("tabs").Build();
  EXPECT_FALSE(simple_overrides::IsSimpleOverrideExtension(*extension));
}

// Verifies that an extension that contains unrecognized manifest keys *is*
// considered a simple override.  This allows extensions to have non-chrome
// keys (like "minimum_browser_version").
TEST(ExtensionSimpleOverridesTest,
     ExtensionWithUnrecognizedKeysIsConsideredSimple) {
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("alpha")
          .SetManifestKey("unknown_key", "unknown_value")
          .Build();
  EXPECT_FALSE(simple_overrides::IsSimpleOverrideExtension(*extension));
}

}  // namespace simple_overrides