first_party_sets_manager_unittest.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/network/first_party_sets/first_party_sets_manager.h"
  5. #include <set>
  6. #include <string>
  7. #include "base/containers/flat_set.h"
  8. #include "base/test/task_environment.h"
  9. #include "base/test/test_future.h"
  10. #include "net/base/schemeful_site.h"
  11. #include "net/cookies/cookie_constants.h"
  12. #include "net/cookies/first_party_set_entry.h"
  13. #include "net/cookies/first_party_set_metadata.h"
  14. #include "net/cookies/same_party_context.h"
  15. #include "services/network/public/mojom/first_party_sets.mojom.h"
  16. #include "testing/gmock/include/gmock/gmock-matchers.h"
  17. #include "testing/gmock/include/gmock/gmock.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. #include "url/gurl.h"
  21. using ::testing::IsEmpty;
  22. using ::testing::Pair;
  23. using ::testing::UnorderedElementsAre;
  24. using ::testing::Value;
  25. using Type = net::SamePartyContext::Type;
  26. using OverrideSets =
  27. base::flat_map<net::SchemefulSite, absl::optional<net::FirstPartySetEntry>>;
  28. namespace network {
  29. MATCHER_P(SerializesTo, want, "") {
  30. const std::string got = arg.Serialize();
  31. return testing::ExplainMatchResult(testing::Eq(want), got, result_listener);
  32. }
  33. class FirstPartySetsManagerTest : public ::testing::Test {
  34. public:
  35. explicit FirstPartySetsManagerTest(bool enabled, bool context_enabled)
  36. : manager_(enabled), fps_context_config_(context_enabled) {}
  37. void SetCompleteSets(
  38. const base::flat_map<net::SchemefulSite, net::FirstPartySetEntry>&
  39. content,
  40. const base::flat_map<net::SchemefulSite, net::SchemefulSite>& aliases) {
  41. network::mojom::PublicFirstPartySetsPtr public_sets =
  42. network::mojom::PublicFirstPartySets::New();
  43. public_sets->sets = content;
  44. public_sets->aliases = aliases;
  45. manager_.SetCompleteSets(std::move(public_sets));
  46. }
  47. net::FirstPartySetMetadata ComputeMetadataAndWait(
  48. const net::SchemefulSite& site,
  49. const net::SchemefulSite* top_frame_site,
  50. const std::set<net::SchemefulSite>& party_context) {
  51. base::test::TestFuture<net::FirstPartySetMetadata> future;
  52. absl::optional<net::FirstPartySetMetadata> result =
  53. manager_.ComputeMetadata(site, top_frame_site, party_context,
  54. fps_context_config_, future.GetCallback());
  55. return result.has_value() ? std::move(result).value() : future.Take();
  56. }
  57. FirstPartySetsManager::OwnersResult FindOwnersAndWait(
  58. const base::flat_set<net::SchemefulSite>& site) {
  59. base::test::TestFuture<FirstPartySetsManager::OwnersResult> future;
  60. absl::optional<FirstPartySetsManager::OwnersResult> result =
  61. manager_.FindOwners(site, fps_context_config_, future.GetCallback());
  62. return result.has_value() ? result.value() : future.Get();
  63. }
  64. FirstPartySetsManager& manager() { return manager_; }
  65. net::FirstPartySetsContextConfig& fps_context_config() {
  66. return fps_context_config_;
  67. }
  68. base::test::TaskEnvironment& env() { return env_; }
  69. protected:
  70. void SetFirstPartySetsContextConfig(bool enabled,
  71. OverrideSets customizations) {
  72. fps_context_config_ = net::FirstPartySetsContextConfig(enabled);
  73. fps_context_config_.SetCustomizations(std::move(customizations));
  74. }
  75. private:
  76. base::test::TaskEnvironment env_;
  77. FirstPartySetsManager manager_;
  78. net::FirstPartySetsContextConfig fps_context_config_;
  79. };
  80. class FirstPartySetsManagerDisabledTest : public FirstPartySetsManagerTest {
  81. public:
  82. FirstPartySetsManagerDisabledTest()
  83. : FirstPartySetsManagerTest(/*enabled=*/false, /*context_enabled=*/true) {
  84. }
  85. };
  86. TEST_F(FirstPartySetsManagerDisabledTest, SetCompleteSets) {
  87. net::SchemefulSite example_cctld(GURL("https://example.cctld"));
  88. net::SchemefulSite example_test(GURL("https://example.test"));
  89. net::SchemefulSite aaaa(GURL("https://aaaa.test"));
  90. SetCompleteSets({{aaaa, net::FirstPartySetEntry(
  91. example_test, net::SiteType::kAssociated, 0)},
  92. {example_test,
  93. net::FirstPartySetEntry(
  94. example_test, net::SiteType::kPrimary, absl::nullopt)}},
  95. {{example_cctld, example_test}});
  96. EXPECT_THAT(FindOwnersAndWait({
  97. aaaa,
  98. example_test,
  99. example_cctld,
  100. }),
  101. IsEmpty());
  102. }
  103. TEST_F(FirstPartySetsManagerDisabledTest, FindOwners) {
  104. net::SchemefulSite kExample =
  105. net::SchemefulSite(GURL("https://example.test"));
  106. EXPECT_THAT(FindOwnersAndWait({kExample}), IsEmpty());
  107. }
  108. TEST_F(FirstPartySetsManagerDisabledTest, ComputeMetadata_InfersSingletons) {
  109. net::SchemefulSite member(GURL("https://member1.test"));
  110. net::SchemefulSite example(GURL("https://example.test"));
  111. net::SchemefulSite wss_member(GURL("wss://member1.test"));
  112. SetFirstPartySetsContextConfig(
  113. true, {{net::SchemefulSite(GURL("https://member1.test")),
  114. {net::FirstPartySetEntry(
  115. net::SchemefulSite(GURL("https://example.test")),
  116. net::SiteType::kAssociated, 0)}},
  117. {net::SchemefulSite(GURL("https://example.test")),
  118. {net::FirstPartySetEntry(
  119. net::SchemefulSite(GURL("https://example.test")),
  120. net::SiteType::kPrimary, absl::nullopt)}}});
  121. // Works if the site is provided with WSS scheme instead of HTTPS.
  122. EXPECT_THAT(
  123. ComputeMetadataAndWait(wss_member, &member, {member, example}).context(),
  124. net::SamePartyContext(Type::kCrossParty));
  125. EXPECT_THAT(ComputeMetadataAndWait(example, &member, {member}).context(),
  126. net::SamePartyContext(Type::kCrossParty));
  127. EXPECT_THAT(ComputeMetadataAndWait(member, &example, {member}).context(),
  128. net::SamePartyContext(Type::kCrossParty));
  129. EXPECT_THAT(
  130. ComputeMetadataAndWait(member, &member, {member, example}).context(),
  131. net::SamePartyContext(Type::kCrossParty));
  132. }
  133. class FirstPartySetsEnabledTest : public FirstPartySetsManagerTest {
  134. public:
  135. FirstPartySetsEnabledTest()
  136. : FirstPartySetsManagerTest(/*enabled=*/true, /*context_enabled=*/true) {}
  137. };
  138. TEST_F(FirstPartySetsEnabledTest, SetCompleteSets) {
  139. net::SchemefulSite example_cctld(GURL("https://example.cctld"));
  140. net::SchemefulSite example_test(GURL("https://example.test"));
  141. net::SchemefulSite aaaa(GURL("https://aaaa.test"));
  142. SetCompleteSets({{aaaa, net::FirstPartySetEntry(
  143. example_test, net::SiteType::kAssociated, 0)},
  144. {example_test,
  145. net::FirstPartySetEntry(
  146. example_test, net::SiteType::kPrimary, absl::nullopt)}},
  147. {{example_cctld, example_test}});
  148. EXPECT_THAT(
  149. FindOwnersAndWait({
  150. aaaa,
  151. example_test,
  152. example_cctld,
  153. }),
  154. UnorderedElementsAre(
  155. Pair(example_test,
  156. net::FirstPartySetEntry(example_test, net::SiteType::kPrimary,
  157. absl::nullopt)),
  158. Pair(example_cctld,
  159. net::FirstPartySetEntry(example_test, net::SiteType::kPrimary,
  160. absl::nullopt)),
  161. Pair(aaaa, net::FirstPartySetEntry(example_test,
  162. net::SiteType::kAssociated, 0))));
  163. }
  164. TEST_F(FirstPartySetsEnabledTest, SetCompleteSets_Idempotent) {
  165. SetCompleteSets({}, {});
  166. EXPECT_THAT(FindOwnersAndWait({}), IsEmpty());
  167. // The second call to SetCompleteSets should have no effect.
  168. SetCompleteSets({{net::SchemefulSite(GURL("https://aaaa.test")),
  169. net::FirstPartySetEntry(
  170. net::SchemefulSite(GURL("https://example.test")),
  171. net::SiteType::kAssociated, 0)},
  172. {net::SchemefulSite(GURL("https://example.test")),
  173. net::FirstPartySetEntry(
  174. net::SchemefulSite(GURL("https://example.test")),
  175. net::SiteType::kPrimary, absl::nullopt)}},
  176. {});
  177. EXPECT_THAT(FindOwnersAndWait({
  178. net::SchemefulSite(GURL("https://aaaa.test")),
  179. net::SchemefulSite(GURL("https://example.test")),
  180. }),
  181. IsEmpty());
  182. }
  183. // Test fixture that allows precise control over when the instance gets FPS
  184. // data. Useful for testing async flows.
  185. class AsyncPopulatedFirstPartySetsManagerTest
  186. : public FirstPartySetsEnabledTest {
  187. public:
  188. AsyncPopulatedFirstPartySetsManagerTest() = default;
  189. protected:
  190. void Populate() {
  191. net::SchemefulSite foo(GURL("https://foo.test"));
  192. net::SchemefulSite example_test(GURL("https://example.test"));
  193. net::SchemefulSite example_cctld(GURL("https://example.cctld"));
  194. // /*content=*/ R"(
  195. // [
  196. // {
  197. // "owner": "https://example.test",
  198. // "members": ["https://member1.test", "https://member3.test"]
  199. // },
  200. // {
  201. // "owner": "https://foo.test",
  202. // "members": ["https://member2.test"]
  203. // }
  204. // ]
  205. // )";
  206. SetCompleteSets(
  207. {
  208. {net::SchemefulSite(GURL("https://member1.test")),
  209. net::FirstPartySetEntry(example_test, net::SiteType::kAssociated,
  210. 0)},
  211. {net::SchemefulSite(GURL("https://member3.test")),
  212. net::FirstPartySetEntry(example_test, net::SiteType::kAssociated,
  213. 0)},
  214. {example_test,
  215. net::FirstPartySetEntry(example_test, net::SiteType::kPrimary,
  216. absl::nullopt)},
  217. {net::SchemefulSite(GURL("https://member2.test")),
  218. net::FirstPartySetEntry(foo, net::SiteType::kAssociated, 0)},
  219. {foo, net::FirstPartySetEntry(foo, net::SiteType::kPrimary,
  220. absl::nullopt)},
  221. },
  222. {{example_cctld, example_test}});
  223. // We don't wait for the sets to be loaded before returning, in order to let
  224. // the tests provoke raciness if any exists.
  225. }
  226. };
  227. TEST_F(AsyncPopulatedFirstPartySetsManagerTest,
  228. QueryBeforeReady_ComputeMetadata) {
  229. base::test::TestFuture<net::FirstPartySetMetadata> future;
  230. {
  231. // Force deallocation to provoke a UAF if the impl just copies the pointer.
  232. net::SchemefulSite member(GURL("https://member1.test"));
  233. EXPECT_FALSE(manager().ComputeMetadata(
  234. member, &member, {member}, fps_context_config(), future.GetCallback()));
  235. }
  236. Populate();
  237. {
  238. net::SchemefulSite owner(GURL("https://example.test"));
  239. net::FirstPartySetEntry entry(owner, net::SiteType::kAssociated, 0);
  240. EXPECT_EQ(future.Get(),
  241. net::FirstPartySetMetadata(
  242. net::SamePartyContext(Type::kSameParty), &entry, &entry));
  243. }
  244. }
  245. TEST_F(AsyncPopulatedFirstPartySetsManagerTest, QueryBeforeReady_FindOwners) {
  246. base::test::TestFuture<FirstPartySetsManager::OwnersResult> future;
  247. EXPECT_FALSE(manager().FindOwners(
  248. {
  249. net::SchemefulSite(GURL("https://member1.test")),
  250. net::SchemefulSite(GURL("https://member2.test")),
  251. net::SchemefulSite(GURL("https://example.cctld")),
  252. },
  253. fps_context_config(), future.GetCallback()));
  254. Populate();
  255. EXPECT_THAT(future.Get(),
  256. UnorderedElementsAre(
  257. Pair(SerializesTo("https://member1.test"),
  258. net::FirstPartySetEntry(
  259. net::SchemefulSite(GURL("https://example.test")),
  260. net::SiteType::kAssociated, 0)),
  261. Pair(SerializesTo("https://example.cctld"),
  262. net::FirstPartySetEntry(
  263. net::SchemefulSite(GURL("https://example.test")),
  264. net::SiteType::kPrimary, absl::nullopt)),
  265. Pair(SerializesTo("https://member2.test"),
  266. net::FirstPartySetEntry(
  267. net::SchemefulSite(GURL("https://foo.test")),
  268. net::SiteType::kAssociated, 0))));
  269. }
  270. class PopulatedFirstPartySetsManagerTest
  271. : public AsyncPopulatedFirstPartySetsManagerTest {
  272. public:
  273. PopulatedFirstPartySetsManagerTest() { Populate(); }
  274. };
  275. TEST_F(PopulatedFirstPartySetsManagerTest, ComputeMetadata_EmptyContext) {
  276. net::SchemefulSite example_site(GURL("https://example.test"));
  277. net::SchemefulSite nonmember(GURL("https://nonmember.test"));
  278. for (const net::SchemefulSite* top_frame :
  279. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  280. EXPECT_EQ(
  281. ComputeMetadataAndWait(
  282. net::SchemefulSite(GURL("https://nonmember.test")), top_frame, {})
  283. .context()
  284. .context_type(),
  285. Type::kCrossParty);
  286. EXPECT_EQ(ComputeMetadataAndWait(example_site, top_frame, {})
  287. .context()
  288. .context_type(),
  289. Type::kSameParty);
  290. EXPECT_EQ(
  291. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  292. top_frame, {})
  293. .context()
  294. .context_type(),
  295. Type::kCrossParty);
  296. }
  297. EXPECT_EQ(ComputeMetadataAndWait(example_site, &nonmember, {})
  298. .context()
  299. .context_type(),
  300. Type::kCrossParty);
  301. EXPECT_EQ(ComputeMetadataAndWait(nonmember, &example_site, {})
  302. .context()
  303. .context_type(),
  304. Type::kCrossParty);
  305. }
  306. TEST_F(PopulatedFirstPartySetsManagerTest, ComputeMetadata_ContextIsNonmember) {
  307. std::set<net::SchemefulSite> context({
  308. net::SchemefulSite(GURL("https://nonmember.test")),
  309. });
  310. net::SchemefulSite example_site(GURL("https://example.test"));
  311. for (const net::SchemefulSite* top_frame :
  312. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  313. EXPECT_EQ(
  314. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  315. top_frame, context)
  316. .context()
  317. .context_type(),
  318. Type::kCrossParty);
  319. EXPECT_EQ(
  320. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  321. top_frame, context)
  322. .context()
  323. .context_type(),
  324. Type::kCrossParty);
  325. EXPECT_EQ(
  326. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  327. top_frame, context)
  328. .context()
  329. .context_type(),
  330. Type::kCrossParty);
  331. EXPECT_EQ(
  332. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  333. top_frame, context)
  334. .context()
  335. .context_type(),
  336. Type::kCrossParty);
  337. EXPECT_EQ(
  338. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  339. top_frame, context)
  340. .context()
  341. .context_type(),
  342. Type::kCrossParty);
  343. EXPECT_EQ(ComputeMetadataAndWait(
  344. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  345. context)
  346. .context()
  347. .context_type(),
  348. Type::kCrossParty);
  349. }
  350. }
  351. TEST_F(PopulatedFirstPartySetsManagerTest, ComputeMetadata_ContextIsOwner) {
  352. std::set<net::SchemefulSite> context(
  353. {net::SchemefulSite(GURL("https://example.test"))});
  354. net::SchemefulSite example_site(GURL("https://example.test"));
  355. for (const net::SchemefulSite* top_frame :
  356. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  357. EXPECT_EQ(
  358. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  359. top_frame, context)
  360. .context()
  361. .context_type(),
  362. Type::kSameParty);
  363. EXPECT_EQ(
  364. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  365. top_frame, context)
  366. .context()
  367. .context_type(),
  368. Type::kCrossParty);
  369. EXPECT_EQ(
  370. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  371. top_frame, context)
  372. .context()
  373. .context_type(),
  374. Type::kSameParty);
  375. EXPECT_EQ(
  376. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  377. top_frame, context)
  378. .context()
  379. .context_type(),
  380. Type::kCrossParty);
  381. EXPECT_EQ(
  382. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  383. top_frame, context)
  384. .context()
  385. .context_type(),
  386. Type::kCrossParty);
  387. EXPECT_EQ(ComputeMetadataAndWait(
  388. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  389. context)
  390. .context()
  391. .context_type(),
  392. Type::kCrossParty);
  393. }
  394. }
  395. TEST_F(PopulatedFirstPartySetsManagerTest, ComputeMetadata_ContextIsMember) {
  396. std::set<net::SchemefulSite> context(
  397. {net::SchemefulSite(GURL("https://member1.test"))});
  398. net::SchemefulSite example_site(GURL("https://example.test"));
  399. for (const net::SchemefulSite* top_frame :
  400. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  401. EXPECT_EQ(
  402. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  403. top_frame, context)
  404. .context()
  405. .context_type(),
  406. Type::kSameParty);
  407. EXPECT_EQ(
  408. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  409. top_frame, context)
  410. .context()
  411. .context_type(),
  412. Type::kCrossParty);
  413. EXPECT_EQ(
  414. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  415. top_frame, context)
  416. .context()
  417. .context_type(),
  418. Type::kSameParty);
  419. EXPECT_EQ(
  420. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  421. top_frame, context)
  422. .context()
  423. .context_type(),
  424. Type::kSameParty);
  425. EXPECT_EQ(
  426. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  427. top_frame, context)
  428. .context()
  429. .context_type(),
  430. Type::kCrossParty);
  431. EXPECT_EQ(
  432. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  433. top_frame, context)
  434. .context()
  435. .context_type(),
  436. Type::kCrossParty);
  437. EXPECT_EQ(ComputeMetadataAndWait(
  438. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  439. context)
  440. .context()
  441. .context_type(),
  442. Type::kCrossParty);
  443. }
  444. }
  445. TEST_F(PopulatedFirstPartySetsManagerTest,
  446. ComputeMetadata_ContextIsOwnerAndMember) {
  447. std::set<net::SchemefulSite> context({
  448. net::SchemefulSite(GURL("https://example.test")),
  449. net::SchemefulSite(GURL("https://member1.test")),
  450. });
  451. net::SchemefulSite example_site(GURL("https://example.test"));
  452. for (const net::SchemefulSite* top_frame :
  453. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  454. EXPECT_EQ(
  455. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  456. top_frame, context)
  457. .context()
  458. .context_type(),
  459. Type::kSameParty);
  460. EXPECT_EQ(
  461. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  462. top_frame, context)
  463. .context()
  464. .context_type(),
  465. Type::kCrossParty);
  466. EXPECT_EQ(
  467. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  468. top_frame, context)
  469. .context()
  470. .context_type(),
  471. Type::kSameParty);
  472. EXPECT_EQ(
  473. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member3.test")),
  474. top_frame, context)
  475. .context()
  476. .context_type(),
  477. Type::kSameParty);
  478. EXPECT_EQ(
  479. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  480. top_frame, context)
  481. .context()
  482. .context_type(),
  483. Type::kCrossParty);
  484. EXPECT_EQ(
  485. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  486. top_frame, context)
  487. .context()
  488. .context_type(),
  489. Type::kCrossParty);
  490. EXPECT_EQ(ComputeMetadataAndWait(
  491. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  492. context)
  493. .context()
  494. .context_type(),
  495. Type::kCrossParty);
  496. }
  497. }
  498. TEST_F(PopulatedFirstPartySetsManagerTest,
  499. ComputeMetadata_ContextMixesParties) {
  500. std::set<net::SchemefulSite> context({
  501. net::SchemefulSite(GURL("https://example.test")),
  502. net::SchemefulSite(GURL("https://member1.test")),
  503. net::SchemefulSite(GURL("https://foo.test")),
  504. });
  505. net::SchemefulSite example_site(GURL("https://example.test"));
  506. for (const net::SchemefulSite* top_frame :
  507. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  508. EXPECT_EQ(
  509. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  510. top_frame, context)
  511. .context()
  512. .context_type(),
  513. Type::kCrossParty);
  514. EXPECT_EQ(
  515. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  516. top_frame, context)
  517. .context()
  518. .context_type(),
  519. Type::kCrossParty);
  520. EXPECT_EQ(
  521. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  522. top_frame, context)
  523. .context()
  524. .context_type(),
  525. Type::kCrossParty);
  526. EXPECT_EQ(
  527. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  528. top_frame, context)
  529. .context()
  530. .context_type(),
  531. Type::kCrossParty);
  532. EXPECT_EQ(
  533. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  534. top_frame, context)
  535. .context()
  536. .context_type(),
  537. Type::kCrossParty);
  538. EXPECT_EQ(ComputeMetadataAndWait(
  539. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  540. context)
  541. .context()
  542. .context_type(),
  543. Type::kCrossParty);
  544. }
  545. }
  546. TEST_F(PopulatedFirstPartySetsManagerTest,
  547. ComputeMetadata_ContextMixesMembersAndNonmembers) {
  548. std::set<net::SchemefulSite> context({
  549. net::SchemefulSite(GURL("https://example.test")),
  550. net::SchemefulSite(GURL("https://member1.test")),
  551. net::SchemefulSite(GURL("http://nonmember.test")),
  552. });
  553. net::SchemefulSite example_site(GURL("https://example.test"));
  554. for (const net::SchemefulSite* top_frame :
  555. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  556. EXPECT_EQ(
  557. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  558. top_frame, context)
  559. .context()
  560. .context_type(),
  561. Type::kCrossParty);
  562. EXPECT_EQ(
  563. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  564. top_frame, context)
  565. .context()
  566. .context_type(),
  567. Type::kCrossParty);
  568. EXPECT_EQ(
  569. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  570. top_frame, context)
  571. .context()
  572. .context_type(),
  573. Type::kCrossParty);
  574. EXPECT_EQ(
  575. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  576. top_frame, context)
  577. .context()
  578. .context_type(),
  579. Type::kCrossParty);
  580. EXPECT_EQ(
  581. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  582. top_frame, context)
  583. .context()
  584. .context_type(),
  585. Type::kCrossParty);
  586. EXPECT_EQ(ComputeMetadataAndWait(
  587. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  588. context)
  589. .context()
  590. .context_type(),
  591. Type::kCrossParty);
  592. }
  593. }
  594. TEST_F(PopulatedFirstPartySetsManagerTest,
  595. ComputeMetadata_ContextMixesSchemes) {
  596. std::set<net::SchemefulSite> context({
  597. net::SchemefulSite(GURL("https://example.test")),
  598. net::SchemefulSite(GURL("https://member1.test")),
  599. net::SchemefulSite(GURL("http://example.test")),
  600. });
  601. net::SchemefulSite example_site(GURL("https://example.test"));
  602. for (const net::SchemefulSite* top_frame :
  603. std::initializer_list<net::SchemefulSite*>{&example_site, nullptr}) {
  604. EXPECT_EQ(
  605. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://example.test")),
  606. top_frame, context)
  607. .context()
  608. .context_type(),
  609. Type::kCrossParty);
  610. EXPECT_EQ(
  611. ComputeMetadataAndWait(net::SchemefulSite(GURL("http://example.test")),
  612. top_frame, context)
  613. .context()
  614. .context_type(),
  615. Type::kCrossParty);
  616. EXPECT_EQ(
  617. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member1.test")),
  618. top_frame, context)
  619. .context()
  620. .context_type(),
  621. Type::kCrossParty);
  622. EXPECT_EQ(
  623. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://foo.test")),
  624. top_frame, context)
  625. .context()
  626. .context_type(),
  627. Type::kCrossParty);
  628. EXPECT_EQ(
  629. ComputeMetadataAndWait(net::SchemefulSite(GURL("https://member2.test")),
  630. top_frame, context)
  631. .context()
  632. .context_type(),
  633. Type::kCrossParty);
  634. EXPECT_EQ(ComputeMetadataAndWait(
  635. net::SchemefulSite(GURL("https://nonmember.test")), top_frame,
  636. context)
  637. .context()
  638. .context_type(),
  639. Type::kCrossParty);
  640. }
  641. }
  642. TEST_F(PopulatedFirstPartySetsManagerTest, ComputeMetadata) {
  643. net::SchemefulSite nonmember(GURL("https://nonmember.test"));
  644. net::SchemefulSite nonmember1(GURL("https://nonmember1.test"));
  645. net::SchemefulSite member(GURL("https://member1.test"));
  646. net::SchemefulSite owner(GURL("https://example.test"));
  647. net::SchemefulSite wss_member(GURL("wss://member1.test"));
  648. net::SchemefulSite wss_nonmember(GURL("wss://nonmember.test"));
  649. net::FirstPartySetEntry primary_entry(owner, net::SiteType::kPrimary,
  650. absl::nullopt);
  651. net::FirstPartySetEntry associated_entry(owner, net::SiteType::kAssociated,
  652. 0);
  653. // Works as usual for sites that are in First-Party sets.
  654. EXPECT_EQ(ComputeMetadataAndWait(member, &member, {member}),
  655. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  656. &associated_entry, &associated_entry));
  657. EXPECT_EQ(ComputeMetadataAndWait(owner, &member, {member}),
  658. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  659. &primary_entry, &associated_entry));
  660. EXPECT_EQ(ComputeMetadataAndWait(member, &owner, {member}),
  661. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  662. &associated_entry, &primary_entry));
  663. EXPECT_EQ(ComputeMetadataAndWait(member, &member, {owner}),
  664. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  665. &associated_entry, &associated_entry));
  666. EXPECT_EQ(ComputeMetadataAndWait(member, &member, {member, owner}),
  667. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  668. &associated_entry, &associated_entry));
  669. // Works if the site is provided with WSS scheme instead of HTTPS.
  670. EXPECT_EQ(ComputeMetadataAndWait(wss_member, &member, {member, owner}),
  671. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  672. &associated_entry, &associated_entry));
  673. EXPECT_EQ(ComputeMetadataAndWait(nonmember, &member, {member}),
  674. net::FirstPartySetMetadata(net::SamePartyContext(Type::kCrossParty),
  675. nullptr, &associated_entry));
  676. EXPECT_EQ(ComputeMetadataAndWait(member, &nonmember, {member}),
  677. net::FirstPartySetMetadata(net::SamePartyContext(Type::kCrossParty),
  678. &associated_entry, nullptr));
  679. EXPECT_EQ(ComputeMetadataAndWait(wss_nonmember, &wss_member, {member, owner}),
  680. net::FirstPartySetMetadata(net::SamePartyContext(Type::kCrossParty),
  681. nullptr, &associated_entry));
  682. EXPECT_EQ(ComputeMetadataAndWait(nonmember, &nonmember, {nonmember}),
  683. net::FirstPartySetMetadata(net::SamePartyContext(Type::kCrossParty),
  684. nullptr, nullptr));
  685. EXPECT_EQ(ComputeMetadataAndWait(member, &member, {member, nonmember}),
  686. net::FirstPartySetMetadata(net::SamePartyContext(Type::kCrossParty),
  687. &associated_entry, &associated_entry));
  688. }
  689. TEST_F(PopulatedFirstPartySetsManagerTest, FindOwners) {
  690. net::SchemefulSite kExample(GURL("https://example.test"));
  691. net::SchemefulSite kFoo(GURL("https://foo.test"));
  692. net::SchemefulSite kMember1(GURL("https://member1.test"));
  693. net::SchemefulSite kMember2(GURL("https://member2.test"));
  694. net::SchemefulSite kNonmember(GURL("https://nonmember.test"));
  695. EXPECT_THAT(FindOwnersAndWait({kExample}),
  696. UnorderedElementsAre(
  697. Pair(SerializesTo("https://example.test"),
  698. net::FirstPartySetEntry(
  699. net::SchemefulSite(GURL("https://example.test")),
  700. net::SiteType::kPrimary, absl::nullopt))));
  701. EXPECT_THAT(FindOwnersAndWait({kMember1}),
  702. UnorderedElementsAre(
  703. Pair(SerializesTo("https://member1.test"),
  704. net::FirstPartySetEntry(
  705. net::SchemefulSite(GURL("https://example.test")),
  706. net::SiteType::kAssociated, 0))));
  707. EXPECT_THAT(FindOwnersAndWait({kNonmember}), IsEmpty());
  708. EXPECT_THAT(FindOwnersAndWait({kExample, kNonmember}),
  709. UnorderedElementsAre(
  710. Pair(SerializesTo("https://example.test"),
  711. net::FirstPartySetEntry(
  712. net::SchemefulSite(GURL("https://example.test")),
  713. net::SiteType::kPrimary, absl::nullopt))));
  714. EXPECT_THAT(FindOwnersAndWait({kMember1, kNonmember}),
  715. UnorderedElementsAre(
  716. Pair(SerializesTo("https://member1.test"),
  717. net::FirstPartySetEntry(
  718. net::SchemefulSite(GURL("https://example.test")),
  719. net::SiteType::kAssociated, 0))));
  720. EXPECT_THAT(FindOwnersAndWait({kExample, kFoo}),
  721. UnorderedElementsAre(
  722. Pair(SerializesTo("https://example.test"),
  723. net::FirstPartySetEntry(
  724. net::SchemefulSite(GURL("https://example.test")),
  725. net::SiteType::kPrimary, absl::nullopt)),
  726. Pair(SerializesTo("https://foo.test"),
  727. net::FirstPartySetEntry(
  728. net::SchemefulSite(GURL("https://foo.test")),
  729. net::SiteType::kPrimary, absl::nullopt))));
  730. EXPECT_THAT(FindOwnersAndWait({kMember1, kFoo}),
  731. UnorderedElementsAre(
  732. Pair(SerializesTo("https://member1.test"),
  733. net::FirstPartySetEntry(
  734. net::SchemefulSite(GURL("https://example.test")),
  735. net::SiteType::kAssociated, 0)),
  736. Pair(SerializesTo("https://foo.test"),
  737. net::FirstPartySetEntry(
  738. net::SchemefulSite(GURL("https://foo.test")),
  739. net::SiteType::kPrimary, absl::nullopt))));
  740. EXPECT_THAT(FindOwnersAndWait({kExample, kMember2}),
  741. UnorderedElementsAre(
  742. Pair(SerializesTo("https://example.test"),
  743. net::FirstPartySetEntry(
  744. net::SchemefulSite(GURL("https://example.test")),
  745. net::SiteType::kPrimary, absl::nullopt)),
  746. Pair(SerializesTo("https://member2.test"),
  747. net::FirstPartySetEntry(
  748. net::SchemefulSite(GURL("https://foo.test")),
  749. net::SiteType::kAssociated, 0))));
  750. EXPECT_THAT(FindOwnersAndWait({kMember1, kMember2}),
  751. UnorderedElementsAre(
  752. Pair(SerializesTo("https://member1.test"),
  753. net::FirstPartySetEntry(
  754. net::SchemefulSite(GURL("https://example.test")),
  755. net::SiteType::kAssociated, 0)),
  756. Pair(SerializesTo("https://member2.test"),
  757. net::FirstPartySetEntry(
  758. net::SchemefulSite(GURL("https://foo.test")),
  759. net::SiteType::kAssociated, 0))));
  760. }
  761. class OverrideSetsFirstPartySetsManagerTest : public FirstPartySetsEnabledTest {
  762. public:
  763. OverrideSetsFirstPartySetsManagerTest() {
  764. net::SchemefulSite foo(GURL("https://foo.test"));
  765. net::SchemefulSite example_test(GURL("https://example.test"));
  766. net::SchemefulSite example_cctld(GURL("https://example.cctld"));
  767. SetCompleteSets(
  768. {
  769. {net::SchemefulSite(GURL("https://member1.test")),
  770. net::FirstPartySetEntry(example_test, net::SiteType::kAssociated,
  771. 0)},
  772. {net::SchemefulSite(GURL("https://member2.test")),
  773. net::FirstPartySetEntry(example_test, net::SiteType::kAssociated,
  774. 0)},
  775. // Below are the owner self mappings.
  776. {example_test,
  777. net::FirstPartySetEntry(example_test, net::SiteType::kPrimary,
  778. absl::nullopt)},
  779. },
  780. {{example_cctld, example_test}});
  781. SetFirstPartySetsContextConfig(
  782. true,
  783. {
  784. // New entry:
  785. {foo,
  786. {net::FirstPartySetEntry(foo, net::SiteType::kPrimary,
  787. absl::nullopt)}},
  788. // Removed entry:
  789. {net::SchemefulSite(GURL("https://member1.test")), absl::nullopt},
  790. // Remapped entry:
  791. {net::SchemefulSite(GURL("https://member2.test")),
  792. {net::FirstPartySetEntry(foo, net::SiteType::kAssociated, 0)}},
  793. // Removed alias:
  794. {example_cctld, absl::nullopt},
  795. });
  796. }
  797. };
  798. TEST_F(OverrideSetsFirstPartySetsManagerTest, FindOwners) {
  799. net::SchemefulSite foo(GURL("https://foo.test"));
  800. net::SchemefulSite example_test(GURL("https://example.test"));
  801. net::SchemefulSite example_cctld(GURL("https://example.cctld"));
  802. net::SchemefulSite member2(GURL("https://member2.test"));
  803. EXPECT_THAT(FindOwnersAndWait({
  804. net::SchemefulSite(GURL("https://member1.test")),
  805. member2,
  806. foo,
  807. // No entry for example_cctld since it's been deleted in the
  808. // override, even though it's an alias and its canonical site
  809. // still has a valid entry.
  810. example_cctld,
  811. example_test,
  812. }),
  813. UnorderedElementsAre(
  814. Pair(foo, net::FirstPartySetEntry(
  815. foo, net::SiteType::kPrimary, absl::nullopt)),
  816. Pair(member2, net::FirstPartySetEntry(
  817. foo, net::SiteType::kAssociated, 0)),
  818. Pair(example_test, net::FirstPartySetEntry(
  819. example_test, net::SiteType::kPrimary,
  820. absl::nullopt))));
  821. }
  822. TEST_F(OverrideSetsFirstPartySetsManagerTest, ComputeMetadata) {
  823. net::SchemefulSite example(GURL("https://example.test"));
  824. net::SchemefulSite foo(GURL("https://foo.test"));
  825. net::SchemefulSite member1(GURL("https://member1.test"));
  826. net::SchemefulSite member2(GURL("https://member2.test"));
  827. net::FirstPartySetEntry example_primary_entry(
  828. example, net::SiteType::kPrimary, absl::nullopt);
  829. net::FirstPartySetEntry foo_primary_entry(foo, net::SiteType::kPrimary,
  830. absl::nullopt);
  831. net::FirstPartySetEntry foo_associated_entry(foo, net::SiteType::kAssociated,
  832. 0);
  833. // member1 has been removed from its set.
  834. EXPECT_EQ(ComputeMetadataAndWait(member1, &example, {}),
  835. net::FirstPartySetMetadata(net::SamePartyContext(Type::kCrossParty),
  836. nullptr, &example_primary_entry));
  837. // member2 and foo are sites in a new set.
  838. EXPECT_EQ(
  839. ComputeMetadataAndWait(member2, &foo, {}),
  840. net::FirstPartySetMetadata(net::SamePartyContext(Type::kSameParty),
  841. &foo_associated_entry, &foo_primary_entry));
  842. }
  843. } // namespace network