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

Side by Side Diff: net/base/data_url.cc

Issue 54233002: Make net::DataURL's MIME string check stricter (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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
« no previous file with comments | « content/content_tests.gypi ('k') | net/base/data_url_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // NOTE: based loosely on mozilla's nsDataChannel.cpp 5 // NOTE: based loosely on mozilla's nsDataChannel.cpp
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "net/base/data_url.h" 9 #include "net/base/data_url.h"
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "net/base/escape.h" 15 #include "net/base/escape.h"
16 #include "net/http/http_util.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
17 18
18 namespace net { 19 namespace net {
19 20
20 // static 21 // static
21 bool DataURL::Parse(const GURL& url, std::string* mime_type, 22 bool DataURL::Parse(const GURL& url, std::string* mime_type,
22 std::string* charset, std::string* data) { 23 std::string* charset, std::string* data) {
23 DCHECK(mime_type->empty()); 24 DCHECK(mime_type->empty());
24 DCHECK(charset->empty()); 25 DCHECK(charset->empty());
25 std::string::const_iterator begin = url.spec().begin(); 26 std::string::const_iterator begin = url.spec().begin();
(...skipping 26 matching lines...) Expand all
52 bool base64_encoded = false; 53 bool base64_encoded = false;
53 for (; iter != meta_data.end(); ++iter) { 54 for (; iter != meta_data.end(); ++iter) {
54 if (!base64_encoded && *iter == kBase64Tag) { 55 if (!base64_encoded && *iter == kBase64Tag) {
55 base64_encoded = true; 56 base64_encoded = true;
56 } else if (charset->empty() && 57 } else if (charset->empty() &&
57 iter->compare(0, kCharsetTagLength, kCharsetTag) == 0) { 58 iter->compare(0, kCharsetTagLength, kCharsetTag) == 0) {
58 charset->assign(iter->substr(kCharsetTagLength)); 59 charset->assign(iter->substr(kCharsetTagLength));
59 } 60 }
60 } 61 }
61 62
62 // fallback to defaults if nothing specified in the URL: 63 if (mime_type->empty()) {
63 if (mime_type->empty()) 64 // fallback to defaults if nothing specified in the URL:
64 mime_type->assign("text/plain"); 65 mime_type->assign("text/plain");
66 } else {
67 // Check grammar.
asanka 2014/03/26 19:42:09 Consider using IsMimeType() instead?
tyoshino (SeeGerritForStatus) 2014/03/26 22:07:00 I considered that. But IsMimeType() doesn't check
asanka 2014/03/26 22:29:42 SGTM
68 std::vector<std::string> mime_type_components;
69 base::SplitString(*mime_type, '/', &mime_type_components);
70 if (mime_type_components.size() != 2 ||
71 !HttpUtil::IsToken(mime_type_components[0]) ||
72 !HttpUtil::IsToken(mime_type_components[1]))
73 return false;
74 }
65 if (charset->empty()) 75 if (charset->empty())
66 charset->assign("US-ASCII"); 76 charset->assign("US-ASCII");
67 77
68 // The caller may not be interested in receiving the data. 78 // The caller may not be interested in receiving the data.
69 if (!data) 79 if (!data)
70 return true; 80 return true;
71 81
72 // Preserve spaces if dealing with text or xml input, same as mozilla: 82 // Preserve spaces if dealing with text or xml input, same as mozilla:
73 // https://bugzilla.mozilla.org/show_bug.cgi?id=138052 83 // https://bugzilla.mozilla.org/show_bug.cgi?id=138052
74 // but strip them otherwise: 84 // but strip them otherwise:
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 temp_data.resize(length + padding_needed, '='); 124 temp_data.resize(length + padding_needed, '=');
115 } 125 }
116 return base::Base64Decode(temp_data, data); 126 return base::Base64Decode(temp_data, data);
117 } 127 }
118 128
119 temp_data.swap(*data); 129 temp_data.swap(*data);
120 return true; 130 return true;
121 } 131 }
122 132
123 } // namespace net 133 } // namespace net
OLDNEW
« no previous file with comments | « content/content_tests.gypi ('k') | net/base/data_url_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698