blob: a42d7ecf09fbe65ea452985a8686e45faff5a47d [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2022 The Chromium Authors
Brianna Goldsteinbd14ec62022-07-21 06:48:532// 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 Goldstein64891e742022-09-20 01:10:2413#include "net/base/network_isolation_key.h"
Brianna Goldsteinbd14ec62022-07-21 06:48:5314#include "net/base/schemeful_site.h"
15#include "third_party/abseil-cpp/absl/types/optional.h"
16
17namespace 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 Goldstein3cbf48a2022-07-27 21:35:4930// be empty when kEnableDoubleKeyNetworkAnonymizationKey is enabled.
Brianna Goldsteinbd14ec62022-07-21 06:48:5331
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 Goldstein3cbf48a2022-07-27 21:35:4934//`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 Goldsteinbd14ec62022-07-21 06:48:5338
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
56class NET_EXPORT NetworkAnonymizationKey {
57 public:
58 NetworkAnonymizationKey(
59 const SchemefulSite& top_frame_site,
Brianna Goldstein32eacb0b2022-08-29 08:52:1960 const absl::optional<SchemefulSite>& frame_site = absl::nullopt,
Brianna Goldstein3cbf48a2022-07-27 21:35:4961 const absl::optional<bool> is_cross_site = absl::nullopt,
Brianna Goldsteinbd14ec62022-07-21 06:48:5362 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 Goldstein64891e742022-09-20 01:10:2497 // 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 Goldsteinbd14ec62022-07-21 06:48:53103 // 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 Goldsteinbd14ec62022-07-21 06:48:53121 const absl::optional<SchemefulSite>& GetTopFrameSite() const {
122 return top_frame_site_;
123 }
124
Brianna Goldstein9c044362022-09-08 14:56:02125 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 Goldsteinbd14ec62022-07-21 06:48:53132 return frame_site_;
133 }
134
Brianna Goldstein3cbf48a2022-07-27 21:35:49135 bool GetIsCrossSite() const;
Brianna Goldsteinbd14ec62022-07-21 06:48:53136
137 const absl::optional<base::UnguessableToken>& GetNonce() const {
138 return nonce_;
139 }
140
Brianna Goldstein3cbf48a2022-07-27 21:35:49141 // 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 Goldstein1ad5e962022-07-25 19:17:46148 // 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 Goldstein3cbf48a2022-07-27 21:35:49152 // `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 Goldstein1ad5e962022-07-25 19:17:46163
Brianna Goldsteinbd14ec62022-07-21 06:48:53164 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_