OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 <shlobj.h> | |
6 #include <shobjidl.h> | |
7 | |
8 #include "chrome/common/win_safe_util.h" | |
9 | |
10 #include "base/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/path_service.h" | |
13 #include "base/string_util.h" | |
14 #include "base/win/scoped_comptr.h" | |
15 #include "ui/base/win/shell.h" | |
16 | |
17 namespace { | |
18 | |
19 // This GUID is associated with any 'don't ask me again' settings that the | |
20 // user can select for different file types. | |
21 // {2676A9A2-D919-4fee-9187-152100393AB2} | |
22 static const GUID kClientID = { 0x2676a9a2, 0xd919, 0x4fee, | |
23 { 0x91, 0x87, 0x15, 0x21, 0x0, 0x39, 0x3a, 0xb2 } }; | |
24 | |
25 // Directly writes the ZoneIdentifier stream, without using the | |
26 // IAttachmentExecute service. | |
27 bool SetInternetZoneIdentifierDirectly(const FilePath& full_path) { | |
28 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | |
29 std::wstring path = full_path.value() + L":Zone.Identifier"; | |
30 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL, | |
31 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
32 if (INVALID_HANDLE_VALUE == file) | |
33 return false; | |
34 | |
35 static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; | |
36 // Don't include trailing null in data written. | |
37 static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1; | |
38 DWORD written = 0; | |
39 BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written, | |
40 NULL); | |
41 BOOL flush_result = FlushFileBuffers(file); | |
42 CloseHandle(file); | |
43 | |
44 if (!result || !flush_result || written != kIdentifierSize) { | |
45 NOTREACHED(); | |
46 return false; | |
47 } | |
48 | |
49 return true; | |
50 } | |
51 | |
52 } | |
53 | |
54 namespace win_util { | |
55 | |
56 // This function implementation is based on the attachment execution | |
57 // services functionally deployed with IE6 or Service pack 2. This | |
58 // functionality is exposed in the IAttachmentExecute COM interface. | |
59 // more information at: | |
60 // http://msdn2.microsoft.com/en-us/library/ms647048.aspx | |
61 bool SaferOpenItemViaShell(HWND hwnd, const std::wstring& window_title, | |
62 const FilePath& full_path, | |
63 const std::wstring& source_url) { | |
64 base::win::ScopedComPtr<IAttachmentExecute> attachment_services; | |
65 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); | |
66 if (FAILED(hr)) { | |
67 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 | |
68 // Windows installation, or the thread does not have COM initialized. | |
69 if (hr == CO_E_NOTINITIALIZED) { | |
70 NOTREACHED(); | |
71 return false; | |
72 } | |
73 return ui::win::OpenItemViaShell(full_path); | |
74 } | |
75 | |
76 attachment_services->SetClientGuid(kClientID); | |
77 | |
78 if (!window_title.empty()) | |
79 attachment_services->SetClientTitle(window_title.c_str()); | |
80 | |
81 // To help windows decide if the downloaded file is dangerous we can provide | |
82 // what the documentation calls evidence. Which we provide now: | |
83 // | |
84 // Set the file itself as evidence. | |
85 hr = attachment_services->SetLocalPath(full_path.value().c_str()); | |
86 if (FAILED(hr)) | |
87 return false; | |
88 // Set the origin URL as evidence. | |
89 hr = attachment_services->SetSource(source_url.c_str()); | |
90 if (FAILED(hr)) | |
91 return false; | |
92 | |
93 // Now check the windows policy. | |
94 if (attachment_services->CheckPolicy() != S_OK) { | |
95 // It is possible that the above call returns an undocumented result | |
96 // equal to 0x800c000e which seems to indicate that the URL failed the | |
97 // the security check. If you proceed with the Prompt() call the | |
98 // Shell might show a dialog that says: | |
99 // "windows found that this file is potentially harmful. To help protect | |
100 // your computer, Windows has blocked access to this file." | |
101 // Upon dismissal of the dialog windows will delete the file (!!). | |
102 // So, we can 'return' in that case but maybe is best to let it happen to | |
103 // fail on the safe side. | |
104 | |
105 ATTACHMENT_ACTION action; | |
106 // We cannot control what the prompt says or does directly but it | |
107 // is a pretty decent dialog; for example, if an executable is signed it can | |
108 // decode and show the publisher and the certificate. | |
109 hr = attachment_services->Prompt(hwnd, ATTACHMENT_PROMPT_EXEC, &action); | |
110 if (FAILED(hr) || (ATTACHMENT_ACTION_CANCEL == action)) { | |
111 // The user has declined opening the item. | |
112 return false; | |
113 } | |
114 } | |
115 return ui::win::OpenItemViaShellNoZoneCheck(full_path); | |
116 } | |
117 | |
118 bool SetInternetZoneIdentifier(const FilePath& full_path, | |
119 const std::wstring& source_url) { | |
120 base::win::ScopedComPtr<IAttachmentExecute> attachment_services; | |
121 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); | |
122 | |
123 if (FAILED(hr)) { | |
124 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 | |
125 // Windows installation, or the thread does not have COM initialized. | |
126 if (hr == CO_E_NOTINITIALIZED) { | |
127 NOTREACHED(); | |
128 return false; | |
129 } | |
130 | |
131 // Write the ZoneIdentifier file directly. | |
132 return SetInternetZoneIdentifierDirectly(full_path); | |
133 } | |
134 | |
135 hr = attachment_services->SetClientGuid(kClientID); | |
136 if (FAILED(hr)) | |
137 return false; | |
138 | |
139 hr = attachment_services->SetLocalPath(full_path.value().c_str()); | |
140 if (FAILED(hr)) | |
141 return false; | |
142 | |
143 // Source is necessary for files ending in ".tmp" to avoid error 0x800c000e. | |
144 hr = attachment_services->SetSource(source_url.c_str()); | |
145 if (FAILED(hr)) | |
146 return false; | |
147 | |
148 hr = attachment_services->Save(); | |
149 if (FAILED(hr)) | |
150 return false; | |
151 | |
152 return true; | |
153 } | |
154 | |
155 } // namespace win_util | |
OLD | NEW |