Home | History | Annotate | Download | only in download
      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 
     15 namespace 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 (at) netscape.com>
     42  *   Dean Tessman <dean_tessman (at) hotmail.com>
     43  *   Brodie Thiesfield <brofield (at) jellycan.com>
     44  *   Jungshik Shin <jshin (at) 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 
     60 static 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   { "apk", ALLOW_ON_USER_GESTURE },
    204   { "dex", ALLOW_ON_USER_GESTURE },  // Really an executable format.
    205 #endif
    206 };
    207 
    208 DownloadDangerLevel GetFileDangerLevel(const base::FilePath& path) {
    209   base::FilePath::StringType extension(path.FinalExtension());
    210   if (extension.empty())
    211     return NOT_DANGEROUS;
    212   if (!base::IsStringASCII(extension))
    213     return NOT_DANGEROUS;
    214 #if defined(OS_WIN)
    215   std::string ascii_extension = base::UTF16ToASCII(extension);
    216 #elif defined(OS_POSIX)
    217   std::string ascii_extension = extension;
    218 #endif
    219 
    220   // Strip out leading dot if it's still there
    221   if (ascii_extension[0] == base::FilePath::kExtensionSeparator)
    222     ascii_extension.erase(0, 1);
    223 
    224   for (size_t i = 0; i < arraysize(g_executables); ++i) {
    225     if (LowerCaseEqualsASCII(ascii_extension, g_executables[i].extension))
    226       return g_executables[i].level;
    227   }
    228   return NOT_DANGEROUS;
    229 }
    230 
    231 static const char* kExecutableWhiteList[] = {
    232   // JavaScript is just as powerful as EXE.
    233   "text/javascript",
    234   "text/javascript;version=*",
    235   "text/html",
    236   // Registry files can cause critical changes to the MS OS behavior.
    237   // Addition of this mimetype also addresses bug 7337.
    238   "text/x-registry",
    239   "text/x-sh",
    240   // Some sites use binary/octet-stream to mean application/octet-stream.
    241   // See http://code.google.com/p/chromium/issues/detail?id=1573
    242   "binary/octet-stream"
    243 };
    244 
    245 static const char* kExecutableBlackList[] = {
    246   // These application types are not executable.
    247   "application/*+xml",
    248   "application/xml"
    249 };
    250 
    251 bool IsExecutableMimeType(const std::string& mime_type) {
    252   for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) {
    253     if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type))
    254       return true;
    255   }
    256   for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) {
    257     if (net::MatchesMimeType(kExecutableBlackList[i], mime_type))
    258       return false;
    259   }
    260   // We consider only other application types to be executable.
    261   return net::MatchesMimeType("application/*", mime_type);
    262 }
    263 
    264 
    265 }  // namespace download_util
    266