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
|
// Copyright 2025 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/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "storage/browser/blob/features.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/switches.h"
#include "url/gurl.h"
namespace policy {
namespace {
GURL CreateBlobUrl(content::RenderFrameHost* rfh) {
std::string blob_url_string =
EvalJs(rfh,
"const blob_url = URL.createObjectURL(new "
" Blob(['<!doctype html><body>potato</body>'], {type: "
" 'text/html'}));"
"blob_url;")
.ExtractString();
return GURL(blob_url_string);
}
bool FetchAndReadBlobUrl(content::RenderFrameHost* rfh, const GURL& blob_url) {
return content::EvalJs(rfh,
content::JsReplace(
"async function test() {"
" try {"
" const blob = await "
" fetch($1).then(response => response.blob());"
" await blob.text();"
" return true;"
" } catch (e) {"
" return false;"
" }"
"}"
"test();",
blob_url))
.ExtractBool();
}
} // namespace
class PartitionedBlobUrlUsagePolicyBrowserTest : public policy::PolicyTest {
public:
PartitionedBlobUrlUsagePolicyBrowserTest() {
scoped_feature_list_.InitWithFeatures(
{features::kBlockCrossPartitionBlobUrlFetching,
blink::features::kEnforceNoopenerOnBlobURLNavigation},
{});
}
void SetUpOnMainThread() override {
PolicyTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data");
content::SetupCrossSiteRedirector(embedded_test_server());
ASSERT_TRUE(embedded_test_server()->Start());
}
void SetEnterprisePolicies(int enabled_value,
const std::vector<std::string>& origins) {
policy::PolicyMap policies;
policies.Set(policy::key::kPartitionedBlobUrlUsage,
policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
policy::POLICY_SOURCE_CLOUD, base::Value(true), nullptr);
policies.Set(policy::key::kDefaultThirdPartyStoragePartitioningSetting,
policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
policy::POLICY_SOURCE_CLOUD, base::Value(enabled_value),
nullptr);
base::Value::List blocked_origins;
for (const auto& origin : origins) {
blocked_origins.Append(origin);
}
policies.Set(policy::key::kThirdPartyStoragePartitioningBlockedForOrigins,
policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(blocked_origins)), nullptr);
UpdateProviderPolicy(policies);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// When blob URL partitioning is enabled via feature flags and its enterprise
// policy, partitioning can still be disabled by the overarching third-party
// storage partitioning enterprise policies.
IN_PROC_BROWSER_TEST_F(
PartitionedBlobUrlUsagePolicyBrowserTest,
FetchingTestWithDefaultThirdPartyStoragePartitioningSetting) {
EXPECT_TRUE(base::FeatureList::IsEnabled(
features::kBlockCrossPartitionBlobUrlFetching));
SetEnterprisePolicies(ContentSetting::CONTENT_SETTING_BLOCK, {});
GURL main_url = embedded_test_server()->GetURL(
"c.com", "/cross_site_iframe_factory.html?c(b(c))");
content::WebContents* web_contents =
chrome_test_utils::GetActiveWebContents(this);
EXPECT_TRUE(content::NavigateToURL(web_contents, main_url));
content::RenderFrameHost* rfh_c = web_contents->GetPrimaryMainFrame();
GURL blob_url(CreateBlobUrl(rfh_c));
content::RenderFrameHost* rfh_b = content::ChildFrameAt(rfh_c, 0);
content::RenderFrameHost* rfh_c_2 = content::ChildFrameAt(rfh_b, 0);
EXPECT_TRUE(FetchAndReadBlobUrl(rfh_c_2, blob_url));
}
IN_PROC_BROWSER_TEST_F(
PartitionedBlobUrlUsagePolicyBrowserTest,
FetchingTestWithThirdPartyStoragePartitioningBlockedForOrigins) {
GURL main_url = embedded_test_server()->GetURL(
"c.com", "/cross_site_iframe_factory.html?c(b(c))");
SetEnterprisePolicies(
ContentSetting::CONTENT_SETTING_ALLOW, /*3PSP disabled for*/
{url::Origin::Create(main_url).Serialize()});
content::WebContents* web_contents =
chrome_test_utils::GetActiveWebContents(this);
EXPECT_TRUE(content::NavigateToURL(web_contents, main_url));
content::RenderFrameHost* rfh_c = web_contents->GetPrimaryMainFrame();
GURL blob_url(CreateBlobUrl(rfh_c));
content::RenderFrameHost* rfh_b = content::ChildFrameAt(rfh_c, 0);
content::RenderFrameHost* rfh_c_2 = content::ChildFrameAt(rfh_b, 0);
EXPECT_TRUE(FetchAndReadBlobUrl(rfh_c_2, blob_url));
}
class PartitionedBlobUrlUsagePolicyBrowserTestP
: public policy::PolicyTest,
public testing::WithParamInterface<bool> {
public:
PartitionedBlobUrlUsagePolicyBrowserTestP() {
scoped_feature_list_.InitWithFeatures(
{features::kBlockCrossPartitionBlobUrlFetching,
blink::features::kEnforceNoopenerOnBlobURLNavigation},
{});
}
void SetUpOnMainThread() override {
PolicyTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data");
content::SetupCrossSiteRedirector(embedded_test_server());
ASSERT_TRUE(embedded_test_server()->Start());
}
void SetUpInProcessBrowserTestFixture() override {
PolicyTest::SetUpInProcessBrowserTestFixture();
// The policy must be set in this function because if the policy is set in
// the test body the test fails because it seems that an existing renderer
// is re-used for which the command line flag hasn't been set.
SetEnterprisePolicies(GetParam());
}
void SetEnterprisePolicies(bool enabled) {
policy::PolicyMap policies;
policies.Set(policy::key::kPartitionedBlobUrlUsage,
policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
policy::POLICY_SOURCE_CLOUD, base::Value(enabled), nullptr);
UpdateProviderPolicy(policies);
}
bool IsPolicyEnabled() { return GetParam(); }
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(PartitionedBlobUrlUsagePolicyBrowserTestP,
FetchingPartitionedBlobUrlUsageEnterprisePolicy) {
EXPECT_TRUE(base::FeatureList::IsEnabled(
features::kBlockCrossPartitionBlobUrlFetching));
GURL main_url = embedded_test_server()->GetURL(
"c.com", "/cross_site_iframe_factory.html?c(b(c))");
content::WebContents* web_contents =
chrome_test_utils::GetActiveWebContents(this);
EXPECT_TRUE(content::NavigateToURL(web_contents, main_url));
content::RenderFrameHost* rfh_c = web_contents->GetPrimaryMainFrame();
GURL blob_url(CreateBlobUrl(rfh_c));
content::RenderFrameHost* rfh_b = content::ChildFrameAt(rfh_c, 0);
content::RenderFrameHost* rfh_c_2 = content::ChildFrameAt(rfh_b, 0);
// The default cookie setting to BLOCK here to ensure that the
// cross-origin blob URL fetch will be blocked due to lack of storage
// access. If cookies are allowed, storage access might be granted, and the
// fetch would succeed even if the blob URL is cross-origin and
// kBlockCrossPartitionBlobUrlFetching is enabled.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(browser()->profile()).get();
settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
bool fetch_results = FetchAndReadBlobUrl(rfh_c_2, blob_url);
if (IsPolicyEnabled()) {
EXPECT_FALSE(fetch_results);
} else {
EXPECT_TRUE(fetch_results);
}
}
IN_PROC_BROWSER_TEST_P(
PartitionedBlobUrlUsagePolicyBrowserTestP,
EnforcingNoopenerOnPartitionedBlobUrlUsageEnterprisePolicy) {
EXPECT_TRUE(base::FeatureList::IsEnabled(
blink::features::kEnforceNoopenerOnBlobURLNavigation));
// 1. navigate to c.com
GURL main_url = embedded_test_server()->GetURL("c.com", "/title1.html");
content::WebContents* web_contents =
chrome_test_utils::GetActiveWebContents(this);
EXPECT_TRUE(content::NavigateToURL(web_contents, main_url));
content::RenderFrameHost* rfh_c = web_contents->GetPrimaryMainFrame();
// 2. create blob_url in c.com (blob url origin is c.com)
GURL blob_url(CreateBlobUrl(rfh_c));
// 3. window.open b.com with c.com embedded
// 3a. navigate to b.com
GURL b_url = embedded_test_server()->GetURL(
"b.com", "/cross_site_iframe_factory.html?b(c)");
// 3b. open new tab from b.com context
ui_test_utils::TabAddedWaiter tab_waiter(browser());
EXPECT_TRUE(
content::ExecJs(rfh_c, content::JsReplace("window.open($1)", b_url)));
tab_waiter.Wait();
content::WebContents* tab_b =
browser()->tab_strip_model()->GetWebContentsAt(1);
ASSERT_NE(nullptr, tab_b) << "No tab found at index 1.";
content::TestNavigationObserver nav_observer(tab_b);
nav_observer.Wait();
content::RenderFrameHost* rfh_b = tab_b->GetPrimaryMainFrame();
content::RenderFrameHost* rfh_c_in_b = content::ChildFrameAt(rfh_b, 0);
// 4. do the window.open of blob url from c.com
ui_test_utils::TabAddedWaiter tab_waiter2(browser());
EXPECT_TRUE(rfh_c_in_b->IsRenderFrameLive());
EXPECT_TRUE(content::ExecJs(
rfh_c_in_b, content::JsReplace("handle = window.open($1);", blob_url)));
tab_waiter2.Wait();
bool handle_null = EvalJs(rfh_c_in_b, "handle === null;").ExtractBool();
tab_b->Close();
EXPECT_EQ(GetParam(), handle_null);
}
INSTANTIATE_TEST_SUITE_P(All,
PartitionedBlobUrlUsagePolicyBrowserTestP,
testing::Values(true, false));
} // namespace policy
|