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