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

Side by Side Diff: url/origin.h

Issue 1153763002: Hardening the 'url::Origin' implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More. Created 5 years, 6 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 | « net/websockets/websocket_stream.cc ('k') | url/origin.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 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 "base/basictypes.h"
10 #include "url/url_export.h" 11 #include "url/url_export.h"
11 12
13 class GURL;
14
12 namespace url { 15 namespace url {
13 16
14 // Origin represents a Web Origin serialized to a string. 17 // An Origin is a tuple of (scheme, host, port), as described in RFC 6454.
15 // See RFC6454 for details. 18 //
19 // Origins are the fundamental component of the web's security model, and
20 // represent the boundries within which user agents generally compartmentalize
21 // information, and between which user agents enforce access controls. That is,
22 // "same-origin" resources are generally more able to directly share information
23 // than "cross-origin" resources.
24 //
25 // This class represents a standard interface which ought to be used when code
26 // needs to determine if two resources are "same-origin", and when a canonical
27 // serialization of an origin is required.
28 //
29 // Some origins are "unique", meaning that they are not same-origin with any
30 // other origin (including themselves). These are represented as ('', '', 0).
31 //
32 // There are a few subtleties to note:
33 //
34 // * Invalid and non-standard GURLs are parsed as unique origins. This includes
35 // non-hierarchical URLs like 'data:text/html,...' and 'javascript:alert(1)'.
36 //
37 // * GURLs with a scheme of 'file' are parsed as ('file', '', 0), and are
38 // same-origin with any other 'file'-based URL.
39 //
40 // TODO(mkwst): When this changes in Blinke should change it here
41 // as well. See https://www.chromestatus.com/features/5755326842273792.
42 //
43 // * Unique origins all serialize to the string "null"; this means that the
44 // serialization of two unique origins will be '==' to each other, though the
45 // origins themselves are not "same-origin". This means that origins'
46 // serializations should not be relied upon for security checks.
47 //
48 // * GURLs with a scheme of 'filesystem' parse the origin out of the "internal
49 // URL". That is 'filesystem:https://example.com/temporary/file.png' is
50 // parsed as ('https', 'example.com', 443).
51 //
52 // * The host component of an IPv6 address includes brackets, just like the
53 // URL representation.
54 //
55 // Usage:
56 //
57 // * To answer the question "Are |this| and |that| "same-origin" with each
58 // other?", use |Origin::IsSameOriginWith|:
59 //
60 // if (this.IsSameOriginWith(that)) {
61 // // Do something amazing here.
62 // }
63 //
64 // * To get the serialization of |origin|, use the |serialize| method:
65 //
66 // std::string serialization = origin.serialize();
67 //
68 // Note that unique origins all serialize to "null". This means that the
69 // serialization of two origins may '==' each other, even through the origins
70 // themselves are not "same-origin". This might be a useful property to use in
71 // some cases; origin-based caches might wish to fold unique origins into an
72 // "everything else" bucket, rather than creating new buckets for each unique
73 // origin. Be aware of the difference, and choose wisely.
16 class URL_EXPORT Origin { 74 class URL_EXPORT Origin {
17 public: 75 public:
76 // Creates a unique Origin.
18 Origin(); 77 Origin();
78
79 // Creates an origin from a scheme/host/port tuple. If the scheme, host, or
80 // port are invalid, a unique origin will be created.
81 //
82 // The constructor expects the host's A-label to be provided here. That is,
83 // 'http://☃.net/' should be provided as ('http', 'xn--n3h.net', 80).
84 Origin(const std::string& scheme, const std::string& host, uint16 port);
85
86 // The mechanism for extracting an origin from a URL is defined in the URL
87 // specification (https://url.spec.whatwg.org/#origin). In particular, note
88 // that:
89 //
90 // * invalid and non-standard URLs are parsed as unique origins
91 // * URLs whose scheme is 'file' are parsed as ('file', '', 0)
92 // * 'filesystem' URLs parse the origin out of the "internal URL"
93 //
94 // TODO(mkwst): 'blob' URLs are incorrectly handled. Perhaps this is an
95 // argument for moving this concept elsewhere, as //url doesn't know anything
96 // about 'blob' URLs at the moment (other than the bare fact that 'blob' is a
97 // scheme that exists).
98 explicit Origin(const GURL& url);
99
100 // This is a shortcut constructor that has the same effect as
101 // 'Origin(GURL([string]))'.
102 //
103 // TODO(mkwst): Remove this once 'blink::WebSerializedOrigin' is gone.
104 // https://crbug.com/490074
19 explicit Origin(const std::string& origin); 105 explicit Origin(const std::string& origin);
20 106
21 const std::string& string() const { return string_; } 107 // Returns true if the origin is "the same" as |other|, as defined in Section
108 // 5 of RFC6454: https://tools.ietf.org/html/rfc6454#section-5. Note that
109 // unique origins are not "the same" as any other origin, including
110 // themselves.
111 bool IsSameOriginWith(const Origin& other) const;
22 112
23 bool IsSameAs(const Origin& that) const { 113 std::string scheme() const { return scheme_; }
24 return string_ == that.string_; 114 std::string host() const { return host_; }
25 } 115 uint16 port() const { return port_; }
116 bool is_valid() const { return valid_; }
117
118 // Returns a serialization of the origin as defined in Section 6 of RFC 6454
119 // (https://tools.ietf.org/html/rfc6454#section-6).
120 //
121 // This is _not_ the serialization of the origin which ought to be displayed
122 // to a user: the guidelines for such display are covered in detail at
123 // https://www.chromium.org/Home/chromium-security/enamel#TOC-Presenting-Origi ns-To-Users.
124 std::string serialize() const;
125
126 // TODO(mkwst): Remove this once blink::WebSerializedOrigin is gone.
127 // https://crbug.com/490074
128 std::string string() const { return serialize(); }
26 129
27 private: 130 private:
131 void Init(const GURL& url);
132
133 std::string scheme_;
134 std::string host_;
135 uint16 port_;
136 bool unique_;
137 bool serialization_requires_port_;
138 bool valid_;
139
28 std::string string_; 140 std::string string_;
29 }; 141 };
30 142
143 // Stream operator so Origin can be used in assertion statements.
144 URL_EXPORT std::ostream& operator<<(std::ostream& out, const url::Origin& url);
145
31 } // namespace url 146 } // namespace url
32 147
33 #endif // URL_ORIGIN_H_ 148 #endif // URL_ORIGIN_H_
OLDNEW
« no previous file with comments | « net/websockets/websocket_stream.cc ('k') | url/origin.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698