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.
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
32 * The Original Code is Mozilla Communicator client code, released
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.
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 static const struct Executables
{
61 const char* extension
;
62 DownloadDangerLevel level
;
64 // Some files are dangerous on all platforms.
66 // Flash files downloaded locally can sometimes access the local filesystem.
69 // Chrome extensions should be obtained through the web store.
70 { "crx", ALLOW_ON_USER_GESTURE
},
72 // Windows, all file categories.
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
},
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
},
92 { "exe", ALLOW_ON_USER_GESTURE
},
93 { "fxp", ALLOW_ON_USER_GESTURE
},
95 { "hlp", ALLOW_ON_USER_GESTURE
},
96 { "hta", ALLOW_ON_USER_GESTURE
},
97 { "htt", ALLOW_ON_USER_GESTURE
},
98 { "inf", ALLOW_ON_USER_GESTURE
},
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
},
166 #if !defined(OS_CHROMEOS)
167 { "class", DANGEROUS
},
168 { "jar", DANGEROUS
},
169 { "jnlp", DANGEROUS
},
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
},
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
},
190 #if defined(OS_MACOSX)
191 { "command", ALLOW_ON_USER_GESTURE
},
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
},
198 #if defined(OS_LINUX)
199 { "deb", ALLOW_ON_USER_GESTURE
},
200 { "rpm", ALLOW_ON_USER_GESTURE
},
202 #if defined(OS_ANDROID)
203 { "dex", ALLOW_ON_USER_GESTURE
}, // Really an executable format.
207 DownloadDangerLevel
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
;
214 std::string ascii_extension
= base::UTF16ToASCII(extension
);
215 #elif defined(OS_POSIX)
216 std::string ascii_extension
= extension
;
219 // Strip out leading dot if it's still there
220 if (ascii_extension
[0] == base::FilePath::kExtensionSeparator
)
221 ascii_extension
.erase(0, 1);
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
;
227 return NOT_DANGEROUS
;
230 static const char* kExecutableWhiteList
[] = {
231 // JavaScript is just as powerful as EXE.
233 "text/javascript;version=*",
235 // Registry files can cause critical changes to the MS OS behavior.
236 // Addition of this mimetype also addresses bug 7337.
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"
244 static const char* kExecutableBlackList
[] = {
245 // These application types are not executable.
250 bool IsExecutableMimeType(const std::string
& mime_type
) {
251 for (size_t i
= 0; i
< arraysize(kExecutableWhiteList
); ++i
) {
252 if (net::MatchesMimeType(kExecutableWhiteList
[i
], mime_type
))
255 for (size_t i
= 0; i
< arraysize(kExecutableBlackList
); ++i
) {
256 if (net::MatchesMimeType(kExecutableBlackList
[i
], mime_type
))
259 // We consider only other application types to be executable.
260 return net::MatchesMimeType("application/*", mime_type
);
264 } // namespace download_util