OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 #include "chrome/browser/search/suggestion_source.h" | |
6 | |
7 #include "base/json/string_escape.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/ref_counted_memory.h" | |
10 #include "base/string_util.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_piece.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/search/instant_io_context.h" | |
15 #include "chrome/browser/search/search.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "content/public/browser/render_view_host.h" | |
18 #include "content/public/browser/resource_request_info.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "grit/browser_resources.h" | |
22 #include "net/base/url_util.h" | |
23 #include "net/url_request/url_request.h" | |
24 #include "ui/base/layout.h" | |
25 #include "ui/base/resource/resource_bundle.h" | |
26 | |
27 namespace { | |
28 | |
29 const char kLoaderHtmlPath[] = "/loader.html"; | |
30 const char kLoaderJSPath[] = "/loader.js"; | |
31 const char kResultHtmlPath[] = "/result.html"; | |
32 const char kResultJSPath[] = "/result.js"; | |
33 const char kProcessIDParam[] = "p"; | |
34 const char kViewIDParam[] = "v"; | |
35 | |
36 } // namespace | |
37 | |
38 SuggestionSource::SuggestionSource() { | |
39 } | |
40 | |
41 SuggestionSource::~SuggestionSource() { | |
42 } | |
43 | |
44 std::string SuggestionSource::GetSource() { | |
45 return chrome::kChromeSearchSuggestionHost; | |
46 } | |
47 | |
48 void SuggestionSource::StartDataRequest( | |
49 const std::string& path_and_query, | |
50 bool is_incognito, | |
51 const content::URLDataSource::GotDataCallback& callback) { | |
52 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + | |
53 path_and_query).path()); | |
54 if (path == kLoaderHtmlPath) | |
55 SendResource(IDR_OMNIBOX_RESULT_LOADER_HTML, callback); | |
56 else if (path == kLoaderJSPath) | |
57 SendJSWithOrigin(IDR_OMNIBOX_RESULT_LOADER_JS, path_and_query, callback); | |
58 else if (path == kResultHtmlPath) | |
59 SendResource(IDR_OMNIBOX_RESULT_HTML, callback); | |
60 else if (path == kResultJSPath) | |
61 SendJSWithOrigin(IDR_OMNIBOX_RESULT_JS, path_and_query, callback); | |
62 else | |
63 callback.Run(NULL); | |
64 } | |
65 | |
66 void SuggestionSource::SendResource( | |
67 int resource_id, | |
68 const content::URLDataSource::GotDataCallback& callback) { | |
69 scoped_refptr<base::RefCountedStaticMemory> response( | |
70 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | |
71 callback.Run(response); | |
72 } | |
73 | |
74 void SuggestionSource::SendJSWithOrigin( | |
75 int resource_id, | |
76 const std::string& path_and_query, | |
77 const content::URLDataSource::GotDataCallback& callback) { | |
78 // Look up the origin for this request using the process id and render view | |
79 // id previously added in WillServiceRequest() on the IO thread. We must do | |
80 // this here because GetOrigin() needs to run on the UI thread. | |
sreeram
2013/04/16 21:23:31
Does it make sense to add DCHECK(IsOnThread(...))
Jered
2013/04/18 18:11:48
I'd really prefer not to.
I have seen this idiom
| |
81 std::string process_id_str; | |
82 std::string render_view_id_str; | |
83 int process_id = -1; | |
84 int render_view_id = -1; | |
85 std::string origin; | |
86 GURL url(chrome::kChromeSearchSuggestionUrl + path_and_query); | |
87 if (!net::GetValueForKeyInQuery(url, kProcessIDParam, &process_id_str) || | |
88 !net::GetValueForKeyInQuery(url, kViewIDParam, &render_view_id_str) || | |
89 !base::StringToInt(process_id_str, &process_id) || | |
90 !base::StringToInt(render_view_id_str, &render_view_id) || | |
91 !GetOrigin(process_id, render_view_id, &origin)) { | |
92 callback.Run(NULL); | |
93 return; | |
94 } | |
95 | |
96 std::string js_escaped_origin; | |
97 base::JsonDoubleQuote(origin, false, &js_escaped_origin); | |
98 base::StringPiece template_js = | |
99 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | |
100 std::string response(template_js.as_string()); | |
101 ReplaceFirstSubstringAfterOffset(&response, 0, "{{ORIGIN}}", origin); | |
102 callback.Run(base::RefCountedString::TakeString(&response)); | |
103 } | |
104 | |
105 std::string SuggestionSource::GetMimeType( | |
106 const std::string& path_and_query) const { | |
107 std::string path(GURL(chrome::kChromeSearchSuggestionUrl + | |
108 path_and_query).path()); | |
109 if (path == kLoaderHtmlPath || path == kResultHtmlPath) | |
110 return "text/html"; | |
111 if (path == kLoaderJSPath || path == kResultJSPath) | |
112 return "application/javascript"; | |
113 return ""; | |
114 } | |
115 | |
116 bool SuggestionSource::ShouldServiceRequest( | |
117 const net::URLRequest* request) const { | |
118 const std::string& path = request->url().path(); | |
119 return InstantIOContext::ShouldServiceRequest(request) && | |
120 request->url().SchemeIs(chrome::kChromeSearchScheme) && | |
121 request->url().host() == chrome::kChromeSearchSuggestionHost && | |
122 (path == kLoaderHtmlPath || path == kLoaderJSPath || | |
123 path == kResultHtmlPath || path == kResultJSPath); | |
124 } | |
125 | |
126 void SuggestionSource::WillServiceRequest( | |
127 const net::URLRequest* request, std::string* path) const { | |
128 // Overwrite the query string for this request with parameters &p and &v | |
129 // giving the process id and render view id which generated this URL request. | |
130 // These will be used to lookup the requestor's origin later. | |
131 std::string query_str; | |
132 const content::ResourceRequestInfo* info = | |
133 content::ResourceRequestInfo::ForRequest(request); | |
134 if (info) { | |
135 int process_id = -1; | |
136 int render_view_id = -1; | |
137 if (info->GetAssociatedRenderView(&process_id, &render_view_id)) { | |
138 query_str.append(kProcessIDParam); | |
139 query_str.append("="); | |
140 query_str.append(base::IntToString(process_id)); | |
141 query_str.append("&"); | |
142 query_str.append(kViewIDParam); | |
143 query_str.append("="); | |
144 query_str.append(base::IntToString(render_view_id)); | |
145 } | |
146 } | |
147 // Note that this replaces any existing query string parameters, including | |
148 // any existing &p and &v parameters. | |
149 GURL url(chrome::kChromeSearchSuggestionUrl + (*path)); | |
150 GURL::Replacements set_query; | |
151 set_query.SetQueryStr(query_str); | |
152 GURL url_with_query(url.ReplaceComponents(set_query)); | |
153 *path = url_with_query.path().substr(1) + "?" + url_with_query.query(); | |
154 } | |
155 | |
156 bool SuggestionSource::ShouldDenyXFrameOptions() const { | |
157 return false; | |
158 } | |
159 | |
160 bool SuggestionSource::GetOrigin( | |
161 int process_id, | |
162 int render_view_id, | |
163 std::string* origin) const { | |
164 content::RenderViewHost* rvh = | |
165 content::RenderViewHost::FromID(process_id, render_view_id); | |
166 if (rvh == NULL) | |
167 return false; | |
168 content::WebContents* contents = | |
169 content::WebContents::FromRenderViewHost(rvh); | |
170 if (contents == NULL) | |
171 return false; | |
172 *origin = contents->GetURL().GetOrigin().spec(); | |
sreeram
2013/04/16 21:23:31
Is it possible for this to be spoofed? Say the sea
sreeram
2013/04/16 21:28:19
Don't waste too much time on this. For the page to
Jered
2013/04/16 23:16:38
I think WebContents is getting its URL from the na
Jered
2013/04/18 18:11:48
tl;dr There doesn't seem to be a viable attack her
| |
173 // Origin should not include a trailing slash. That is part of the path. | |
174 TrimString(*origin, "/", origin); | |
175 return true; | |
176 } | |
OLD | NEW |