OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/ui/webui/devtools_ui.h" | 5 #include "chrome/browser/ui/webui/devtools_ui.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/strings/string_split.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/devtools_frontend_host.h" | 15 #include "content/public/browser/devtools_frontend_host.h" |
15 #include "content/public/browser/url_data_source.h" | 16 #include "content/public/browser/url_data_source.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
18 #include "content/public/common/user_agent.h" | 19 #include "content/public/common/user_agent.h" |
| 20 #include "net/base/escape.h" |
| 21 #include "net/base/url_util.h" |
19 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
22 | 25 |
23 using content::BrowserThread; | 26 using content::BrowserThread; |
24 using content::WebContents; | 27 using content::WebContents; |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 std::string PathWithoutParams(const std::string& path) { | 31 std::string PathWithoutParams(const std::string& path) { |
(...skipping 10 matching lines...) Expand all Loading... |
39 #if defined(DEBUG_DEVTOOLS) | 42 #if defined(DEBUG_DEVTOOLS) |
40 // Local frontend url provided by InspectUI. | 43 // Local frontend url provided by InspectUI. |
41 const char kFallbackFrontendURL[] = | 44 const char kFallbackFrontendURL[] = |
42 "chrome-devtools://devtools/bundled/inspector.html"; | 45 "chrome-devtools://devtools/bundled/inspector.html"; |
43 #else | 46 #else |
44 // URL causing the DevTools window to display a plain text warning. | 47 // URL causing the DevTools window to display a plain text warning. |
45 const char kFallbackFrontendURL[] = | 48 const char kFallbackFrontendURL[] = |
46 "data:text/plain,Cannot load DevTools frontend from an untrusted origin"; | 49 "data:text/plain,Cannot load DevTools frontend from an untrusted origin"; |
47 #endif // defined(DEBUG_DEVTOOLS) | 50 #endif // defined(DEBUG_DEVTOOLS) |
48 | 51 |
| 52 GURL SanitizeFrontendURL( |
| 53 const GURL& url, |
| 54 const std::string& scheme, |
| 55 const std::string& host, |
| 56 const std::string& path, |
| 57 bool allow_query); |
| 58 |
| 59 std::string SanitizeRevision(const std::string& revision) { |
| 60 for (size_t i = 0; i < revision.length(); i++) { |
| 61 if (!(revision[i] == '@' && i == 0) |
| 62 && !(revision[i] >= '0' && revision[i] <= '9') |
| 63 && !(revision[i] >= 'a' && revision[i] <= 'z') |
| 64 && !(revision[i] >= 'A' && revision[i] <= 'Z')) { |
| 65 return std::string(); |
| 66 } |
| 67 } |
| 68 return revision; |
| 69 } |
| 70 |
| 71 std::string SanitizeFrontendPath(const std::string& path) { |
| 72 for (size_t i = 0; i < path.length(); i++) { |
| 73 if (path[i] != '/' && path[i] != '-' && path[i] != '_' |
| 74 && path[i] != '.' && path[i] != '@' |
| 75 && !(path[i] >= '0' && path[i] <= '9') |
| 76 && !(path[i] >= 'a' && path[i] <= 'z') |
| 77 && !(path[i] >= 'A' && path[i] <= 'Z')) { |
| 78 return std::string(); |
| 79 } |
| 80 } |
| 81 return path; |
| 82 } |
| 83 |
| 84 std::string SanitizeEndpoint(const std::string& value) { |
| 85 if (value.find('&') != std::string::npos |
| 86 || value.find('?') != std::string::npos) |
| 87 return std::string(); |
| 88 return value; |
| 89 } |
| 90 |
| 91 std::string SanitizeRemoteBase(const std::string& value) { |
| 92 GURL url(value); |
| 93 std::string path = url.path(); |
| 94 std::vector<std::string> parts = base::SplitString( |
| 95 path, "/", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 96 std::string revision = parts.size() > 2 ? parts[2] : ""; |
| 97 revision = SanitizeRevision(revision); |
| 98 path = base::StringPrintf("/%s/%s/", kRemoteFrontendPath, revision.c_str()); |
| 99 return SanitizeFrontendURL(url, url::kHttpsScheme, |
| 100 kRemoteFrontendDomain, path, false).spec(); |
| 101 } |
| 102 |
| 103 std::string SanitizeRemoteFrontendURL(const std::string& value) { |
| 104 GURL url(net::UnescapeURLComponent(value, |
| 105 net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS | |
| 106 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | |
| 107 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE)); |
| 108 std::string path = url.path(); |
| 109 std::vector<std::string> parts = base::SplitString( |
| 110 path, "/", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 111 std::string revision = parts.size() > 2 ? parts[2] : ""; |
| 112 revision = SanitizeRevision(revision); |
| 113 std::string filename = parts.size() ? parts[parts.size() - 1] : ""; |
| 114 if (filename != "devtools.html") |
| 115 filename = "inspector.html"; |
| 116 path = base::StringPrintf("/serve_rev/%s/%s", |
| 117 revision.c_str(), filename.c_str()); |
| 118 std::string sanitized = SanitizeFrontendURL(url, url::kHttpsScheme, |
| 119 kRemoteFrontendDomain, path, true).spec(); |
| 120 return net::EscapeQueryParamValue(sanitized, false); |
| 121 } |
| 122 |
| 123 std::string SanitizeFrontendQueryParam( |
| 124 const std::string& key, |
| 125 const std::string& value) { |
| 126 // Convert boolean flags to true. |
| 127 if (key == "can_dock" || key == "debugFrontend" || key == "experiments" || |
| 128 key == "isSharedWorker" || key == "v8only" || key == "remoteFrontend") |
| 129 return "true"; |
| 130 |
| 131 // Pass connection endpoints as is. |
| 132 if (key == "ws" || key == "service-backend") |
| 133 return SanitizeEndpoint(value); |
| 134 |
| 135 // Only support undocked for old frontends. |
| 136 if (key == "dockSide" && value == "undocked") |
| 137 return value; |
| 138 |
| 139 if (key == "remoteBase") |
| 140 return SanitizeRemoteBase(value); |
| 141 |
| 142 if (key == "remoteFrontendUrl") |
| 143 return SanitizeRemoteFrontendURL(value); |
| 144 |
| 145 return std::string(); |
| 146 } |
| 147 |
| 148 GURL SanitizeFrontendURL( |
| 149 const GURL& url, |
| 150 const std::string& scheme, |
| 151 const std::string& host, |
| 152 const std::string& path, |
| 153 bool allow_query) { |
| 154 std::vector<std::string> query_parts; |
| 155 if (allow_query) { |
| 156 for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { |
| 157 std::string value = SanitizeFrontendQueryParam(it.GetKey(), |
| 158 it.GetValue()); |
| 159 if (!value.empty()) { |
| 160 query_parts.push_back( |
| 161 base::StringPrintf("%s=%s", it.GetKey().c_str(), value.c_str())); |
| 162 } |
| 163 } |
| 164 } |
| 165 std::string query = |
| 166 query_parts.empty() ? "" : "?" + base::JoinString(query_parts, "&"); |
| 167 std::string constructed = base::StringPrintf("%s://%s%s%s", |
| 168 scheme.c_str(), host.c_str(), path.c_str(), query.c_str()); |
| 169 GURL result = GURL(constructed); |
| 170 if (!result.is_valid()) |
| 171 return GURL(); |
| 172 return result; |
| 173 } |
| 174 |
49 // DevToolsDataSource --------------------------------------------------------- | 175 // DevToolsDataSource --------------------------------------------------------- |
50 | 176 |
51 std::string GetMimeTypeForPath(const std::string& path) { | 177 std::string GetMimeTypeForPath(const std::string& path) { |
52 std::string filename = PathWithoutParams(path); | 178 std::string filename = PathWithoutParams(path); |
53 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) { | 179 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) { |
54 return "text/html"; | 180 return "text/html"; |
55 } else if (base::EndsWith(filename, ".css", | 181 } else if (base::EndsWith(filename, ".css", |
56 base::CompareCase::INSENSITIVE_ASCII)) { | 182 base::CompareCase::INSENSITIVE_ASCII)) { |
57 return "text/css"; | 183 return "text/css"; |
58 } else if (base::EndsWith(filename, ".js", | 184 } else if (base::EndsWith(filename, ".js", |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 | 369 |
244 // static | 370 // static |
245 GURL DevToolsUI::GetRemoteBaseURL() { | 371 GURL DevToolsUI::GetRemoteBaseURL() { |
246 return GURL(base::StringPrintf( | 372 return GURL(base::StringPrintf( |
247 "%s%s/%s/", | 373 "%s%s/%s/", |
248 kRemoteFrontendBase, | 374 kRemoteFrontendBase, |
249 kRemoteFrontendPath, | 375 kRemoteFrontendPath, |
250 content::GetWebKitRevision().c_str())); | 376 content::GetWebKitRevision().c_str())); |
251 } | 377 } |
252 | 378 |
| 379 // static |
| 380 GURL DevToolsUI::SanitizeFrontendURL(const GURL& url) { |
| 381 return ::SanitizeFrontendURL(url, content::kChromeDevToolsScheme, |
| 382 chrome::kChromeUIDevToolsHost, SanitizeFrontendPath(url.path()), true); |
| 383 } |
| 384 |
253 DevToolsUI::DevToolsUI(content::WebUI* web_ui) | 385 DevToolsUI::DevToolsUI(content::WebUI* web_ui) |
254 : WebUIController(web_ui), | 386 : WebUIController(web_ui) { |
255 bindings_(web_ui->GetWebContents()) { | |
256 web_ui->SetBindings(0); | 387 web_ui->SetBindings(0); |
257 Profile* profile = Profile::FromWebUI(web_ui); | 388 Profile* profile = Profile::FromWebUI(web_ui); |
258 content::URLDataSource::Add( | 389 content::URLDataSource::Add( |
259 profile, | 390 profile, |
260 new DevToolsDataSource(profile->GetRequestContext())); | 391 new DevToolsDataSource(profile->GetRequestContext())); |
| 392 |
| 393 GURL url = web_ui->GetWebContents()->GetVisibleURL(); |
| 394 if (url.spec() == SanitizeFrontendURL(url).spec()) |
| 395 bindings_.reset(new DevToolsUIBindings(web_ui->GetWebContents())); |
261 } | 396 } |
262 | 397 |
263 DevToolsUI::~DevToolsUI() { | 398 DevToolsUI::~DevToolsUI() { |
264 } | 399 } |
OLD | NEW |