Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 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 CONTENT_BROWSER_AGENT_CLUSTER_KEY_H_ |
| 6 | #define CONTENT_BROWSER_AGENT_CLUSTER_KEY_H_ |
| 7 | |
| 8 | #include <optional> |
Victor Hugo Vianna Silva | 0069272 | 2025-03-18 19:51:48 | [diff] [blame] | 9 | #include <variant> |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 10 | |
| 11 | #include "content/browser/security/coop/cross_origin_isolation_mode.h" |
| 12 | #include "content/common/content_export.h" |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 13 | #include "url/gurl.h" |
| 14 | #include "url/origin.h" |
| 15 | |
| 16 | namespace content { |
| 17 | |
| 18 | // AgentClusterKey represents the implementation in the browser process of the |
| 19 | // AgentClusterKey concept of the HTML spec: |
| 20 | // https://html.spec.whatwg.org/multipage/webappapis.html#agent-cluster-key |
| 21 | // |
| 22 | // SiteInstances have an AgentClusterKey in their SiteInfo, which represents the |
| 23 | // AgentClusterKey of the execution contexts hosted by the SiteInstance. In a |
| 24 | // BrowsingInstance, all regular web execution contexts needing the same |
| 25 | // AgentClusterKey are hosted in the same SiteInstance. There can be exceptions |
| 26 | // for non-regular web contexts, such as Guest Views, as they may require to be |
| 27 | // hosted in a separate SiteInstance for security reasons. |
| 28 | // |
| 29 | // The AgentClusterKey is computed upon navigation, or when launching a worker. |
| 30 | // It is then passed to RenderFrameHostManager to determine which SiteInstance |
| 31 | // is appropriate to host the execution context. |
| 32 | // TODO(crbug.com/342365078): Currently, AgentClusterKey is only computed when a |
| 33 | // document has a Document-Isolation-Policy. Compute it on all navigations. Once |
| 34 | // this is properly done, use the AgentClusterKey to replace the site URL in |
| 35 | // SiteInfo, as it will only duplicate the information in AgentClusterKey. |
| 36 | class CONTENT_EXPORT AgentClusterKey { |
| 37 | public: |
| 38 | // Cross-origin isolated agent clusters have an additional isolation key. |
| 39 | struct CONTENT_EXPORT CrossOriginIsolationKey { |
| 40 | CrossOriginIsolationKey( |
| 41 | const url::Origin& common_coi_origin, |
| 42 | CrossOriginIsolationMode cross_origin_isolation_mode); |
| 43 | CrossOriginIsolationKey(const CrossOriginIsolationKey& other); |
| 44 | ~CrossOriginIsolationKey(); |
| 45 | bool operator==(const CrossOriginIsolationKey& b) const; |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 46 | // The origin of the document which triggered cross-origin isolation. This |
| 47 | // might be different from the origin returned by AgentClusterKey::GetOrigin |
| 48 | // when cross-origin isolation was enabled by COOP + COEP. It should always |
| 49 | // match when cross-origin isolation was enabled by |
| 50 | // Document-Isolation-Policy. |
| 51 | url::Origin common_coi_origin; |
| 52 | |
| 53 | // Whether cross-origin isolation is effective or logical. Effective |
| 54 | // cross-origin isolation grants access to extra web APIs. Some platforms |
| 55 | // might not have the process model needed to support cross-origin |
| 56 | // isolation. In this case, the web-visible isolation restrictions apply, |
| 57 | // but do not lead to access to extra APIs. This is logical cross-origin |
| 58 | // isolation. |
| 59 | CrossOriginIsolationMode cross_origin_isolation_mode; |
| 60 | }; |
| 61 | |
Camille Lamy | 5ce9b96 | 2025-08-08 12:10:45 | [diff] [blame] | 62 | // Tracks the state of an Origin-Agent-Cluster request for a document. |
| 63 | // The Origin-Agent-Cluster header can be used to request either an |
| 64 | // origin-keyed agent cluster (1?) or a site-keyed one (0?). In the absence of |
| 65 | // an OAC header, agent clusters will be either site-keyed or origin-keyed by |
| 66 | // default, depending on whether features::kOriginKeyedProcessesByDefault is |
| 67 | // enabled. |
| 68 | enum class OACStatus { |
| 69 | kOriginKeyedByHeader, |
| 70 | kSiteKeyedByHeader, |
| 71 | kOriginKeyedByDefault, |
| 72 | kSiteKeyedByDefault |
| 73 | }; |
| 74 | |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 75 | // Following the deprecation of document.domain by default (a.k.a. |
| 76 | // Origin-Agent-Cluster by default), AgentClusterKeys should be origin keyed |
| 77 | // unless the document sends a "Origin-Agent-Cluster: ?0" header. However, |
| 78 | // without SiteInstanceGroup, this would lead to extra process creation. So |
| 79 | // when computing AgentClusterKeys for all navigations, we might need to make |
| 80 | // them site-keyed by default until SiteInstanceGroup ships. |
| 81 | // See crbug.com/40176090. |
| 82 | static AgentClusterKey CreateSiteKeyed(const GURL& site_url); |
| 83 | static AgentClusterKey CreateOriginKeyed(const url::Origin& origin); |
| 84 | |
| 85 | static AgentClusterKey CreateWithCrossOriginIsolationKey( |
| 86 | const url::Origin& origin, |
| 87 | const AgentClusterKey::CrossOriginIsolationKey& isolation_key); |
| 88 | |
Camille Lamy | 52a5120 | 2025-07-29 14:16:12 | [diff] [blame] | 89 | // The default constructor will create an AgentClusterKey site-keyed to the |
| 90 | // empty URL. |
| 91 | // TODO(crbug.com/342366372): Once SiteInstanceGroup has launched for all |
| 92 | // SiteInstances, the default constructor should return an origin-keyed |
| 93 | // AgentClusterKey with an empty origin. |
| 94 | AgentClusterKey(); |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 95 | AgentClusterKey(const AgentClusterKey& other); |
| 96 | ~AgentClusterKey(); |
| 97 | |
| 98 | // Whether the Agent Cluster is keyed using Site URL or Origin. |
| 99 | bool IsSiteKeyed() const; |
| 100 | bool IsOriginKeyed() const; |
| 101 | |
| 102 | // The site URL or the origin of the AgentClusterKey. Each function should |
| 103 | // only be called when the Agent Cluster is site-keyed or origin-keyed |
| 104 | // respectively. The functions will CHECK fail if called in the wrong cases. |
| 105 | const GURL& GetSite() const; |
| 106 | const url::Origin& GetOrigin() const; |
| 107 | |
| 108 | // Returns nullopt if the AgentClusterKey is not cross-origin isolated. |
| 109 | // Otherwise, returns the CrossOriginIsolationKey associated to the |
| 110 | // AgentClusterKey. |
| 111 | const std::optional<AgentClusterKey::CrossOriginIsolationKey>& |
| 112 | GetCrossOriginIsolationKey() const; |
| 113 | |
Camille Lamy | 52a5120 | 2025-07-29 14:16:12 | [diff] [blame] | 114 | // Returns true if the AgentClusterKey is cross-origin isolated. |
| 115 | bool IsCrossOriginIsolated() const; |
| 116 | |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 117 | bool operator==(const AgentClusterKey& b) const; |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 118 | |
| 119 | // Needed for tie comparisons in SiteInfo. |
| 120 | bool operator<(const AgentClusterKey& b) const; |
| 121 | |
| 122 | private: |
Victor Hugo Vianna Silva | 0069272 | 2025-03-18 19:51:48 | [diff] [blame] | 123 | AgentClusterKey(const std::variant<GURL, url::Origin>& key, |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 124 | const std::optional<AgentClusterKey::CrossOriginIsolationKey>& |
| 125 | isolation_key); |
| 126 | |
Camille Lamy | 52a5120 | 2025-07-29 14:16:12 | [diff] [blame] | 127 | // The origin or site URL that all execution contexts in the agent cluster |
| 128 | // must share. By default, this is a site URL and the agent cluster is |
| 129 | // site-keyed. The agent cluster can also be origin-keyed, in which case |
| 130 | // execution contexts in the agent cluster must share the same origin, as |
| 131 | // opposed to the site URL. |
| 132 | // |
| 133 | // For example, execution contexts with origin "https://example.com" and |
| 134 | // "https://subdomain.example.com" can be placed in the same site-keyed agent |
| 135 | // cluster with site URL key "https://example.com". But an execution context |
| 136 | // with origin "https://subdomain.example.com" cannot be placed in |
| 137 | // origin-keyed agent cluster with origin key "https://example.com" (because |
| 138 | // it is not same-origin with the origin key of the agent cluster). |
| 139 | // |
| 140 | // When used in ProcessLocks, in the case of an unlocked AllowAnySite process, |
| 141 | // the key_ will be an empty GURL in non-cross-origin isolated cases. For |
| 142 | // cross-origin isolated cases, it will be an empty origin (along with the |
| 143 | // appropriate cross-origin isolation key). |
Victor Hugo Vianna Silva | 0069272 | 2025-03-18 19:51:48 | [diff] [blame] | 144 | std::variant<GURL, url::Origin> key_; |
Camille Lamy | d1f015d | 2024-07-06 14:14:10 | [diff] [blame] | 145 | |
| 146 | // This is used by DocumentIsolationPolicy to isolate the document in an agent |
| 147 | // cluster with the appropriate cross-origin isolation status. Setting this to |
| 148 | // nullopt means that the AgentClusterKey is not cross-origin isolated. |
| 149 | // TODO(crbug.com/342365083): Currently the CrossOriginIsolationKey is only |
| 150 | // set based on DocumentIsolationPolicy. It should also be set for documents |
| 151 | // in a page with COOP and COEP. |
| 152 | std::optional<AgentClusterKey::CrossOriginIsolationKey> isolation_key_; |
| 153 | }; |
| 154 | |
| 155 | CONTENT_EXPORT std::ostream& operator<<( |
| 156 | std::ostream& out, |
| 157 | const AgentClusterKey& agent_cluster_key); |
| 158 | |
| 159 | } // namespace content |
| 160 | |
| 161 | #endif // CONTENT_BROWSER_AGENT_CLUSTER_KEY_H_ |