• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "net/base/filename_util.h"
6 
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "net/base/escape.h"
15 #include "net/base/filename_util_internal.h"
16 #include "net/base/mime_util.h"
17 #include "net/base/net_string_util.h"
18 #include "net/http/http_content_disposition.h"
19 #include "url/gurl.h"
20 
21 namespace net {
22 
SanitizeGeneratedFileName(base::FilePath::StringType * filename,bool replace_trailing)23 void SanitizeGeneratedFileName(base::FilePath::StringType* filename,
24                                bool replace_trailing) {
25   const base::FilePath::CharType kReplace[] = FILE_PATH_LITERAL("-");
26   if (filename->empty())
27     return;
28   if (replace_trailing) {
29     // Handle CreateFile() stripping trailing dots and spaces on filenames
30     // http://support.microsoft.com/kb/115827
31     size_t length = filename->size();
32     size_t pos = filename->find_last_not_of(FILE_PATH_LITERAL(" ."));
33     filename->resize((pos == std::string::npos) ? 0 : (pos + 1));
34     base::TrimWhitespace(*filename, base::TRIM_TRAILING, filename);
35     if (filename->empty())
36       return;
37     size_t trimmed = length - filename->size();
38     if (trimmed)
39       filename->insert(filename->end(), trimmed, kReplace[0]);
40   }
41   base::TrimString(*filename, FILE_PATH_LITERAL("."), filename);
42   if (filename->empty())
43     return;
44   // Replace any path information by changing path separators.
45   ReplaceSubstringsAfterOffset(filename, 0, FILE_PATH_LITERAL("/"), kReplace);
46   ReplaceSubstringsAfterOffset(filename, 0, FILE_PATH_LITERAL("\\"), kReplace);
47 }
48 
49 // Returns the filename determined from the last component of the path portion
50 // of the URL.  Returns an empty string if the URL doesn't have a path or is
51 // invalid. If the generated filename is not reliable,
52 // |should_overwrite_extension| will be set to true, in which case a better
53 // extension should be determined based on the content type.
GetFileNameFromURL(const GURL & url,const std::string & referrer_charset,bool * should_overwrite_extension)54 std::string GetFileNameFromURL(const GURL& url,
55                                const std::string& referrer_charset,
56                                bool* should_overwrite_extension) {
57   // about: and data: URLs don't have file names, but esp. data: URLs may
58   // contain parts that look like ones (i.e., contain a slash).  Therefore we
59   // don't attempt to divine a file name out of them.
60   if (!url.is_valid() || url.SchemeIs("about") || url.SchemeIs("data"))
61     return std::string();
62 
63   const std::string unescaped_url_filename = UnescapeURLComponent(
64       url.ExtractFileName(),
65       UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
66 
67   // The URL's path should be escaped UTF-8, but may not be.
68   std::string decoded_filename = unescaped_url_filename;
69   if (!base::IsStringUTF8(decoded_filename)) {
70     // TODO(jshin): this is probably not robust enough. To be sure, we need
71     // encoding detection.
72     base::string16 utf16_output;
73     if (!referrer_charset.empty() &&
74         net::ConvertToUTF16(
75             unescaped_url_filename, referrer_charset.c_str(), &utf16_output)) {
76       decoded_filename = base::UTF16ToUTF8(utf16_output);
77     } else {
78       decoded_filename =
79           base::WideToUTF8(base::SysNativeMBToWide(unescaped_url_filename));
80     }
81   }
82   // If the URL contains a (possibly empty) query, assume it is a generator, and
83   // allow the determined extension to be overwritten.
84   *should_overwrite_extension = !decoded_filename.empty() && url.has_query();
85 
86   return decoded_filename;
87 }
88 
89 // Returns whether the specified extension is automatically integrated into the
90 // windows shell.
IsShellIntegratedExtension(const base::FilePath::StringType & extension)91 bool IsShellIntegratedExtension(const base::FilePath::StringType& extension) {
92   base::FilePath::StringType extension_lower = StringToLowerASCII(extension);
93 
94   // http://msdn.microsoft.com/en-us/library/ms811694.aspx
95   // Right-clicking on shortcuts can be magical.
96   if ((extension_lower == FILE_PATH_LITERAL("local")) ||
97       (extension_lower == FILE_PATH_LITERAL("lnk")))
98     return true;
99 
100   // http://www.juniper.net/security/auto/vulnerabilities/vuln2612.html
101   // Files become magical if they end in a CLSID, so block such extensions.
102   if (!extension_lower.empty() &&
103       (extension_lower[0] == FILE_PATH_LITERAL('{')) &&
104       (extension_lower[extension_lower.length() - 1] == FILE_PATH_LITERAL('}')))
105     return true;
106   return false;
107 }
108 
109 // Returns whether the specified file name is a reserved name on windows.
110 // This includes names like "com2.zip" (which correspond to devices) and
111 // desktop.ini and thumbs.db which have special meaning to the windows shell.
IsReservedName(const base::FilePath::StringType & filename)112 bool IsReservedName(const base::FilePath::StringType& filename) {
113   // This list is taken from the MSDN article "Naming a file"
114   // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx
115   // I also added clock$ because GetSaveFileName seems to consider it as a
116   // reserved name too.
117   static const char* const known_devices[] = {
118       "con",  "prn",  "aux",  "nul",  "com1", "com2", "com3",  "com4",
119       "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2",  "lpt3",
120       "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "clock$"};
121 #if defined(OS_WIN)
122   std::string filename_lower = StringToLowerASCII(base::WideToUTF8(filename));
123 #elif defined(OS_POSIX)
124   std::string filename_lower = StringToLowerASCII(filename);
125 #endif
126 
127   for (size_t i = 0; i < arraysize(known_devices); ++i) {
128     // Exact match.
129     if (filename_lower == known_devices[i])
130       return true;
131     // Starts with "DEVICE.".
132     if (filename_lower.find(std::string(known_devices[i]) + ".") == 0)
133       return true;
134   }
135 
136   static const char* const magic_names[] = {
137     // These file names are used by the "Customize folder" feature of the shell.
138     "desktop.ini",
139     "thumbs.db",
140   };
141 
142   for (size_t i = 0; i < arraysize(magic_names); ++i) {
143     if (filename_lower == magic_names[i])
144       return true;
145   }
146 
147   return false;
148 }
149 
150 // Examines the current extension in |file_name| and modifies it if necessary in
151 // order to ensure the filename is safe.  If |file_name| doesn't contain an
152 // extension or if |ignore_extension| is true, then a new extension will be
153 // constructed based on the |mime_type|.
154 //
155 // We're addressing two things here:
156 //
157 // 1) Usability.  If there is no reliable file extension, we want to guess a
158 //    reasonable file extension based on the content type.
159 //
160 // 2) Shell integration.  Some file extensions automatically integrate with the
161 //    shell.  We block these extensions to prevent a malicious web site from
162 //    integrating with the user's shell.
EnsureSafeExtension(const std::string & mime_type,bool ignore_extension,base::FilePath * file_name)163 void EnsureSafeExtension(const std::string& mime_type,
164                          bool ignore_extension,
165                          base::FilePath* file_name) {
166   // See if our file name already contains an extension.
167   base::FilePath::StringType extension = file_name->Extension();
168   if (!extension.empty())
169     extension.erase(extension.begin());  // Erase preceding '.'.
170 
171   if ((ignore_extension || extension.empty()) && !mime_type.empty()) {
172     base::FilePath::StringType preferred_mime_extension;
173     std::vector<base::FilePath::StringType> all_mime_extensions;
174     net::GetPreferredExtensionForMimeType(mime_type, &preferred_mime_extension);
175     net::GetExtensionsForMimeType(mime_type, &all_mime_extensions);
176     // If the existing extension is in the list of valid extensions for the
177     // given type, use it. This avoids doing things like pointlessly renaming
178     // "foo.jpg" to "foo.jpeg".
179     if (std::find(all_mime_extensions.begin(),
180                   all_mime_extensions.end(),
181                   extension) != all_mime_extensions.end()) {
182       // leave |extension| alone
183     } else if (!preferred_mime_extension.empty()) {
184       extension = preferred_mime_extension;
185     }
186   }
187 
188 #if defined(OS_WIN)
189   static const base::FilePath::CharType default_extension[] =
190       FILE_PATH_LITERAL("download");
191 
192   // Rename shell-integrated extensions.
193   // TODO(asanka): Consider stripping out the bad extension and replacing it
194   // with the preferred extension for the MIME type if one is available.
195   if (IsShellIntegratedExtension(extension))
196     extension.assign(default_extension);
197 #endif
198 
199   *file_name = file_name->ReplaceExtension(extension);
200 }
201 
FilePathToString16(const base::FilePath & path,base::string16 * converted)202 bool FilePathToString16(const base::FilePath& path, base::string16* converted) {
203 #if defined(OS_WIN)
204   return base::WideToUTF16(
205       path.value().c_str(), path.value().size(), converted);
206 #elif defined(OS_POSIX)
207   std::string component8 = path.AsUTF8Unsafe();
208   return !component8.empty() &&
209          base::UTF8ToUTF16(component8.c_str(), component8.size(), converted);
210 #endif
211 }
212 
GetSuggestedFilenameImpl(const GURL & url,const std::string & content_disposition,const std::string & referrer_charset,const std::string & suggested_name,const std::string & mime_type,const std::string & default_name,ReplaceIllegalCharactersCallback replace_illegal_characters_callback)213 base::string16 GetSuggestedFilenameImpl(
214     const GURL& url,
215     const std::string& content_disposition,
216     const std::string& referrer_charset,
217     const std::string& suggested_name,
218     const std::string& mime_type,
219     const std::string& default_name,
220     ReplaceIllegalCharactersCallback replace_illegal_characters_callback) {
221   // TODO: this function to be updated to match the httpbis recommendations.
222   // Talk to abarth for the latest news.
223 
224   // We don't translate this fallback string, "download". If localization is
225   // needed, the caller should provide localized fallback in |default_name|.
226   static const base::FilePath::CharType kFinalFallbackName[] =
227       FILE_PATH_LITERAL("download");
228   std::string filename;  // In UTF-8
229   bool overwrite_extension = false;
230 
231   // Try to extract a filename from content-disposition first.
232   if (!content_disposition.empty()) {
233     HttpContentDisposition header(content_disposition, referrer_charset);
234     filename = header.filename();
235   }
236 
237   // Then try to use the suggested name.
238   if (filename.empty() && !suggested_name.empty())
239     filename = suggested_name;
240 
241   // Now try extracting the filename from the URL.  GetFileNameFromURL() only
242   // looks at the last component of the URL and doesn't return the hostname as a
243   // failover.
244   if (filename.empty())
245     filename = GetFileNameFromURL(url, referrer_charset, &overwrite_extension);
246 
247   // Finally try the URL hostname, but only if there's no default specified in
248   // |default_name|.  Some schemes (e.g.: file:, about:, data:) do not have a
249   // host name.
250   if (filename.empty() && default_name.empty() && url.is_valid() &&
251       !url.host().empty()) {
252     // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
253     filename = url.host();
254   }
255 
256   bool replace_trailing = false;
257   base::FilePath::StringType result_str, default_name_str;
258 #if defined(OS_WIN)
259   replace_trailing = true;
260   result_str = base::UTF8ToUTF16(filename);
261   default_name_str = base::UTF8ToUTF16(default_name);
262 #else
263   result_str = filename;
264   default_name_str = default_name;
265 #endif
266   SanitizeGeneratedFileName(&result_str, replace_trailing);
267   if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) ==
268       base::FilePath::StringType::npos) {
269     result_str = !default_name_str.empty()
270                      ? default_name_str
271                      : base::FilePath::StringType(kFinalFallbackName);
272     overwrite_extension = false;
273   }
274   replace_illegal_characters_callback.Run(&result_str, '-');
275   base::FilePath result(result_str);
276   GenerateSafeFileName(mime_type, overwrite_extension, &result);
277 
278   base::string16 result16;
279   if (!FilePathToString16(result, &result16)) {
280     result = base::FilePath(default_name_str);
281     if (!FilePathToString16(result, &result16)) {
282       result = base::FilePath(kFinalFallbackName);
283       FilePathToString16(result, &result16);
284     }
285   }
286   return result16;
287 }
288 
GenerateFileNameImpl(const GURL & url,const std::string & content_disposition,const std::string & referrer_charset,const std::string & suggested_name,const std::string & mime_type,const std::string & default_file_name,ReplaceIllegalCharactersCallback replace_illegal_characters_callback)289 base::FilePath GenerateFileNameImpl(
290     const GURL& url,
291     const std::string& content_disposition,
292     const std::string& referrer_charset,
293     const std::string& suggested_name,
294     const std::string& mime_type,
295     const std::string& default_file_name,
296     ReplaceIllegalCharactersCallback replace_illegal_characters_callback) {
297   base::string16 file_name =
298       GetSuggestedFilenameImpl(url,
299                                content_disposition,
300                                referrer_charset,
301                                suggested_name,
302                                mime_type,
303                                default_file_name,
304                                replace_illegal_characters_callback);
305 
306 #if defined(OS_WIN)
307   base::FilePath generated_name(file_name);
308 #else
309   base::FilePath generated_name(
310       base::SysWideToNativeMB(base::UTF16ToWide(file_name)));
311 #endif
312 
313   DCHECK(!generated_name.empty());
314 
315   return generated_name;
316 }
317 
318 }  // namespace net
319