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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/network_isolation_key.h"
#include <array>
#include <optional>
#include "base/test/scoped_feature_list.h"
#include "base/unguessable_token.h"
#include "base/values.h"
#include "net/base/features.h"
#include "net/base/network_isolation_partition.h"
#include "net/base/schemeful_site.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_util.h"
namespace net {
namespace {
const char kDataUrl[] = "data:text/html,<body>Hello World</body>";
TEST(NetworkIsolationKeyTest, EmptyKey) {
NetworkIsolationKey key;
EXPECT_FALSE(key.IsFullyPopulated());
EXPECT_EQ(std::nullopt, key.ToCacheKeyString());
EXPECT_TRUE(key.IsTransient());
EXPECT_EQ("null null", key.ToDebugString());
}
TEST(NetworkIsolationKeyTest, NonEmptySameSiteKey) {
SchemefulSite site1 = SchemefulSite(GURL("http://a.test/"));
NetworkIsolationKey key(site1, site1);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(site1.Serialize() + " " + site1.Serialize(),
key.ToCacheKeyString());
EXPECT_EQ(site1.GetDebugString() + " " + site1.GetDebugString(),
key.ToDebugString());
EXPECT_FALSE(key.IsTransient());
}
TEST(NetworkIsolationKeyTest, NonEmptyCrossSiteKey) {
SchemefulSite site1 = SchemefulSite(GURL("http://a.test/"));
SchemefulSite site2 = SchemefulSite(GURL("http://b.test/"));
NetworkIsolationKey key(site1, site2);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(site1.Serialize() + " " + site2.Serialize(),
key.ToCacheKeyString());
EXPECT_EQ(site1.GetDebugString() + " " + site2.GetDebugString(),
key.ToDebugString());
EXPECT_FALSE(key.IsTransient());
}
TEST(NetworkIsolationKeyTest, KeyWithNonce) {
SchemefulSite site1 = SchemefulSite(GURL("http://a.test/"));
SchemefulSite site2 = SchemefulSite(GURL("http://b.test/"));
base::UnguessableToken nonce = base::UnguessableToken::Create();
NetworkIsolationKey key(site1, site2, nonce);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(std::nullopt, key.ToCacheKeyString());
EXPECT_TRUE(key.IsTransient());
EXPECT_EQ(site1.GetDebugString() + " " + site2.GetDebugString() +
" (with nonce " + nonce.ToString() + ")",
key.ToDebugString());
// Create another NetworkIsolationKey with the same input parameters, and
// check that it is equal.
NetworkIsolationKey same_key(site1, site2, nonce);
EXPECT_EQ(key, same_key);
// Create another NetworkIsolationKey with a different nonce and check that
// it's different.
base::UnguessableToken nonce2 = base::UnguessableToken::Create();
NetworkIsolationKey key2(site1, site2, nonce2);
EXPECT_NE(key, key2);
EXPECT_NE(key.ToDebugString(), key2.ToDebugString());
}
TEST(NetworkIsolationKeyTest, KeyWithNonGeneralNetworkPartition) {
SchemefulSite site1 = SchemefulSite(GURL("http://a.test/"));
SchemefulSite site2 = SchemefulSite(GURL("http://b.test/"));
NetworkIsolationKey key(
site1, site2, /*nonce=*/std::nullopt,
NetworkIsolationPartition::kProtectedAudienceSellerWorklet);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(NetworkIsolationPartition::kProtectedAudienceSellerWorklet,
key.GetNetworkIsolationPartition());
EXPECT_EQ(site1.Serialize() + " " + site2.Serialize() + " 1",
key.ToCacheKeyString());
EXPECT_FALSE(key.IsTransient());
EXPECT_EQ(site1.GetDebugString() + " " + site2.GetDebugString() +
" (protected audience seller worklet partition)",
key.ToDebugString());
EXPECT_EQ(site1.Serialize() + " " + site2.Serialize() + " 1",
key.ToCacheKeyString());
// Create another NetworkIsolationKey with the same input parameters, and
// check that it is equal.
NetworkIsolationKey same_key(
site1, site2, /*nonce=*/std::nullopt,
NetworkIsolationPartition::kProtectedAudienceSellerWorklet);
EXPECT_EQ(NetworkIsolationPartition::kProtectedAudienceSellerWorklet,
same_key.GetNetworkIsolationPartition());
EXPECT_EQ(key, same_key);
EXPECT_EQ(key.ToCacheKeyString(), same_key.ToCacheKeyString());
EXPECT_EQ(key.ToDebugString(), same_key.ToDebugString());
// Create another NetworkIsolationKey with a different
// NetworkIsolationPartition and check that it's different.
NetworkIsolationKey key2(site1, site2, /*nonce=*/std::nullopt,
NetworkIsolationPartition::kGeneral);
EXPECT_EQ(NetworkIsolationPartition::kGeneral,
key2.GetNetworkIsolationPartition());
EXPECT_NE(key, key2);
EXPECT_NE(key.ToCacheKeyString(), key2.ToCacheKeyString());
EXPECT_NE(key.ToDebugString(), key2.ToDebugString());
// Make sure if a nonce is included in addition to a
// non-general NetworkPartition, the NIK is transient.
auto nonce = base::UnguessableToken::Create();
NetworkIsolationKey key3(
site1, site2, /*nonce=*/nonce,
NetworkIsolationPartition::kProtectedAudienceSellerWorklet);
EXPECT_TRUE(key3.IsTransient());
// Make sure if there's an opaque origin, the NIK is still transient.
NetworkIsolationKey key4(
site1, SchemefulSite(GURL(kDataUrl)), /*nonce=*/std::nullopt,
NetworkIsolationPartition::kProtectedAudienceSellerWorklet);
EXPECT_TRUE(key4.IsTransient());
}
TEST(NetworkIsolationKeyTest, OpaqueOriginKey) {
SchemefulSite site_data = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey key(site_data, site_data);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(std::nullopt, key.ToCacheKeyString());
EXPECT_TRUE(key.IsTransient());
EXPECT_EQ(site_data.GetDebugString() + " " + site_data.GetDebugString(),
key.ToDebugString());
// Create another site with an opaque origin, and make sure it's different and
// has a different debug string.
SchemefulSite other_site = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey other_key(other_site, other_site);
EXPECT_NE(key, other_key);
EXPECT_NE(key.ToDebugString(), other_key.ToDebugString());
EXPECT_EQ(other_site.GetDebugString() + " " + other_site.GetDebugString(),
other_key.ToDebugString());
}
TEST(NetworkIsolationKeyTest, OpaqueOriginTopLevelSiteKey) {
SchemefulSite site1 = SchemefulSite(GURL("http://a.test/"));
SchemefulSite site_data = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey key(site_data, site1);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(std::nullopt, key.ToCacheKeyString());
EXPECT_TRUE(key.IsTransient());
EXPECT_EQ(site_data.GetDebugString() + " " + site1.GetDebugString(),
key.ToDebugString());
// Create another site with an opaque origin, and make sure it's different and
// has a different debug string.
SchemefulSite other_site = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey other_key(other_site, site1);
EXPECT_NE(key, other_key);
EXPECT_NE(key.ToDebugString(), other_key.ToDebugString());
EXPECT_EQ(other_site.GetDebugString() + " " + site1.GetDebugString(),
other_key.ToDebugString());
}
TEST(NetworkIsolationKeyTest, OpaqueOriginIframeKey) {
SchemefulSite site1 = SchemefulSite(GURL("http://a.test/"));
SchemefulSite site_data = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey key(site1, site_data);
EXPECT_TRUE(key.IsFullyPopulated());
EXPECT_EQ(std::nullopt, key.ToCacheKeyString());
EXPECT_TRUE(key.IsTransient());
EXPECT_EQ(site1.GetDebugString() + " " + site_data.GetDebugString(),
key.ToDebugString());
// Create another site with an opaque origin iframe, and make sure it's
// different and has a different debug string when the frame site is in use.
SchemefulSite other_site = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey other_key(site1, other_site);
EXPECT_NE(key, other_key);
EXPECT_NE(key.ToDebugString(), other_key.ToDebugString());
EXPECT_EQ(site1.GetDebugString() + " " + other_site.GetDebugString(),
other_key.ToDebugString());
}
TEST(NetworkIsolationKeyTest, Operators) {
base::UnguessableToken nonce1 = base::UnguessableToken::Create();
base::UnguessableToken nonce2 = base::UnguessableToken::Create();
if (nonce2 < nonce1)
std::swap(nonce1, nonce2);
// These are in ascending order.
const auto kKeys = std::to_array<NetworkIsolationKey>({
NetworkIsolationKey(),
// Site with unique origins are still sorted by scheme, so data is before
// file, and file before http.
NetworkIsolationKey(SchemefulSite(GURL(kDataUrl)),
SchemefulSite(GURL(kDataUrl))),
NetworkIsolationKey(SchemefulSite(GURL("file:///foo")),
SchemefulSite(GURL("file:///foo"))),
NetworkIsolationKey(SchemefulSite(GURL("http://a.test/")),
SchemefulSite(GURL("http://a.test/"))),
NetworkIsolationKey(SchemefulSite(GURL("http://b.test/")),
SchemefulSite(GURL("http://b.test/"))),
NetworkIsolationKey(SchemefulSite(GURL("https://a.test/")),
SchemefulSite(GURL("https://a.test/")),
/*nonce=*/std::nullopt,
NetworkIsolationPartition::kGeneral),
NetworkIsolationKey(
SchemefulSite(GURL("https://a.test/")),
SchemefulSite(GURL("https://a.test/")), /*nonce=*/std::nullopt,
NetworkIsolationPartition::kProtectedAudienceSellerWorklet),
NetworkIsolationKey(SchemefulSite(GURL("https://a.test/")),
SchemefulSite(GURL("https://a.test/")), nonce1),
NetworkIsolationKey(SchemefulSite(GURL("https://a.test/")),
SchemefulSite(GURL("https://a.test/")), nonce2),
});
for (size_t first = 0; first < std::size(kKeys); ++first) {
NetworkIsolationKey key1 = kKeys[first];
SCOPED_TRACE(key1.ToDebugString());
EXPECT_TRUE(key1 == key1);
EXPECT_FALSE(key1 != key1);
EXPECT_FALSE(key1 < key1);
// Make sure that copying a key doesn't change the results of any operation.
// This check is a bit more interesting with unique origins.
NetworkIsolationKey key1_copy = key1;
EXPECT_TRUE(key1 == key1_copy);
EXPECT_FALSE(key1 < key1_copy);
EXPECT_FALSE(key1_copy < key1);
for (size_t second = first + 1; second < std::size(kKeys); ++second) {
NetworkIsolationKey key2 = kKeys[second];
SCOPED_TRACE(key2.ToDebugString());
EXPECT_TRUE(key1 < key2);
EXPECT_FALSE(key2 < key1);
EXPECT_FALSE(key1 == key2);
EXPECT_FALSE(key2 == key1);
}
}
}
TEST(NetworkIsolationKeyTest, UniqueOriginOperators) {
const auto kSite1 = SchemefulSite(GURL(kDataUrl));
const auto kSite2 = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey key1(kSite1, kSite1);
NetworkIsolationKey key2(kSite2, kSite2);
EXPECT_TRUE(key1 == key1);
EXPECT_TRUE(key2 == key2);
// Creating copies shouldn't affect comparison result.
EXPECT_TRUE(NetworkIsolationKey(key1) == NetworkIsolationKey(key1));
EXPECT_TRUE(NetworkIsolationKey(key2) == NetworkIsolationKey(key2));
EXPECT_FALSE(key1 == key2);
EXPECT_FALSE(key2 == key1);
// Order of Nonces isn't predictable, but they should have an ordering.
EXPECT_TRUE(key1 < key2 || key2 < key1);
EXPECT_TRUE(!(key1 < key2) || !(key2 < key1));
}
TEST(NetworkIsolationKeyTest, OpaqueSiteKeyBoth) {
SchemefulSite site_data_1 = SchemefulSite(GURL(kDataUrl));
SchemefulSite site_data_2 = SchemefulSite(GURL(kDataUrl));
SchemefulSite site_data_3 = SchemefulSite(GURL(kDataUrl));
NetworkIsolationKey key1(site_data_1, site_data_2);
NetworkIsolationKey key2(site_data_1, site_data_2);
NetworkIsolationKey key3(site_data_1, site_data_3);
// All the keys should be fully populated and transient.
EXPECT_TRUE(key1.IsFullyPopulated());
EXPECT_TRUE(key2.IsFullyPopulated());
EXPECT_TRUE(key3.IsFullyPopulated());
EXPECT_TRUE(key1.IsTransient());
EXPECT_TRUE(key2.IsTransient());
EXPECT_TRUE(key3.IsTransient());
// Test the equality/comparisons of the various keys
EXPECT_TRUE(key1 == key2);
EXPECT_FALSE(key1 < key2 || key2 < key1);
EXPECT_FALSE(key1 == key3);
EXPECT_TRUE(key1 < key3 || key3 < key1);
EXPECT_NE(key1.ToDebugString(), key3.ToDebugString());
// Test the ToString and ToDebugString
EXPECT_EQ(key1.ToDebugString(), key2.ToDebugString());
EXPECT_EQ(std::nullopt, key1.ToCacheKeyString());
EXPECT_EQ(std::nullopt, key2.ToCacheKeyString());
EXPECT_EQ(std::nullopt, key3.ToCacheKeyString());
}
// Make sure that the logic to extract the registerable domain from an origin
// does not affect the host when using a non-standard scheme.
TEST(NetworkIsolationKeyTest, NonStandardScheme) {
// Have to register the scheme, or SchemefulSite() will return an opaque
// origin.
url::ScopedSchemeRegistryForTests scoped_registry;
url::AddStandardScheme("foo", url::SCHEME_WITH_HOST);
SchemefulSite site = SchemefulSite(GURL("foo://a.foo.com"));
NetworkIsolationKey key(site, site);
EXPECT_FALSE(key.GetTopFrameSite()->opaque());
EXPECT_EQ("foo://a.foo.com foo://a.foo.com", key.ToCacheKeyString());
}
TEST(NetworkIsolationKeyTest, CreateWithNewFrameSite) {
SchemefulSite site_a = SchemefulSite(GURL("http://a.com"));
SchemefulSite site_b = SchemefulSite(GURL("http://b.com"));
SchemefulSite site_c = SchemefulSite(GURL("http://c.com"));
NetworkIsolationKey key(site_a, site_b);
NetworkIsolationKey key_c = key.CreateWithNewFrameSite(site_c);
EXPECT_EQ(site_c, key_c.GetFrameSiteForTesting());
EXPECT_NE(key_c, key);
EXPECT_EQ(site_a, key_c.GetTopFrameSite());
// Ensure that `CreateWithNewFrameSite()` preserves the nonce if one exists.
base::UnguessableToken nonce = base::UnguessableToken::Create();
NetworkIsolationKey key_with_nonce(site_a, site_b, nonce);
NetworkIsolationKey key_with_nonce_c =
key_with_nonce.CreateWithNewFrameSite(site_c);
EXPECT_EQ(key_with_nonce.GetNonce(), key_with_nonce_c.GetNonce());
EXPECT_TRUE(key_with_nonce_c.IsTransient());
// Ensure that `CreateWithNewFrameSite()` preserves the
// NetworkIsolationPartition.
NetworkIsolationKey key_with_partition(
site_a, site_b, /*nonce=*/std::nullopt,
NetworkIsolationPartition::kProtectedAudienceSellerWorklet);
NetworkIsolationKey key_with_partition_c =
key_with_partition.CreateWithNewFrameSite(site_c);
EXPECT_EQ(key_with_partition.GetNetworkIsolationPartition(),
NetworkIsolationPartition::kProtectedAudienceSellerWorklet);
EXPECT_EQ(key_with_partition.GetNetworkIsolationPartition(),
key_with_partition_c.GetNetworkIsolationPartition());
}
TEST(NetworkIsolationKeyTest, CreateTransientForTesting) {
NetworkIsolationKey transient_key =
NetworkIsolationKey::CreateTransientForTesting();
EXPECT_TRUE(transient_key.IsFullyPopulated());
EXPECT_TRUE(transient_key.IsTransient());
EXPECT_FALSE(transient_key.IsEmpty());
EXPECT_EQ(transient_key, transient_key);
// Make sure that subsequent calls don't return the same NIK.
for (int i = 0; i < 1000; ++i) {
EXPECT_NE(transient_key, NetworkIsolationKey::CreateTransientForTesting());
}
}
} // namespace
} // namespace net
|