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 "chrome/common/safe_browsing/download_protection_util.h"
6
7#include "base/files/file_path.h"
8#include "base/logging.h"
9
10namespace safe_browsing {
11namespace download_protection_util {
12
13bool IsArchiveFile(const base::FilePath& file) {
14  // TODO(mattm): should .dmg be checked here instead of IsBinaryFile?
15  return file.MatchesExtension(FILE_PATH_LITERAL(".zip"));
16}
17
18bool IsBinaryFile(const base::FilePath& file) {
19  return (
20      // Executable extensions for MS Windows.
21      file.MatchesExtension(FILE_PATH_LITERAL(".bas")) ||
22      file.MatchesExtension(FILE_PATH_LITERAL(".bat")) ||
23      file.MatchesExtension(FILE_PATH_LITERAL(".cab")) ||
24      file.MatchesExtension(FILE_PATH_LITERAL(".cmd")) ||
25      file.MatchesExtension(FILE_PATH_LITERAL(".com")) ||
26      file.MatchesExtension(FILE_PATH_LITERAL(".exe")) ||
27      file.MatchesExtension(FILE_PATH_LITERAL(".hta")) ||
28      file.MatchesExtension(FILE_PATH_LITERAL(".msi")) ||
29      file.MatchesExtension(FILE_PATH_LITERAL(".pif")) ||
30      file.MatchesExtension(FILE_PATH_LITERAL(".reg")) ||
31      file.MatchesExtension(FILE_PATH_LITERAL(".scr")) ||
32      file.MatchesExtension(FILE_PATH_LITERAL(".vb")) ||
33      file.MatchesExtension(FILE_PATH_LITERAL(".vbs")) ||
34      // Chrome extensions and android APKs are also reported.
35      file.MatchesExtension(FILE_PATH_LITERAL(".crx")) ||
36      file.MatchesExtension(FILE_PATH_LITERAL(".apk")) ||
37      // Mac extensions.
38      file.MatchesExtension(FILE_PATH_LITERAL(".dmg")) ||
39      file.MatchesExtension(FILE_PATH_LITERAL(".pkg")) ||
40      file.MatchesExtension(FILE_PATH_LITERAL(".osx")) ||
41      file.MatchesExtension(FILE_PATH_LITERAL(".app")) ||
42      // Archives _may_ contain binaries, we'll check in ExtractFileFeatures.
43      IsArchiveFile(file));
44}
45
46ClientDownloadRequest::DownloadType GetDownloadType(
47    const base::FilePath& file) {
48  DCHECK(IsBinaryFile(file));
49  if (file.MatchesExtension(FILE_PATH_LITERAL(".apk")))
50    return ClientDownloadRequest::ANDROID_APK;
51  else if (file.MatchesExtension(FILE_PATH_LITERAL(".crx")))
52    return ClientDownloadRequest::CHROME_EXTENSION;
53  // For zip files, we use the ZIPPED_EXECUTABLE type since we will only send
54  // the pingback if we find an executable inside the zip archive.
55  else if (file.MatchesExtension(FILE_PATH_LITERAL(".zip")))
56    return ClientDownloadRequest::ZIPPED_EXECUTABLE;
57  else if (file.MatchesExtension(FILE_PATH_LITERAL(".dmg")) ||
58           file.MatchesExtension(FILE_PATH_LITERAL(".pkg")) ||
59           file.MatchesExtension(FILE_PATH_LITERAL(".osx")) ||
60           file.MatchesExtension(FILE_PATH_LITERAL(".app")))
61    return ClientDownloadRequest::MAC_EXECUTABLE;
62  return ClientDownloadRequest::WIN_EXECUTABLE;
63}
64
65}  // namespace download_protection_util
66}  // namespace safe_browsing
67