Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_NETWORK_ANONYMIZATION_KEY_H_ |
| 6 | #define NET_BASE_NETWORK_ANONYMIZATION_KEY_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <tuple> |
| 10 | |
| 11 | #include "base/unguessable_token.h" |
| 12 | #include "net/base/net_export.h" |
Brianna Goldstein | 64891e74 | 2022-09-20 01:10:24 | [diff] [blame] | 13 | #include "net/base/network_isolation_key.h" |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 14 | #include "net/base/schemeful_site.h" |
| 15 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | // NetworkAnonymizationKey will be used to partition shared network state based |
| 20 | // on the context on which they were made. This class is an expiremental key |
| 21 | // that contains properties that will be changed via feature flags. |
| 22 | |
| 23 | // NetworkAnonymizationKey contains the following properties: |
| 24 | |
| 25 | // `top_frame_site` represents the SchemefulSite of the pages top level frame. |
| 26 | // In order to separate first and third party context from each other this field |
| 27 | // will always be populated. |
| 28 | |
| 29 | // `frame_site` represents the SchemefulSite of the requestor frame. This will |
Brianna Goldstein | 3cbf48a | 2022-07-27 21:35:49 | [diff] [blame] | 30 | // be empty when kEnableDoubleKeyNetworkAnonymizationKey is enabled. |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 31 | |
| 32 | //`is_cross_site` is an expiremental boolean that will be used with the |
| 33 | //`top_frame_site` to create a partition key that separates the |
Brianna Goldstein | 3cbf48a | 2022-07-27 21:35:49 | [diff] [blame] | 34 | //`top_frame_site`s first party partition from any cross-site iframes. This will |
| 35 | // be used only when `kEnableCrossSiteFlagNetworkAnonymizationKey` is enabled. |
| 36 | // When `kEnableCrossSiteFlagNetworkAnonymizationKey` is disabled, |
| 37 | // `is_cross_site_` will be an empty optional. |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 38 | |
| 39 | // The following show how the `is_cross_site` boolean is populated for the |
| 40 | // innermost frame in the chain. |
| 41 | // a->a => is_cross_site = true |
| 42 | // a->b => is_cross_site = false |
| 43 | // a->b->a => is_cross_site = false |
| 44 | // a->(sandboxed a [has nonce]) => is_cross_site = true |
| 45 | |
| 46 | // The `nonce` value creates a key for anonymous iframes by giving them a |
| 47 | // temporary `nonce` value which changes per top level navigation. For now, any |
| 48 | // NetworkAnonymizationKey with a nonce will be considered transient. This is |
| 49 | // being considered to possibly change in the future in an effort to allow |
| 50 | // anonymous iframes with the same partition key access to shared resources. |
| 51 | // The nonce value will be empty except for anonymous iframes. |
| 52 | |
| 53 | // TODO @brgoldstein, add link to public documentation of key scheme naming |
| 54 | // conventions. |
| 55 | |
| 56 | class NET_EXPORT NetworkAnonymizationKey { |
| 57 | public: |
| 58 | NetworkAnonymizationKey( |
| 59 | const SchemefulSite& top_frame_site, |
Brianna Goldstein | 32eacb0b | 2022-08-29 08:52:19 | [diff] [blame] | 60 | const absl::optional<SchemefulSite>& frame_site = absl::nullopt, |
Brianna Goldstein | 3cbf48a | 2022-07-27 21:35:49 | [diff] [blame] | 61 | const absl::optional<bool> is_cross_site = absl::nullopt, |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 62 | const absl::optional<base::UnguessableToken> nonce = absl::nullopt); |
| 63 | |
| 64 | // Construct an empty key. |
| 65 | NetworkAnonymizationKey(); |
| 66 | |
| 67 | NetworkAnonymizationKey( |
| 68 | const NetworkAnonymizationKey& network_anonymization_key); |
| 69 | NetworkAnonymizationKey(NetworkAnonymizationKey&& network_anonymization_key); |
| 70 | |
| 71 | ~NetworkAnonymizationKey(); |
| 72 | |
| 73 | NetworkAnonymizationKey& operator=( |
| 74 | const NetworkAnonymizationKey& network_anonymization_key); |
| 75 | NetworkAnonymizationKey& operator=( |
| 76 | NetworkAnonymizationKey&& network_anonymization_key); |
| 77 | |
| 78 | // Compare keys for equality, true if all enabled fields are equal. |
| 79 | bool operator==(const NetworkAnonymizationKey& other) const { |
| 80 | return std::tie(top_frame_site_, frame_site_, is_cross_site_, nonce_) == |
| 81 | std::tie(other.top_frame_site_, other.frame_site_, |
| 82 | other.is_cross_site_, other.nonce_); |
| 83 | } |
| 84 | |
| 85 | // Compare keys for inequality, true if any enabled field varies. |
| 86 | bool operator!=(const NetworkAnonymizationKey& other) const { |
| 87 | return !(*this == other); |
| 88 | } |
| 89 | |
| 90 | // Provide an ordering for keys based on all enabled fields. |
| 91 | bool operator<(const NetworkAnonymizationKey& other) const { |
| 92 | return std::tie(top_frame_site_, frame_site_, is_cross_site_, nonce_) < |
| 93 | std::tie(other.top_frame_site_, other.frame_site_, |
| 94 | other.is_cross_site_, other.nonce_); |
| 95 | } |
| 96 | |
Brianna Goldstein | 64891e74 | 2022-09-20 01:10:24 | [diff] [blame] | 97 | // Creates a NetworkAnonymizationKey from a NetworkIsolationKey. This is |
| 98 | // possible because a NetworkIsolationKey must always be more granular than a |
| 99 | // NetworkAnonymizationKey. |
| 100 | static NetworkAnonymizationKey CreateFromNetworkIsolationKey( |
| 101 | const net::NetworkIsolationKey& network_isolation_key); |
| 102 | |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 103 | // Returns the string representation of the key. |
| 104 | std::string ToDebugString() const; |
| 105 | |
| 106 | // Returns true if all parts of the key are empty. |
| 107 | bool IsEmpty() const; |
| 108 | |
| 109 | // Returns true if `top_frame_site_` and `frame_site_` of the key are |
| 110 | // non-empty. |
| 111 | bool IsFullyPopulated() const; |
| 112 | |
| 113 | // Returns true if this key's lifetime is short-lived. It may not make sense |
| 114 | // to persist state to disk related to it (e.g., disk cache). |
| 115 | // A NetworkAnonymizationKey will be considered transient if either |
| 116 | // `top_frame_site_` or `frame_site_` are empty or opaque or if the key has a |
| 117 | // `nonce_`. |
| 118 | bool IsTransient() const; |
| 119 | |
| 120 | // Getters for the top frame, frame site, nonce and is cross site flag. |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 121 | const absl::optional<SchemefulSite>& GetTopFrameSite() const { |
| 122 | return top_frame_site_; |
| 123 | } |
| 124 | |
Brianna Goldstein | 9c04436 | 2022-09-08 14:56:02 | [diff] [blame] | 125 | const absl::optional<SchemefulSite>& GetFrameSite() const; |
| 126 | |
| 127 | // Do not use outside of testing. Returns the `frame_site_` if neither |
| 128 | // `kEnableCrossSiteFlagNetworkAnonymizationKey` or |
| 129 | // `kEnableDoubleKeyNetworkAnonymizationKey` are enabled. Else it |
| 130 | // returns nullopt. |
| 131 | const absl::optional<SchemefulSite>& GetFrameSiteForTesting() const { |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 132 | return frame_site_; |
| 133 | } |
| 134 | |
Brianna Goldstein | 3cbf48a | 2022-07-27 21:35:49 | [diff] [blame] | 135 | bool GetIsCrossSite() const; |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 136 | |
| 137 | const absl::optional<base::UnguessableToken>& GetNonce() const { |
| 138 | return nonce_; |
| 139 | } |
| 140 | |
Brianna Goldstein | 3cbf48a | 2022-07-27 21:35:49 | [diff] [blame] | 141 | // Returns true if the NetworkAnonymizationKey has a triple keyed scheme. This |
| 142 | // means the values of the NetworkAnonymizationKey are as follows: |
| 143 | // `top_frame_site` -> the schemeful site of the top level page. |
| 144 | // `frame_site ` -> the schemeful site of the requestor frame |
| 145 | // `is_cross_site` -> nullopt |
| 146 | static bool IsFrameSiteEnabled(); |
| 147 | |
Brianna Goldstein | 1ad5e96 | 2022-07-25 19:17:46 | [diff] [blame] | 148 | // Returns true if the NetworkAnonymizationKey has a double keyed scheme. This |
| 149 | // means the values of the NetworkAnonymizationKey are as follows: |
| 150 | // `top_frame_site` -> the schemeful site of the top level page. |
| 151 | // `frame_site ` -> nullopt |
Brianna Goldstein | 3cbf48a | 2022-07-27 21:35:49 | [diff] [blame] | 152 | // `is_cross_site` -> nullopt |
| 153 | static bool IsDoubleKeySchemeEnabled(); |
| 154 | |
| 155 | // Returns true if the NetworkAnonymizationKey has a <double keyed + |
| 156 | // is_cross_site> scheme. This means the values of the NetworkAnonymizationKey |
| 157 | // are as follows: |
| 158 | // `top_frame_site` -> the schemeful site of the top level page. |
| 159 | // `frame_site ` -> nullopt |
| 160 | // `is_cross_site` -> a boolean indicating if the requestor frame site is |
| 161 | // cross site from the top level site. |
| 162 | static bool IsCrossSiteFlagSchemeEnabled(); |
Brianna Goldstein | 1ad5e96 | 2022-07-25 19:17:46 | [diff] [blame] | 163 | |
Brianna Goldstein | bd14ec6 | 2022-07-21 06:48:53 | [diff] [blame] | 164 | private: |
| 165 | std::string GetSiteDebugString( |
| 166 | const absl::optional<SchemefulSite>& site) const; |
| 167 | // The origin/etld+1 of the top frame of the page making the request. This |
| 168 | // will always be populated unless all other fields are also nullopt. |
| 169 | absl::optional<SchemefulSite> top_frame_site_; |
| 170 | |
| 171 | // The origin/etld+1 of the frame that initiates the request. |
| 172 | absl::optional<SchemefulSite> frame_site_; |
| 173 | |
| 174 | // True if the frame site is cross site when compared to the top frame site. |
| 175 | absl::optional<bool> is_cross_site_; |
| 176 | |
| 177 | // for non-opaque origins. |
| 178 | absl::optional<base::UnguessableToken> nonce_; |
| 179 | }; |
| 180 | |
| 181 | } // namespace net |
| 182 | |
| 183 | #endif // NET_BASE_NETWORK_ANONYMIZATION_KEY_H_ |