From 8039288f6404e45bafa1fe42d50c7abfa6ce4031 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 17 Jun 2023 17:01:45 -0700 Subject: [PATCH] Harden verification of Sparkle update download (#2392) --- Autoupdate/AppInstaller.m | 22 ++++++++++++++++++++++ Downloader/SPUDownloader.m | 1 + 2 files changed, 23 insertions(+) diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 824c6913..7fb14b9f 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -361,6 +361,28 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; return; } + // Validate the download URL before moving it + { + NSArray *downloadURLPathComponents = downloadURL.URLByResolvingSymlinksInPath.pathComponents; + if (downloadURLPathComponents == nil) { + [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUInstallerError userInfo:@{ NSLocalizedDescriptionKey: @"Error: Failed to retrieve path components from download URL" }]]; + + return; + } + + if ([downloadURLPathComponents containsObject:@".."]) { + [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUInstallerError userInfo:@{ NSLocalizedDescriptionKey: @"Error: download URL path components contains '..' which is unsafe" }]]; + + return; + } + + if (![downloadURLPathComponents containsObject:@SPARKLE_BUNDLE_IDENTIFIER] || ![downloadURLPathComponents containsObject:@"PersistentDownloads"]) { + [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUInstallerError userInfo:@{ NSLocalizedDescriptionKey: @"Error: download URL path components does not contain PersistentDownloads or "@SPARKLE_BUNDLE_IDENTIFIER }]]; + + return; + } + } + if (!isStale) { SULog(SULogLevelError, @"Error: bookmark data for update download is stale.. but still continuing."); } diff --git a/Downloader/SPUDownloader.m b/Downloader/SPUDownloader.m index f9bf0df4..f0f25b35 100644 --- a/Downloader/SPUDownloader.m +++ b/Downloader/SPUDownloader.m @@ -106,6 +106,7 @@ static NSString *SUDownloadingReason = @"Downloading update related file"; - (NSString *)rootPersistentDownloadCachePathForBundleIdentifier:(NSString *)bundleIdentifier SPU_OBJC_DIRECT { + // Note: The installer verifies this "PersistentDownloads" path component return [[SPULocalCacheDirectory cachePathForBundleIdentifier:bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; }