Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / download / download_extensions.cc
blob54adce8ffd4fd9ac752e10aeb8b06587d3851ca5
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
3 // found in the LICENSE file.
5 #include <set>
6 #include <string>
8 #include "chrome/browser/download/download_extensions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "net/base/mime_util.h"
13 #include "net/base/net_util.h"
15 namespace download_util {
17 // For file extensions taken from mozilla:
19 /* ***** BEGIN LICENSE BLOCK *****
20 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
22 * The contents of this file are subject to the Mozilla Public License Version
23 * 1.1 (the "License"); you may not use this file except in compliance with
24 * the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
27 * Software distributed under the License is distributed on an "AS IS" basis,
28 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
29 * for the specific language governing rights and limitations under the
30 * License.
32 * The Original Code is Mozilla Communicator client code, released
33 * March 31, 1998.
35 * The Initial Developer of the Original Code is
36 * Netscape Communications Corporation.
37 * Portions created by the Initial Developer are Copyright (C) 1998-1999
38 * the Initial Developer. All Rights Reserved.
40 * Contributor(s):
41 * Doug Turner <dougt@netscape.com>
42 * Dean Tessman <dean_tessman@hotmail.com>
43 * Brodie Thiesfield <brofield@jellycan.com>
44 * Jungshik Shin <jshin@i18nl10n.com>
46 * Alternatively, the contents of this file may be used under the terms of
47 * either of the GNU General Public License Version 2 or later (the "GPL"),
48 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
49 * in which case the provisions of the GPL or the LGPL are applicable instead
50 * of those above. If you wish to allow use of your version of this file only
51 * under the terms of either the GPL or the LGPL, and not to allow others to
52 * use your version of this file under the terms of the MPL, indicate your
53 * decision by deleting the provisions above and replace them with the notice
54 * and other provisions required by the GPL or the LGPL. If you do not delete
55 * the provisions above, a recipient may use your version of this file under
56 * the terms of any one of the MPL, the GPL or the LGPL.
58 * ***** END LICENSE BLOCK ***** */
60 namespace {
62 enum DownloadAutoOpenHint {
63 ALLOW_AUTO_OPEN,
65 // The file type should not be allowed to open automatically.
67 // Criteria for disallowing a file type from opening automatically:
69 // Includes file types that upon opening may either:
70 // * ... execute arbitrary or harmful code with user privileges.
71 // * ... change configuration of the system to cause harmful behavior
72 // immediately or at some time in the future.
74 // Doesn't include file types that upon opening:
75 // * ... sufficiently warn the user about the fact that:
76 // - This file was downloaded from the internet.
77 // - Opening it can make specified changes to the system.
78 // (Note that any such warnings need to be displayed prior to the harmful
79 // logic being executed).
80 // * ... does nothing particularly dangerous, despite the act of downloading
81 // itself being dangerous (E.g. .local and .manifest files).
82 DISALLOW_AUTO_OPEN,
85 // Guidelines for adding a new dangerous file type:
87 // * Include a comment above the file type that:
88 // - Describes the file type.
89 // - Justifies why it is considered dangerous if this isn't obvious from the
90 // description.
91 // - Justifies why the file type is disallowed from auto opening, if
92 // necessary.
93 // * Add the file extension to the kDangerousFileTypes array in
94 // download_stats.cc.
96 // TODO(asanka): All file types listed below should have descriptions.
97 const struct FileType {
98 const char* extension; // Extension sans leading extension separator.
99 DownloadDangerLevel danger_level;
100 DownloadAutoOpenHint auto_open_hint;
101 } kDownloadFileTypes[] = {
102 // Some files are dangerous on all platforms.
104 // Flash files downloaded locally can sometimes access the local filesystem.
105 {"swf", DANGEROUS, DISALLOW_AUTO_OPEN},
106 {"spl", DANGEROUS, DISALLOW_AUTO_OPEN},
108 // Chrome extensions should be obtained through the web store. Allowed to
109 // open automatically because Chrome displays a prompt prior to
110 // installation.
111 {"crx", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
113 // Windows, all file categories. The list is in alphabetical order of
114 // extensions. Exceptions are made for logical groupings of file types.
116 // Some file descriptions are based on
117 // https://support.office.com/article/Blocked-attachments-in-Outlook-3811cddc-17c3-4279-a30c-060ba0207372
118 #if defined(OS_WIN)
119 {"ad", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
121 // Microsoft Access related.
122 {"ade", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Project extension
123 {"adp", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Project.
124 {"mad", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Module Shortcut.
125 {"maf", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
126 {"mag", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Diagram Shortcut.
127 {"mam", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Macro Shortcut.
128 {"maq", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Query Shortcut.
129 {"mar", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Report Shortcut.
130 {"mas", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Stored Procedures.
131 {"mat", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Table Shortcut.
132 {"mav", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // View Shortcut.
133 {"maw", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Data Access Page.
134 {"mda", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Access Add-in.
135 {"mdb", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Database.
136 {"mde", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Database.
137 {"mdt", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Add-in Data.
138 {"mdw", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Workgroup Information.
139 {"mdz", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN}, // Wizard Template.
141 // Executable Application.
142 {"app", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
144 // Microsoft ClickOnce depolyment manifest. By default, opens with
145 // dfshim.dll which should prompt the user before running untrusted code.
146 {"application", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
148 // Active Server Pages source file.
149 {"asp", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
151 // Advanced Stream Redirector. Contains a playlist of media files.
152 {"asx", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
154 // Microsoft Visual Basic source file. Opens by default in an editor.
155 {"bas", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
157 // Command script.
158 {"bat", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
160 {"cfg", DANGEROUS, ALLOW_AUTO_OPEN},
162 // Windows Compiled HTML Help files.
163 {"chi", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
164 {"chm", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
166 // Command script.
167 {"cmd", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
169 // Windows legacy executable.
170 {"com", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
172 // Control panel tool. Executable.
173 {"cpl", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
175 // Signed certificate file.
176 {"crt", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
178 // Windows executables.
179 {"dll", DANGEROUS, DISALLOW_AUTO_OPEN},
180 {"drv", DANGEROUS, DISALLOW_AUTO_OPEN},
181 {"exe", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
183 // Microsoft FoxPro Compiled Source.
184 {"fxp", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
185 {"grp", DANGEROUS, ALLOW_AUTO_OPEN},
187 // Windows legacy help file format.
188 {"hlp", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
190 // HTML Application. Executes as a fully trusted application.
191 {"hta", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
193 // Hypertext Template File. See https://support.microsoft.com/kb/181689.
194 {"htt", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
196 // Device installation information.
197 {"inf", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
199 // Generic configuration file.
200 {"ini", DANGEROUS, ALLOW_AUTO_OPEN},
202 // Microsoft IIS Internet Communication Settings.
203 {"ins", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
205 // Microsoft IIS Internet Service Provider Settings.
206 {"isp", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
208 // JavaScript file. May open using Windows Script Host with user level
209 // privileges.
210 {"js", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
212 // JScript encoded script file. Usually produced by running Microsoft Script
213 // Encoder over a .js file.
214 // See https://msdn.microsoft.com/library/d14c8zsc.aspx
215 {"jse", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
217 // Shortcuts. May open anything.
218 {"lnk", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
220 // .local files affect DLL search path for .exe file with same base name.
221 {"local", DANGEROUS, ALLOW_AUTO_OPEN},
223 // While being a generic name, having a .manifest file with the same
224 // basename as .exe file (foo.exe + foo.exe.manifest) changes the dll search
225 // order for the .exe file. Downloading this kind of file to the users'
226 // download directory is almost always the wrong thing to do.
227 {"manifest", DANGEROUS, ALLOW_AUTO_OPEN},
229 // Media Attachment Unit.
230 {"mau", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
232 // Multipart HTML.
233 {"mht", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
234 {"mhtml", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
236 {"mmc", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
237 {"mof", DANGEROUS, ALLOW_AUTO_OPEN},
239 // Microsoft Management Console Snap-in. Contains executable code.
240 {"msc", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
242 // Microsoft Shell.
243 {"msh", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
244 {"msh1", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
245 {"msh2", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
246 {"mshxml", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
247 {"msh1xml", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
248 {"msh2xml", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
250 // Windows Installer.
251 {"msi", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
252 {"msp", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
253 {"mst", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
255 // ActiveX Control.
256 {"ocx", DANGEROUS, DISALLOW_AUTO_OPEN},
258 // Microsoft Office Profile Settings File.
259 {"ops", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
261 // Microsoft Visual Test.
262 {"pcd", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
264 // Program Information File. Originally intended to configure execution
265 // environment for legacy DOS files. They aren't meant to contain executable
266 // code. But Windows may execute a PIF file that is sniffed as a PE file.
267 {"pif", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
269 // Developer Studio Build Log.
270 {"plg", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
272 // Windows System File.
273 {"prf", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
275 // Program File.
276 {"prg", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
278 // Microsoft Exchange Address Book File. Microsoft Outlook Personal Folder
279 // File.
280 {"pst", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
282 // Microsoft Windows PowerShell.
283 {"ps1", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
284 {"ps1xml", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
285 {"ps2", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
286 {"ps2xml", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
287 {"psc1", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
288 {"psc2", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
290 // Registry file. Opening may cause registry settings to change. Users still
291 // need to click through a prompt. So we could consider relaxing the
292 // DISALLOW_AUTO_OPEN restriction.
293 {"reg", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
295 // Microsoft Windows Explorer Command.
296 // See https://support.microsoft.com/kb/190355 for an example.
297 {"scf", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
299 // Microsoft Windows Screen Saver.
300 {"scr", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
302 // Microsoft Windows Script Component. Microsoft FoxPro Screen.
303 // A Script Component is a COM component created using script.
304 // See https://msdn.microsoft.com/library/aa233148.aspx for an example.
305 {"sct", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
307 // Microsoft Windows Shortcut into a document.
308 // See https://support.microsoft.com/kb/212344
309 {"shb", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
311 // Shell Scrap Object File.
312 {"shs", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
314 // System executable. Windows tries hard to prevent you from opening these
315 // types of files.
316 {"sys", DANGEROUS, DISALLOW_AUTO_OPEN},
318 // Internet Shortcut (new since IE9). Both .url and .website are .ini files
319 // that describe a shortcut that points to a URL. They can point at
320 // anything. Dropping a download of this type and opening it automatically
321 // can in effect sidestep origin restrictions etc.
322 {"url", DANGEROUS, DISALLOW_AUTO_OPEN},
323 {"website", DANGEROUS, DISALLOW_AUTO_OPEN},
325 // VBScript files. My open with Windows Script Host and execute with user
326 // privileges.
327 {"vb", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
328 {"vbe", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
329 {"vbs", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
331 {"vsd", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
333 // Microsoft Visual Studio Binary-based Macro Project.
334 {"vsmacros", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
336 {"vss", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
337 {"vst", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
339 // Microsoft Visio Workspace.
340 {"vsw", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
342 // Windows Script Host related.
343 {"ws", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
344 {"wsc", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
345 {"wsf", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
346 {"wsh", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
348 // XAML Browser Application.
349 {"xbap", DANGEROUS, DISALLOW_AUTO_OPEN},
351 // Microsoft Exchange Public Folder Shortcut.
352 {"xnk", ALLOW_ON_USER_GESTURE, ALLOW_AUTO_OPEN},
353 #endif // OS_WIN
355 // Java.
356 #if !defined(OS_CHROMEOS)
357 {"class", DANGEROUS, DISALLOW_AUTO_OPEN},
358 {"jar", DANGEROUS, DISALLOW_AUTO_OPEN},
359 {"jnlp", DANGEROUS, DISALLOW_AUTO_OPEN},
360 #endif
362 // Scripting languages. (Shells are handled below.)
363 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
364 {"pl", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
365 {"py", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
366 {"pyc", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
367 {"pyw", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
368 {"rb", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
369 #endif
371 // Shell languages. (OS_ANDROID is OS_POSIX.) OS_WIN shells are handled above.
372 #if defined(OS_POSIX)
373 {"bash", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
374 {"csh", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
375 {"ksh", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
376 {"sh", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
377 {"shar", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
378 {"tcsh", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
379 #endif
380 #if defined(OS_MACOSX)
381 {"command", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
382 #endif
384 // Package management formats. OS_WIN package formats are handled above.
385 #if defined(OS_MACOSX) || defined(OS_LINUX)
386 {"pkg", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
387 #endif
388 #if defined(OS_LINUX)
389 {"deb", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
390 {"rpm", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
391 #endif
392 #if defined(OS_ANDROID)
393 {"dex", ALLOW_ON_USER_GESTURE, DISALLOW_AUTO_OPEN},
394 #endif
397 // FileType for files with an empty extension.
398 const FileType kEmptyFileType = {nullptr, NOT_DANGEROUS, DISALLOW_AUTO_OPEN};
400 // Default FileType for non-empty extensions that aren't in the list above.
401 const FileType kUnknownFileType = {nullptr, NOT_DANGEROUS, ALLOW_AUTO_OPEN};
403 const FileType& GetFileType(const base::FilePath& path) {
404 base::FilePath::StringType extension(path.FinalExtension());
405 if (extension.empty())
406 return kEmptyFileType;
407 if (!base::IsStringASCII(extension))
408 return kUnknownFileType;
409 #if defined(OS_WIN)
410 std::string ascii_extension = base::UTF16ToASCII(extension);
411 #elif defined(OS_POSIX)
412 std::string ascii_extension = extension;
413 #endif
415 // Strip out leading dot if it's still there
416 if (ascii_extension[0] == base::FilePath::kExtensionSeparator)
417 ascii_extension.erase(0, 1);
419 for (const auto& file_type : kDownloadFileTypes) {
420 if (base::LowerCaseEqualsASCII(ascii_extension, file_type.extension))
421 return file_type;
424 return kUnknownFileType;
427 } // namespace
429 DownloadDangerLevel GetFileDangerLevel(const base::FilePath& path) {
430 return GetFileType(path).danger_level;
433 bool IsAllowedToOpenAutomatically(const base::FilePath& path) {
434 return GetFileType(path).auto_open_hint == ALLOW_AUTO_OPEN;
437 } // namespace download_util