1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // NOTE: based loosely on mozilla's nsDataChannel.cpp
6
7 #include "net/base/data_url.h"
8
9 #include <string>
10 #include <string_view>
11
12 #include "base/base64.h"
13 #include "base/feature_list.h"
14 #include "base/features.h"
15 #include "base/ranges/algorithm.h"
16 #include "base/strings/escape.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "net/base/mime_util.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_util.h"
22 #include "url/gurl.h"
23
24 namespace net {
25 namespace {
26
27 // https://infra.spec.whatwg.org/#ascii-whitespace, which is referenced by
28 // https://infra.spec.whatwg.org/#forgiving-base64, does not include \v in the
29 // set of ASCII whitespace characters the way Unicode does.
IsBase64Whitespace(char c)30 bool IsBase64Whitespace(char c) {
31 return c != '\v' && base::IsAsciiWhitespace(c);
32 }
33
34 // A data URL is ready for decode if it:
35 // - Doesn't need any extra padding.
36 // - Does not have any escaped characters.
37 // - Does not have any whitespace.
IsDataURLReadyForDecode(std::string_view body)38 bool IsDataURLReadyForDecode(std::string_view body) {
39 return (body.length() % 4) == 0 && base::ranges::none_of(body, [](char c) {
40 return c == '%' || IsBase64Whitespace(c);
41 });
42 }
43
44 } // namespace
45
Parse(const GURL & url,std::string * mime_type,std::string * charset,std::string * data)46 bool DataURL::Parse(const GURL& url,
47 std::string* mime_type,
48 std::string* charset,
49 std::string* data) {
50 if (!url.is_valid() || !url.has_scheme())
51 return false;
52
53 DCHECK(mime_type->empty());
54 DCHECK(charset->empty());
55 DCHECK(!data || data->empty());
56
57 std::string_view content;
58 std::string content_string;
59 if (base::FeatureList::IsEnabled(base::features::kOptimizeDataUrls)) {
60 // Avoid copying the URL content which can be expensive for large URLs.
61 content = url.GetContentPiece();
62 } else {
63 content_string = url.GetContent();
64 content = content_string;
65 }
66
67 std::string_view::const_iterator comma = base::ranges::find(content, ',');
68 if (comma == content.end())
69 return false;
70
71 std::vector<std::string_view> meta_data =
72 base::SplitStringPiece(base::MakeStringPiece(content.begin(), comma), ";",
73 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
74
75 // These are moved to |mime_type| and |charset| on success.
76 std::string mime_type_value;
77 std::string charset_value;
78 auto iter = meta_data.cbegin();
79 if (iter != meta_data.cend()) {
80 mime_type_value = base::ToLowerASCII(*iter);
81 ++iter;
82 }
83
84 static constexpr std::string_view kBase64Tag("base64");
85 static constexpr std::string_view kCharsetTag("charset=");
86
87 bool base64_encoded = false;
88 for (; iter != meta_data.cend(); ++iter) {
89 if (!base64_encoded &&
90 base::EqualsCaseInsensitiveASCII(*iter, kBase64Tag)) {
91 base64_encoded = true;
92 } else if (charset_value.empty() &&
93 base::StartsWith(*iter, kCharsetTag,
94 base::CompareCase::INSENSITIVE_ASCII)) {
95 charset_value = std::string(iter->substr(kCharsetTag.size()));
96 // The grammar for charset is not specially defined in RFC2045 and
97 // RFC2397. It just needs to be a token.
98 if (!HttpUtil::IsToken(charset_value))
99 return false;
100 }
101 }
102
103 if (mime_type_value.empty()) {
104 // Fallback to the default if nothing specified in the mediatype part as
105 // specified in RFC2045. As specified in RFC2397, we use |charset| even if
106 // |mime_type| is empty.
107 mime_type_value = "text/plain";
108 if (charset_value.empty())
109 charset_value = "US-ASCII";
110 } else if (!ParseMimeTypeWithoutParameter(mime_type_value, nullptr,
111 nullptr)) {
112 // Fallback to the default as recommended in RFC2045 when the mediatype
113 // value is invalid. For this case, we don't respect |charset| but force it
114 // set to "US-ASCII".
115 mime_type_value = "text/plain";
116 charset_value = "US-ASCII";
117 }
118
119 // The caller may not be interested in receiving the data.
120 if (data) {
121 // Preserve spaces if dealing with text or xml input, same as mozilla:
122 // https://bugzilla.mozilla.org/show_bug.cgi?id=138052
123 // but strip them otherwise:
124 // https://bugzilla.mozilla.org/show_bug.cgi?id=37200
125 // (Spaces in a data URL should be escaped, which is handled below, so any
126 // spaces now are wrong. People expect to be able to enter them in the URL
127 // bar for text, and it can't hurt, so we allow it.)
128 //
129 // TODO(mmenke): Is removing all spaces reasonable? GURL removes trailing
130 // spaces itself, anyways. Should we just trim leading spaces instead?
131 // Allowing random intermediary spaces seems unnecessary.
132
133 auto raw_body = base::MakeStringPiece(comma + 1, content.end());
134
135 // For base64, we may have url-escaped whitespace which is not part
136 // of the data, and should be stripped. Otherwise, the escaped whitespace
137 // could be part of the payload, so don't strip it.
138 if (base64_encoded) {
139 // If the data URL is well formed, we can decode it immediately.
140 if (base::FeatureList::IsEnabled(base::features::kOptimizeDataUrls) &&
141 IsDataURLReadyForDecode(raw_body)) {
142 if (!base::Base64Decode(raw_body, data))
143 return false;
144 } else {
145 std::string unescaped_body = base::UnescapeBinaryURLComponent(raw_body);
146 if (!base::Base64Decode(unescaped_body, data,
147 base::Base64DecodePolicy::kForgiving))
148 return false;
149 }
150 } else {
151 // Strip whitespace for non-text MIME types.
152 std::string temp;
153 if (!(mime_type_value.compare(0, 5, "text/") == 0 ||
154 mime_type_value.find("xml") != std::string::npos)) {
155 temp = std::string(raw_body);
156 std::erase_if(temp, base::IsAsciiWhitespace<char>);
157 raw_body = temp;
158 }
159
160 *data = base::UnescapeBinaryURLComponent(raw_body);
161 }
162 }
163
164 *mime_type = std::move(mime_type_value);
165 *charset = std::move(charset_value);
166 return true;
167 }
168
BuildResponse(const GURL & url,std::string_view method,std::string * mime_type,std::string * charset,std::string * data,scoped_refptr<HttpResponseHeaders> * headers)169 Error DataURL::BuildResponse(const GURL& url,
170 std::string_view method,
171 std::string* mime_type,
172 std::string* charset,
173 std::string* data,
174 scoped_refptr<HttpResponseHeaders>* headers) {
175 DCHECK(data);
176 DCHECK(!*headers);
177
178 if (!DataURL::Parse(url, mime_type, charset, data))
179 return ERR_INVALID_URL;
180
181 // |mime_type| set by DataURL::Parse() is guaranteed to be in
182 // token "/" token
183 // form. |charset| can be an empty string.
184 DCHECK(!mime_type->empty());
185
186 // "charset" in the Content-Type header is specified explicitly to follow
187 // the "token" ABNF in the HTTP spec. When the DataURL::Parse() call is
188 // successful, it's guaranteed that the string in |charset| follows the
189 // "token" ABNF.
190 std::string content_type = *mime_type;
191 if (!charset->empty())
192 content_type.append(";charset=" + *charset);
193 // The terminal double CRLF isn't needed by TryToCreate().
194 *headers = HttpResponseHeaders::TryToCreate(
195 "HTTP/1.1 200 OK\r\n"
196 "Content-Type:" +
197 content_type);
198 // Above line should always succeed - TryToCreate() only fails when there are
199 // nulls in the string, and DataURL::Parse() can't return nulls in anything
200 // but the |data| argument.
201 DCHECK(*headers);
202
203 if (base::EqualsCaseInsensitiveASCII(method, "HEAD"))
204 data->clear();
205
206 return OK;
207 }
208
209 } // namespace net
210