blob: 4af685acc31a38c9b8164e6f4c90eebdd5f2ac4d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/common/origin_util.h"
#include <string>
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/url_constants.h"
namespace content {
namespace {
class SchemeAndOriginWhitelist {
public:
SchemeAndOriginWhitelist() { Reset(); }
~SchemeAndOriginWhitelist() {}
void Reset() {
secure_schemes_.clear();
secure_origins_.clear();
service_worker_schemes_.clear();
GetContentClient()->AddSecureSchemesAndOrigins(&secure_schemes_,
&secure_origins_);
GetContentClient()->AddServiceWorkerSchemes(&service_worker_schemes_);
}
const std::set<std::string>& secure_schemes() const {
return secure_schemes_;
}
const std::set<GURL>& secure_origins() const { return secure_origins_; }
const std::set<std::string>& service_worker_schemes() const {
return service_worker_schemes_;
}
private:
std::set<std::string> secure_schemes_;
std::set<GURL> secure_origins_;
std::set<std::string> service_worker_schemes_;
DISALLOW_COPY_AND_ASSIGN(SchemeAndOriginWhitelist);
};
base::LazyInstance<SchemeAndOriginWhitelist>::Leaky g_trustworthy_whitelist =
LAZY_INSTANCE_INITIALIZER;
} // namespace
bool IsOriginSecure(const GURL& url) {
if (url.SchemeIsCryptographic() || url.SchemeIsFile())
return true;
if (url.SchemeIsFileSystem() && url.inner_url() &&
IsOriginSecure(*url.inner_url())) {
return true;
}
std::string hostname = url.HostNoBrackets();
if (net::IsLocalhost(hostname))
return true;
if (base::ContainsKey(g_trustworthy_whitelist.Get().secure_schemes(),
url.scheme()))
return true;
if (base::ContainsKey(g_trustworthy_whitelist.Get().secure_origins(),
url.GetOrigin())) {
return true;
}
return false;
}
bool OriginCanAccessServiceWorkers(const GURL& url) {
if (url.SchemeIsHTTPOrHTTPS() && IsOriginSecure(url))
return true;
if (base::ContainsKey(g_trustworthy_whitelist.Get().service_worker_schemes(),
url.scheme())) {
return true;
}
return false;
}
void ResetSchemesAndOriginsWhitelistForTesting() {
g_trustworthy_whitelist.Get().Reset();
}
bool HasSuborigin(const GURL& url) {
if (!url.is_valid())
return false;
if (url.scheme() != kHttpSuboriginScheme &&
url.scheme() != kHttpsSuboriginScheme) {
return false;
}
base::StringPiece host_piece = url.host_piece();
size_t first_period = host_piece.find('.');
// If the first period is the first position in the hostname, or there is no
// period at all, there is no suborigin serialized in the hostname.
if (first_period == 0 || first_period == base::StringPiece::npos)
return false;
// If there's nothing after the first dot, then there is no host for the
// physical origin, which is not a valid suborigin serialization.
if (first_period == (host_piece.size() - 1))
return false;
return true;
}
std::string SuboriginFromUrl(const GURL& url) {
if (!HasSuborigin(url))
return "";
std::string host = url.host();
size_t suborigin_end = host.find(".");
return (suborigin_end == std::string::npos) ? ""
: host.substr(0, suborigin_end);
}
GURL StripSuboriginFromUrl(const GURL& url) {
if (!HasSuborigin(url))
return url;
GURL::Replacements replacements;
if (url.scheme() == kHttpSuboriginScheme) {
replacements.SetSchemeStr(url::kHttpScheme);
} else {
DCHECK(url.scheme() == kHttpsSuboriginScheme);
replacements.SetSchemeStr(url::kHttpsScheme);
}
std::string host = url.host();
size_t suborigin_end = host.find(".");
std::string new_host(
(suborigin_end == std::string::npos)
? ""
: host.substr(suborigin_end + 1,
url.host().length() - suborigin_end - 1));
replacements.SetHostStr(new_host);
return url.ReplaceComponents(replacements);
}
} // namespace content
|