Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 1 | // 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 Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 7 | #include <ostream> |
Peter Varga | ec19305 | 2021-12-01 10:25:05 | [diff] [blame] | 8 | #include <tuple> |
| 9 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 10 | #include "base/feature_list.h" |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 11 | #include "base/stl_util.h" |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 12 | #include "net/base/features.h" |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 13 | #include "net/cookies/cookie_constants.h" |
| 14 | |
| 15 | namespace net { |
| 16 | |
| 17 | CookiePartitionKey::CookiePartitionKey() = default; |
| 18 | |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 19 | CookiePartitionKey::CookiePartitionKey( |
| 20 | const SchemefulSite& site, |
| 21 | absl::optional<base::UnguessableToken> nonce) |
| 22 | : site_(site), nonce_(nonce) {} |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 23 | |
| 24 | CookiePartitionKey::CookiePartitionKey(const GURL& url) |
Dylan Cutler | 402f6c5d4 | 2021-11-10 17:31:33 | [diff] [blame] | 25 | : site_(SchemefulSite(url)) {} |
Dylan Cutler | 4a461f1b | 2021-11-03 01:08:36 | [diff] [blame] | 26 | |
| 27 | CookiePartitionKey::CookiePartitionKey(bool from_script) |
| 28 | : from_script_(from_script) {} |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 29 | |
| 30 | CookiePartitionKey::CookiePartitionKey(const CookiePartitionKey& other) = |
| 31 | default; |
| 32 | |
| 33 | CookiePartitionKey::CookiePartitionKey(CookiePartitionKey&& other) = default; |
| 34 | |
| 35 | CookiePartitionKey& CookiePartitionKey::operator=( |
| 36 | const CookiePartitionKey& other) = default; |
| 37 | |
| 38 | CookiePartitionKey& CookiePartitionKey::operator=(CookiePartitionKey&& other) = |
| 39 | default; |
| 40 | |
| 41 | CookiePartitionKey::~CookiePartitionKey() = default; |
| 42 | |
| 43 | bool CookiePartitionKey::operator==(const CookiePartitionKey& other) const { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 44 | return site_ == other.site_ && nonce_ == other.nonce_; |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 45 | } |
| 46 | |
Dylan Cutler | 7f12154 | 2021-09-28 20:41:48 | [diff] [blame] | 47 | bool CookiePartitionKey::operator!=(const CookiePartitionKey& other) const { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 48 | return site_ != other.site_ || nonce_ != other.nonce_; |
Dylan Cutler | 7f12154 | 2021-09-28 20:41:48 | [diff] [blame] | 49 | } |
| 50 | |
Dylan Cutler | 8b928ad | 2021-08-11 16:24:09 | [diff] [blame] | 51 | bool CookiePartitionKey::operator<(const CookiePartitionKey& other) const { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 52 | return std::tie(site_, nonce_) < std::tie(other.site_, other.nonce_); |
Dylan Cutler | 8b928ad | 2021-08-11 16:24:09 | [diff] [blame] | 53 | } |
| 54 | |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 55 | // static |
| 56 | bool CookiePartitionKey::Serialize(const absl::optional<CookiePartitionKey>& in, |
| 57 | std::string& out) { |
| 58 | if (!in) { |
| 59 | out = kEmptyCookiePartitionKey; |
| 60 | return true; |
| 61 | } |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 62 | if (!in->IsSerializeable()) |
Dylan Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 63 | return false; |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 64 | out = in->site_.GetURL().SchemeIsFile() |
| 65 | ? in->site_.SerializeFileSiteWithHost() |
| 66 | : in->site_.Serialize(); |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
| 70 | // static |
| 71 | bool CookiePartitionKey::Deserialize(const std::string& in, |
| 72 | absl::optional<CookiePartitionKey>& out) { |
| 73 | if (in == kEmptyCookiePartitionKey) { |
| 74 | out = absl::nullopt; |
| 75 | return true; |
| 76 | } |
Dylan Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 77 | if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) |
| 78 | return false; |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 79 | auto schemeful_site = SchemefulSite::Deserialize(in); |
| 80 | // SchemfulSite is opaque if the input is invalid. |
| 81 | if (schemeful_site.opaque()) |
| 82 | return false; |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 83 | out = absl::make_optional(CookiePartitionKey(schemeful_site, absl::nullopt)); |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 87 | absl::optional<CookiePartitionKey> CookiePartitionKey::FromNetworkIsolationKey( |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 88 | const NetworkIsolationKey& network_isolation_key, |
| 89 | const SchemefulSite* first_party_set_owner_site) { |
| 90 | if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 91 | return absl::nullopt; |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 92 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 93 | // 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 Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 95 | 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 Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 100 | return absl::nullopt; |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 101 | |
| 102 | absl::optional<base::UnguessableToken> nonce = |
| 103 | network_isolation_key.GetNonce(); |
| 104 | return absl::make_optional( |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 105 | net::CookiePartitionKey(*partition_key_site, nonce)); |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 106 | } |
| 107 | |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 108 | bool 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_); |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 113 | return !site_.opaque() && !nonce_.has_value(); |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 114 | } |
| 115 | |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 116 | std::ostream& operator<<(std::ostream& os, const CookiePartitionKey& cpk) { |
| 117 | os << cpk.site(); |
| 118 | return os; |
| 119 | } |
| 120 | |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 121 | } // namespace net |