blob: 569c6b8de5fdf32bea65bd58e70ada8416916baa [file] [log] [blame]
Dylan Cutler27307332021-08-02 21:00:411// Copyright 2021 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
5#include "net/cookies/cookie_partition_key.h"
6
Dylan Cutler69a51c92021-12-08 16:32:017#include <ostream>
Peter Vargaec193052021-12-01 10:25:058#include <tuple>
9
Dylan Cutler39fa227e2021-08-10 22:40:0410#include "base/feature_list.h"
Dylan Cutler69a51c92021-12-08 16:32:0111#include "base/stl_util.h"
Dylan Cutler39fa227e2021-08-10 22:40:0412#include "net/base/features.h"
Dylan Cutler27307332021-08-02 21:00:4113#include "net/cookies/cookie_constants.h"
14
15namespace net {
16
17CookiePartitionKey::CookiePartitionKey() = default;
18
shivanigithub7aefb232021-11-18 19:26:5919CookiePartitionKey::CookiePartitionKey(
20 const SchemefulSite& site,
21 absl::optional<base::UnguessableToken> nonce)
22 : site_(site), nonce_(nonce) {}
Dylan Cutler27307332021-08-02 21:00:4123
24CookiePartitionKey::CookiePartitionKey(const GURL& url)
Dylan Cutler402f6c5d42021-11-10 17:31:3325 : site_(SchemefulSite(url)) {}
Dylan Cutler4a461f1b2021-11-03 01:08:3626
27CookiePartitionKey::CookiePartitionKey(bool from_script)
28 : from_script_(from_script) {}
Dylan Cutler27307332021-08-02 21:00:4129
30CookiePartitionKey::CookiePartitionKey(const CookiePartitionKey& other) =
31 default;
32
33CookiePartitionKey::CookiePartitionKey(CookiePartitionKey&& other) = default;
34
35CookiePartitionKey& CookiePartitionKey::operator=(
36 const CookiePartitionKey& other) = default;
37
38CookiePartitionKey& CookiePartitionKey::operator=(CookiePartitionKey&& other) =
39 default;
40
41CookiePartitionKey::~CookiePartitionKey() = default;
42
43bool CookiePartitionKey::operator==(const CookiePartitionKey& other) const {
shivanigithub7aefb232021-11-18 19:26:5944 return site_ == other.site_ && nonce_ == other.nonce_;
Dylan Cutler27307332021-08-02 21:00:4145}
46
Dylan Cutler7f121542021-09-28 20:41:4847bool CookiePartitionKey::operator!=(const CookiePartitionKey& other) const {
shivanigithub7aefb232021-11-18 19:26:5948 return site_ != other.site_ || nonce_ != other.nonce_;
Dylan Cutler7f121542021-09-28 20:41:4849}
50
Dylan Cutler8b928ad2021-08-11 16:24:0951bool CookiePartitionKey::operator<(const CookiePartitionKey& other) const {
shivanigithub7aefb232021-11-18 19:26:5952 return std::tie(site_, nonce_) < std::tie(other.site_, other.nonce_);
Dylan Cutler8b928ad2021-08-11 16:24:0953}
54
Dylan Cutler27307332021-08-02 21:00:4155// static
56bool CookiePartitionKey::Serialize(const absl::optional<CookiePartitionKey>& in,
57 std::string& out) {
58 if (!in) {
59 out = kEmptyCookiePartitionKey;
60 return true;
61 }
Dylan Cutler46cb8a92021-11-17 23:29:5662 if (!in->IsSerializeable())
Dylan Cutlerc68e593e2021-09-15 22:07:2763 return false;
Dylan Cutler46cb8a92021-11-17 23:29:5664 out = in->site_.GetURL().SchemeIsFile()
65 ? in->site_.SerializeFileSiteWithHost()
66 : in->site_.Serialize();
Dylan Cutler27307332021-08-02 21:00:4167 return true;
68}
69
70// static
71bool CookiePartitionKey::Deserialize(const std::string& in,
72 absl::optional<CookiePartitionKey>& out) {
73 if (in == kEmptyCookiePartitionKey) {
74 out = absl::nullopt;
75 return true;
76 }
Dylan Cutlerc68e593e2021-09-15 22:07:2777 if (!base::FeatureList::IsEnabled(features::kPartitionedCookies))
78 return false;
Dylan Cutler27307332021-08-02 21:00:4179 auto schemeful_site = SchemefulSite::Deserialize(in);
80 // SchemfulSite is opaque if the input is invalid.
81 if (schemeful_site.opaque())
82 return false;
shivanigithub7aefb232021-11-18 19:26:5983 out = absl::make_optional(CookiePartitionKey(schemeful_site, absl::nullopt));
Dylan Cutler27307332021-08-02 21:00:4184 return true;
85}
86
Dylan Cutler39fa227e2021-08-10 22:40:0487absl::optional<CookiePartitionKey> CookiePartitionKey::FromNetworkIsolationKey(
Dylan Cutler69a51c92021-12-08 16:32:0188 const NetworkIsolationKey& network_isolation_key,
89 const SchemefulSite* first_party_set_owner_site) {
90 if (!base::FeatureList::IsEnabled(features::kPartitionedCookies))
Dylan Cutler39fa227e2021-08-10 22:40:0491 return absl::nullopt;
shivanigithub7aefb232021-11-18 19:26:5992
Dylan Cutler39fa227e2021-08-10 22:40:0493 // TODO(crbug.com/1225444): Check if the top frame site is in a First-Party
94 // Set or if it is an extension URL.
Dylan Cutler69a51c92021-12-08 16:32:0195 const SchemefulSite* partition_key_site =
96 first_party_set_owner_site
97 ? first_party_set_owner_site
98 : base::OptionalOrNullptr(network_isolation_key.GetTopFrameSite());
99 if (!partition_key_site)
Dylan Cutler39fa227e2021-08-10 22:40:04100 return absl::nullopt;
shivanigithub7aefb232021-11-18 19:26:59101
102 absl::optional<base::UnguessableToken> nonce =
103 network_isolation_key.GetNonce();
104 return absl::make_optional(
Dylan Cutler69a51c92021-12-08 16:32:01105 net::CookiePartitionKey(*partition_key_site, nonce));
Dylan Cutler39fa227e2021-08-10 22:40:04106}
107
Dylan Cutler46cb8a92021-11-17 23:29:56108bool CookiePartitionKey::IsSerializeable() const {
109 if (!base::FeatureList::IsEnabled(features::kPartitionedCookies))
110 return false;
111 // We should not try to serialize a partition key created by a renderer.
112 DCHECK(!from_script_);
shivanigithub7aefb232021-11-18 19:26:59113 return !site_.opaque() && !nonce_.has_value();
Dylan Cutler46cb8a92021-11-17 23:29:56114}
115
Dylan Cutler69a51c92021-12-08 16:32:01116std::ostream& operator<<(std::ostream& os, const CookiePartitionKey& cpk) {
117 os << cpk.site();
118 return os;
119}
120
Dylan Cutler27307332021-08-02 21:00:41121} // namespace net