Fix NSKeyedUnarchiver usage

This commit is contained in:
Ali 2023-09-23 13:33:04 +04:00
parent b7fbcbf118
commit 2091b47bb0
7 changed files with 45 additions and 14 deletions

View File

@ -10,3 +10,9 @@
- (void)removeObjectForKey:(NSString *)aKey group:(NSString *)group; - (void)removeObjectForKey:(NSString *)aKey group:(NSString *)group;
@end @end
@interface MTDeprecated : NSObject
+ (id)unarchiveDeprecatedWithData:(NSData *)data;
@end

View File

@ -82,6 +82,10 @@
[bindRequestData appendInt32:expiresAt]; [bindRequestData appendInt32:expiresAt];
[bindRequestData appendTLBytes:encryptedMessage]; [bindRequestData appendTLBytes:encryptedMessage];
if (MTLogEnabled()) {
MTLog(@"[MTBindKeyMessageService#%p binding temp %" PRId64 " to persistent %" PRId64 "]", self, _ephemeralKey.authKeyId, _persistentKey.authKeyId);
}
MTOutgoingMessage *outgoingMessage = [[MTOutgoingMessage alloc] initWithData:bindRequestData.data metadata:[NSString stringWithFormat:@"auth.bindTempAuthKey"] additionalDebugDescription:nil shortMetadata:@"auth.bindTempAuthKey" messageId:bindingMessageId messageSeqNo:bindingSeqNo]; MTOutgoingMessage *outgoingMessage = [[MTOutgoingMessage alloc] initWithData:bindRequestData.data metadata:[NSString stringWithFormat:@"auth.bindTempAuthKey"] additionalDebugDescription:nil shortMetadata:@"auth.bindTempAuthKey" messageId:bindingMessageId messageSeqNo:bindingSeqNo];
return [[MTMessageTransaction alloc] initWithMessagePayload:@[outgoingMessage] prepared:nil failed:nil completion:^(NSDictionary *messageInternalIdToTransactionId, NSDictionary *messageInternalIdToPreparedMessage, __unused NSDictionary *messageInternalIdToQuickAckId) { return [[MTMessageTransaction alloc] initWithMessagePayload:@[outgoingMessage] prepared:nil failed:nil completion:^(NSDictionary *messageInternalIdToTransactionId, NSDictionary *messageInternalIdToPreparedMessage, __unused NSDictionary *messageInternalIdToQuickAckId) {

View File

@ -1 +1,18 @@
#import <MtProtoKit/MTKeychain.h> #import <MtProtoKit/MTKeychain.h>
@implementation MTDeprecated
+ (id)unarchiveDeprecatedWithData:(NSData *)data {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@try {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
} @catch(NSException *e) {
return nil;
}
#pragma clang diagnostic pop
}
@end

View File

@ -865,9 +865,9 @@ static const NSUInteger MTMaxUnacknowledgedMessageCount = 64;
} }
} }
- (NSString *)outgoingMessageDescription:(MTOutgoingMessage *)message messageId:(int64_t)messageId messageSeqNo:(int32_t)messageSeqNo - (NSString *)outgoingMessageDescription:(MTOutgoingMessage *)message messageId:(int64_t)messageId messageSeqNo:(int32_t)messageSeqNo authKeyId:(int64_t)authKeyId
{ {
return [[NSString alloc] initWithFormat:@"%@%@ (%" PRId64 "/%" PRId32 ")", message.metadata, message.additionalDebugDescription != nil ? message.additionalDebugDescription : @"", message.messageId == 0 ? messageId : message.messageId, message.messageSeqNo == 0 ? message.messageSeqNo : messageSeqNo]; return [[NSString alloc] initWithFormat:@"%@%@ (%" PRId64 "/%" PRId32 " %" PRId64 ")", message.metadata, message.additionalDebugDescription != nil ? message.additionalDebugDescription : @"", message.messageId == 0 ? messageId : message.messageId, message.messageSeqNo == 0 ? message.messageSeqNo : messageSeqNo, authKeyId];
} }
- (NSString *)outgoingShortMessageDescription:(MTOutgoingMessage *)message messageId:(int64_t)messageId messageSeqNo:(int32_t)messageSeqNo - (NSString *)outgoingShortMessageDescription:(MTOutgoingMessage *)message messageId:(int64_t)messageId messageSeqNo:(int32_t)messageSeqNo
@ -1100,7 +1100,7 @@ static const NSUInteger MTMaxUnacknowledgedMessageCount = 64;
NSData *data = messageData; NSData *data = messageData;
if (MTLogEnabled()) { if (MTLogEnabled()) {
NSString *messageDescription = [self outgoingMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo]; NSString *messageDescription = [self outgoingMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo authKeyId:authKey.authKeyId];
MTLogWithPrefix(_getLogPrefix, @"[MTProto#%p@%p preparing %@]", self, _context, messageDescription); MTLogWithPrefix(_getLogPrefix, @"[MTProto#%p@%p preparing %@]", self, _context, messageDescription);
} }
NSString *shortMessageDescription = [self outgoingShortMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo]; NSString *shortMessageDescription = [self outgoingShortMessageDescription:outgoingMessage messageId:messageId messageSeqNo:messageSeqNo];

View File

@ -838,7 +838,7 @@ public func accountBackupData(postbox: Postbox) -> Signal<AccountBackupData?, No
guard let authInfoData = transaction.keychainEntryForKey("persistent:datacenterAuthInfoById") else { guard let authInfoData = transaction.keychainEntryForKey("persistent:datacenterAuthInfoById") else {
return nil return nil
} }
guard let authInfo = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSDictionary.self, from: authInfoData) else { guard let authInfo = MTDeprecated.unarchiveDeprecated(with: authInfoData) as? NSDictionary else {
return nil return nil
} }
guard let datacenterAuthInfo = authInfo.object(forKey: state.masterDatacenterId as NSNumber) as? MTDatacenterAuthInfo else { guard let datacenterAuthInfo = authInfo.object(forKey: state.masterDatacenterId as NSNumber) as? MTDatacenterAuthInfo else {

View File

@ -18,6 +18,10 @@ public enum ConnectionStatus: Equatable {
case online(proxyAddress: String?) case online(proxyAddress: String?)
} }
public func legacy_unarchiveDeprecated(data: Data) -> Any? {
return MTDeprecated.unarchiveDeprecated(with: data)
}
private struct MTProtoConnectionFlags: OptionSet { private struct MTProtoConnectionFlags: OptionSet {
let rawValue: Int let rawValue: Int
@ -1138,12 +1142,11 @@ class Keychain: NSObject, MTKeychain {
} }
if let data = self.get(group + ":" + aKey) { if let data = self.get(group + ":" + aKey) {
var result: NSDictionary? var result: NSDictionary?
MTContext.perform(objCTry: { result = MTDeprecated.unarchiveDeprecated(with: data as Data) as? NSDictionary
result = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSDictionary.self, from: data as Data)
})
if let result = result { if let result = result {
return result as? [AnyHashable : Any] return result as? [AnyHashable : Any]
} }
assertionFailure("Unexpected keychain entry type")
} }
return nil return nil
} }
@ -1154,11 +1157,12 @@ class Keychain: NSObject, MTKeychain {
} }
if let data = self.get(group + ":" + aKey) { if let data = self.get(group + ":" + aKey) {
var result: NSNumber? var result: NSNumber?
MTContext.perform(objCTry: { result = MTDeprecated.unarchiveDeprecated(with: data as Data) as? NSNumber
result = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSNumber.self, from: data as Data) if let result = result {
})
return result return result
} }
assertionFailure("Unexpected keychain entry type")
}
return nil return nil
} }

View File

@ -334,7 +334,7 @@ public func fetchVideoLibraryMediaResource(postbox: Postbox, resource: VideoLibr
if let values = try? JSONDecoder().decode(MediaEditorValues.self, from: adjustmentsValue.data.makeData()) { if let values = try? JSONDecoder().decode(MediaEditorValues.self, from: adjustmentsValue.data.makeData()) {
mediaEditorValues = values mediaEditorValues = values
} }
} else if let dict = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSDictionary.self, from: adjustmentsValue.data.makeData()) as? [AnyHashable : Any], let legacyAdjustments = TGVideoEditAdjustments(dictionary: dict) { } else if let dict = legacy_unarchiveDeprecated(data: adjustmentsValue.data.makeData()) as? [AnyHashable : Any], let legacyAdjustments = TGVideoEditAdjustments(dictionary: dict) {
if alwaysUseModernPipeline { if alwaysUseModernPipeline {
mediaEditorValues = MediaEditorValues(legacyAdjustments: legacyAdjustments, defaultPreset: qualityPreset) mediaEditorValues = MediaEditorValues(legacyAdjustments: legacyAdjustments, defaultPreset: qualityPreset)
} else { } else {
@ -500,7 +500,7 @@ public func fetchLocalFileVideoMediaResource(postbox: Postbox, resource: LocalFi
if let values = try? JSONDecoder().decode(MediaEditorValues.self, from: videoAdjustments.data.makeData()) { if let values = try? JSONDecoder().decode(MediaEditorValues.self, from: videoAdjustments.data.makeData()) {
mediaEditorValues = values mediaEditorValues = values
} }
} else if let dict = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSDictionary.self, from: videoAdjustments.data.makeData()) as? [AnyHashable : Any], let legacyAdjustments = TGVideoEditAdjustments(dictionary: dict) { } else if let dict = legacy_unarchiveDeprecated(data: videoAdjustments.data.makeData()) as? [AnyHashable : Any], let legacyAdjustments = TGVideoEditAdjustments(dictionary: dict) {
if alwaysUseModernPipeline && !isImage { if alwaysUseModernPipeline && !isImage {
mediaEditorValues = MediaEditorValues(legacyAdjustments: legacyAdjustments, defaultPreset: qualityPreset) mediaEditorValues = MediaEditorValues(legacyAdjustments: legacyAdjustments, defaultPreset: qualityPreset)
} else { } else {
@ -710,7 +710,7 @@ public func fetchVideoLibraryMediaResourceHash(resource: VideoLibraryMediaResour
adjustments = nil adjustments = nil
case let .compress(adjustmentsValue): case let .compress(adjustmentsValue):
if let adjustmentsValue = adjustmentsValue { if let adjustmentsValue = adjustmentsValue {
if let dict = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSDictionary.self, from: adjustmentsValue.data.makeData()) as? [AnyHashable : Any], let legacyAdjustments = TGVideoEditAdjustments(dictionary: dict) { if let dict = legacy_unarchiveDeprecated(data: adjustmentsValue.data.makeData()) as? [AnyHashable : Any], let legacyAdjustments = TGVideoEditAdjustments(dictionary: dict) {
adjustments = legacyAdjustments adjustments = legacyAdjustments
} }
} }