1 // Copyright 2019 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 #include "net/base/network_isolation_key.h"
6
7 #include <cstddef>
8 #include <optional>
9 #include <string>
10
11 #include "base/unguessable_token.h"
12 #include "net/base/features.h"
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14 #include "schemeful_site.h"
15 #include "url/gurl.h"
16 #include "url/origin.h"
17 #include "url/url_constants.h"
18
19 namespace net {
20
21 namespace {
22
GetSiteDebugString(const std::optional<SchemefulSite> & site)23 std::string GetSiteDebugString(const std::optional<SchemefulSite>& site) {
24 return site ? site->GetDebugString() : "null";
25 }
26
27 } // namespace
28
NetworkIsolationKey(const SchemefulSite & top_frame_site,const SchemefulSite & frame_site,const std::optional<base::UnguessableToken> & nonce)29 NetworkIsolationKey::NetworkIsolationKey(
30 const SchemefulSite& top_frame_site,
31 const SchemefulSite& frame_site,
32 const std::optional<base::UnguessableToken>& nonce)
33 : NetworkIsolationKey(SchemefulSite(top_frame_site),
34 SchemefulSite(frame_site),
35 std::optional<base::UnguessableToken>(nonce)) {}
36
NetworkIsolationKey(SchemefulSite && top_frame_site,SchemefulSite && frame_site,std::optional<base::UnguessableToken> && nonce)37 NetworkIsolationKey::NetworkIsolationKey(
38 SchemefulSite&& top_frame_site,
39 SchemefulSite&& frame_site,
40 std::optional<base::UnguessableToken>&& nonce)
41 : top_frame_site_(std::move(top_frame_site)),
42 frame_site_(std::make_optional(std::move(frame_site))),
43 nonce_(std::move(nonce)) {
44 DCHECK(!nonce_ || !nonce_->is_empty());
45 }
46
47 NetworkIsolationKey::NetworkIsolationKey() = default;
48
49 NetworkIsolationKey::NetworkIsolationKey(
50 const NetworkIsolationKey& network_isolation_key) = default;
51
52 NetworkIsolationKey::NetworkIsolationKey(
53 NetworkIsolationKey&& network_isolation_key) = default;
54
55 NetworkIsolationKey::~NetworkIsolationKey() = default;
56
57 NetworkIsolationKey& NetworkIsolationKey::operator=(
58 const NetworkIsolationKey& network_isolation_key) = default;
59
60 NetworkIsolationKey& NetworkIsolationKey::operator=(
61 NetworkIsolationKey&& network_isolation_key) = default;
62
CreateTransientForTesting()63 NetworkIsolationKey NetworkIsolationKey::CreateTransientForTesting() {
64 SchemefulSite site_with_opaque_origin;
65 return NetworkIsolationKey(site_with_opaque_origin, site_with_opaque_origin);
66 }
67
CreateWithNewFrameSite(const SchemefulSite & new_frame_site) const68 NetworkIsolationKey NetworkIsolationKey::CreateWithNewFrameSite(
69 const SchemefulSite& new_frame_site) const {
70 if (!top_frame_site_)
71 return NetworkIsolationKey();
72 return NetworkIsolationKey(top_frame_site_.value(), new_frame_site, nonce_);
73 }
74
ToCacheKeyString() const75 std::optional<std::string> NetworkIsolationKey::ToCacheKeyString() const {
76 if (IsTransient())
77 return std::nullopt;
78
79 return top_frame_site_->Serialize() + " " + frame_site_->Serialize();
80 }
81
ToDebugString() const82 std::string NetworkIsolationKey::ToDebugString() const {
83 // The space-separated serialization of |top_frame_site_| and
84 // |frame_site_|.
85 std::string return_string = GetSiteDebugString(top_frame_site_);
86 return_string += " " + GetSiteDebugString(frame_site_);
87
88 if (nonce_.has_value()) {
89 return_string += " (with nonce " + nonce_->ToString() + ")";
90 }
91
92 return return_string;
93 }
94
IsFullyPopulated() const95 bool NetworkIsolationKey::IsFullyPopulated() const {
96 if (!top_frame_site_.has_value()) {
97 return false;
98 }
99 if (!frame_site_.has_value()) {
100 return false;
101 }
102 return true;
103 }
104
IsTransient() const105 bool NetworkIsolationKey::IsTransient() const {
106 if (!IsFullyPopulated())
107 return true;
108 return IsOpaque();
109 }
110
IsEmpty() const111 bool NetworkIsolationKey::IsEmpty() const {
112 return !top_frame_site_.has_value() && !frame_site_.has_value();
113 }
114
IsOpaque() const115 bool NetworkIsolationKey::IsOpaque() const {
116 if (top_frame_site_->opaque()) {
117 return true;
118 }
119 if (frame_site_->opaque()) {
120 return true;
121 }
122 if (nonce_.has_value()) {
123 return true;
124 }
125 return false;
126 }
127
operator <<(std::ostream & os,const NetworkIsolationKey & nik)128 NET_EXPORT std::ostream& operator<<(std::ostream& os,
129 const NetworkIsolationKey& nik) {
130 os << nik.ToDebugString();
131 return os;
132 }
133
134 } // namespace net
135