Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: url/origin.cc

Issue 2714813003: Add an identity component for unique/opaque url::Origins.
Patch Set: Switch to base::UnguessableToken, add to SecurityOrigin. No conversions yet. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « url/origin.h ('k') | url/origin_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "url/origin.h" 5 #include "url/origin.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <tuple>
11 #include <utility>
12
10 #include "base/logging.h" 13 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
12 #include "url/gurl.h" 15 #include "url/gurl.h"
13 #include "url/url_canon.h" 16 #include "url/url_canon.h"
14 #include "url/url_canon_stdstring.h" 17 #include "url/url_canon_stdstring.h"
15 #include "url/url_constants.h" 18 #include "url/url_constants.h"
16 #include "url/url_util.h" 19 #include "url/url_util.h"
17 20
18 namespace url { 21 namespace url {
19 22
20 namespace { 23 namespace {
21 24
22 GURL AddSuboriginToUrl(const GURL& url, const std::string& suborigin) { 25 GURL AddSuboriginToUrl(const GURL& url, const std::string& suborigin) {
23 GURL::Replacements replacements; 26 GURL::Replacements replacements;
24 if (url.scheme() == kHttpScheme) { 27 if (url.scheme() == kHttpScheme) {
25 replacements.SetSchemeStr(kHttpSuboriginScheme); 28 replacements.SetSchemeStr(kHttpSuboriginScheme);
26 } else { 29 } else {
27 DCHECK(url.scheme() == kHttpsScheme); 30 DCHECK(url.scheme() == kHttpsScheme);
28 replacements.SetSchemeStr(kHttpsSuboriginScheme); 31 replacements.SetSchemeStr(kHttpsSuboriginScheme);
29 } 32 }
30 std::string new_host = suborigin + "." + url.host(); 33 std::string new_host = suborigin + "." + url.host();
31 replacements.SetHostStr(new_host); 34 replacements.SetHostStr(new_host);
32 return url.ReplaceComponents(replacements); 35 return url.ReplaceComponents(replacements);
33 } 36 }
34 37
35 } // namespace 38 } // namespace
36 39
37 Origin::Origin() : unique_(true), suborigin_(std::string()) {} 40 Origin::Origin()
41 : unique_(true), unique_id_(base::UnguessableToken::Create()) {}
38 42
39 Origin::Origin(const GURL& url) : unique_(true), suborigin_(std::string()) { 43 Origin::Origin(const GURL& url) : unique_(true) {
40 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) 44 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) {
45 unique_id_ = base::UnguessableToken::Create();
41 return; 46 return;
47 }
42 48
49 std::string suborigin;
43 if (url.SchemeIsFileSystem()) { 50 if (url.SchemeIsFileSystem()) {
44 tuple_ = SchemeHostPort(*url.inner_url()); 51 tuple_ = SchemeHostPort(*url.inner_url());
45 } else if (url.SchemeIsBlob()) { 52 } else if (url.SchemeIsBlob()) {
46 // If we're dealing with a 'blob:' URL, https://url.spec.whatwg.org/#origin 53 // If we're dealing with a 'blob:' URL, https://url.spec.whatwg.org/#origin
47 // defines the origin as the origin of the URL which results from parsing 54 // defines the origin as the origin of the URL which results from parsing
48 // the "path", which boils down to everything after the scheme. GURL's 55 // the "path", which boils down to everything after the scheme. GURL's
49 // 'GetContent()' gives us exactly that. 56 // 'GetContent()' gives us exactly that.
50 tuple_ = SchemeHostPort(GURL(url.GetContent())); 57 tuple_ = SchemeHostPort(GURL(url.GetContent()));
51 } else if (url.SchemeIsSuborigin()) { 58 } else if (url.SchemeIsSuborigin()) {
52 GURL::Replacements replacements; 59 GURL::Replacements replacements;
(...skipping 12 matching lines...) Expand all
65 : host.substr(suborigin_end + 1, 72 : host.substr(suborigin_end + 1,
66 url.host().length() - suborigin_end - 1)); 73 url.host().length() - suborigin_end - 1));
67 replacements.SetHostStr(new_host); 74 replacements.SetHostStr(new_host);
68 75
69 tuple_ = SchemeHostPort(url.ReplaceComponents(replacements)); 76 tuple_ = SchemeHostPort(url.ReplaceComponents(replacements));
70 77
71 bool invalid_suborigin = no_dot || suborigin_end == 0; 78 bool invalid_suborigin = no_dot || suborigin_end == 0;
72 if (invalid_suborigin || tuple_.IsInvalid()) 79 if (invalid_suborigin || tuple_.IsInvalid())
73 return; 80 return;
74 81
75 suborigin_ = host.substr(0, suborigin_end); 82 suborigin = host.substr(0, suborigin_end);
76 } else { 83 } else {
77 tuple_ = SchemeHostPort(url); 84 tuple_ = SchemeHostPort(url);
78 } 85 }
79 86
80 unique_ = tuple_.IsInvalid(); 87 unique_ = tuple_.IsInvalid();
88 if (unique_)
89 unique_id_ = base::UnguessableToken::Create();
90 else
91 suborigin_ = std::move(suborigin);
81 } 92 }
82 93
83 Origin::Origin(base::StringPiece scheme, 94 Origin::Origin(base::StringPiece scheme,
84 base::StringPiece host, 95 base::StringPiece host,
85 uint16_t port, 96 uint16_t port,
86 base::StringPiece suborigin, 97 base::StringPiece suborigin,
87 SchemeHostPort::ConstructPolicy policy) 98 SchemeHostPort::ConstructPolicy policy)
88 : tuple_(scheme.as_string(), host.as_string(), port, policy) { 99 : Origin(scheme.as_string(),
89 unique_ = tuple_.IsInvalid(); 100 host.as_string(),
90 suborigin_ = suborigin.as_string(); 101 port,
91 } 102 suborigin.as_string(),
103 policy) {}
92 104
93 Origin::Origin(std::string scheme, 105 Origin::Origin(std::string scheme,
94 std::string host, 106 std::string host,
95 uint16_t port, 107 uint16_t port,
96 std::string suborigin, 108 std::string suborigin,
97 SchemeHostPort::ConstructPolicy policy) 109 SchemeHostPort::ConstructPolicy policy)
98 : tuple_(std::move(scheme), std::move(host), port, policy) { 110 : tuple_(std::move(scheme), std::move(host), port, policy) {
99 unique_ = tuple_.IsInvalid(); 111 unique_ = tuple_.IsInvalid();
100 suborigin_ = std::move(suborigin); 112 if (unique_)
113 unique_id_ = base::UnguessableToken::Create();
114 else
115 suborigin_ = std::move(suborigin);
101 } 116 }
102 117
103 Origin::~Origin() { 118 Origin::~Origin() {
104 } 119 }
105 120
106 // static 121 // static
107 Origin Origin::UnsafelyCreateOriginWithoutNormalization( 122 Origin Origin::UnsafelyCreateOriginWithoutNormalization(
108 base::StringPiece scheme, 123 base::StringPiece scheme,
109 base::StringPiece host, 124 base::StringPiece host,
110 uint16_t port) { 125 uint16_t port) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 166
152 GURL tuple_url(tuple_.GetURL()); 167 GURL tuple_url(tuple_.GetURL());
153 168
154 if (!suborigin_.empty()) 169 if (!suborigin_.empty())
155 return AddSuboriginToUrl(tuple_url, suborigin_); 170 return AddSuboriginToUrl(tuple_url, suborigin_);
156 171
157 return tuple_url; 172 return tuple_url;
158 } 173 }
159 174
160 bool Origin::IsSameOriginWith(const Origin& other) const { 175 bool Origin::IsSameOriginWith(const Origin& other) const {
161 if (unique_ || other.unique_) 176 if (unique_ || other.unique_) {
162 return false; 177 return unique_ && other.unique_ ? unique_id_ == other.unique_id_ : false;
178 }
163 179
164 return tuple_.Equals(other.tuple_) && suborigin_ == other.suborigin_; 180 return tuple_.Equals(other.tuple_) && suborigin_ == other.suborigin_;
165 } 181 }
166 182
167 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { 183 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const {
168 return GetPhysicalOrigin().IsSameOriginWith(other.GetPhysicalOrigin()); 184 return GetPhysicalOrigin().IsSameOriginWith(other.GetPhysicalOrigin());
169 } 185 }
170 186
171 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { 187 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const {
172 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); 188 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain);
173 } 189 }
174 190
175 bool Origin::operator<(const Origin& other) const { 191 bool Origin::operator<(const Origin& other) const {
176 return tuple_ < other.tuple_; 192 // TODO(dcheng): Doesn't this need to account for suborigins?
193 return std::tie(tuple_, unique_id_) <
194 std::tie(other.tuple_, other.unique_id_);
177 } 195 }
178 196
179 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { 197 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) {
180 return out << origin.Serialize(); 198 return out << origin.Serialize();
181 } 199 }
182 200
183 bool IsSameOriginWith(const GURL& a, const GURL& b) { 201 bool IsSameOriginWith(const GURL& a, const GURL& b) {
184 return Origin(a).IsSameOriginWith(Origin(b)); 202 return Origin(a).IsSameOriginWith(Origin(b));
185 } 203 }
186 204
187 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { 205 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) {
188 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); 206 return Origin(a).IsSamePhysicalOriginWith(Origin(b));
189 } 207 }
190 208
191 } // namespace url 209 } // namespace url
OLDNEW
« no previous file with comments | « url/origin.h ('k') | url/origin_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698