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.
4
5#include <set>
6#include <string>
7
8#include "chrome/browser/download/download_extensions.h"
9
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"
14
15namespace download_util {
16
17// For file extensions taken from mozilla:
18
19/* ***** BEGIN LICENSE BLOCK *****
20 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
21 *
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/
26 *
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.
31 *
32 * The Original Code is Mozilla Communicator client code, released
33 * March 31, 1998.
34 *
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.
39 *
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>
45 *
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.
57 *
58 * ***** END LICENSE BLOCK ***** */
59
60static const struct Executables {
61    const char* extension;
62    DownloadDangerLevel level;
63} g_executables[] = {
64  // Some files are dangerous on all platforms.
65  //
66  // Flash files downloaded locally can sometimes access the local filesystem.
67  { "swf", DANGEROUS },
68  { "spl", DANGEROUS },
69  // Chrome extensions should be obtained through the web store.
70  { "crx", ALLOW_ON_USER_GESTURE },
71
72  // Windows, all file categories.
73#if defined(OS_WIN)
74  { "ad", ALLOW_ON_USER_GESTURE },
75  { "ade", ALLOW_ON_USER_GESTURE },
76  { "adp", ALLOW_ON_USER_GESTURE },
77  { "app", ALLOW_ON_USER_GESTURE },
78  { "application", ALLOW_ON_USER_GESTURE },
79  { "asp", ALLOW_ON_USER_GESTURE },
80  { "asx", ALLOW_ON_USER_GESTURE },
81  { "bas", ALLOW_ON_USER_GESTURE },
82  { "bat", ALLOW_ON_USER_GESTURE },
83  { "cfg", DANGEROUS },
84  { "chi", ALLOW_ON_USER_GESTURE },
85  { "chm", ALLOW_ON_USER_GESTURE },
86  { "cmd", ALLOW_ON_USER_GESTURE },
87  { "com", ALLOW_ON_USER_GESTURE },
88  { "cpl", ALLOW_ON_USER_GESTURE },
89  { "crt", ALLOW_ON_USER_GESTURE },
90  { "dll", DANGEROUS },
91  { "drv", DANGEROUS },
92  { "exe", ALLOW_ON_USER_GESTURE },
93  { "fxp", ALLOW_ON_USER_GESTURE },
94  { "grp", DANGEROUS },
95  { "hlp", ALLOW_ON_USER_GESTURE },
96  { "hta", ALLOW_ON_USER_GESTURE },
97  { "htt", ALLOW_ON_USER_GESTURE },
98  { "inf", ALLOW_ON_USER_GESTURE },
99  { "ini", DANGEROUS },
100  { "ins", ALLOW_ON_USER_GESTURE },
101  { "isp", ALLOW_ON_USER_GESTURE },
102  { "js", ALLOW_ON_USER_GESTURE },
103  { "jse", ALLOW_ON_USER_GESTURE },
104  { "lnk", ALLOW_ON_USER_GESTURE },
105  { "local", DANGEROUS },
106  { "mad", ALLOW_ON_USER_GESTURE },
107  { "maf", ALLOW_ON_USER_GESTURE },
108  { "mag", ALLOW_ON_USER_GESTURE },
109  { "mam", ALLOW_ON_USER_GESTURE },
110  { "manifest", DANGEROUS },
111  { "maq", ALLOW_ON_USER_GESTURE },
112  { "mar", ALLOW_ON_USER_GESTURE },
113  { "mas", ALLOW_ON_USER_GESTURE },
114  { "mat", ALLOW_ON_USER_GESTURE },
115  { "mau", ALLOW_ON_USER_GESTURE },
116  { "mav", ALLOW_ON_USER_GESTURE },
117  { "maw", ALLOW_ON_USER_GESTURE },
118  { "mda", ALLOW_ON_USER_GESTURE },
119  { "mdb", ALLOW_ON_USER_GESTURE },
120  { "mde", ALLOW_ON_USER_GESTURE },
121  { "mdt", ALLOW_ON_USER_GESTURE },
122  { "mdw", ALLOW_ON_USER_GESTURE },
123  { "mdz", ALLOW_ON_USER_GESTURE },
124  { "mht", ALLOW_ON_USER_GESTURE },
125  { "mhtml", ALLOW_ON_USER_GESTURE },
126  { "mmc", ALLOW_ON_USER_GESTURE },
127  { "mof", DANGEROUS },
128  { "msc", ALLOW_ON_USER_GESTURE },
129  { "msh", ALLOW_ON_USER_GESTURE },
130  { "mshxml", ALLOW_ON_USER_GESTURE },
131  { "msi", ALLOW_ON_USER_GESTURE },
132  { "msp", ALLOW_ON_USER_GESTURE },
133  { "mst", ALLOW_ON_USER_GESTURE },
134  { "ocx", DANGEROUS },
135  { "ops", ALLOW_ON_USER_GESTURE },
136  { "pcd", ALLOW_ON_USER_GESTURE },
137  { "pif", ALLOW_ON_USER_GESTURE },
138  { "plg", ALLOW_ON_USER_GESTURE },
139  { "prf", ALLOW_ON_USER_GESTURE },
140  { "prg", ALLOW_ON_USER_GESTURE },
141  { "pst", ALLOW_ON_USER_GESTURE },
142  { "reg", ALLOW_ON_USER_GESTURE },
143  { "scf", ALLOW_ON_USER_GESTURE },
144  { "scr", ALLOW_ON_USER_GESTURE },
145  { "sct", ALLOW_ON_USER_GESTURE },
146  { "shb", ALLOW_ON_USER_GESTURE },
147  { "shs", ALLOW_ON_USER_GESTURE },
148  { "sys", DANGEROUS },
149  { "url", DANGEROUS },
150  { "vb", ALLOW_ON_USER_GESTURE },
151  { "vbe", ALLOW_ON_USER_GESTURE },
152  { "vbs", ALLOW_ON_USER_GESTURE },
153  { "vsd", ALLOW_ON_USER_GESTURE },
154  { "vsmacros", ALLOW_ON_USER_GESTURE },
155  { "vss", ALLOW_ON_USER_GESTURE },
156  { "vst", ALLOW_ON_USER_GESTURE },
157  { "vsw", ALLOW_ON_USER_GESTURE },
158  { "ws", ALLOW_ON_USER_GESTURE },
159  { "wsc", ALLOW_ON_USER_GESTURE },
160  { "wsf", ALLOW_ON_USER_GESTURE },
161  { "wsh", ALLOW_ON_USER_GESTURE },
162  { "xbap", DANGEROUS },
163#endif  // OS_WIN
164
165  // Java.
166#if !defined(OS_CHROMEOS)
167  { "class", DANGEROUS },
168  { "jar", DANGEROUS },
169  { "jnlp", DANGEROUS },
170#endif
171
172  // Scripting languages. (Shells are handled below.)
173#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
174  { "pl", ALLOW_ON_USER_GESTURE },
175  { "py", ALLOW_ON_USER_GESTURE },
176  { "pyc", ALLOW_ON_USER_GESTURE },
177  { "pyw", ALLOW_ON_USER_GESTURE },
178  { "rb", ALLOW_ON_USER_GESTURE },
179#endif
180
181  // Shell languages. (OS_ANDROID is OS_POSIX.) OS_WIN shells are handled above.
182#if defined(OS_POSIX)
183  { "bash", ALLOW_ON_USER_GESTURE },
184  { "csh", ALLOW_ON_USER_GESTURE },
185  { "ksh", ALLOW_ON_USER_GESTURE },
186  { "sh", ALLOW_ON_USER_GESTURE },
187  { "shar", ALLOW_ON_USER_GESTURE },
188  { "tcsh", ALLOW_ON_USER_GESTURE },
189#endif
190#if defined(OS_MACOSX)
191  { "command", ALLOW_ON_USER_GESTURE },
192#endif
193
194  // Package management formats. OS_WIN package formats are handled above.
195#if defined(OS_MACOSX) || defined(OS_LINUX)
196  { "pkg", ALLOW_ON_USER_GESTURE },
197#endif
198#if defined(OS_LINUX)
199  { "deb", ALLOW_ON_USER_GESTURE },
200  { "rpm", ALLOW_ON_USER_GESTURE },
201#endif
202#if defined(OS_ANDROID)
203  { "dex", ALLOW_ON_USER_GESTURE },  // Really an executable format.
204#endif
205};
206
207DownloadDangerLevel GetFileDangerLevel(const base::FilePath& path) {
208  base::FilePath::StringType extension(path.FinalExtension());
209  if (extension.empty())
210    return NOT_DANGEROUS;
211  if (!base::IsStringASCII(extension))
212    return NOT_DANGEROUS;
213#if defined(OS_WIN)
214  std::string ascii_extension = base::UTF16ToASCII(extension);
215#elif defined(OS_POSIX)
216  std::string ascii_extension = extension;
217#endif
218
219  // Strip out leading dot if it's still there
220  if (ascii_extension[0] == base::FilePath::kExtensionSeparator)
221    ascii_extension.erase(0, 1);
222
223  for (size_t i = 0; i < arraysize(g_executables); ++i) {
224    if (LowerCaseEqualsASCII(ascii_extension, g_executables[i].extension))
225      return g_executables[i].level;
226  }
227  return NOT_DANGEROUS;
228}
229
230static const char* kExecutableWhiteList[] = {
231  // JavaScript is just as powerful as EXE.
232  "text/javascript",
233  "text/javascript;version=*",
234  "text/html",
235  // Registry files can cause critical changes to the MS OS behavior.
236  // Addition of this mimetype also addresses bug 7337.
237  "text/x-registry",
238  "text/x-sh",
239  // Some sites use binary/octet-stream to mean application/octet-stream.
240  // See http://code.google.com/p/chromium/issues/detail?id=1573
241  "binary/octet-stream"
242};
243
244static const char* kExecutableBlackList[] = {
245  // These application types are not executable.
246  "application/*+xml",
247  "application/xml"
248};
249
250bool IsExecutableMimeType(const std::string& mime_type) {
251  for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) {
252    if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type))
253      return true;
254  }
255  for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) {
256    if (net::MatchesMimeType(kExecutableBlackList[i], mime_type))
257      return false;
258  }
259  // We consider only other application types to be executable.
260  return net::MatchesMimeType("application/*", mime_type);
261}
262
263
264}  // namespace download_util
265