Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: content/browser/download/quarantine_mac.mm

Issue 2124373002: [PPAPI] Quarantine files that are writeable by a Pepper plugin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@consolidate-file-metadata
Patch Set: Address comments. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "content/browser/download/quarantine.h"
6
7 #import <ApplicationServices/ApplicationServices.h>
8 #import <Foundation/Foundation.h>
9
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/mac/foundation_util.h"
13 #include "base/mac/mac_logging.h"
14 #include "base/mac/mac_util.h"
15 #include "base/mac/scoped_cftyperef.h"
16 #include "base/mac/scoped_nsobject.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "base/threading/thread_restrictions.h"
19 #include "url/gurl.h"
20
21 namespace {
22
23 // Once Chrome no longer supports macOS 10.9, this code will no longer be
24 // necessary. Note that LSCopyItemAttribute was deprecated in macOS 10.8, but
25 // the replacement to kLSItemQuarantineProperties did not exist until macOS
26 // 10.10.
27 #if !defined(MAC_OS_X_VERSION_10_10) || \
28 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
31 bool GetQuarantinePropertiesDeprecated(
32 const base::FilePath& file,
33 base::scoped_nsobject<NSMutableDictionary>* properties) {
34 const UInt8* path = reinterpret_cast<const UInt8*>(file.value().c_str());
35 FSRef file_ref;
36 if (FSPathMakeRef(path, &file_ref, nullptr) != noErr)
37 return false;
38
39 base::ScopedCFTypeRef<CFTypeRef> quarantine_properties;
40 OSStatus status = LSCopyItemAttribute(&file_ref, kLSRolesAll,
41 kLSItemQuarantineProperties, quarantine_properties.InitializeInto());
42 if (status != noErr)
43 return true;
44
45 CFDictionaryRef quarantine_properties_dict =
46 base::mac::CFCast<CFDictionaryRef>(quarantine_properties.get());
47 if (!quarantine_properties_dict) {
48 LOG(WARNING) << "kLSItemQuarantineProperties is not a dictionary on file "
49 << file.value();
50 return false;
51 }
52
53 properties->reset(
54 [base::mac::CFToNSCast(quarantine_properties_dict) mutableCopy]);
55 return true;
56 }
57
58 bool SetQuarantinePropertiesDeprecated(const base::FilePath& file,
59 NSDictionary* properties) {
60 const UInt8* path = reinterpret_cast<const UInt8*>(file.value().c_str());
61 FSRef file_ref;
62 if (FSPathMakeRef(path, &file_ref, nullptr) != noErr)
63 return false;
64 OSStatus os_error = LSSetItemAttribute(
65 &file_ref, kLSRolesAll, kLSItemQuarantineProperties, properties);
66 if (os_error != noErr) {
67 OSSTATUS_LOG(WARNING, os_error)
68 << "Unable to set quarantine attributes on file " << file.value();
69 return false;
70 }
71 return true;
72 }
73 #pragma clang diagnostic pop
74 #endif
75
76 bool GetQuarantineProperties(
77 const base::FilePath& file,
78 base::scoped_nsobject<NSMutableDictionary>* properties) {
79 base::scoped_nsobject<NSURL> file_url([[NSURL alloc]
80 initFileURLWithPath:base::SysUTF8ToNSString(file.value())]);
81 if (!file_url)
82 return false;
83
84 // NSURLQuarantinePropertiesKey is only available on macOS 10.10+.
85 #pragma clang diagnostic push
86 #pragma clang diagnostic ignored "-Wunguarded-availability"
87 NSError* error = nil;
88 id quarantine_properties = nil;
89 BOOL success = [file_url getResourceValue:&quarantine_properties
90 forKey:NSURLQuarantinePropertiesKey
91 error:&error];
92 #pragma clang diagnostic pop
93 if (!success) {
94 std::string error_message(error ? error.description.UTF8String : "");
95 LOG(WARNING) << "Unable to get quarantine attributes for file "
96 << file.value() << ". Error: " << error_message;
97 return false;
98 }
99
100 if (!quarantine_properties)
101 return true;
102
103 NSDictionary* quarantine_properties_dict =
104 base::mac::ObjCCast<NSDictionary>(quarantine_properties);
105 if (!quarantine_properties_dict) {
106 LOG(WARNING) << "Quarantine properties have wrong class: "
107 << [[[quarantine_properties class] description] UTF8String];
108 return false;
109 }
110
111 properties->reset([quarantine_properties_dict mutableCopy]);
112 return true;
113 }
114
115 bool SetQuarantineProperties(const base::FilePath& file,
116 NSDictionary* properties) {
117 base::scoped_nsobject<NSURL> file_url([[NSURL alloc]
118 initFileURLWithPath:base::SysUTF8ToNSString(file.value())]);
119 if (!file_url)
120 return false;
121
122 // NSURLQuarantinePropertiesKey is only available on macOS 10.10+.
123 #pragma clang diagnostic push
124 #pragma clang diagnostic ignored "-Wunguarded-availability"
125 NSError* error = nil;
126 bool success = [file_url setResourceValue:properties
127 forKey:NSURLQuarantinePropertiesKey
128 error:&error];
129 #pragma clang diagnostic pop
130 if (!success) {
131 std::string error_message(error ? error.description.UTF8String : "");
132 LOG(WARNING) << "Unable to set quarantine attributes on file "
133 << file.value() << ". Error: " << error_message;
134 return false;
135 }
136 return true;
137 }
138
139 } // namespace
140
141 namespace content {
142
143 namespace {
144
145 // As of Mac OS X 10.4 ("Tiger"), files can be tagged with metadata describing
146 // various attributes. Metadata is integrated with the system's Spotlight
147 // feature and is searchable. Ordinarily, metadata can only be set by
148 // Spotlight importers, which requires that the importer own the target file.
149 // However, there's an attribute intended to describe the origin of a
150 // file, that can store the source URL and referrer of a downloaded file.
151 // It's stored as a "com.apple.metadata:kMDItemWhereFroms" extended attribute,
152 // structured as a binary1-format plist containing a list of sources. This
153 // attribute can only be populated by the downloader, not a Spotlight importer.
154 // Safari on 10.4 and later populates this attribute.
155 //
156 // With this metadata set, you can locate downloads by performing a Spotlight
157 // search for their source or referrer URLs, either from within the Spotlight
158 // UI or from the command line:
159 // mdfind 'kMDItemWhereFroms == "http://releases.mozilla.org/*"'
160 //
161 // There is no documented API to set metadata on a file directly as of the
162 // 10.5 SDK. The MDSetItemAttribute function does exist to perform this task,
163 // but it's undocumented.
164 bool AddOriginMetadataToFile(const base::FilePath& file,
165 const GURL& source,
166 const GURL& referrer) {
167 base::ThreadRestrictions::AssertIOAllowed();
168 // There's no declaration for MDItemSetAttribute in any known public SDK.
169 // It exists in the 10.4 and 10.5 runtimes. To play it safe, do the lookup
170 // at runtime instead of declaring it ourselves and linking against what's
171 // provided. This has two benefits:
172 // - If Apple relents and declares the function in a future SDK (it's
173 // happened before), our build won't break.
174 // - If Apple removes or renames the function in a future runtime, the
175 // loader won't refuse to let the application launch. Instead, we'll
176 // silently fail to set any metadata.
177 typedef OSStatus (*MDItemSetAttribute_type)(MDItemRef, CFStringRef,
178 CFTypeRef);
179 static MDItemSetAttribute_type md_item_set_attribute_func = NULL;
180
181 static bool did_symbol_lookup = false;
182 if (!did_symbol_lookup) {
183 did_symbol_lookup = true;
184 CFBundleRef metadata_bundle =
185 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Metadata"));
186 if (!metadata_bundle)
187 return false;
188
189 md_item_set_attribute_func =
190 (MDItemSetAttribute_type)CFBundleGetFunctionPointerForName(
191 metadata_bundle, CFSTR("MDItemSetAttribute"));
192 }
193 if (!md_item_set_attribute_func)
194 return false;
195
196 NSString* file_path = [NSString stringWithUTF8String:file.value().c_str()];
197 if (!file_path)
198 return false;
199
200 base::ScopedCFTypeRef<MDItemRef> md_item(
201 MDItemCreate(NULL, base::mac::NSToCFCast(file_path)));
202 if (!md_item)
203 return false;
204
205 // We won't put any more than 2 items into the attribute.
206 NSMutableArray* list = [NSMutableArray arrayWithCapacity:2];
207
208 // Follow Safari's lead: the first item in the list is the source URL of
209 // the downloaded file. If the referrer is known, store that, too.
210 NSString* origin_url = [NSString stringWithUTF8String:source.spec().c_str()];
211 if (origin_url)
212 [list addObject:origin_url];
213 NSString* referrer_url =
214 [NSString stringWithUTF8String:referrer.spec().c_str()];
215 if (referrer_url)
216 [list addObject:referrer_url];
217
218 md_item_set_attribute_func(md_item, kMDItemWhereFroms,
219 base::mac::NSToCFCast(list));
220 return true;
221 }
222
223 // Adds quarantine metadata to the file, assuming it has already been
224 // quarantined by the OS.
225 // |source| should be the source URL for the download, and |referrer| should be
226 // the URL the user initiated the download from.
227
228 // The OS will automatically quarantine files due to the
229 // LSFileQuarantineEnabled entry in our Info.plist, but it knows relatively
230 // little about the files. We add more information about the download to
231 // improve the UI shown by the OS when the users tries to open the file.
232 bool AddQuarantineMetadataToFile(const base::FilePath& file,
233 const GURL& source,
234 const GURL& referrer) {
235 base::ThreadRestrictions::AssertIOAllowed();
236 base::scoped_nsobject<NSMutableDictionary> properties;
237 bool success = false;
238 if (base::mac::IsAtLeastOS10_10()) {
239 success = GetQuarantineProperties(file, &properties);
240 } else {
241 success = GetQuarantinePropertiesDeprecated(file, &properties);
242 }
243
244 if (!success)
245 return false;
246
247 if (!properties) {
248 // If there are no quarantine properties, then the file isn't quarantined
249 // (e.g., because the user has set up exclusions for certain file types).
250 // We don't want to add any metadata, because that will cause the file to
251 // be quarantined against the user's wishes.
252 return true;
253 }
254
255 // kLSQuarantineAgentNameKey, kLSQuarantineAgentBundleIdentifierKey, and
256 // kLSQuarantineTimeStampKey are set for us (see LSQuarantine.h), so we only
257 // need to set the values that the OS can't infer.
258
259 if (![properties valueForKey:(NSString*)kLSQuarantineTypeKey]) {
260 CFStringRef type = source.SchemeIsHTTPOrHTTPS()
261 ? kLSQuarantineTypeWebDownload
262 : kLSQuarantineTypeOtherDownload;
263 [properties setValue:(NSString*)type
264 forKey:(NSString*)kLSQuarantineTypeKey];
265 }
266
267 if (![properties valueForKey:(NSString*)kLSQuarantineOriginURLKey] &&
268 referrer.is_valid()) {
269 NSString* referrer_url =
270 [NSString stringWithUTF8String:referrer.spec().c_str()];
271 [properties setValue:referrer_url
272 forKey:(NSString*)kLSQuarantineOriginURLKey];
273 }
274
275 if (![properties valueForKey:(NSString*)kLSQuarantineDataURLKey] &&
276 source.is_valid()) {
277 NSString* origin_url =
278 [NSString stringWithUTF8String:source.spec().c_str()];
279 [properties setValue:origin_url forKey:(NSString*)kLSQuarantineDataURLKey];
280 }
281
282 if (base::mac::IsAtLeastOS10_10()) {
283 return SetQuarantineProperties(file, properties);
284 } else {
285 return SetQuarantinePropertiesDeprecated(file, properties);
286 }
287 }
288
289 } // namespace
290
291 QuarantineFileResult QuarantineFile(const base::FilePath& file,
292 const GURL& source_url,
293 const GURL& referrer_url,
294 const std::string& client_guid) {
295 bool quarantine_succeeded =
296 AddQuarantineMetadataToFile(file, source_url, referrer_url);
297 bool origin_succeeded =
298 AddOriginMetadataToFile(file, source_url, referrer_url);
299 return quarantine_succeeded && origin_succeeded
300 ? QuarantineFileResult::OK
301 : QuarantineFileResult::ANNOTATION_FAILED;
302 }
303
304 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/quarantine_linux_unittest.cc ('k') | content/browser/download/quarantine_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698