1// Copyright 2024 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#import "base/mac/info_plist_data.h" 6 7#import <Foundation/Foundation.h> 8#include <stdint.h> 9 10#include "base/apple/bridging.h" 11#include "base/apple/bundle_locations.h" 12#include "base/apple/foundation_util.h" 13#include "base/containers/span.h" 14 15extern "C" { 16// Key used within CoreFoundation for loaded Info plists 17extern const CFStringRef _kCFBundleNumericVersionKey; 18} 19 20namespace base::mac { 21 22std::vector<uint8_t> OuterBundleCachedInfoPlistData() { 23 // NSBundle's info dictionary is used to ensure that any changes to Info.plist 24 // on disk due to pending updates do not result in a version of the data being 25 // used that doesn't match the code signature of the running app. 26 NSMutableDictionary* info_plist_dictionary = 27 [base::apple::OuterBundle().infoDictionary mutableCopy]; 28 if (!info_plist_dictionary.count) { 29 return {}; 30 } 31 32 // NSBundle inserts CFBundleNumericVersion into its in-memory copy of the info 33 // dictionary despite it not being present on disk. Remove it so that the 34 // serialized dictionary matches the Info.plist that was present at signing 35 // time. 36 info_plist_dictionary[base::apple::CFToNSPtrCast( 37 _kCFBundleNumericVersionKey)] = nil; 38 39 NSData* data = [NSPropertyListSerialization 40 dataWithPropertyList:info_plist_dictionary 41 format:NSPropertyListXMLFormat_v1_0 42 options:0 43 error:nullptr]; 44 base::span<const uint8_t> span = apple::NSDataToSpan(data); 45 return {span.begin(), span.end()}; 46} 47 48} // namespace base::mac 49