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

Side by Side Diff: chrome/browser/search_engines/template_url_parser.cc

Issue 10908044: Refuse invalid SearchProvider and OSDD suggest URLs; etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Bail on invalid TemplateURLRefs in searching and OSDD parsing. Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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/search_engines/template_url_parser.h" 5 #include "chrome/browser/search_engines/template_url_parser.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 137
138 static void StartElementImpl(void* ctx, 138 static void StartElementImpl(void* ctx,
139 const xmlChar* name, 139 const xmlChar* name,
140 const xmlChar** atts); 140 const xmlChar** atts);
141 static void EndElementImpl(void* ctx, const xmlChar* name); 141 static void EndElementImpl(void* ctx, const xmlChar* name);
142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); 142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len);
143 143
144 // Returns a heap-allocated TemplateURL representing the result of parsing. 144 // Returns a heap-allocated TemplateURL representing the result of parsing.
145 // This will be NULL if parsing failed or if the results were invalid for some 145 // This will be NULL if parsing failed or if the results were invalid for some
146 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, 146 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied,
147 // etc.). 147 // a resulting TempateURLRef was invalid, etc.).
148 TemplateURL* GetTemplateURL(Profile* profile, bool show_in_default_list); 148 TemplateURL* GetTemplateURL(Profile* profile, bool show_in_default_list);
149 149
150 private: 150 private:
151 // Key is UTF8 encoded. 151 // Key is UTF8 encoded.
152 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; 152 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap;
153 153
154 static void InitMapping(); 154 static void InitMapping();
155 155
156 void ParseURL(const xmlChar** atts); 156 void ParseURL(const xmlChar** atts);
157 void ParseImage(const xmlChar** atts); 157 void ParseImage(const xmlChar** atts);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 void TemplateURLParsingContext::CharactersImpl(void* ctx, 284 void TemplateURLParsingContext::CharactersImpl(void* ctx,
285 const xmlChar* ch, 285 const xmlChar* ch,
286 int len) { 286 int len) {
287 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += 287 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ +=
288 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(ch), len)); 288 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(ch), len));
289 } 289 }
290 290
291 TemplateURL* TemplateURLParsingContext::GetTemplateURL( 291 TemplateURL* TemplateURLParsingContext::GetTemplateURL(
292 Profile* profile, 292 Profile* profile,
293 bool show_in_default_list) { 293 bool show_in_default_list) {
294 // Basic legality checks. 294 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107
295 if (data_.short_name.empty() || !IsHTTPRef(data_.url()) || 295 if (method_ == TemplateURLParsingContext::POST ||
296 !IsHTTPRef(data_.suggestions_url)) 296 data_.short_name.empty() || data_.url().empty() ||
Peter Kasting 2012/09/05 23:31:27 There's no need to add the check for data_.url().e
msw 2012/09/06 20:02:29 Done.
297 return NULL; 297 !IsHTTPRef(data_.url()) || !IsHTTPRef(data_.suggestions_url))
298
299 // If the image was a data URL, use the favicon from the search URL instead.
300 // (see TODO inEndElementImpl()).
301 GURL url(data_.url());
302 if (derive_image_from_url_ && data_.favicon_url.is_empty())
303 data_.favicon_url = TemplateURL::GenerateFaviconURL(url);
304
305 // TODO(jcampan): http://b/issue?id=1196285 we do not support search engines
306 // that use POST yet.
307 if (method_ == TemplateURLParsingContext::POST)
308 return NULL; 298 return NULL;
309 if (suggestion_method_ == TemplateURLParsingContext::POST) 299 if (suggestion_method_ == TemplateURLParsingContext::POST)
310 data_.suggestions_url.clear(); 300 data_.suggestions_url.clear();
311 301
312 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); 302 // If the image was a data URL, use the favicon from the search URL instead.
303 // (see the TODO in EndElementImpl()).
304 GURL search_url(data_.url());
Peter Kasting 2012/09/05 23:31:27 Urf. Both GenerateFaviconURL() and GenerateKeywor
msw 2012/09/06 20:02:29 Invalid search/suggest URLs bail above in the IsHT
305 if (derive_image_from_url_ && data_.favicon_url.is_empty())
306 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url);
307
308 data_.SetKeyword(TemplateURLService::GenerateKeyword(search_url));
313 data_.show_in_default_list = show_in_default_list; 309 data_.show_in_default_list = show_in_default_list;
314 return new TemplateURL(profile, data_); 310
311 // Bail if the search URL is empty or if either TemplateURLRef is invalid.
312 scoped_ptr<TemplateURL> template_url(new TemplateURL(profile, data_));
313 if (template_url->url().empty() || !template_url->url_ref().IsValid() ||
314 (!template_url->suggestions_url().empty() &&
315 !template_url->suggestions_url_ref().IsValid()))
316 return NULL;
317
318 return template_url.release();
315 } 319 }
316 320
317 // static 321 // static
318 void TemplateURLParsingContext::InitMapping() { 322 void TemplateURLParsingContext::InitMapping() {
319 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; 323 kElementNameToElementTypeMap = new std::map<std::string, ElementType>;
320 (*kElementNameToElementTypeMap)[kURLElement] = URL; 324 (*kElementNameToElementTypeMap)[kURLElement] = URL;
321 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; 325 (*kElementNameToElementTypeMap)[kParamElement] = PARAM;
322 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; 326 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME;
323 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; 327 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE;
324 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = 328 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] =
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 // &#38; . Unfortunately xmlSubstituteEntitiesDefault affects global state. 482 // &#38; . Unfortunately xmlSubstituteEntitiesDefault affects global state.
479 // If this becomes problematic we'll need to provide our own entity 483 // If this becomes problematic we'll need to provide our own entity
480 // type for &amp;, or strip out &#38; by hand after parsing. 484 // type for &amp;, or strip out &#38; by hand after parsing.
481 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); 485 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1);
482 TemplateURLParsingContext context(param_filter); 486 TemplateURLParsingContext context(param_filter);
483 xmlSAXHandler sax_handler; 487 xmlSAXHandler sax_handler;
484 memset(&sax_handler, 0, sizeof(sax_handler)); 488 memset(&sax_handler, 0, sizeof(sax_handler));
485 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; 489 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl;
486 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; 490 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl;
487 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; 491 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl;
488 xmlSAXUserParseMemory(&sax_handler, &context, data, static_cast<int>(length)); 492 xmlSAXUserParseMemory(&sax_handler, &context, data, static_cast<int>(length));
Peter Kasting 2012/09/05 23:31:27 I think if this returns nonzero, we should return
msw 2012/09/06 20:02:29 Done.
489 xmlSubstituteEntitiesDefault(last_sub_entities_value); 493 xmlSubstituteEntitiesDefault(last_sub_entities_value);
490 494
491 return context.GetTemplateURL(profile, show_in_default_list); 495 return context.GetTemplateURL(profile, show_in_default_list);
492 } 496 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698