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 "net/base/mime_util.h"
     12 #include "net/base/net_util.h"
     13 
     14 namespace download_util {
     15 
     16 // For file extensions taken from mozilla:
     17 
     18 /* ***** BEGIN LICENSE BLOCK *****
     19  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     20  *
     21  * The contents of this file are subject to the Mozilla Public License Version
     22  * 1.1 (the "License"); you may not use this file except in compliance with
     23  * the License. You may obtain a copy of the License at
     24  * http://www.mozilla.org/MPL/
     25  *
     26  * Software distributed under the License is distributed on an "AS IS" basis,
     27  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     28  * for the specific language governing rights and limitations under the
     29  * License.
     30  *
     31  * The Original Code is Mozilla Communicator client code, released
     32  * March 31, 1998.
     33  *
     34  * The Initial Developer of the Original Code is
     35  * Netscape Communications Corporation.
     36  * Portions created by the Initial Developer are Copyright (C) 1998-1999
     37  * the Initial Developer. All Rights Reserved.
     38  *
     39  * Contributor(s):
     40  *   Doug Turner <dougt (at) netscape.com>
     41  *   Dean Tessman <dean_tessman (at) hotmail.com>
     42  *   Brodie Thiesfield <brofield (at) jellycan.com>
     43  *   Jungshik Shin <jshin (at) i18nl10n.com>
     44  *
     45  * Alternatively, the contents of this file may be used under the terms of
     46  * either of the GNU General Public License Version 2 or later (the "GPL"),
     47  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
     48  * in which case the provisions of the GPL or the LGPL are applicable instead
     49  * of those above. If you wish to allow use of your version of this file only
     50  * under the terms of either the GPL or the LGPL, and not to allow others to
     51  * use your version of this file under the terms of the MPL, indicate your
     52  * decision by deleting the provisions above and replace them with the notice
     53  * and other provisions required by the GPL or the LGPL. If you do not delete
     54  * the provisions above, a recipient may use your version of this file under
     55  * the terms of any one of the MPL, the GPL or the LGPL.
     56  *
     57  * ***** END LICENSE BLOCK ***** */
     58 
     59 static const struct Executables {
     60     const char* extension;
     61     DownloadDangerLevel level;
     62 } g_executables[] = {
     63 // Chrome OS does not suffer from some of the problems of older OS'es.
     64 #if !defined(OS_CHROMEOS)
     65   // Relating to Java.
     66   { "class", DANGEROUS },
     67   { "jar", DANGEROUS },
     68   { "jnlp", DANGEROUS },
     69 #endif
     70 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
     71   // Relating to PDF.
     72   { "pdf", ALLOW_ON_USER_GESTURE },
     73   { "pdfxml", ALLOW_ON_USER_GESTURE },
     74   { "mars", ALLOW_ON_USER_GESTURE },
     75   { "fdf", ALLOW_ON_USER_GESTURE },
     76   { "xfdf", ALLOW_ON_USER_GESTURE },
     77   { "xdp", ALLOW_ON_USER_GESTURE },
     78   { "xfd", ALLOW_ON_USER_GESTURE },
     79   // Relating to scripting languages.
     80   { "pl", ALLOW_ON_USER_GESTURE },
     81   { "py", ALLOW_ON_USER_GESTURE },
     82   { "pyc", ALLOW_ON_USER_GESTURE },
     83   { "pyw", ALLOW_ON_USER_GESTURE },
     84   { "rb", ALLOW_ON_USER_GESTURE },
     85 #endif
     86   // Some files are dangerous on all platforms.
     87   // Flash files downloaded locally can sometimes access the local filesystem.
     88   { "swf", ALLOW_ON_USER_GESTURE },
     89   // Chrome extensions should be obtained through the web store.
     90   { "crx", ALLOW_ON_USER_GESTURE },
     91   // These types can run JavaScript (e.g. HTML and HTML-like).
     92   // TODO(cevans): work out whether our restrictions on file:/// are strong
     93   // enough to mark these types as no longer dangerous.
     94   { "htm", ALLOW_ON_USER_GESTURE },
     95   { "html", ALLOW_ON_USER_GESTURE },
     96   { "shtm", ALLOW_ON_USER_GESTURE },
     97   { "shtml", ALLOW_ON_USER_GESTURE },
     98   { "svg", ALLOW_ON_USER_GESTURE },
     99   { "xht", ALLOW_ON_USER_GESTURE },
    100   { "xhtm", ALLOW_ON_USER_GESTURE },
    101   { "xhtml", ALLOW_ON_USER_GESTURE },
    102   { "xml", ALLOW_ON_USER_GESTURE },
    103   { "xsl", ALLOW_ON_USER_GESTURE },
    104   { "xslt", ALLOW_ON_USER_GESTURE },
    105 #if defined(OS_WIN)
    106   { "ad", ALLOW_ON_USER_GESTURE },
    107   { "ade", ALLOW_ON_USER_GESTURE },
    108   { "adp", ALLOW_ON_USER_GESTURE },
    109   { "app", ALLOW_ON_USER_GESTURE },
    110   { "application", ALLOW_ON_USER_GESTURE },
    111   { "asp", ALLOW_ON_USER_GESTURE },
    112   { "asx", ALLOW_ON_USER_GESTURE },
    113   { "bas", ALLOW_ON_USER_GESTURE },
    114   { "bat", ALLOW_ON_USER_GESTURE },
    115   { "cfg", DANGEROUS },
    116   { "chi", ALLOW_ON_USER_GESTURE },
    117   { "chm", ALLOW_ON_USER_GESTURE },
    118   { "cmd", ALLOW_ON_USER_GESTURE },
    119   { "com", ALLOW_ON_USER_GESTURE },
    120   { "cpl", ALLOW_ON_USER_GESTURE },
    121   { "crt", ALLOW_ON_USER_GESTURE },
    122   { "dll", DANGEROUS },
    123   { "drv", DANGEROUS },
    124   { "exe", ALLOW_ON_USER_GESTURE },
    125   { "fxp", ALLOW_ON_USER_GESTURE },
    126   { "grp", DANGEROUS },
    127   { "hlp", ALLOW_ON_USER_GESTURE },
    128   { "hta", ALLOW_ON_USER_GESTURE },
    129   { "htt", ALLOW_ON_USER_GESTURE },
    130   { "inf", ALLOW_ON_USER_GESTURE },
    131   { "ini", DANGEROUS },
    132   { "ins", ALLOW_ON_USER_GESTURE },
    133   { "isp", ALLOW_ON_USER_GESTURE },
    134   { "js", ALLOW_ON_USER_GESTURE },
    135   { "jse", ALLOW_ON_USER_GESTURE },
    136   { "lnk", ALLOW_ON_USER_GESTURE },
    137   { "local", DANGEROUS },
    138   { "mad", ALLOW_ON_USER_GESTURE },
    139   { "maf", ALLOW_ON_USER_GESTURE },
    140   { "mag", ALLOW_ON_USER_GESTURE },
    141   { "mam", ALLOW_ON_USER_GESTURE },
    142   { "manifest", DANGEROUS },
    143   { "maq", ALLOW_ON_USER_GESTURE },
    144   { "mar", ALLOW_ON_USER_GESTURE },
    145   { "mas", ALLOW_ON_USER_GESTURE },
    146   { "mat", ALLOW_ON_USER_GESTURE },
    147   { "mau", ALLOW_ON_USER_GESTURE },
    148   { "mav", ALLOW_ON_USER_GESTURE },
    149   { "maw", ALLOW_ON_USER_GESTURE },
    150   { "mda", ALLOW_ON_USER_GESTURE },
    151   { "mdb", ALLOW_ON_USER_GESTURE },
    152   { "mde", ALLOW_ON_USER_GESTURE },
    153   { "mdt", ALLOW_ON_USER_GESTURE },
    154   { "mdw", ALLOW_ON_USER_GESTURE },
    155   { "mdz", ALLOW_ON_USER_GESTURE },
    156   { "mht", ALLOW_ON_USER_GESTURE },
    157   { "mhtml", ALLOW_ON_USER_GESTURE },
    158   { "mmc", ALLOW_ON_USER_GESTURE },
    159   { "mof", DANGEROUS },
    160   { "msc", ALLOW_ON_USER_GESTURE },
    161   { "msh", ALLOW_ON_USER_GESTURE },
    162   { "mshxml", ALLOW_ON_USER_GESTURE },
    163   { "msi", ALLOW_ON_USER_GESTURE },
    164   { "msp", ALLOW_ON_USER_GESTURE },
    165   { "mst", ALLOW_ON_USER_GESTURE },
    166   { "ocx", DANGEROUS },
    167   { "ops", ALLOW_ON_USER_GESTURE },
    168   { "pcd", ALLOW_ON_USER_GESTURE },
    169   { "pif", ALLOW_ON_USER_GESTURE },
    170   { "plg", ALLOW_ON_USER_GESTURE },
    171   { "prf", ALLOW_ON_USER_GESTURE },
    172   { "prg", ALLOW_ON_USER_GESTURE },
    173   { "pst", ALLOW_ON_USER_GESTURE },
    174   { "reg", ALLOW_ON_USER_GESTURE },
    175   { "scf", ALLOW_ON_USER_GESTURE },
    176   { "scr", ALLOW_ON_USER_GESTURE },
    177   { "sct", ALLOW_ON_USER_GESTURE },
    178   { "shb", ALLOW_ON_USER_GESTURE },
    179   { "shs", ALLOW_ON_USER_GESTURE },
    180   { "sys", DANGEROUS },
    181   { "url", ALLOW_ON_USER_GESTURE },
    182   // TODO(davidben): Remove this when double-extensions are no longer
    183   // a nuisance.
    184   { "user.js", ALLOW_ON_USER_GESTURE },
    185   { "vb", ALLOW_ON_USER_GESTURE },
    186   { "vbe", ALLOW_ON_USER_GESTURE },
    187   { "vbs", ALLOW_ON_USER_GESTURE },
    188   { "vsd", ALLOW_ON_USER_GESTURE },
    189   { "vsmacros", ALLOW_ON_USER_GESTURE },
    190   { "vss", ALLOW_ON_USER_GESTURE },
    191   { "vst", ALLOW_ON_USER_GESTURE },
    192   { "vsw", ALLOW_ON_USER_GESTURE },
    193   { "ws", ALLOW_ON_USER_GESTURE },
    194   { "wsc", ALLOW_ON_USER_GESTURE },
    195   { "wsf", ALLOW_ON_USER_GESTURE },
    196   { "wsh", ALLOW_ON_USER_GESTURE },
    197   { "xbap", DANGEROUS },
    198 #elif defined(OS_MACOSX)
    199   { "bash", ALLOW_ON_USER_GESTURE },
    200   { "command", ALLOW_ON_USER_GESTURE },
    201   { "csh", ALLOW_ON_USER_GESTURE },
    202   { "ksh", ALLOW_ON_USER_GESTURE },
    203   { "pkg", ALLOW_ON_USER_GESTURE },
    204   { "sh", ALLOW_ON_USER_GESTURE },
    205   { "shar", ALLOW_ON_USER_GESTURE },
    206   { "tcsh", ALLOW_ON_USER_GESTURE },
    207 #elif defined(OS_ANDROID)
    208   { "apk", ALLOW_ON_USER_GESTURE },
    209   { "sh", ALLOW_ON_USER_GESTURE },
    210   { "shar", ALLOW_ON_USER_GESTURE },
    211   { "dex", ALLOW_ON_USER_GESTURE },
    212 #elif defined(OS_POSIX)
    213   // TODO(estade): lengthen this list.
    214   { "bash", ALLOW_ON_USER_GESTURE },
    215   { "csh", ALLOW_ON_USER_GESTURE },
    216   { "deb", ALLOW_ON_USER_GESTURE },
    217   { "exe", ALLOW_ON_USER_GESTURE },
    218   { "ksh", ALLOW_ON_USER_GESTURE },
    219   { "rpm", ALLOW_ON_USER_GESTURE },
    220   { "sh", ALLOW_ON_USER_GESTURE },
    221   { "shar", ALLOW_ON_USER_GESTURE },
    222   { "tcsh", ALLOW_ON_USER_GESTURE },
    223 #endif
    224 };
    225 
    226 DownloadDangerLevel GetFileDangerLevel(const base::FilePath& path) {
    227   base::FilePath::StringType extension(path.Extension());
    228   if (extension.empty())
    229     return NOT_DANGEROUS;
    230   if (!IsStringASCII(extension))
    231     return NOT_DANGEROUS;
    232 #if defined(OS_WIN)
    233   std::string ascii_extension = WideToASCII(extension);
    234 #elif defined(OS_POSIX)
    235   std::string ascii_extension = extension;
    236 #endif
    237 
    238   // Strip out leading dot if it's still there
    239   if (ascii_extension[0] == base::FilePath::kExtensionSeparator)
    240     ascii_extension.erase(0, 1);
    241 
    242   for (size_t i = 0; i < arraysize(g_executables); ++i) {
    243     if (LowerCaseEqualsASCII(ascii_extension, g_executables[i].extension))
    244       return g_executables[i].level;
    245   }
    246   return NOT_DANGEROUS;
    247 }
    248 
    249 static const char* kExecutableWhiteList[] = {
    250   // JavaScript is just as powerful as EXE.
    251   "text/javascript",
    252   "text/javascript;version=*",
    253   "text/html",
    254   // Registry files can cause critical changes to the MS OS behavior.
    255   // Addition of this mimetype also addresses bug 7337.
    256   "text/x-registry",
    257   "text/x-sh",
    258   // Some sites use binary/octet-stream to mean application/octet-stream.
    259   // See http://code.google.com/p/chromium/issues/detail?id=1573
    260   "binary/octet-stream"
    261 };
    262 
    263 static const char* kExecutableBlackList[] = {
    264   // These application types are not executable.
    265   "application/*+xml",
    266   "application/xml"
    267 };
    268 
    269 bool IsExecutableMimeType(const std::string& mime_type) {
    270   for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) {
    271     if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type))
    272       return true;
    273   }
    274   for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) {
    275     if (net::MatchesMimeType(kExecutableBlackList[i], mime_type))
    276       return false;
    277   }
    278   // We consider only other application types to be executable.
    279   return net::MatchesMimeType("application/*", mime_type);
    280 }
    281 
    282 
    283 }  // namespace download_util
    284