| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2010 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/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@netscape.com> | 
|  | 41  *   Dean Tessman <dean_tessman@hotmail.com> | 
|  | 42  *   Brodie Thiesfield <brofield@jellycan.com> | 
|  | 43  *   Jungshik Shin <jshin@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   { "class", AllowOnUserGesture }, | 
|  | 64   { "htm", AllowOnUserGesture }, | 
|  | 65   { "html", AllowOnUserGesture }, | 
|  | 66   { "jar", AllowOnUserGesture }, | 
|  | 67   { "jnlp", AllowOnUserGesture }, | 
|  | 68   { "pdf", AllowOnUserGesture }, | 
|  | 69   { "pdfxml", AllowOnUserGesture }, | 
|  | 70   { "mars", AllowOnUserGesture }, | 
|  | 71   { "fdf", AllowOnUserGesture }, | 
|  | 72   { "xfdf", AllowOnUserGesture }, | 
|  | 73   { "xdp", AllowOnUserGesture }, | 
|  | 74   { "xfd", AllowOnUserGesture }, | 
|  | 75   { "pl", AllowOnUserGesture }, | 
|  | 76   { "py", AllowOnUserGesture }, | 
|  | 77   { "rb", AllowOnUserGesture }, | 
|  | 78   { "shtm", AllowOnUserGesture }, | 
|  | 79   { "shtml", AllowOnUserGesture }, | 
|  | 80   { "svg", AllowOnUserGesture }, | 
|  | 81   { "swf", AllowOnUserGesture }, | 
|  | 82   { "xht", AllowOnUserGesture }, | 
|  | 83   { "xhtm", AllowOnUserGesture }, | 
|  | 84   { "xhtml", AllowOnUserGesture }, | 
|  | 85   { "xml", AllowOnUserGesture }, | 
|  | 86   { "xsl", AllowOnUserGesture }, | 
|  | 87   { "xslt", AllowOnUserGesture }, | 
|  | 88 #if defined(OS_WIN) | 
|  | 89   { "ad", AllowOnUserGesture }, | 
|  | 90   { "ade", AllowOnUserGesture }, | 
|  | 91   { "adp", AllowOnUserGesture }, | 
|  | 92   { "app", AllowOnUserGesture }, | 
|  | 93   { "application", AllowOnUserGesture }, | 
|  | 94   { "asp", AllowOnUserGesture }, | 
|  | 95   { "asx", AllowOnUserGesture }, | 
|  | 96   { "bas", AllowOnUserGesture }, | 
|  | 97   { "bat", AllowOnUserGesture }, | 
|  | 98   { "chi", AllowOnUserGesture }, | 
|  | 99   { "chm", AllowOnUserGesture }, | 
|  | 100   { "cmd", AllowOnUserGesture }, | 
|  | 101   { "com", AllowOnUserGesture }, | 
|  | 102   { "cpl", AllowOnUserGesture }, | 
|  | 103   { "crt", AllowOnUserGesture }, | 
|  | 104   { "dll", Dangerous }, | 
|  | 105   { "drv", Dangerous }, | 
|  | 106   { "exe", AllowOnUserGesture }, | 
|  | 107   { "fxp", AllowOnUserGesture }, | 
|  | 108   { "hlp", AllowOnUserGesture }, | 
|  | 109   { "hta", AllowOnUserGesture }, | 
|  | 110   { "htt", AllowOnUserGesture }, | 
|  | 111   { "inf", AllowOnUserGesture }, | 
|  | 112   { "ins", AllowOnUserGesture }, | 
|  | 113   { "isp", AllowOnUserGesture }, | 
|  | 114   { "js", AllowOnUserGesture }, | 
|  | 115   { "jse", AllowOnUserGesture }, | 
|  | 116   { "lnk", AllowOnUserGesture }, | 
|  | 117   { "mad", AllowOnUserGesture }, | 
|  | 118   { "maf", AllowOnUserGesture }, | 
|  | 119   { "mag", AllowOnUserGesture }, | 
|  | 120   { "mam", AllowOnUserGesture }, | 
|  | 121   { "maq", AllowOnUserGesture }, | 
|  | 122   { "mar", AllowOnUserGesture }, | 
|  | 123   { "mas", AllowOnUserGesture }, | 
|  | 124   { "mat", AllowOnUserGesture }, | 
|  | 125   { "mau", AllowOnUserGesture }, | 
|  | 126   { "mav", AllowOnUserGesture }, | 
|  | 127   { "maw", AllowOnUserGesture }, | 
|  | 128   { "mda", AllowOnUserGesture }, | 
|  | 129   { "mdb", AllowOnUserGesture }, | 
|  | 130   { "mde", AllowOnUserGesture }, | 
|  | 131   { "mdt", AllowOnUserGesture }, | 
|  | 132   { "mdw", AllowOnUserGesture }, | 
|  | 133   { "mdz", AllowOnUserGesture }, | 
|  | 134   { "mht", AllowOnUserGesture }, | 
|  | 135   { "mhtml", AllowOnUserGesture }, | 
|  | 136   { "mmc", AllowOnUserGesture }, | 
|  | 137   { "msc", AllowOnUserGesture }, | 
|  | 138   { "msh", AllowOnUserGesture }, | 
|  | 139   { "mshxml", AllowOnUserGesture }, | 
|  | 140   { "msi", AllowOnUserGesture }, | 
|  | 141   { "msp", AllowOnUserGesture }, | 
|  | 142   { "mst", AllowOnUserGesture }, | 
|  | 143   { "ocx", AllowOnUserGesture }, | 
|  | 144   { "ops", AllowOnUserGesture }, | 
|  | 145   { "pcd", AllowOnUserGesture }, | 
|  | 146   { "pif", AllowOnUserGesture }, | 
|  | 147   { "plg", AllowOnUserGesture }, | 
|  | 148   { "prf", AllowOnUserGesture }, | 
|  | 149   { "prg", AllowOnUserGesture }, | 
|  | 150   { "pst", AllowOnUserGesture }, | 
|  | 151   { "reg", AllowOnUserGesture }, | 
|  | 152   { "scf", AllowOnUserGesture }, | 
|  | 153   { "scr", AllowOnUserGesture }, | 
|  | 154   { "sct", AllowOnUserGesture }, | 
|  | 155   { "shb", AllowOnUserGesture }, | 
|  | 156   { "shs", AllowOnUserGesture }, | 
|  | 157   { "sys", Dangerous }, | 
|  | 158   { "url", AllowOnUserGesture }, | 
|  | 159   { "vb", AllowOnUserGesture }, | 
|  | 160   { "vbe", AllowOnUserGesture }, | 
|  | 161   { "vbs", AllowOnUserGesture }, | 
|  | 162   { "vsd", AllowOnUserGesture }, | 
|  | 163   { "vsmacros", AllowOnUserGesture }, | 
|  | 164   { "vss", AllowOnUserGesture }, | 
|  | 165   { "vst", AllowOnUserGesture }, | 
|  | 166   { "vsw", AllowOnUserGesture }, | 
|  | 167   { "ws", AllowOnUserGesture }, | 
|  | 168   { "wsc", AllowOnUserGesture }, | 
|  | 169   { "wsf", AllowOnUserGesture }, | 
|  | 170   { "wsh", AllowOnUserGesture }, | 
|  | 171   { "xbap", Dangerous }, | 
|  | 172 #elif defined(OS_MACOSX) | 
|  | 173   // TODO(thakis): Figure out what makes sense here -- crbug.com/19096 | 
|  | 174   { "app", AllowOnUserGesture }, | 
|  | 175   { "dmg", AllowOnUserGesture }, | 
|  | 176 #elif defined(OS_POSIX) | 
|  | 177   // TODO(estade): lengthen this list. | 
|  | 178   { "bash", AllowOnUserGesture }, | 
|  | 179   { "csh", AllowOnUserGesture }, | 
|  | 180   { "deb", AllowOnUserGesture }, | 
|  | 181   { "exe", AllowOnUserGesture }, | 
|  | 182   { "ksh", AllowOnUserGesture }, | 
|  | 183   { "rpm", AllowOnUserGesture }, | 
|  | 184   { "sh", AllowOnUserGesture }, | 
|  | 185   { "tcsh", AllowOnUserGesture }, | 
|  | 186 #endif | 
|  | 187 }; | 
|  | 188 | 
|  | 189 DownloadDangerLevel GetFileDangerLevel(const FilePath& path) { | 
|  | 190   return GetFileExtensionDangerLevel(path.Extension()); | 
|  | 191 } | 
|  | 192 | 
|  | 193 DownloadDangerLevel GetFileExtensionDangerLevel( | 
|  | 194     const FilePath::StringType& extension) { | 
|  | 195   if (extension.empty()) | 
|  | 196     return NotDangerous; | 
|  | 197   if (!IsStringASCII(extension)) | 
|  | 198     return NotDangerous; | 
|  | 199 #if defined(OS_WIN) | 
|  | 200   std::string ascii_extension = WideToASCII(extension); | 
|  | 201 #elif defined(OS_POSIX) | 
|  | 202   std::string ascii_extension = extension; | 
|  | 203 #endif | 
|  | 204 | 
|  | 205   // Strip out leading dot if it's still there | 
|  | 206   if (ascii_extension[0] == FilePath::kExtensionSeparator) | 
|  | 207     ascii_extension.erase(0, 1); | 
|  | 208 | 
|  | 209   for (size_t i = 0; i < arraysize(g_executables); ++i) { | 
|  | 210     if (LowerCaseEqualsASCII(ascii_extension, g_executables[i].extension)) | 
|  | 211       return g_executables[i].level; | 
|  | 212   } | 
|  | 213   return NotDangerous; | 
|  | 214 } | 
|  | 215 | 
|  | 216 bool IsFileExtensionSafe(const FilePath::StringType& extension) { | 
|  | 217   return GetFileExtensionDangerLevel(extension) == NotDangerous; | 
|  | 218 } | 
|  | 219 | 
|  | 220 bool IsFileSafe(const FilePath& path) { | 
|  | 221   return GetFileDangerLevel(path) == NotDangerous; | 
|  | 222 } | 
|  | 223 | 
|  | 224 static const char* kExecutableWhiteList[] = { | 
|  | 225   // JavaScript is just as powerful as EXE. | 
|  | 226   "text/javascript", | 
|  | 227   "text/javascript;version=*", | 
|  | 228   "text/html", | 
|  | 229   // Registry files can cause critical changes to the MS OS behavior. | 
|  | 230   // Addition of this mimetype also addresses bug 7337. | 
|  | 231   "text/x-registry", | 
|  | 232   "text/x-sh", | 
|  | 233   // Some sites use binary/octet-stream to mean application/octet-stream. | 
|  | 234   // See http://code.google.com/p/chromium/issues/detail?id=1573 | 
|  | 235   "binary/octet-stream" | 
|  | 236 }; | 
|  | 237 | 
|  | 238 static const char* kExecutableBlackList[] = { | 
|  | 239   // These application types are not executable. | 
|  | 240   "application/*+xml", | 
|  | 241   "application/xml" | 
|  | 242 }; | 
|  | 243 | 
|  | 244 bool IsExecutableMimeType(const std::string& mime_type) { | 
|  | 245   for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) { | 
|  | 246     if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type)) | 
|  | 247       return true; | 
|  | 248   } | 
|  | 249   for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) { | 
|  | 250     if (net::MatchesMimeType(kExecutableBlackList[i], mime_type)) | 
|  | 251       return false; | 
|  | 252   } | 
|  | 253   // We consider only other application types to be executable. | 
|  | 254   return net::MatchesMimeType("application/*", mime_type); | 
|  | 255 } | 
|  | 256 | 
|  | 257 | 
|  | 258 }  // namespace download_util | 
| OLD | NEW | 
|---|