mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef URL_ORIGIN_H_ |
| 6 | #define URL_ORIGIN_H_ |
| 7 | |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Robert Ogden | dd74d73 | 2020-03-12 17:33:19 | [diff] [blame^] | 10 | #include <memory> |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 13 | #include "base/component_export.h" |
Lukasz Anforowicz | 3d58b939 | 2018-01-05 20:56:22 | [diff] [blame] | 14 | #include "base/debug/alias.h" |
Lukasz Anforowicz | 731c39c | 2020-02-07 23:35:58 | [diff] [blame] | 15 | #include "base/debug/crash_logging.h" |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 16 | #include "base/optional.h" |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
| 20 | #include "base/unguessable_token.h" |
Rouslan Solomakhin | 63cdb148 | 2019-09-30 21:53:14 | [diff] [blame] | 21 | #include "build/build_config.h" |
Nasko Oskov | d9e41d5 | 2018-09-27 23:12:42 | [diff] [blame] | 22 | #include "ipc/ipc_param_traits.h" |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 23 | #include "url/scheme_host_port.h" |
| 24 | #include "url/third_party/mozilla/url_parse.h" |
| 25 | #include "url/url_canon.h" |
| 26 | #include "url/url_constants.h" |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 27 | |
Rouslan Solomakhin | 63cdb148 | 2019-09-30 21:53:14 | [diff] [blame] | 28 | #if defined(OS_ANDROID) |
| 29 | #include <jni.h> |
| 30 | |
| 31 | namespace base { |
| 32 | namespace android { |
| 33 | template <typename> |
| 34 | class ScopedJavaLocalRef; |
| 35 | template <typename> |
| 36 | class JavaRef; |
| 37 | } // namespace android |
| 38 | } // namespace base |
| 39 | #endif // OS_ANDROID |
| 40 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 41 | class GURL; |
| 42 | |
Nasko Oskov | 99445acd | 2018-10-10 16:46:00 | [diff] [blame] | 43 | namespace blink { |
| 44 | class SecurityOrigin; |
| 45 | } // namespace blink |
| 46 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 47 | namespace ipc_fuzzer { |
| 48 | template <class T> |
| 49 | struct FuzzTraits; |
| 50 | } // namespace ipc_fuzzer |
| 51 | |
Nasko Oskov | d9e41d5 | 2018-09-27 23:12:42 | [diff] [blame] | 52 | namespace mojo { |
| 53 | template <typename DataViewType, typename T> |
| 54 | struct StructTraits; |
Nasko Oskov | 99445acd | 2018-10-10 16:46:00 | [diff] [blame] | 55 | struct UrlOriginAdapter; |
Nasko Oskov | d9e41d5 | 2018-09-27 23:12:42 | [diff] [blame] | 56 | } // namespace mojo |
| 57 | |
Robert Ogden | dd74d73 | 2020-03-12 17:33:19 | [diff] [blame^] | 58 | namespace net { |
| 59 | class NetworkIsolationKey; |
| 60 | class OpaqueNonTransientNetworkIsolationKeyTest; |
| 61 | } // namespace net |
| 62 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 63 | namespace url { |
| 64 | |
Nasko Oskov | d9e41d5 | 2018-09-27 23:12:42 | [diff] [blame] | 65 | namespace mojom { |
| 66 | class OriginDataView; |
| 67 | } // namespace mojom |
| 68 | |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 69 | // Per https://html.spec.whatwg.org/multipage/origin.html#origin, an origin is |
| 70 | // either: |
| 71 | // - a tuple origin of (scheme, host, port) as described in RFC 6454. |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 72 | // - an opaque origin with an internal value, and a memory of the tuple origin |
| 73 | // from which it was derived. |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 74 | // |
| 75 | // TL;DR: If you need to make a security-relevant decision, use 'url::Origin'. |
| 76 | // If you only need to extract the bits of a URL which are relevant for a |
| 77 | // network connection, use 'url::SchemeHostPort'. |
| 78 | // |
| 79 | // STL;SDR: If you aren't making actual network connections, use 'url::Origin'. |
| 80 | // |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 81 | // This class ought to be used when code needs to determine if two resources |
| 82 | // are "same-origin", and when a canonical serialization of an origin is |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 83 | // required. Note that the canonical serialization of an origin *must not* be |
| 84 | // used to determine if two resources are same-origin. |
| 85 | // |
| 86 | // A tuple origin, like 'SchemeHostPort', is composed of a tuple of (scheme, |
| 87 | // host, port), but contains a number of additional concepts which make it |
| 88 | // appropriate for use as a security boundary and access control mechanism |
| 89 | // between contexts. Two tuple origins are same-origin if the tuples are equal. |
| 90 | // A tuple origin may also be re-created from its serialization. |
| 91 | // |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 92 | // An opaque origin has an internal globally unique identifier. When creating a |
| 93 | // new opaque origin from a URL, a fresh globally unique identifier is |
| 94 | // generated. However, if an opaque origin is copied or moved, the internal |
| 95 | // globally unique identifier is preserved. Two opaque origins are same-origin |
| 96 | // iff the globally unique identifiers match. Unlike tuple origins, an opaque |
| 97 | // origin cannot be re-created from its serialization, which is always the |
| 98 | // string "null". |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 99 | // |
| 100 | // IMPORTANT: Since opaque origins always serialize as the string "null", it is |
| 101 | // *never* safe to use the serialization for security checks! |
| 102 | // |
| 103 | // A tuple origin and an opaque origin are never same-origin. |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 104 | // |
| 105 | // There are a few subtleties to note: |
| 106 | // |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 107 | // * A default constructed Origin is opaque, with no precursor origin. |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 108 | // |
| 109 | // * Invalid and non-standard GURLs are parsed as opaque origins. This includes |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 110 | // non-hierarchical URLs like 'data:text/html,...' and 'javascript:alert(1)'. |
| 111 | // |
| 112 | // * GURLs with schemes of 'filesystem' or 'blob' parse the origin out of the |
| 113 | // internals of the URL. That is, 'filesystem:https://example.com/temporary/f' |
| 114 | // is parsed as ('https', 'example.com', 443). |
| 115 | // |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 116 | // * GURLs with a 'file' scheme are tricky. They are parsed as ('file', '', 0), |
| 117 | // but their behavior may differ from embedder to embedder. |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 118 | // TODO(dcheng): This behavior is not consistent with Blink's notion of file |
| 119 | // URLs, which always creates an opaque origin. |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 120 | // |
| 121 | // * The host component of an IPv6 address includes brackets, just like the URL |
| 122 | // representation. |
| 123 | // |
| 124 | // Usage: |
| 125 | // |
| 126 | // * Origins are generally constructed from an already-canonicalized GURL: |
| 127 | // |
| 128 | // GURL url("https://example.com/"); |
Daniel Vogelheim | df4cf9e | 2018-06-14 11:42:40 | [diff] [blame] | 129 | // url::Origin origin = Origin::Create(url); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 130 | // origin.scheme(); // "https" |
| 131 | // origin.host(); // "example.com" |
| 132 | // origin.port(); // 443 |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 133 | // origin.opaque(); // false |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 134 | // |
| 135 | // * To answer the question "Are |this| and |that| "same-origin" with each |
| 136 | // other?", use |Origin::IsSameOriginWith|: |
| 137 | // |
| 138 | // if (this.IsSameOriginWith(that)) { |
| 139 | // // Amazingness goes here. |
| 140 | // } |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 141 | class COMPONENT_EXPORT(URL) Origin { |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 142 | public: |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 143 | // Creates an opaque Origin with a nonce that is different from all previously |
| 144 | // existing origins. |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 145 | Origin(); |
| 146 | |
| 147 | // Creates an Origin from |url|, as described at |
| 148 | // https://url.spec.whatwg.org/#origin, with the following additions: |
| 149 | // |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 150 | // 1. If |url| is invalid or non-standard, an opaque Origin is constructed. |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 151 | // 2. 'filesystem' URLs behave as 'blob' URLs (that is, the origin is parsed |
| 152 | // out of everything in the URL which follows the scheme). |
| 153 | // 3. 'file' URLs all parse as ("file", "", 0). |
Lukasz Anforowicz | fd7834ec | 2020-03-04 02:11:57 | [diff] [blame] | 154 | // |
| 155 | // Note that the returned Origin may have a different scheme and host from |
| 156 | // |url| (e.g. in case of blob URLs - see OriginTest.ConstructFromGURL). |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 157 | static Origin Create(const GURL& url); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 158 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 159 | // Creates an Origin for the resource |url| as if it were requested |
| 160 | // from the context of |base_origin|. If |url| is standard |
| 161 | // (in the sense that it embeds a complete origin, like http/https), |
| 162 | // this returns the same value as would Create(). |
| 163 | // |
| 164 | // If |url| is "about:blank", this returns a copy of |base_origin|. |
| 165 | // |
| 166 | // Otherwise, returns a new opaque origin derived from |base_origin|. |
| 167 | // In this case, the resulting opaque origin will inherit the tuple |
| 168 | // (or precursor tuple) of |base_origin|, but will not be same origin |
| 169 | // with |base_origin|, even if |base_origin| is already opaque. |
| 170 | static Origin Resolve(const GURL& url, const Origin& base_origin); |
| 171 | |
John Mellor | ef62bce | 2017-10-04 11:25:33 | [diff] [blame] | 172 | // Copyable and movable. |
Daniel Cheng | 6ae11ad7 | 2017-10-17 20:42:33 | [diff] [blame] | 173 | Origin(const Origin&); |
| 174 | Origin& operator=(const Origin&); |
| 175 | Origin(Origin&&); |
| 176 | Origin& operator=(Origin&&); |
John Mellor | ef62bce | 2017-10-04 11:25:33 | [diff] [blame] | 177 | |
Takashi Toyoshima | 5641d755 | 2018-02-09 08:57:52 | [diff] [blame] | 178 | // Creates an Origin from a |scheme|, |host|, and |port|. All the parameters |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 179 | // must be valid and canonicalized. Returns nullopt if any parameter is not |
| 180 | // canonical, or if all the parameters are empty. |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 181 | // |
| 182 | // This constructor should be used in order to pass 'Origin' objects back and |
| 183 | // forth over IPC (as transitioning through GURL would risk potentially |
| 184 | // dangerous recanonicalization); other potential callers should prefer the |
| 185 | // 'GURL'-based constructor. |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 186 | static base::Optional<Origin> UnsafelyCreateTupleOriginWithoutNormalization( |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 187 | base::StringPiece scheme, |
| 188 | base::StringPiece host, |
Takashi Toyoshima | 5641d755 | 2018-02-09 08:57:52 | [diff] [blame] | 189 | uint16_t port); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 190 | |
csharrison | edf893f | 2016-10-12 01:42:56 | [diff] [blame] | 191 | // Creates an origin without sanity checking that the host is canonicalized. |
| 192 | // This should only be used when converting between already normalized types, |
csharrison | f07ac3c | 2016-12-13 04:15:02 | [diff] [blame] | 193 | // and should NOT be used for IPC. Method takes std::strings for use with move |
| 194 | // operators to avoid copies. |
Takashi Toyoshima | 5641d755 | 2018-02-09 08:57:52 | [diff] [blame] | 195 | static Origin CreateFromNormalizedTuple(std::string scheme, |
| 196 | std::string host, |
| 197 | uint16_t port); |
jww | 908428c | 2016-10-26 21:51:46 | [diff] [blame] | 198 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 199 | ~Origin(); |
| 200 | |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 201 | // For opaque origins, these return ("", "", 0). |
| 202 | const std::string& scheme() const { |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 203 | return !opaque() ? tuple_.scheme() : base::EmptyString(); |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 204 | } |
| 205 | const std::string& host() const { |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 206 | return !opaque() ? tuple_.host() : base::EmptyString(); |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 207 | } |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 208 | uint16_t port() const { return !opaque() ? tuple_.port() : 0; } |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 209 | |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 210 | bool opaque() const { return nonce_.has_value(); } |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 211 | |
| 212 | // An ASCII serialization of the Origin as per Section 6.2 of RFC 6454, with |
| 213 | // the addition that all Origins with a 'file' scheme serialize to "file://". |
| 214 | std::string Serialize() const; |
| 215 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 216 | // Two non-opaque Origins are "same-origin" if their schemes, hosts, and ports |
| 217 | // are exact matches. Two opaque origins are same-origin only if their |
| 218 | // internal nonce values match. A non-opaque origin is never same-origin with |
| 219 | // an opaque origin. |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 220 | bool IsSameOriginWith(const Origin& other) const; |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 221 | bool operator==(const Origin& other) const { return IsSameOriginWith(other); } |
| 222 | bool operator!=(const Origin& other) const { |
| 223 | return !IsSameOriginWith(other); |
| 224 | } |
| 225 | |
Nasko Oskov | 97e305f | 2019-01-05 03:52:10 | [diff] [blame] | 226 | // This method returns true for any |url| which if navigated to could result |
| 227 | // in an origin compatible with |this|. |
| 228 | bool CanBeDerivedFrom(const GURL& url) const; |
| 229 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 230 | // Get the scheme, host, and port from which this origin derives. For |
| 231 | // a tuple Origin, this gives the same values as calling scheme(), host() |
| 232 | // and port(). For an opaque Origin that was created by calling |
| 233 | // Origin::DeriveNewOpaqueOrigin() on a precursor or Origin::Resolve(), |
| 234 | // this returns the tuple inherited from the precursor. |
| 235 | // |
| 236 | // If this Origin is opaque and was created via the default constructor or |
| 237 | // Origin::Create(), the precursor origin is unknown. |
| 238 | // |
| 239 | // Use with great caution: opaque origins should generally not inherit |
| 240 | // privileges from the origins they derive from. However, in some cases |
| 241 | // (such as restrictions on process placement, or determining the http lock |
| 242 | // icon) this information may be relevant to ensure that entering an |
| 243 | // opaque origin does not grant privileges initially denied to the original |
| 244 | // non-opaque origin. |
| 245 | // |
| 246 | // This method has a deliberately obnoxious name to prompt caution in its use. |
| 247 | const SchemeHostPort& GetTupleOrPrecursorTupleIfOpaque() const { |
| 248 | return tuple_; |
mek | 0126c13 | 2016-02-17 23:50:59 | [diff] [blame] | 249 | } |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 250 | |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 251 | // Efficiently returns what GURL(Serialize()) would without re-parsing the |
| 252 | // URL. This can be used for the (rare) times a GURL representation is needed |
| 253 | // for an Origin. |
| 254 | // Note: The returned URL will not necessarily be serialized to the same value |
| 255 | // as the Origin would. The GURL will have an added "/" path for Origins with |
| 256 | // valid SchemeHostPorts and file Origins. |
Charles Harrison | c5f8c91b | 2017-08-22 18:08:24 | [diff] [blame] | 257 | // |
| 258 | // Try not to use this method under normal circumstances, as it loses type |
| 259 | // information. Downstream consumers can mistake the returned GURL with a full |
| 260 | // URL (e.g. with a path component). |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 261 | GURL GetURL() const; |
| 262 | |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 263 | // Same as GURL::DomainIs. If |this| origin is opaque, then returns false. |
Charles Harrison | 81dc2fb | 2017-08-30 23:41:12 | [diff] [blame] | 264 | bool DomainIs(base::StringPiece canonical_domain) const; |
pkalinnikov | 054f403 | 2016-08-31 10:54:17 | [diff] [blame] | 265 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 266 | // Allows Origin to be used as a key in STL (for example, a std::set or |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 267 | // std::map). |
| 268 | bool operator<(const Origin& other) const; |
| 269 | |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 270 | // Creates a new opaque origin that is guaranteed to be cross-origin to all |
| 271 | // currently existing origins. An origin created by this method retains its |
| 272 | // identity across copies. Copies are guaranteed to be same-origin to each |
| 273 | // other, e.g. |
| 274 | // |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 275 | // url::Origin page = Origin::Create(GURL("http://example.com")) |
| 276 | // url::Origin a = page.DeriveNewOpaqueOrigin(); |
| 277 | // url::Origin b = page.DeriveNewOpaqueOrigin(); |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 278 | // url::Origin c = a; |
| 279 | // url::Origin d = b; |
| 280 | // |
| 281 | // |a| and |c| are same-origin, since |c| was copied from |a|. |b| and |d| are |
| 282 | // same-origin as well, since |d| was copied from |b|. All other combinations |
| 283 | // of origins are considered cross-origin, e.g. |a| is cross-origin to |b| and |
| 284 | // |d|, |b| is cross-origin to |a| and |c|, |c| is cross-origin to |b| and |
| 285 | // |d|, and |d| is cross-origin to |a| and |c|. |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 286 | Origin DeriveNewOpaqueOrigin() const; |
| 287 | |
Nasko Oskov | 1ed4e8f | 2019-02-13 01:39:19 | [diff] [blame] | 288 | // Creates a string representation of the object that can be used for logging |
| 289 | // and debugging. It serializes the internal state, such as the nonce value |
| 290 | // and precursor information. |
| 291 | std::string GetDebugString() const; |
| 292 | |
Rouslan Solomakhin | 63cdb148 | 2019-09-30 21:53:14 | [diff] [blame] | 293 | #if defined(OS_ANDROID) |
| 294 | base::android::ScopedJavaLocalRef<jobject> CreateJavaObject() const; |
| 295 | static Origin FromJavaObject( |
| 296 | const base::android::JavaRef<jobject>& java_origin); |
| 297 | #endif // OS_ANDROID |
| 298 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 299 | private: |
Nasko Oskov | 99445acd | 2018-10-10 16:46:00 | [diff] [blame] | 300 | friend class blink::SecurityOrigin; |
Robert Ogden | dd74d73 | 2020-03-12 17:33:19 | [diff] [blame^] | 301 | friend class net::NetworkIsolationKey; |
| 302 | friend class net::OpaqueNonTransientNetworkIsolationKeyTest; |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 303 | friend class OriginTest; |
Nasko Oskov | 99445acd | 2018-10-10 16:46:00 | [diff] [blame] | 304 | friend struct mojo::UrlOriginAdapter; |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 305 | friend struct ipc_fuzzer::FuzzTraits<Origin>; |
Nasko Oskov | d9e41d5 | 2018-09-27 23:12:42 | [diff] [blame] | 306 | friend struct mojo::StructTraits<url::mojom::OriginDataView, url::Origin>; |
Nasko Oskov | 99445acd | 2018-10-10 16:46:00 | [diff] [blame] | 307 | friend IPC::ParamTraits<url::Origin>; |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 308 | friend COMPONENT_EXPORT(URL) std::ostream& operator<<(std::ostream& out, |
| 309 | const Origin& origin); |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 310 | |
| 311 | // Origin::Nonce is a wrapper around base::UnguessableToken that generates |
| 312 | // the random value only when the value is first accessed. The lazy generation |
| 313 | // allows Origin to be default-constructed quickly, without spending time |
| 314 | // in random number generation. |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 315 | // |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 316 | // TODO(nick): Should this optimization move into UnguessableToken, once it no |
| 317 | // longer treats the Null case specially? |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 318 | class COMPONENT_EXPORT(URL) Nonce { |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 319 | public: |
| 320 | // Creates a nonce to hold a newly-generated UnguessableToken. The actual |
| 321 | // token value will be generated lazily. |
| 322 | Nonce(); |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 323 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 324 | // Creates a nonce to hold an already-generated UnguessableToken value. This |
| 325 | // constructor should only be used for IPC serialization and testing -- |
| 326 | // regular code should never need to touch the UnguessableTokens directly, |
| 327 | // and the default constructor is faster. |
| 328 | explicit Nonce(const base::UnguessableToken& token); |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 329 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 330 | // Accessor, which lazily initializes the underlying |token_| member. |
| 331 | const base::UnguessableToken& token() const; |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 332 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 333 | // Do not use in cases where lazy initialization is expected! This |
| 334 | // accessor does not initialize the |token_| member. |
| 335 | const base::UnguessableToken& raw_token() const; |
| 336 | |
| 337 | // Copyable and movable. Copying a Nonce triggers lazy-initialization, |
| 338 | // moving it does not. |
| 339 | Nonce(const Nonce&); |
| 340 | Nonce& operator=(const Nonce&); |
| 341 | Nonce(Nonce&&); |
| 342 | Nonce& operator=(Nonce&&); |
| 343 | |
| 344 | // Note that operator<, used by maps type containers, will trigger |token_| |
| 345 | // lazy-initialization. Equality comparisons do not. |
| 346 | bool operator<(const Nonce& other) const; |
| 347 | bool operator==(const Nonce& other) const; |
| 348 | bool operator!=(const Nonce& other) const; |
| 349 | |
| 350 | private: |
| 351 | friend class OriginTest; |
| 352 | |
| 353 | // mutable to support lazy generation. |
| 354 | mutable base::UnguessableToken token_; |
| 355 | }; |
| 356 | |
| 357 | // This needs to be friended within Origin as well, since Nonce is a private |
| 358 | // nested class of Origin. |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 359 | friend COMPONENT_EXPORT(URL) std::ostream& operator<<(std::ostream& out, |
| 360 | const Nonce& nonce); |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 361 | |
| 362 | // Creates an origin without sanity checking that the host is canonicalized. |
| 363 | // This should only be used when converting between already normalized types, |
| 364 | // and should NOT be used for IPC. Method takes std::strings for use with move |
| 365 | // operators to avoid copies. |
| 366 | static Origin CreateOpaqueFromNormalizedPrecursorTuple( |
| 367 | std::string precursor_scheme, |
| 368 | std::string precursor_host, |
| 369 | uint16_t precursor_port, |
| 370 | const Nonce& nonce); |
| 371 | |
| 372 | // Creates an opaque Origin with the identity given by |nonce|, and an |
| 373 | // optional precursor origin given by |precursor_scheme|, |precursor_host| and |
| 374 | // |precursor_port|. Returns nullopt if any parameter is not canonical. When |
| 375 | // the precursor is unknown, the precursor parameters should be ("", "", 0). |
| 376 | // |
| 377 | // This factory method should be used in order to pass opaque Origin objects |
| 378 | // back and forth over IPC (as transitioning through GURL would risk |
| 379 | // potentially dangerous recanonicalization). |
| 380 | static base::Optional<Origin> UnsafelyCreateOpaqueOriginWithoutNormalization( |
| 381 | base::StringPiece precursor_scheme, |
| 382 | base::StringPiece precursor_host, |
| 383 | uint16_t precursor_port, |
| 384 | const Nonce& nonce); |
| 385 | |
| 386 | // Constructs a non-opaque tuple origin. |tuple| must be valid. |
Takashi Toyoshima | 5641d755 | 2018-02-09 08:57:52 | [diff] [blame] | 387 | explicit Origin(SchemeHostPort tuple); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 388 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 389 | // Constructs an opaque origin derived from the |precursor| tuple, with the |
| 390 | // given |nonce|. |
| 391 | Origin(const Nonce& nonce, SchemeHostPort precursor); |
| 392 | |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 393 | // Get the nonce associated with this origin, if it is opaque. This should be |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 394 | // used only when trying to send an Origin across an IPC pipe. |
| 395 | base::Optional<base::UnguessableToken> GetNonceForSerialization() const; |
| 396 | |
Robert Ogden | dd74d73 | 2020-03-12 17:33:19 | [diff] [blame^] | 397 | // Serializes this Origin, including its nonce if it is opaque. If an opaque |
| 398 | // origin's |tuple_| is invalid or the nonce isn't initialized, nullopt is |
| 399 | // returned. Use of this method should be limited as an opaque origin will |
| 400 | // never be matchable in future browser sessions. |
| 401 | base::Optional<std::string> SerializeWithNonce() const; |
| 402 | |
| 403 | // Deserializes an origin from |ToValueWithNonce|. Returns nullopt if the |
| 404 | // value was invalid in any way. |
| 405 | static base::Optional<Origin> Deserialize(const std::string& value); |
| 406 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 407 | // The tuple is used for both tuple origins (e.g. https://example.com:80), as |
| 408 | // well as for opaque origins, where it tracks the tuple origin from which |
| 409 | // the opaque origin was initially derived (we call this the "precursor" |
| 410 | // origin). |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 411 | SchemeHostPort tuple_; |
Daniel Cheng | 62ff018 | 2018-08-25 07:59:33 | [diff] [blame] | 412 | |
| 413 | // The nonce is used for maintaining identity of an opaque origin. This |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 414 | // nonce is preserved when an opaque origin is copied or moved. An Origin |
| 415 | // is considered opaque if and only if |nonce_| holds a value. |
| 416 | base::Optional<Nonce> nonce_; |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 417 | }; |
| 418 | |
Nasko Oskov | 9277dfc | 2018-09-17 23:20:54 | [diff] [blame] | 419 | // Pretty-printers for logging. These expose the internal state of the nonce. |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 420 | COMPONENT_EXPORT(URL) |
| 421 | std::ostream& operator<<(std::ostream& out, const Origin& origin); |
| 422 | COMPONENT_EXPORT(URL) |
| 423 | std::ostream& operator<<(std::ostream& out, const Origin::Nonce& origin); |
palmer | 5c437bcc | 2016-02-03 23:21:36 | [diff] [blame] | 424 | |
Staphany Park | 6fd74a2 | 2018-12-04 21:15:41 | [diff] [blame] | 425 | COMPONENT_EXPORT(URL) bool IsSameOriginWith(const GURL& a, const GURL& b); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 426 | |
Lukasz Anforowicz | 3d58b939 | 2018-01-05 20:56:22 | [diff] [blame] | 427 | // DEBUG_ALIAS_FOR_ORIGIN(var_name, origin) copies |origin| into a new |
| 428 | // stack-allocated variable named |<var_name>|. This helps ensure that the |
| 429 | // value of |origin| gets preserved in crash dumps. |
| 430 | #define DEBUG_ALIAS_FOR_ORIGIN(var_name, origin) \ |
Daniel Cheng | afbf635 | 2018-04-24 23:59:25 | [diff] [blame] | 431 | DEBUG_ALIAS_FOR_CSTR(var_name, (origin).Serialize().c_str(), 128) |
Lukasz Anforowicz | 3d58b939 | 2018-01-05 20:56:22 | [diff] [blame] | 432 | |
Lukasz Anforowicz | 731c39c | 2020-02-07 23:35:58 | [diff] [blame] | 433 | namespace debug { |
| 434 | |
| 435 | class COMPONENT_EXPORT(URL) ScopedOriginCrashKey |
| 436 | : public base::debug::ScopedCrashKeyString { |
| 437 | public: |
| 438 | ScopedOriginCrashKey(base::debug::CrashKeyString* crash_key, |
| 439 | const url::Origin* value); |
| 440 | ~ScopedOriginCrashKey(); |
| 441 | |
| 442 | ScopedOriginCrashKey(const ScopedOriginCrashKey&) = delete; |
| 443 | ScopedOriginCrashKey& operator=(const ScopedOriginCrashKey&) = delete; |
| 444 | }; |
| 445 | |
| 446 | } // namespace debug |
| 447 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 448 | } // namespace url |
| 449 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 450 | #endif // URL_ORIGIN_H_ |