Skip to content

Commit 4143477

Browse files
add check that the downloaded file has a valid shasum before trying to run it
1 parent fea47d1 commit 4143477

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

Sources/ApolloCodegenLib/ApolloCLI.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public struct ApolloCLI {
1313
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
1414
public static func createCLI(cliFolderURL: URL, timeout: Double) throws -> ApolloCLI {
1515
try CLIDownloader.downloadIfNeeded(cliFolderURL: cliFolderURL, timeout: timeout)
16+
17+
if !(try CLIExtractor.validateSHASUMOfDownloadedFile(in: cliFolderURL)) {
18+
CodegenLogger.log("Downloaded zip file has incorrect SHASUM, forcing redownolad")
19+
try CLIDownloader.forceRedownload(cliFolderURL: cliFolderURL, timeout: timeout)
20+
}
21+
1622
let binaryFolderURL = try CLIExtractor.extractCLIIfNeeded(from: cliFolderURL)
1723
return ApolloCLI(binaryFolderURL: binaryFolderURL)
1824
}

Sources/ApolloCodegenLib/CLIExtractor.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ struct CLIExtractor {
7878
return true
7979
}
8080

81+
static func validateSHASUMOfDownloadedFile(in cliFolderURL: URL, expected: String = CLIExtractor.expectedSHASUM) throws -> Bool {
82+
let zipFileURL = ApolloFilePathHelper.zipFileURL(fromCLIFolder: cliFolderURL)
83+
84+
do {
85+
try self.validateZipFileSHASUM(at: zipFileURL)
86+
return true
87+
} catch {
88+
switch error {
89+
case CLIExtractorError.zipFileHasInvalidSHASUM:
90+
return false
91+
default:
92+
throw error
93+
}
94+
}
95+
}
96+
8197
/// Writes the SHASUM of the extracted version of the CLI to a file for faster checks to ensure we have the correct version.
8298
///
8399
/// - Parameter apolloFolderURL: The URL to the extracted apollo folder.
@@ -119,7 +135,7 @@ struct CLIExtractor {
119135
/// - Parameter expected: The expected SHASUM. Defaults to the real expected SHASUM. This parameter exists mostly for testing.
120136
static func validateZipFileSHASUM(at zipFileURL: URL, expected: String = CLIExtractor.expectedSHASUM) throws {
121137
let shasum = try FileManager.default.apollo.shasum(at: zipFileURL)
122-
print("SHASUM: \(shasum)")
138+
print("SHASUM of downloaded file: \(shasum)")
123139
guard shasum == expected else {
124140
throw CLIExtractorError.zipFileHasInvalidSHASUM(expectedSHASUM: expected, gotSHASUM: shasum)
125141
}

0 commit comments

Comments
 (0)