Skip to content

Commit c5c702f

Browse files
committed
fix(install): fallback to official source when mirror checksum mismatches
Mirror servers may serve tarballs with different compression parameters or build timestamps, resulting in SHA256 sums that differ from the official version map. When verification fails after a mirror download, automatically delete the mirror archive and re-download from the original URL, then re-verify. This avoids forcing users to manually pass --nomirror when a cached mirror happens to have a mismatched checksum.
1 parent fee7fce commit c5c702f

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

src/command/install.zig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,27 @@ fn installVersion(
133133
};
134134

135135
if (!matches) {
136-
console.err("SHA256 checksum mismatch!", .{});
137-
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
138-
return error.ShasumMismatch;
136+
// Mirror tarball may have a different shasum than the official source.
137+
// Fallback to the original URL and re-verify.
138+
if (actual_url.ptr != tar_url.ptr) {
139+
console.warn("Mirror checksum mismatch, falling back to official source...", .{});
140+
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
141+
try http_client.downloadToFileWithProxy(allocator, zvm.io, zvm.environ_map, tar_url, archive_path, zvm.settings.proxy, stdout);
142+
143+
const fallback_matches = crypto.verifyFileSha256(zvm.io, archive_path, expected) catch {
144+
console.err("Failed to verify checksum", .{});
145+
return error.ShasumMismatch;
146+
};
147+
if (!fallback_matches) {
148+
console.err("SHA256 checksum mismatch!", .{});
149+
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
150+
return error.ShasumMismatch;
151+
}
152+
} else {
153+
console.err("SHA256 checksum mismatch!", .{});
154+
std.Io.Dir.cwd().deleteFile(zvm.io, archive_path) catch {};
155+
return error.ShasumMismatch;
156+
}
139157
}
140158
console.success("Checksum verified.", .{});
141159
}

0 commit comments

Comments
 (0)