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 | |
Peter Varga | ec19305 | 2021-12-01 10:25:05 | [diff] [blame^] | 7 | #include <tuple> |
| 8 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 9 | #include "base/feature_list.h" |
| 10 | #include "net/base/features.h" |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 11 | #include "net/cookies/cookie_constants.h" |
| 12 | |
| 13 | namespace net { |
| 14 | |
| 15 | CookiePartitionKey::CookiePartitionKey() = default; |
| 16 | |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 17 | CookiePartitionKey::CookiePartitionKey( |
| 18 | const SchemefulSite& site, |
| 19 | absl::optional<base::UnguessableToken> nonce) |
| 20 | : site_(site), nonce_(nonce) {} |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 21 | |
| 22 | CookiePartitionKey::CookiePartitionKey(const GURL& url) |
Dylan Cutler | 402f6c5d4 | 2021-11-10 17:31:33 | [diff] [blame] | 23 | : site_(SchemefulSite(url)) {} |
Dylan Cutler | 4a461f1b | 2021-11-03 01:08:36 | [diff] [blame] | 24 | |
| 25 | CookiePartitionKey::CookiePartitionKey(bool from_script) |
| 26 | : from_script_(from_script) {} |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 27 | |
| 28 | CookiePartitionKey::CookiePartitionKey(const CookiePartitionKey& other) = |
| 29 | default; |
| 30 | |
| 31 | CookiePartitionKey::CookiePartitionKey(CookiePartitionKey&& other) = default; |
| 32 | |
| 33 | CookiePartitionKey& CookiePartitionKey::operator=( |
| 34 | const CookiePartitionKey& other) = default; |
| 35 | |
| 36 | CookiePartitionKey& CookiePartitionKey::operator=(CookiePartitionKey&& other) = |
| 37 | default; |
| 38 | |
| 39 | CookiePartitionKey::~CookiePartitionKey() = default; |
| 40 | |
| 41 | bool CookiePartitionKey::operator==(const CookiePartitionKey& other) const { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 42 | return site_ == other.site_ && nonce_ == other.nonce_; |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 43 | } |
| 44 | |
Dylan Cutler | 7f12154 | 2021-09-28 20:41:48 | [diff] [blame] | 45 | bool CookiePartitionKey::operator!=(const CookiePartitionKey& other) const { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 46 | return site_ != other.site_ || nonce_ != other.nonce_; |
Dylan Cutler | 7f12154 | 2021-09-28 20:41:48 | [diff] [blame] | 47 | } |
| 48 | |
Dylan Cutler | 8b928ad | 2021-08-11 16:24:09 | [diff] [blame] | 49 | bool CookiePartitionKey::operator<(const CookiePartitionKey& other) const { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 50 | return std::tie(site_, nonce_) < std::tie(other.site_, other.nonce_); |
Dylan Cutler | 8b928ad | 2021-08-11 16:24:09 | [diff] [blame] | 51 | } |
| 52 | |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 53 | // static |
| 54 | bool CookiePartitionKey::Serialize(const absl::optional<CookiePartitionKey>& in, |
| 55 | std::string& out) { |
| 56 | if (!in) { |
| 57 | out = kEmptyCookiePartitionKey; |
| 58 | return true; |
| 59 | } |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 60 | if (!in->IsSerializeable()) |
Dylan Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 61 | return false; |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 62 | out = in->site_.GetURL().SchemeIsFile() |
| 63 | ? in->site_.SerializeFileSiteWithHost() |
| 64 | : in->site_.Serialize(); |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // static |
| 69 | bool CookiePartitionKey::Deserialize(const std::string& in, |
| 70 | absl::optional<CookiePartitionKey>& out) { |
| 71 | if (in == kEmptyCookiePartitionKey) { |
| 72 | out = absl::nullopt; |
| 73 | return true; |
| 74 | } |
Dylan Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 75 | if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) |
| 76 | return false; |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 77 | auto schemeful_site = SchemefulSite::Deserialize(in); |
| 78 | // SchemfulSite is opaque if the input is invalid. |
| 79 | if (schemeful_site.opaque()) |
| 80 | return false; |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 81 | out = absl::make_optional(CookiePartitionKey(schemeful_site, absl::nullopt)); |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 85 | absl::optional<CookiePartitionKey> CookiePartitionKey::FromNetworkIsolationKey( |
| 86 | const NetworkIsolationKey& network_isolation_key) { |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 87 | if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) { |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 88 | return absl::nullopt; |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 89 | } |
| 90 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 91 | // TODO(crbug.com/1225444): Check if the top frame site is in a First-Party |
| 92 | // Set or if it is an extension URL. |
| 93 | absl::optional<net::SchemefulSite> top_frame_site = |
| 94 | network_isolation_key.GetTopFrameSite(); |
| 95 | if (!top_frame_site) |
| 96 | return absl::nullopt; |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 97 | |
| 98 | absl::optional<base::UnguessableToken> nonce = |
| 99 | network_isolation_key.GetNonce(); |
| 100 | return absl::make_optional( |
| 101 | net::CookiePartitionKey(top_frame_site.value(), nonce)); |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 102 | } |
| 103 | |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 104 | bool CookiePartitionKey::IsSerializeable() const { |
| 105 | if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) |
| 106 | return false; |
| 107 | // We should not try to serialize a partition key created by a renderer. |
| 108 | DCHECK(!from_script_); |
shivanigithub | 7aefb23 | 2021-11-18 19:26:59 | [diff] [blame] | 109 | return !site_.opaque() && !nonce_.has_value(); |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame] | 110 | } |
| 111 | |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 112 | } // namespace net |