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
|
// Copyright 2024 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/strings/string_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/policy_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/web_applications/sub_apps_install_dialog_controller.h"
#include "chrome/browser/ui/web_applications/sub_apps_service_impl.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "third_party/blink/public/common/features.h"
using blink::mojom::SubAppsService;
using testing::AllOf;
using testing::Eq;
using testing::HasSubstr;
using testing::IsEmpty;
using testing::IsFalse;
using testing::IsTrue;
using testing::Not;
using testing::SizeIs;
namespace web_app {
namespace {
constexpr const char kSub1[] = "/sub1/page.html";
constexpr const char kSub2[] = "/sub2/page.html";
} // namespace
typedef base::AutoReset<
std::optional<SubAppsInstallDialogController::DialogActionForTesting>>
DialogOverride;
class SubAppsAdminPolicyTest : public IsolatedWebAppBrowserTestHarness {
public:
SubAppsAdminPolicyTest() = default;
~SubAppsAdminPolicyTest() override = default;
void SetUpInProcessBrowserTestFixture() override {
policy_provider_.SetDefaultReturns(
/*is_initialization_complete_return=*/true,
/*is_first_policy_load_complete_return=*/true);
policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
&policy_provider_);
IsolatedWebAppBrowserTestHarness::SetUpInProcessBrowserTestFixture();
}
content::RenderFrameHost* InstallAndOpenParentIwaApp() {
IsolatedWebAppUrlInfo parent_app = InstallIwaParentApp();
return OpenApp(parent_app.app_id());
}
IsolatedWebAppUrlInfo InstallIwaParentApp() {
std::unique_ptr<ScopedBundledIsolatedWebApp> app =
IsolatedWebAppBuilder(
ManifestBuilder().AddPermissionsPolicy(
network::mojom::PermissionsPolicyFeature::kSubApps,
/*self=*/true,
/*origins=*/{}))
.AddFolderFromDisk("/", "web_apps/subapps_isolated_app")
.BuildBundle();
app->TrustSigningKey();
IsolatedWebAppUrlInfo parent_app = app->InstallChecked(profile());
parent_app_id_ = parent_app.app_id();
EXPECT_EQ(provider().registrar_unsafe().GetInstallState(parent_app_id_),
proto::InstallState::INSTALLED_WITH_OS_INTEGRATION);
EXPECT_THAT(provider().registrar_unsafe().IsIsolated(parent_app_id_),
IsTrue());
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), IsEmpty());
return parent_app;
}
std::vector<webapps::AppId> GetAllSubAppIds(
const webapps::AppId& parent_app_id) {
return provider().registrar_unsafe().GetAllSubAppIds(parent_app_id);
}
// sub_app_paths should contain paths, not full URLs.
std::string AddSubAppsScript(
const std::vector<std::string>& sub_app_paths) const {
std::vector<std::string> script_parts;
const std::string format = R"("$1": {"installURL": "$1"},)";
script_parts.push_back("navigator.subApps.add({");
for (const std::string& path : sub_app_paths) {
script_parts.push_back(
base::ReplaceStringPlaceholders(format, {path}, nullptr));
}
script_parts.push_back("})");
return base::StrCat(script_parts);
}
void SetAllowlistedOrigins(
const std::vector<std::string>& allowlisted_origins) {
policy::PolicyMap policy_map;
base::Value::List allowed_origins;
for (auto& origin : allowlisted_origins) {
allowed_origins.Append(base::Value(origin));
}
policy_map.Set(
policy::key::
kSubAppsAPIsAllowedWithoutGestureAndAuthorizationForOrigins,
policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
policy::POLICY_SOURCE_PLATFORM, base::Value(std::move(allowed_origins)),
nullptr);
policy_provider_.UpdateChromePolicy(policy_map);
}
DialogOverride AcceptConfirmationDialog() {
return SubAppsInstallDialogController::SetAutomaticActionForTesting(
SubAppsInstallDialogController::DialogActionForTesting::kAccept);
}
DialogOverride DeclineConfirmationDialog() {
return SubAppsInstallDialogController::SetAutomaticActionForTesting(
SubAppsInstallDialogController::DialogActionForTesting::kCancel);
}
bool CheckPolicyValue(content::RenderFrameHost* frame) const {
auto* contents = content::WebContents::FromRenderFrameHost(frame);
auto const* prefs =
Profile::FromBrowserContext(frame->GetBrowserContext())->GetPrefs();
return policy::IsOriginInAllowlist(
contents->GetURL(), prefs,
prefs::kSubAppsAPIsAllowedWithoutGestureAndAuthorizationForOrigins);
}
protected:
base::test::ScopedFeatureList features_{blink::features::kDesktopPWAsSubApps};
std::unique_ptr<net::EmbeddedTestServer> iwa_dev_server_;
testing::NiceMock<policy::MockConfigurationPolicyProvider> policy_provider_;
webapps::AppId parent_app_id_;
};
IN_PROC_BROWSER_TEST_F(SubAppsAdminPolicyTest, SucceedsWithGestureNoPolicy) {
auto* iwa_frame = InstallAndOpenParentIwaApp();
auto dialog_override = AcceptConfirmationDialog();
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsFalse());
auto ret = EvalJs(iwa_frame, AddSubAppsScript({kSub1, kSub2}));
EXPECT_THAT(ret.error, IsEmpty());
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), SizeIs(2));
}
IN_PROC_BROWSER_TEST_F(SubAppsAdminPolicyTest, FailsNoGestureNoPolicy) {
auto* iwa_frame = InstallAndOpenParentIwaApp();
auto dialog_override = DeclineConfirmationDialog();
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsFalse());
auto ret = EvalJs(iwa_frame, AddSubAppsScript({kSub1, kSub2}),
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(ret.error, AllOf(Not(IsEmpty()),
HasSubstr("This API can only be called shortly "
"after a user activation.")));
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), IsEmpty());
}
IN_PROC_BROWSER_TEST_F(SubAppsAdminPolicyTest, SucceedsNoGestureWithPolicy) {
auto parent = InstallIwaParentApp();
SetAllowlistedOrigins({parent.origin().GetURL().spec()});
auto dialog_override = DeclineConfirmationDialog();
auto* iwa_frame = OpenApp(parent.app_id());
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsTrue());
auto ret1 = EvalJs(iwa_frame, AddSubAppsScript({kSub1}),
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(ret1.error, IsEmpty());
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), SizeIs(1));
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsTrue());
auto ret2 = EvalJs(iwa_frame, AddSubAppsScript({kSub2}),
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(ret2.error, IsEmpty());
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), SizeIs(2));
}
IN_PROC_BROWSER_TEST_F(SubAppsAdminPolicyTest, SucceedsWithPolicyAndGesture) {
auto parent = InstallIwaParentApp();
SetAllowlistedOrigins({parent.origin().GetURL().spec()});
auto dialog_override = DeclineConfirmationDialog();
auto* iwa_frame = OpenApp(parent.app_id());
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsTrue());
auto ret = EvalJs(iwa_frame, AddSubAppsScript({kSub1, kSub2}));
EXPECT_THAT(ret.error, IsEmpty());
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), SizeIs(2));
}
IN_PROC_BROWSER_TEST_F(SubAppsAdminPolicyTest,
SucceedsWithMultipleOriginsInPolicyAndNoGesture) {
auto parent = InstallIwaParentApp();
SetAllowlistedOrigins({"https://www.google.com",
parent.origin().GetURL().spec(),
"https:/another.domain.com"});
auto dialog_override = DeclineConfirmationDialog();
auto* iwa_frame = OpenApp(parent.app_id());
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsTrue());
auto ret = EvalJs(iwa_frame, AddSubAppsScript({kSub1, kSub2}),
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(ret.error, IsEmpty());
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), SizeIs(2));
}
IN_PROC_BROWSER_TEST_F(SubAppsAdminPolicyTest,
FailsWithDifferentOriginAndNoGesture) {
auto parent = InstallIwaParentApp();
SetAllowlistedOrigins({"https://www.google.com/"});
auto dialog_override = DeclineConfirmationDialog();
auto* iwa_frame = OpenApp(parent.app_id());
EXPECT_THAT(CheckPolicyValue(iwa_frame), IsFalse());
auto ret = EvalJs(iwa_frame, AddSubAppsScript({kSub1, kSub2}),
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(ret.error, AllOf(Not(IsEmpty()),
HasSubstr("This API can only be called shortly "
"after a user activation.")));
EXPECT_THAT(GetAllSubAppIds(parent_app_id_), IsEmpty());
}
} // namespace web_app
|