From 94719edeb343408df5aacec0f05e089cf600728e Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Apr 2022 00:38:42 -0700 Subject: [PATCH] Ignore custom icons set via resource forks in delta updates (#2114) For creation, we will disallow creating delta updates if a custom resource fork icon data is found in either the old or app bundles. For applying, we will just ignore the icon data when performing hash verification and continue applying the patch. --- Autoupdate/SUBinaryDeltaCommon.h | 3 ++ Autoupdate/SUBinaryDeltaCommon.m | 5 ++- Autoupdate/SUBinaryDeltaCreate.m | 20 ++++++++++++ Tests/SUBinaryDeltaTest.m | 54 +++++++++++++++++++++++++++++++ Tests/SUCodeSigningVerifierTest.m | 1 - 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Autoupdate/SUBinaryDeltaCommon.h b/Autoupdate/SUBinaryDeltaCommon.h index 94f87e0a..0bbbd7a2 100644 --- a/Autoupdate/SUBinaryDeltaCommon.h +++ b/Autoupdate/SUBinaryDeltaCommon.h @@ -31,6 +31,9 @@ #define MAJOR_VERSION_IS_AT_LEAST(actualMajor, expectedMajor) (actualMajor >= expectedMajor) +// Relative path of custom icon data that may be set on a bundle via a resource fork +#define CUSTOM_ICON_PATH @"/Icon\r" + // Changes that break backwards compatibility will have different major versions // Changes that affect creating but not applying patches will have different minor versions typedef NS_ENUM(uint16_t, SUBinaryDeltaMajorVersion) diff --git a/Autoupdate/SUBinaryDeltaCommon.m b/Autoupdate/SUBinaryDeltaCommon.m index 533b18b1..05a95105 100644 --- a/Autoupdate/SUBinaryDeltaCommon.m +++ b/Autoupdate/SUBinaryDeltaCommon.m @@ -256,8 +256,11 @@ BOOL getRawHashOfTreeAndFileTablesWithVersion(unsigned char *hashBuffer, NSStrin continue; NSString *relativePath = pathRelativeToDirectory(normalizedPath, stringWithFileSystemRepresentation(ent->fts_path)); - if (relativePath.length == 0) + + // Ignore icon resource fork data + if (relativePath.length == 0 || [relativePath isEqualToString:CUSTOM_ICON_PATH]) { continue; + } unsigned char fileHash[CC_SHA1_DIGEST_LENGTH]; if (!_hashOfFileContents(fileHash, ent, tempBuffer, tempBufferSize)) { diff --git a/Autoupdate/SUBinaryDeltaCreate.m b/Autoupdate/SUBinaryDeltaCreate.m index 1a15bc61..4be9f713 100644 --- a/Autoupdate/SUBinaryDeltaCreate.m +++ b/Autoupdate/SUBinaryDeltaCreate.m @@ -440,6 +440,16 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF if (![key length]) { continue; } + + if ([key isEqualToString:CUSTOM_ICON_PATH]) { + if (verbose) { + fprintf(stderr, "\n"); + } + if (error != NULL) { + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Diffing bundles with a custom icon set via a resource fork is not supported. Detected presence of %@", @(ent->fts_path)] }]; + } + return NO; + } NSDictionary *info = infoForFile(ent); if (!info) { @@ -531,6 +541,16 @@ BOOL createBinaryDelta(NSString *source, NSString *destination, NSString *patchF if (![key length]) { continue; } + + if ([key isEqualToString:CUSTOM_ICON_PATH]) { + if (verbose) { + fprintf(stderr, "\n"); + } + if (error != NULL) { + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnknownError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Diffing bundles with a custom icon set via a resource fork is not supported. Detected presence of %@", @(ent->fts_path)] }]; + } + return NO; + } NSDictionary *info = infoForFile(ent); if (!info) { diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index eff1f977..bbd759b9 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -1729,4 +1729,58 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec XCTAssertFalse(success); } +- (void)testCreatingPatchWithCustomIconInBeforeTree +{ + BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"]; + + XCTAssertTrue([[NSData data] writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([[NSData dataWithBytes:"loltest" length:7] writeToFile:destinationFile atomically:YES]); + + NSImage *iconImage = [NSImage imageNamed:NSImageNameAdvanced]; + XCTAssertNotNil(iconImage); + + BOOL setIcon = [[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:sourceDirectory options:0]; + XCTAssertTrue(setIcon); + } afterDiffHandler:nil afterPatchHandler:nil]; + XCTAssertFalse(success); +} + +- (void)testCreatingPatchWithCustomIconInAfterTree +{ + BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"]; + + XCTAssertTrue([[NSData data] writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([[NSData dataWithBytes:"loltest" length:7] writeToFile:destinationFile atomically:YES]); + + NSImage *iconImage = [NSImage imageNamed:NSImageNameAdvanced]; + XCTAssertNotNil(iconImage); + + BOOL setIcon = [[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:destinationDirectory options:0]; + XCTAssertTrue(setIcon); + } afterDiffHandler:nil afterPatchHandler:nil]; + XCTAssertFalse(success); +} + +- (void)testApplyingPatchAfterSettingCustomIcon +{ + BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:@"A"]; + NSString *destinationFile = [destinationDirectory stringByAppendingPathComponent:@"A"]; + + XCTAssertTrue([[NSData data] writeToFile:sourceFile atomically:YES]); + XCTAssertTrue([[NSData dataWithBytes:"loltest" length:7] writeToFile:destinationFile atomically:YES]); + } afterDiffHandler:^(NSFileManager *__unused fileManager, NSString *sourceDirectory, NSString *__unused destinationDirectory) { + NSImage *iconImage = [NSImage imageNamed:NSImageNameAdvanced]; + XCTAssertNotNil(iconImage); + + BOOL setIcon = [[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:sourceDirectory options:0]; + XCTAssertTrue(setIcon); + } afterPatchHandler:nil]; + XCTAssertTrue(success); +} + @end diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index d9beccd2..82700375 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -47,7 +47,6 @@ [super setUp]; NSBundle *unitTestBundle = [NSBundle bundleForClass:[self class]]; - NSString *unitTestBundleIdentifier = unitTestBundle.bundleIdentifier; NSString *zippedAppURL = [unitTestBundle pathForResource:@"SparkleTestCodeSignApp" ofType:@"zip"]; SUFileManager *fileManager = [[SUFileManager alloc] init];