This source file includes following definitions.
- GetFileDangerLevel
- IsExecutableMimeType
#include <set>
#include <string>
#include "chrome/browser/download/download_extensions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "net/base/mime_util.h"
#include "net/base/net_util.h"
namespace download_util {
static const struct Executables {
const char* extension;
DownloadDangerLevel level;
} g_executables[] = {
{ "swf", DANGEROUS },
{ "spl", DANGEROUS },
{ "crx", ALLOW_ON_USER_GESTURE },
#if defined(OS_WIN)
{ "ad", ALLOW_ON_USER_GESTURE },
{ "ade", ALLOW_ON_USER_GESTURE },
{ "adp", ALLOW_ON_USER_GESTURE },
{ "app", ALLOW_ON_USER_GESTURE },
{ "application", ALLOW_ON_USER_GESTURE },
{ "asp", ALLOW_ON_USER_GESTURE },
{ "asx", ALLOW_ON_USER_GESTURE },
{ "bas", ALLOW_ON_USER_GESTURE },
{ "bat", ALLOW_ON_USER_GESTURE },
{ "cfg", DANGEROUS },
{ "chi", ALLOW_ON_USER_GESTURE },
{ "chm", ALLOW_ON_USER_GESTURE },
{ "cmd", ALLOW_ON_USER_GESTURE },
{ "com", ALLOW_ON_USER_GESTURE },
{ "cpl", ALLOW_ON_USER_GESTURE },
{ "crt", ALLOW_ON_USER_GESTURE },
{ "dll", DANGEROUS },
{ "drv", DANGEROUS },
{ "exe", ALLOW_ON_USER_GESTURE },
{ "fxp", ALLOW_ON_USER_GESTURE },
{ "grp", DANGEROUS },
{ "hlp", ALLOW_ON_USER_GESTURE },
{ "hta", ALLOW_ON_USER_GESTURE },
{ "htt", ALLOW_ON_USER_GESTURE },
{ "inf", ALLOW_ON_USER_GESTURE },
{ "ini", DANGEROUS },
{ "ins", ALLOW_ON_USER_GESTURE },
{ "isp", ALLOW_ON_USER_GESTURE },
{ "js", ALLOW_ON_USER_GESTURE },
{ "jse", ALLOW_ON_USER_GESTURE },
{ "lnk", ALLOW_ON_USER_GESTURE },
{ "local", DANGEROUS },
{ "mad", ALLOW_ON_USER_GESTURE },
{ "maf", ALLOW_ON_USER_GESTURE },
{ "mag", ALLOW_ON_USER_GESTURE },
{ "mam", ALLOW_ON_USER_GESTURE },
{ "manifest", DANGEROUS },
{ "maq", ALLOW_ON_USER_GESTURE },
{ "mar", ALLOW_ON_USER_GESTURE },
{ "mas", ALLOW_ON_USER_GESTURE },
{ "mat", ALLOW_ON_USER_GESTURE },
{ "mau", ALLOW_ON_USER_GESTURE },
{ "mav", ALLOW_ON_USER_GESTURE },
{ "maw", ALLOW_ON_USER_GESTURE },
{ "mda", ALLOW_ON_USER_GESTURE },
{ "mdb", ALLOW_ON_USER_GESTURE },
{ "mde", ALLOW_ON_USER_GESTURE },
{ "mdt", ALLOW_ON_USER_GESTURE },
{ "mdw", ALLOW_ON_USER_GESTURE },
{ "mdz", ALLOW_ON_USER_GESTURE },
{ "mht", ALLOW_ON_USER_GESTURE },
{ "mhtml", ALLOW_ON_USER_GESTURE },
{ "mmc", ALLOW_ON_USER_GESTURE },
{ "mof", DANGEROUS },
{ "msc", ALLOW_ON_USER_GESTURE },
{ "msh", ALLOW_ON_USER_GESTURE },
{ "mshxml", ALLOW_ON_USER_GESTURE },
{ "msi", ALLOW_ON_USER_GESTURE },
{ "msp", ALLOW_ON_USER_GESTURE },
{ "mst", ALLOW_ON_USER_GESTURE },
{ "ocx", DANGEROUS },
{ "ops", ALLOW_ON_USER_GESTURE },
{ "pcd", ALLOW_ON_USER_GESTURE },
{ "pif", ALLOW_ON_USER_GESTURE },
{ "plg", ALLOW_ON_USER_GESTURE },
{ "prf", ALLOW_ON_USER_GESTURE },
{ "prg", ALLOW_ON_USER_GESTURE },
{ "pst", ALLOW_ON_USER_GESTURE },
{ "reg", ALLOW_ON_USER_GESTURE },
{ "scf", ALLOW_ON_USER_GESTURE },
{ "scr", ALLOW_ON_USER_GESTURE },
{ "sct", ALLOW_ON_USER_GESTURE },
{ "shb", ALLOW_ON_USER_GESTURE },
{ "shs", ALLOW_ON_USER_GESTURE },
{ "sys", DANGEROUS },
{ "url", DANGEROUS },
{ "vb", ALLOW_ON_USER_GESTURE },
{ "vbe", ALLOW_ON_USER_GESTURE },
{ "vbs", ALLOW_ON_USER_GESTURE },
{ "vsd", ALLOW_ON_USER_GESTURE },
{ "vsmacros", ALLOW_ON_USER_GESTURE },
{ "vss", ALLOW_ON_USER_GESTURE },
{ "vst", ALLOW_ON_USER_GESTURE },
{ "vsw", ALLOW_ON_USER_GESTURE },
{ "ws", ALLOW_ON_USER_GESTURE },
{ "wsc", ALLOW_ON_USER_GESTURE },
{ "wsf", ALLOW_ON_USER_GESTURE },
{ "wsh", ALLOW_ON_USER_GESTURE },
{ "xbap", DANGEROUS },
#endif
#if !defined(OS_CHROMEOS)
{ "class", DANGEROUS },
{ "jar", DANGEROUS },
{ "jnlp", DANGEROUS },
#endif
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
{ "pl", ALLOW_ON_USER_GESTURE },
{ "py", ALLOW_ON_USER_GESTURE },
{ "pyc", ALLOW_ON_USER_GESTURE },
{ "pyw", ALLOW_ON_USER_GESTURE },
{ "rb", ALLOW_ON_USER_GESTURE },
#endif
#if defined(OS_POSIX)
{ "bash", ALLOW_ON_USER_GESTURE },
{ "csh", ALLOW_ON_USER_GESTURE },
{ "ksh", ALLOW_ON_USER_GESTURE },
{ "sh", ALLOW_ON_USER_GESTURE },
{ "shar", ALLOW_ON_USER_GESTURE },
{ "tcsh", ALLOW_ON_USER_GESTURE },
#endif
#if defined(OS_MACOSX)
{ "command", ALLOW_ON_USER_GESTURE },
#endif
#if defined(OS_MACOSX) || defined(OS_LINUX)
{ "pkg", ALLOW_ON_USER_GESTURE },
#endif
#if defined(OS_LINUX)
{ "deb", ALLOW_ON_USER_GESTURE },
{ "rpm", ALLOW_ON_USER_GESTURE },
#endif
#if defined(OS_ANDROID)
{ "apk", ALLOW_ON_USER_GESTURE },
{ "dex", ALLOW_ON_USER_GESTURE },
#endif
};
DownloadDangerLevel GetFileDangerLevel(const base::FilePath& path) {
base::FilePath::StringType extension(path.FinalExtension());
if (extension.empty())
return NOT_DANGEROUS;
if (!IsStringASCII(extension))
return NOT_DANGEROUS;
#if defined(OS_WIN)
std::string ascii_extension = base::UTF16ToASCII(extension);
#elif defined(OS_POSIX)
std::string ascii_extension = extension;
#endif
if (ascii_extension[0] == base::FilePath::kExtensionSeparator)
ascii_extension.erase(0, 1);
for (size_t i = 0; i < arraysize(g_executables); ++i) {
if (LowerCaseEqualsASCII(ascii_extension, g_executables[i].extension))
return g_executables[i].level;
}
return NOT_DANGEROUS;
}
static const char* kExecutableWhiteList[] = {
"text/javascript",
"text/javascript;version=*",
"text/html",
"text/x-registry",
"text/x-sh",
"binary/octet-stream"
};
static const char* kExecutableBlackList[] = {
"application/*+xml",
"application/xml"
};
bool IsExecutableMimeType(const std::string& mime_type) {
for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) {
if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type))
return true;
}
for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) {
if (net::MatchesMimeType(kExecutableBlackList[i], mime_type))
return false;
}
return net::MatchesMimeType("application/*", mime_type);
}
}