Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef URL_ORIGIN_H_ | 5 #ifndef URL_ORIGIN_H_ |
| 6 #define URL_ORIGIN_H_ | 6 #define URL_ORIGIN_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "url/gurl.h" | |
|
Ryan Sleevi
2015/05/28 07:46:29
not needed - forward declare?
Mike West
2015/05/28 13:49:09
Done.
| |
| 10 #include "url/url_export.h" | 11 #include "url/url_export.h" |
| 11 | 12 |
| 12 namespace url { | 13 namespace url { |
| 13 | 14 |
| 14 // Origin represents a Web Origin serialized to a string. | 15 // An Origin is a tuple of (scheme, host, port), as described in RFC 6454. |
| 15 // See RFC6454 for details. | 16 // |
| 17 // Origins are the fundamental component of the web's security model, and | |
| 18 // represent the boundries within which user agents generally compartmentalize | |
| 19 // information, and between which user agents enforce access controls. That is, | |
| 20 // "same-origin" resources are generally more able to directly share information | |
| 21 // than "cross-origin" resources. | |
| 22 // | |
| 23 // This class represents a standard interface which ought to be used when code | |
| 24 // needs to determine if two resources are "same-origin", and when a canonical | |
| 25 // serialization of an origin is required. | |
| 26 // | |
| 27 // Some origins are "unique", meaning that they are not same-origin with any | |
| 28 // other origin (including themselves). These are represented as ('', '', 0). | |
| 29 // | |
| 30 // As you'd expect, there are a few subtleties to note: | |
|
Ryan Sleevi
2015/05/28 07:46:30
STYLE: Not strictly forbidden by http://google-sty
Mike West
2015/05/28 13:49:09
Ok. So comments should be as boring as possible. C
| |
| 31 // | |
| 32 // * Invalid and non-standard GURLs are parsed as unique origins. This includes | |
| 33 // non-hierarchical URLs like 'data:text/html,...' and 'javascript:alert(1)'. | |
| 34 // | |
| 35 // * GURLs with a scheme of 'file' are parsed as ('file', '', 0), and are | |
| 36 // same-origin with any other 'file'-based URL. | |
| 37 // | |
| 38 // TODO(mkwst): If we end up changing this in Blink, we should change it here | |
| 39 // as well. See https://www.chromestatus.com/features/5755326842273792. | |
|
Ryan Sleevi
2015/05/28 07:46:30
pronouns blah blah
TODO(mkwst): If this changes i
Mike West
2015/05/28 13:49:09
Actually, this is more complicated than I thought.
| |
| 40 // | |
| 41 // * Unique origins all serialize to the string "null"; this means that the | |
| 42 // serialization of two unique origins will be '==' to each other, though the | |
| 43 // origins themselves are not "same-origin". This means that origins' | |
| 44 // serializations should not be relied upon for security checks. | |
| 45 // | |
| 46 // * GURLs with a scheme of 'filesystem' parse the origin out of the "internal | |
| 47 // URL". That is 'filesystem:https%3F//example.com/temporary/file.png' is | |
|
Ryan Sleevi
2015/05/28 07:46:30
Is the 3F strictly necessary?
Mike West
2015/05/28 13:49:09
Probably not.
| |
| 48 // parsed as ('https', 'example.com', 443). | |
| 49 // | |
| 50 // * The host component of an IPv6 address includes brackets. | |
|
Ryan Sleevi
2015/05/28 07:46:29
s/brackets./brackets, the same as in the URL repre
Mike West
2015/05/28 13:49:09
Done.
| |
| 51 // | |
| 52 // Usage: | |
| 53 // | |
| 54 // * To answer the question "Are |this| and |that| "same-origin" with each | |
| 55 // other?", use |Origin::IsSameOriginWith|: | |
| 56 // | |
| 57 // if (this.IsSameOriginWith(that)) { | |
| 58 // // Do something amazing here. | |
| 59 // } | |
| 60 // | |
| 61 // * To get the serialization of |origin|, use the |serialize| method: | |
| 62 // | |
| 63 // std::string serialization = origin.serialize(); | |
| 64 // | |
| 65 // Note that unique origins all serialize to "null"; this means that the | |
| 66 // serialization of two origins may '==' each other, even through the origins | |
| 67 // themselves are not "same-origin". This might be a useful property to use in | |
| 68 // some cases: 'net::AuthHandler', for instance, builds an authorization cache | |
|
Ryan Sleevi
2015/05/28 07:46:30
LAYERING: Talking about how it's used is a layerin
Mike West
2015/05/28 13:49:09
Done (though I think I want to contest the notion
| |
| 69 // HashMap based on origin serializations, and folds unique origins into the | |
| 70 // "everything else" bucket. Be aware of the difference, however, and choose | |
| 71 // wisely. | |
| 16 class URL_EXPORT Origin { | 72 class URL_EXPORT Origin { |
| 17 public: | 73 public: |
| 74 // Creates a unique Origin. | |
| 18 Origin(); | 75 Origin(); |
| 76 | |
| 77 // Creates an origin from a scheme/host/port tuple. If the scheme, host, or | |
| 78 // port are invalid, a unique origin will be created. | |
| 79 Origin(const std::string& scheme, const std::string& host, uint16 port); | |
|
Ryan Sleevi
2015/05/28 07:46:30
something something what's the form of host
(U-La
Mike West
2015/05/28 13:49:09
A-label.
| |
| 80 | |
| 81 // The mechanism for extracting an origin from a URL is defined in the URL | |
| 82 // specification (https://url.spec.whatwg.org/#origin). In particular, note | |
| 83 // that: | |
| 84 // | |
| 85 // * invalid and non-standard URLs are parsed as unique origins | |
| 86 // * URLs whose scheme is 'file' are parsed as ('file', '', 0) | |
| 87 // * 'filesystem' URLs parse the origin out of the "internal URL" | |
| 88 // | |
| 89 // TODO(mkwst): 'blob' URLs are incorrectly handled. Perhaps this is an | |
| 90 // argument for moving this concept elsewhere, as //url doesn't know anything | |
| 91 // about 'blob' URLs at the moment. | |
|
Ryan Sleevi
2015/05/28 07:46:30
s/moment./moment, other than that they exist./
Mike West
2015/05/28 13:49:09
Done.
| |
| 92 explicit Origin(const GURL& url); | |
| 93 | |
| 94 // This is a shortcut constructor that has the same effect as | |
| 95 // 'Origin(GURL([string]))'. | |
|
Ryan Sleevi
2015/05/28 07:46:30
Is this true? I seem to recall abarth@'s argument
Mike West
2015/05/28 13:49:09
Since it's implemented here as 'Init(GURL([string]
| |
| 96 // | |
| 97 // TODO(mkwst): Remove this once 'blink::WebSerializedOrigin' is gone. | |
| 19 explicit Origin(const std::string& origin); | 98 explicit Origin(const std::string& origin); |
| 20 | 99 |
| 21 const std::string& string() const { return string_; } | 100 // Returns true if the origin is "the same" as |other|, as defined in Section |
| 101 // 5 of RFC6454: https://tools.ietf.org/html/rfc6454#section-5. Note that | |
| 102 // unique origins are not "the same" as any other origin, including | |
| 103 // themselves. | |
| 104 bool IsSameOriginWith(const Origin& other) const; | |
| 22 | 105 |
| 23 bool IsSameAs(const Origin& that) const { | 106 bool SchemeIs(const char* scheme) const; |
| 24 return string_ == that.string_; | 107 bool SchemeIsCryptographic() const; |
|
Ryan Sleevi
2015/05/28 07:46:30
If these aren't demonstrably needed yet, it'd be g
Mike West
2015/05/28 13:49:09
I can add them when I start replacing `GetOrigin()
| |
| 25 } | 108 |
| 109 std::string scheme() const { return scheme_; } | |
| 110 std::string host() const { return host_; } | |
| 111 uint16 port() const { return port_; } | |
| 112 bool is_valid() const { return valid_; } | |
| 113 | |
| 114 // Returns a serialization of the origin as defined in Section 6 of RFC 6454 | |
| 115 // (https://tools.ietf.org/html/rfc6454#section-6). | |
| 116 // | |
| 117 // This is _not_ the serialization of the origin which ought to be displayed | |
| 118 // to a user: the guidelines for such display are covered in detail at | |
| 119 // https://www.chromium.org/Home/chromium-security/enamel#TOC-Presenting-Origi ns-To-Users. | |
|
Ryan Sleevi
2015/05/28 07:46:29
My gut is that this is a layering issue. I see a f
Mike West
2015/05/28 13:49:09
Then let's wait to see if people want to fight abo
| |
| 120 std::string serialize() const; | |
| 121 | |
| 122 // TODO(mkwst): Remove once blink::WebSerializedOrigin is gone. | |
|
Ryan Sleevi
2015/05/28 07:46:30
something something bug #?
Mike West
2015/05/28 13:49:09
Done.
| |
| 123 std::string string() const { return serialize(); } | |
| 26 | 124 |
| 27 private: | 125 private: |
| 126 void Init(const GURL& url); | |
| 127 | |
| 128 std::string scheme_; | |
| 129 std::string host_; | |
| 130 uint16 port_; | |
| 131 bool unique_; | |
| 132 bool serialization_requires_port_; | |
| 133 bool valid_; | |
| 134 | |
| 28 std::string string_; | 135 std::string string_; |
| 29 }; | 136 }; |
| 30 | 137 |
| 138 // Stream operator so Origin can be used in assertion statements. | |
| 139 URL_EXPORT std::ostream& operator<<(std::ostream& out, const url::Origin& url); | |
| 140 | |
| 31 } // namespace url | 141 } // namespace url |
| 32 | 142 |
| 33 #endif // URL_ORIGIN_H_ | 143 #endif // URL_ORIGIN_H_ |
| OLD | NEW |