OLD | NEW |
---|---|
(Empty) | |
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 | |
8 #include <string> | |
9 | |
10 #include "base/strings/string16.h" | |
11 #include "url/scheme_host_port.h" | |
12 #include "url/third_party/mozilla/url_parse.h" | |
13 #include "url/url_canon.h" | |
14 #include "url/url_constants.h" | |
15 #include "url/url_export.h" | |
16 | |
17 class GURL; | |
18 | |
19 namespace url { | |
20 | |
21 // An Origin is a tuple of (scheme, host, port), as described in RFC 6454. | |
Ryan Sleevi
2015/07/10 11:59:54
I would hope that immediately after stating this,
Mike West
2015/07/17 09:58:09
Poked at the text a bit.
| |
22 // | |
23 // Origins are the fundamental component of the web's security model, and | |
24 // represent the boundries within which user agents generally compartmentalize | |
25 // information, and between which user agents enforce access controls. | |
26 // | |
27 // This class ought to be used when code needs to determine if two resources | |
28 // are "same-origin", and when a canonical serialization of an origin is | |
29 // required. | |
30 // | |
31 // Some origins are "unique", meaning that they are not same-origin with any | |
32 // other origin (including themselves). | |
33 // | |
34 // There are a few subtleties to note: | |
35 // | |
36 // * Invalid and non-standard GURLs are parsed as unique origins. This includes | |
37 // non-hierarchical URLs like 'data:text/html,...' and 'javascript:alert(1)'. | |
38 // | |
39 // * GURLs with a 'file' scheme are tricky. They are parsed as ('file', '', 0), | |
40 // and are difficult to reason about in the abstract. | |
Ryan Sleevi
2015/07/10 11:59:54
I feel like something more may be warranted to her
| |
41 // | |
42 // * Unique origins all serialize to the string "null"; this means that the | |
43 // serializations of two unique origins are '==' to each other, though the | |
Ryan Sleevi
2015/07/10 11:59:54
s/are '==' to each other/are identical/
Mike West
2015/07/17 09:58:09
Done.
| |
44 // origins themselves are not "the same". This means that origins' | |
45 // serializations must not be relied upon for security checks. | |
46 // | |
47 // * GURLs with schemes of 'filesystem' or 'blob' parse the origin out of the | |
48 // internals of the URL. That is, 'filesystem:https://example.com/temporary/f' | |
49 // is parsed as ('https', 'example.com', 443). | |
50 // | |
51 // * The host component of an IPv6 address includes brackets, just like the URL | |
52 // representation. | |
Ryan Sleevi
2015/07/10 11:59:53
Why is this? I found it a bit surprising?
Mike West
2015/07/17 09:58:09
Why wouldn't we do this? It matches the representa
| |
53 // | |
54 // Usage: | |
55 // | |
56 // * Origins are generally constructed from GURL objects: | |
Ryan Sleevi
2015/07/10 11:59:53
When I first read this comment, I had to scroll do
Mike West
2015/07/17 09:58:09
Ran with something like that.
| |
57 // | |
58 // GURL url("https://example.com/"); | |
Ryan Sleevi
2015/07/10 11:59:53
wrong indent level (2 != 4)
Mike West
2015/07/17 09:58:09
[Insert eye rolling here.]
| |
59 // url::Origin origin(url); | |
60 // origin.scheme(); // "https" | |
61 // origin.host(); // "example.com" | |
62 // origin.port(); // 443 | |
63 // origin.IsUnique(); // false | |
64 // | |
65 // * To answer the question "Are |this| and |that| "same-origin" with each | |
66 // other?", use |Origin::IsSameOriginWith|: | |
67 // | |
68 // if (this.IsSameOriginWith(that)) { | |
Ryan Sleevi
2015/07/10 11:59:53
wrong indent level (2 != 4)
Mike West
2015/07/17 09:58:09
[And here]
| |
69 // // Amazingness goes here. | |
Ryan Sleevi
2015/07/10 11:59:54
wrong indent level (2 != 4)
Mike West
2015/07/17 09:58:09
[Also here]
| |
70 // } | |
71 class URL_EXPORT Origin { | |
72 public: | |
73 // Creates a unique Origin. | |
74 Origin(); | |
75 | |
76 // Creates an Origin from |url|, as described at | |
77 // https://url.spec.whatwg.org/#origin, with the following additions: | |
78 // | |
79 // 1. If |url| is invalid or non-standard, a unique Origin is constructed. | |
80 // 2. 'filesystem' URLs behave as 'blob' URLs (that is, the origin is parsed | |
81 // out of everything in the URL which follows the scheme). | |
82 // 3. 'file' URLs all parse as ("file", "", 0). | |
83 Origin(const GURL& url); | |
84 | |
85 ~Origin(); | |
86 | |
87 std::string scheme() const { return tuple_.scheme(); } | |
88 std::string host() const { return tuple_.host(); } | |
89 uint16 port() const { return tuple_.port(); } | |
90 | |
91 // Origins may be forcibly treated as unique; this operation is irreversable. | |
92 void ForceUnique() { unique_ = true; } | |
Ryan Sleevi
2015/07/10 11:59:53
I find this quite surprising. Why allow mutable st
Mike West
2015/07/17 09:58:09
I think we'll need this for `file:` URLs, as we ei
| |
93 bool unique() const { return unique_; } | |
94 | |
95 // An ASCII serialization of the Origin as per Section 6.2 of RFC 6454, with | |
96 // the addition that Origins with a 'file' scheme serialize to "file://". | |
97 std::string Serialize() const; | |
98 | |
99 // Two Origins are "same-origin" iff their schemes, hosts, and ports are exact | |
100 // matches; and neither is unique. | |
101 bool IsSameOriginWith(const Origin& other) const; | |
102 | |
103 // Allows SchemeHostPort to used as a key in STL (for example, a std::set or | |
104 // std::map). | |
105 bool operator<(const Origin& other) const; | |
106 bool operator>(const Origin& other) const; | |
Ryan Sleevi
2015/07/10 11:59:54
operator> is not needed. All that's needed for any
Mike West
2015/07/17 09:58:09
Done.
| |
107 | |
108 private: | |
109 url::SchemeHostPort tuple_; | |
110 bool unique_; | |
Ryan Sleevi
2015/07/10 11:59:54
DISALLOW_COPY_AND_ASSIGN ?
| |
111 }; | |
112 | |
113 } // namespace url | |
114 | |
115 #endif // URL_SCHEME_HOST_PORT_H_ | |
OLD | NEW |