File: supervised_user_extension_browsertest.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 (392 lines) | stat: -rw-r--r-- 16,159 bytes parent folder | download | duplicates (4)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/test/gtest_util.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/supervised_user/supervised_user_extensions_delegate_impl.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/supervised_user/parent_permission_dialog_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chrome/test/supervised_user/supervision_mixin.h"
#include "components/supervised_user/core/browser/supervised_user_preferences.h"
#include "components/supervised_user/core/browser/supervised_user_utils.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_launcher.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/supervised_user_extensions_delegate.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/supervised_user/chromeos/parent_access_extension_approvals_manager.h"
#include "chromeos/crosapi/mojom/parent_access.mojom.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace {
constexpr char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
}  // namespace

namespace extensions {

using SupervisionMixinSigninModeCallback =
    base::RepeatingCallback<supervised_user::SupervisionMixin::SignInMode()>;

// Tests interaction between supervised users and extensions.
class SupervisionExtensionTestBase
    : public InProcessBrowserTestMixinHostSupport<ExtensionBrowserTest>,
      public ::testing::WithParamInterface<SupervisionMixinSigninModeCallback> {
 public:
  SupervisionExtensionTestBase() = default;

 protected:
  bool IsDisabledForCustodianApproval(const std::string& extension_id) {
    ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile());
    return extension_prefs->HasDisableReason(
        extension_id,
        extensions::disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);
  }

  void InstallExtensionAndCheckStatus(
      bool should_be_loaded,
      bool should_be_enabled,
      const std::string& extension_id = kGoodCrxId,
      const std::string& extension_crx = "good.crx") {
    base::FilePath path = test_data_dir_.AppendASCII(extension_crx);
    EXPECT_EQ(LoadExtension(path) != nullptr, should_be_loaded);
    const Extension* extension =
        extension_registry()->GetInstalledExtension(extension_id);
    EXPECT_TRUE(extension);

    EXPECT_EQ(
        extension_registry()->disabled_extensions().Contains(extension_id),
        !should_be_enabled);
    EXPECT_EQ(IsDisabledForCustodianApproval(extension_id), !should_be_enabled);
    EXPECT_EQ(extension_registry()->enabled_extensions().Contains(extension_id),
              should_be_enabled);
  }

  supervised_user::SupervisionMixin::SignInMode GetMixinSigninMode() {
    return GetParam().Run();
  }

 private:
  supervised_user::SupervisionMixin supervision_mixin_{
      mixin_host_,
      this,
      embedded_test_server(),
      {.sign_in_mode = GetMixinSigninMode()}};
};

// Tests interaction between supervised users and extensions after the optional
// supervision is removed from the account.
class SupervisionRemovalExtensionTest : public SupervisionExtensionTestBase {};

// Tests that removing supervision should also remove associated disable
// reasons, such as DISABLE_CUSTODIAN_APPROVAL_REQUIRED. Extensions should
// become enabled again after removing supervision. Prevents a regression to
// crbug.com/1045625.
IN_PROC_BROWSER_TEST_P(SupervisionRemovalExtensionTest,
                       PRE_RemoveCustodianApprovalRequirement) {
  ASSERT_TRUE(profile()->IsChild());

  base::FilePath path = test_data_dir_.AppendASCII("good.crx");
  bool extension_should_be_loaded = false;
  EXPECT_EQ(LoadExtension(path) != nullptr, extension_should_be_loaded);
  const Extension* extension =
      extension_registry()->GetInstalledExtension(kGoodCrxId);
  EXPECT_TRUE(extension);

    // This extension is a supervised user initiated install and should remain
    // disabled.
    EXPECT_TRUE(
        extension_registry()->disabled_extensions().Contains(kGoodCrxId));
    EXPECT_TRUE(IsDisabledForCustodianApproval(kGoodCrxId));
}

IN_PROC_BROWSER_TEST_P(SupervisionRemovalExtensionTest,
                       RemoveCustodianApprovalRequirement) {
  ASSERT_FALSE(profile()->IsChild());

  // The extension should still be installed since we are sharing the same data
  // directory as the PRE test.
  const Extension* extension =
      extension_registry()->GetInstalledExtension(kGoodCrxId);
  EXPECT_TRUE(extension);

  // The extension should be enabled now after removing supervision.
  EXPECT_TRUE(extension_registry()->enabled_extensions().Contains(kGoodCrxId));
  EXPECT_FALSE(
      extension_registry()->disabled_extensions().Contains(kGoodCrxId));

  EXPECT_FALSE(IsDisabledForCustodianApproval(kGoodCrxId));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    SupervisionRemovalExtensionTest,
    testing::Values(base::BindRepeating([]() {
      // The test covers the removal of supervision. Pre-test should start with
      // a supervised profile, main test with a regular profile.
      return content::IsPreTest()
                 ? supervised_user::SupervisionMixin::SignInMode::kSupervised
                 : supervised_user::SupervisionMixin::SignInMode::kRegular;
    })));

// Tests interaction between supervised users and extensions after the optional
// supervision is added to an the account.
class UserGellerizationExtensionTest : public SupervisionExtensionTestBase {
 public:
  UserGellerizationExtensionTest() = default;
};

IN_PROC_BROWSER_TEST_P(UserGellerizationExtensionTest,
                       PRE_UserGellerizationDisablesExistingExtensions) {
  ASSERT_FALSE(profile()->IsChild());
  base::FilePath path = test_data_dir_.AppendASCII("good.crx");
  EXPECT_TRUE(LoadExtension(path) != nullptr);
  const Extension* extension =
      extension_registry()->GetInstalledExtension(kGoodCrxId);
  EXPECT_TRUE(extension);

  // The extension is installed and enabled.
  EXPECT_TRUE(extension_registry()->enabled_extensions().Contains(kGoodCrxId));
}

IN_PROC_BROWSER_TEST_P(UserGellerizationExtensionTest,
                       UserGellerizationDisablesExistingExtensions) {
  ASSERT_TRUE(profile()->IsChild());

  // The extension should still be installed since we are sharing the same data
  // directory as the PRE test.
  const Extension* extension =
      extension_registry()->GetInstalledExtension(kGoodCrxId);
  EXPECT_TRUE(extension);

  // The extension should be disabled, pending parent approval.
  EXPECT_TRUE(extension_registry()->disabled_extensions().Contains(kGoodCrxId));
  EXPECT_TRUE(IsDisabledForCustodianApproval(kGoodCrxId));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    UserGellerizationExtensionTest,
    testing::Values(base::BindRepeating([]() {
      //  Pre-test should start with a regular
      //  profile, main test with a supervised profile.
      return content::IsPreTest()
                 ? supervised_user::SupervisionMixin::SignInMode::kRegular
                 : supervised_user::SupervisionMixin::SignInMode::kSupervised;
    })));

// Tests the parental controls applied on extensions for supervised users
// under different values of the Family Link Extensions Switch
// ("Allow to add extensions without asking for permission").
class ParentApprovalHandlingByExtensionSwitchTest
    : public SupervisionExtensionTestBase {
 public:
  ParentApprovalHandlingByExtensionSwitchTest() = default;
};

IN_PROC_BROWSER_TEST_P(
    ParentApprovalHandlingByExtensionSwitchTest,
    PRE_GrantParentApprovalWhenExtensionSwitchBecomesEnabled) {
  ASSERT_TRUE(profile()->IsChild());

  // Set the Extensions preference to OFF, requiring parent approval for
  // installations.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), false);
  bool should_be_loaded = false;
  bool should_be_enabled = false;
  InstallExtensionAndCheckStatus(should_be_loaded, should_be_enabled);
}

IN_PROC_BROWSER_TEST_P(ParentApprovalHandlingByExtensionSwitchTest,
                       GrantParentApprovalWhenExtensionSwitchBecomesEnabled) {
  ASSERT_TRUE(profile()->IsChild());

  // Flip the Extensions preference to ON.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), true);

  // The extension should become enabled and parent-approved.
  EXPECT_TRUE(extension_registry()->enabled_extensions().Contains(kGoodCrxId));

  // Flip the Extensions preference to OFF.
  // The previously approved and enabled extensions should remain enabled.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), false);
  EXPECT_TRUE(extension_registry()->enabled_extensions().Contains(kGoodCrxId));
}

IN_PROC_BROWSER_TEST_P(
    ParentApprovalHandlingByExtensionSwitchTest,
    GrantParentParentApprovalOnInstallationIfExtensionSwitchEnabled) {
  // Set the Extensions preference to ON.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), true);
  bool should_be_loaded = true;
  bool should_be_enabled = should_be_loaded;
  InstallExtensionAndCheckStatus(should_be_loaded, should_be_enabled);

  // Flip the Extensions preference to OFF.
  // The previously approved and enabled extensions should remain enabled.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), false);
  EXPECT_EQ(should_be_enabled,
            extension_registry()->enabled_extensions().Contains(kGoodCrxId));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    ParentApprovalHandlingByExtensionSwitchTest,
    testing::Values(base::BindRepeating([]() {
      return supervised_user::SupervisionMixin::SignInMode::kSupervised;
    })));

class ParentApprovalRequestTest
    : public SupervisionExtensionTestBase,
#if BUILDFLAG(IS_CHROMEOS)
      public TestExtensionApprovalsManagerObserver,
#endif
      public TestParentPermissionDialogViewObserver {
 public:
  ParentApprovalRequestTest()
      :
#if BUILDFLAG(IS_CHROMEOS)
        TestExtensionApprovalsManagerObserver(this),
#endif
        TestParentPermissionDialogViewObserver(this) {
  }

#if BUILDFLAG(IS_CHROMEOS)
  // TestExtensionApprovalsManagerObserver implementation:
  void OnTestParentAccessDialogCreated() override {
    parent_permission_dialog_appeared_ = true;
    SetParentAccessDialogResult(crosapi::mojom::ParentAccessResult::NewCanceled(
        crosapi::mojom::ParentAccessCanceledResult::New()));
  }
#endif

  // TestParentPermissionDialogViewObserver implementation:
  void OnTestParentPermissionDialogViewCreated(
      ParentPermissionDialogView* view) override {
    parent_permission_dialog_appeared_ = true;
  }

  void ClearCustodianPrefs() {
    // Clears the preferences relating to both custodians.
    profile()->GetPrefs()->ClearPref(prefs::kSupervisedUserCustodianProfileURL);
    profile()->GetPrefs()->ClearPref(prefs::kSupervisedUserCustodianName);
    profile()->GetPrefs()->ClearPref(prefs::kSupervisedUserCustodianEmail);
    profile()->GetPrefs()->ClearPref(
        prefs::kSupervisedUserCustodianObfuscatedGaiaId);
    profile()->GetPrefs()->ClearPref(
        prefs::kSupervisedUserCustodianProfileImageURL);
    profile()->GetPrefs()->ClearPref(
        prefs::kSupervisedUserSecondCustodianProfileURL);
    profile()->GetPrefs()->ClearPref(prefs::kSupervisedUserSecondCustodianName);
    profile()->GetPrefs()->ClearPref(
        prefs::kSupervisedUserSecondCustodianEmail);
    profile()->GetPrefs()->ClearPref(
        prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId);
    profile()->GetPrefs()->ClearPref(
        prefs::kSupervisedUserSecondCustodianProfileImageURL);
  }

  bool parent_permission_dialog_appeared_ = false;
};

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
// Tests that installation fails when the custodian info is missing and the
// right histograms are recorded. Regression test for crbug.com/35071637.
IN_PROC_BROWSER_TEST_P(ParentApprovalRequestTest,
                       RequestToInstallExtensionMissingCustodianInfo) {
  // Set the preferences to the default values from Family Link.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), false);

  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("An extension").Build();
  CHECK(extension);

  ClearCustodianPrefs();

  // Request Approval to add a new extension.
  base::HistogramTester histogram_tester;
  SkBitmap icon;
  auto supervised_user_extensions_delegate =
      std::make_unique<SupervisedUserExtensionsDelegateImpl>(profile());
  supervised_user_extensions_delegate->RequestToAddExtensionOrShowError(
      *extension.get(), browser()->tab_strip_model()->GetActiveWebContents(),
      gfx::ImageSkia::CreateFrom1xBitmap(icon),
      SupervisedUserExtensionParentApprovalEntryPoint::kOnWebstoreInstallation,
      base::DoNothing());

  // The dialog should not have appeared.
  EXPECT_FALSE(parent_permission_dialog_appeared_);
  histogram_tester.ExpectBucketCount(SupervisedUserExtensionsMetricsRecorder::
                                         kParentPermissionDialogHistogramName,
                                     SupervisedUserExtensionsMetricsRecorder::
                                         ParentPermissionDialogState::kFailed,
                                     1);
  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::
          kParentPermissionDialogHistogramName,
      SupervisedUserExtensionsMetricsRecorder::ParentPermissionDialogState::
          kNoParentError,
      1);
}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

// Tests that the method to request extension approval can be triggered
// without errors for new (uninstalled) extensions that already have been
// granted parent approval.
// Prevents regressions to b/321016032.
IN_PROC_BROWSER_TEST_P(ParentApprovalRequestTest,
                       RequestToInstallApprovedExtension) {
  // Create and extension and give it parent approval.
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("An extension").Build();
  CHECK(extension);

  base::Value::Dict approved_extensions;
  approved_extensions.Set(extension->id(), true);
  profile()->GetPrefs()->SetDict(prefs::kSupervisedUserApprovedExtensions,
                                 std::move(approved_extensions));
  ASSERT_FALSE(extension_registry()->GetInstalledExtension(extension->id()));

  // Request Approval to add a new extension,
  SkBitmap icon;
  auto supervised_user_extensions_delegate =
      std::make_unique<SupervisedUserExtensionsDelegateImpl>(profile());
  supervised_user_extensions_delegate->RequestToAddExtensionOrShowError(
      *extension.get(), browser()->tab_strip_model()->GetActiveWebContents(),
      gfx::ImageSkia::CreateFrom1xBitmap(icon),
      SupervisedUserExtensionParentApprovalEntryPoint::kOnWebstoreInstallation,
      base::DoNothing());
  // Confirm that the parent approval dialog for extensions for each OS is
  // created.
  EXPECT_TRUE(parent_permission_dialog_appeared_);
}

INSTANTIATE_TEST_SUITE_P(
    All,
    ParentApprovalRequestTest,
    testing::Values(base::BindRepeating([]() {
      return supervised_user::SupervisionMixin::SignInMode::kSupervised;
    })));

}  // namespace extensions