From 7b62035ec5b343414e8a760696e5fadcd57d3b09 Mon Sep 17 00:00:00 2001 From: Gustavo Figueiredo Date: Mon, 23 Nov 2026 17:26:00 +0100 Subject: [PATCH 1/3] feat(compilers): pin Solar release checksums Solar installs checked the archive against the `.sha256` file served next to it, which only detects corrupted downloads. Known releases now use checksums pinned in this crate, generated from each release's `sha256.sum` by `scripts/update-solar-releases.sh`. Unknown releases keep the old check. Add `Solc::released_solar_versions`, `Solc::installed_solar_versions`, and `Solc::remove_solar`, so tools can list and manage Solar installs the way svm does for solc. --- .../compilers/src/compilers/solc/mod.rs | 2 + .../src/compilers/solc/solar_release.rs | 157 ++++++++++++++---- .../src/compilers/solc/solar_releases.rs | 69 ++++++++ scripts/update-solar-releases.sh | 56 +++++++ 4 files changed, 251 insertions(+), 33 deletions(-) create mode 100644 crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs create mode 100755 scripts/update-solar-releases.sh diff --git a/crates/compilers/crates/compilers/src/compilers/solc/mod.rs b/crates/compilers/crates/compilers/src/compilers/solc/mod.rs index c4057a45..0c8fa479 100644 --- a/crates/compilers/crates/compilers/src/compilers/solc/mod.rs +++ b/crates/compilers/crates/compilers/src/compilers/solc/mod.rs @@ -35,6 +35,8 @@ pub use compiler::{SOLC_EXTENSIONS, Solc}; #[cfg(feature = "svm-solc")] mod solar_release; +#[cfg(feature = "svm-solc")] +mod solar_releases; #[derive(Clone, Debug)] #[cfg_attr(feature = "svm-solc", derive(Default))] diff --git a/crates/compilers/crates/compilers/src/compilers/solc/solar_release.rs b/crates/compilers/crates/compilers/src/compilers/solc/solar_release.rs index 3f057ed3..26b84068 100644 --- a/crates/compilers/crates/compilers/src/compilers/solc/solar_release.rs +++ b/crates/compilers/crates/compilers/src/compilers/solc/solar_release.rs @@ -1,6 +1,6 @@ //! Installs Solar releases into `~/.foundry/solar`. -use super::Solc; +use super::{Solc, solar_releases::SOLAR_RELEASES}; use alloy_primitives::hex; use foundry_compilers_core::{ error::{Result, SolcError}, @@ -10,8 +10,8 @@ use semver::Version; use sha2::Digest; use std::{ fs, - io::{Read, Write}, - path::PathBuf, + io::{ErrorKind, Read, Write}, + path::{Path, PathBuf}, time::Duration, }; @@ -41,6 +41,14 @@ const RELEASE_TARGET: Option<&str> = if cfg!(all(target_os = "macos", target_arc /// The archive extension of Solar releases on this platform. const ARCHIVE_EXTENSION: &str = if cfg!(windows) { "zip" } else { "tar.gz" }; +/// A Solar release and the SHA-256 checksums of its archives. +pub(super) struct SolarRelease { + /// The release version. + pub(super) version: Version, + /// The archive file names and their SHA-256 checksums. + pub(super) archives: &'static [(&'static str, [u8; 32])], +} + impl Solc { /// Returns the Solar `version` installed in `~/.foundry/solar`, if any. /// @@ -55,8 +63,10 @@ impl Solc { /// Downloads the Solar `version` release from GitHub into `~/.foundry/solar`. /// - /// The archive is checked against the release's SHA-256 checksum file, which detects corrupted - /// downloads, and the binary must report the requested version. + /// If the release is in [`Self::released_solar_versions`], the archive must match the checksum + /// pinned in this crate. Otherwise, the archive is checked against the release's SHA-256 + /// checksum file, which only detects corrupted downloads. The binary must report the requested + /// version. #[instrument(name = "Solc::blocking_install_solar", skip_all)] pub fn blocking_install_solar(version: &Version) -> Result { crate::report::solar_installation_start(version); @@ -67,6 +77,43 @@ impl Solc { } result } + + /// Returns the Solar releases known to this crate, oldest first. + /// + /// These releases have pinned checksums. Other releases can still be installed. + pub fn released_solar_versions() -> Vec { + SOLAR_RELEASES.iter().map(|release| release.version.clone()).collect() + } + + /// Returns the Solar versions installed in `~/.foundry/solar`, oldest first. + pub fn installed_solar_versions() -> Vec { + let Ok(dir) = solar_home() else { return Vec::new() }; + let Ok(entries) = fs::read_dir(dir) else { return Vec::new() }; + let mut versions = entries + .filter_map(|entry| Version::parse(entry.ok()?.file_name().to_str()?).ok()) + .filter(|version| solar_binary(version).is_ok_and(|binary| binary.is_file())) + .collect::>(); + versions.sort(); + versions + } + + /// Removes the Solar `version` binary from `~/.foundry/solar`. + /// + /// Returns `false` if the version is not installed. The lock file stays, as in + /// [`Self::blocking_install_solar`]. + pub fn remove_solar(version: &Version) -> Result { + let dir = solar_dir(version)?; + if !dir.is_dir() { + return Ok(false); + } + let _lock = lock_solar_dir(&dir)?; + let binary = solar_binary(version)?; + match fs::remove_file(&binary) { + Ok(()) => Ok(true), + Err(err) if err.kind() == ErrorKind::NotFound => Ok(false), + Err(err) => Err(SolcError::io(err, &binary)), + } + } } fn install_solar(version: &Version) -> Result { @@ -79,33 +126,28 @@ fn install_solar(version: &Version) -> Result { let dir = solar_dir(version)?; fs::create_dir_all(&dir).map_err(|err| SolcError::io(err, &dir))?; - // Serializes parallel installs of the same version. The lock file is never removed, as in svm. - let lock_path = dir.join(".lock"); - let lock = fs::OpenOptions::new() - .create(true) - .truncate(false) - .write(true) - .open(&lock_path) - .map_err(|err| SolcError::io(err, &lock_path))?; - lock.lock().map_err(|err| SolcError::io(err, &lock_path))?; + let _lock = lock_solar_dir(&dir)?; if let Some(solar) = Solc::find_installed_solar(version) { return Ok(solar); } - let url = format!( - "https://github.com/paradigmxyz/solar/releases/download/v{version}/solar-{target}.{ARCHIVE_EXTENSION}" - ); - let (archive, checksum) = RuntimeOrHandle::new().block_on(async { + let file_name = format!("solar-{target}.{ARCHIVE_EXTENSION}"); + let url = + format!("https://github.com/paradigmxyz/solar/releases/download/v{version}/{file_name}"); + let pinned = pinned_checksum(version, &file_name); + let (archive, expected) = RuntimeOrHandle::new().block_on(async { let client = reqwest::Client::builder() .timeout(REQUEST_TIMEOUT) .build() .map_err(|err| SolcError::msg(format!("failed to create HTTP client: {err}")))?; - Ok::<_, SolcError>(( - download(&client, &url).await?, - download(&client, &format!("{url}.sha256")).await?, - )) + let archive = download(&client, &url).await?; + let expected = match pinned { + Some(checksum) => checksum.to_vec(), + None => parse_checksum(&download(&client, &format!("{url}.sha256")).await?, &url)?, + }; + Ok::<_, SolcError>((archive, expected)) })?; - verify_checksum(&archive, &checksum, &url)?; + verify_checksum(&archive, &expected, &url)?; let binary = if cfg!(windows) { extract_zip(&archive)? } else { extract_tar_gz(&archive)? }; // Checks the binary under a fresh path before publishing it, since versions are cached by path. @@ -136,14 +178,19 @@ fn install_solar(version: &Version) -> Result { Ok(Solc::new_with_version(path, found.version)) } -/// Returns the directory of the Solar `version` installation: `~/.foundry/solar/`. +/// Returns the directory of Solar installations: `~/.foundry/solar`. /// /// This stays outside svm's data directory, since svm-rs 0.5.26 and older fail to list installed /// solc versions when it contains an entry that is not a version. -fn solar_dir(version: &Version) -> Result { +fn solar_home() -> Result { let home = dirs::home_dir() .ok_or_else(|| SolcError::msg("could not detect the user home directory"))?; - Ok(home.join(".foundry").join("solar").join(version.to_string())) + Ok(home.join(".foundry").join("solar")) +} + +/// Returns the directory of the Solar `version` installation: `~/.foundry/solar/`. +fn solar_dir(version: &Version) -> Result { + Ok(solar_home()?.join(version.to_string())) } /// Returns the path of the installed Solar `version` binary. @@ -151,6 +198,27 @@ fn solar_binary(version: &Version) -> Result { Ok(solar_dir(version)?.join(format!("solar{}", std::env::consts::EXE_SUFFIX))) } +/// Locks the Solar installation `dir`, which serializes installs and removals of one version. +/// +/// The lock file is never removed, as in svm. +fn lock_solar_dir(dir: &Path) -> Result { + let lock_path = dir.join(".lock"); + let lock = fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(&lock_path) + .map_err(|err| SolcError::io(err, &lock_path))?; + lock.lock().map_err(|err| SolcError::io(err, &lock_path))?; + Ok(lock) +} + +/// Returns the checksum pinned in this crate for the archive `file_name` of the Solar `version`. +fn pinned_checksum(version: &Version, file_name: &str) -> Option<&'static [u8; 32]> { + let release = SOLAR_RELEASES.iter().find(|release| release.version == *version)?; + release.archives.iter().find(|(name, _)| *name == file_name).map(|(_, checksum)| checksum) +} + async fn download(client: &reqwest::Client, url: &str) -> Result> { let response = client .get(url) @@ -165,13 +233,17 @@ async fn download(client: &reqwest::Client, url: &str) -> Result> { Ok(bytes.into()) } -/// Checks `archive` against a ` ` checksum file. -fn verify_checksum(archive: &[u8], checksum: &[u8], url: &str) -> Result<()> { - let expected = std::str::from_utf8(checksum) +/// Returns the checksum of a ` ` checksum file. +fn parse_checksum(checksum: &[u8], url: &str) -> Result> { + std::str::from_utf8(checksum) .ok() .and_then(|checksum| checksum.split_whitespace().next()) .and_then(|checksum| hex::decode(checksum).ok()) - .ok_or_else(|| SolcError::msg(format!("invalid checksum file for {url}")))?; + .ok_or_else(|| SolcError::msg(format!("invalid checksum file for {url}"))) +} + +/// Checks `archive` against the `expected` SHA-256 checksum. +fn verify_checksum(archive: &[u8], expected: &[u8], url: &str) -> Result<()> { let detected = sha2::Sha256::digest(archive); if detected[..] != expected[..] { return Err(SolcError::msg(format!( @@ -229,9 +301,26 @@ mod tests { fn rejects_checksum_mismatch() { let archive = b"archive"; let checksum = format!("{} solar.tar.gz\n", hex::encode(sha2::Sha256::digest(archive))); - verify_checksum(archive, checksum.as_bytes(), "url").unwrap(); - verify_checksum(b"tampered", checksum.as_bytes(), "url").unwrap_err(); - verify_checksum(archive, b"not hex", "url").unwrap_err(); + let expected = parse_checksum(checksum.as_bytes(), "url").unwrap(); + verify_checksum(archive, &expected, "url").unwrap(); + verify_checksum(b"tampered", &expected, "url").unwrap_err(); + parse_checksum(b"not hex", "url").unwrap_err(); + } + + #[test] + fn pins_supported_release_checksums() { + assert!(SOLAR_RELEASES.is_sorted_by(|a, b| a.version < b.version)); + assert!(SOLAR_RELEASES.first().unwrap().version >= Version::new(0, 2, 0)); + // Every release must pin the archive that this platform downloads. + if let Some(target) = RELEASE_TARGET { + let file_name = format!("solar-{target}.{ARCHIVE_EXTENSION}"); + for release in SOLAR_RELEASES { + assert!(pinned_checksum(&release.version, &file_name).is_some()); + } + } + assert!( + pinned_checksum(&Version::new(0, 1, 8), "solar-x86_64-apple-darwin.tar.gz").is_none() + ); } #[test] @@ -281,5 +370,7 @@ mod tests { let solar = Solc::blocking_install_solar(&version).unwrap(); assert_eq!(solar.solar_version(), Some(version.clone())); assert_eq!(Solc::find_installed_solar(&version).unwrap().solc, solar.solc); + assert!(Solc::installed_solar_versions().contains(&version)); + assert!(!Solc::remove_solar(&Version::new(0, 0, 1)).unwrap()); } } diff --git a/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs b/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs new file mode 100644 index 00000000..0054de67 --- /dev/null +++ b/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs @@ -0,0 +1,69 @@ +//! Solar releases with the SHA-256 checksum of each release archive. +//! +//! Generated by `scripts/update-solar-releases.sh`. Do not edit. + +use super::solar_release::SolarRelease; +use alloy_primitives::hex; +use semver::Version; + +/// Solar releases, oldest first. +pub(super) const SOLAR_RELEASES: &[SolarRelease] = &[ + SolarRelease { + version: Version::new(0, 2, 0), + archives: &[ + ( + "solar-aarch64-apple-darwin.tar.gz", + hex!("8152febc0e832fc75d44de50854e208879777091dad46707cfeef864e9351586"), + ), + ( + "solar-aarch64-unknown-linux-gnu.tar.gz", + hex!("5d2b0aa46ad71d17d4c9371aac900cdefce87db136a0f7f09c29fae7fbd299ff"), + ), + ( + "solar-x86_64-apple-darwin.tar.gz", + hex!("7c5d0608a5a18ba297f01e8c5e48de8ed05edd9c6225551338502f82e1f89343"), + ), + ( + "solar-x86_64-pc-windows-msvc.zip", + hex!("a7f1d68f3fa57f45d78aca97fb29e79c0cd49cd4e47d450a50d3e776bfbf782a"), + ), + ( + "solar-x86_64-unknown-linux-gnu.tar.gz", + hex!("56cde37acb69700e6506f2b4b17851135b129226a5fb6a27d5df788310b43022"), + ), + ( + "solar-x86_64-unknown-linux-musl.tar.gz", + hex!("027cd53c38deb240fb7240e9bb2a2be92d7138c544a3aae39e177f6fa3b41d4d"), + ), + ], + }, + SolarRelease { + version: Version::new(0, 3, 0), + archives: &[ + ( + "solar-aarch64-apple-darwin.tar.gz", + hex!("6d213a61324b77d55868795b516661c84ec77b41c60befddb4f0cb7a0168d0b1"), + ), + ( + "solar-aarch64-unknown-linux-gnu.tar.gz", + hex!("8da0507426756ec175d281d421ea4fb44d02e3fb9f46ff8ff79802c5d547e95c"), + ), + ( + "solar-x86_64-apple-darwin.tar.gz", + hex!("df93b34f1568d4bd7bd2ddf46d5d5056be76166c99d008c4186166c2dbe8238a"), + ), + ( + "solar-x86_64-pc-windows-msvc.zip", + hex!("d3c75bc2d28c330f3c83b6a122a5490c4ad01d2afe64f4598238e75434affc0d"), + ), + ( + "solar-x86_64-unknown-linux-gnu.tar.gz", + hex!("15a613746dd30ca772664824d97feddb5c5163ec2380aa556a2d270eacedc5fc"), + ), + ( + "solar-x86_64-unknown-linux-musl.tar.gz", + hex!("e01eece887e28438a2398e9ec17f1038cfd2b2f93e721382a9b6422020a7fa66"), + ), + ], + }, +]; diff --git a/scripts/update-solar-releases.sh b/scripts/update-solar-releases.sh new file mode 100755 index 00000000..ef393281 --- /dev/null +++ b/scripts/update-solar-releases.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Regenerates the Solar release table from the `sha256.sum` file of each Solar release. +# +# Run it after a Solar release and commit the result, so installs check the archive against a +# checksum pinned in this repository instead of one served next to the archive. +set -euo pipefail + +cd "$(dirname "$0")/.." + +repo="paradigmxyz/solar" +out="crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs" +# Releases before 0.2.0 do not report their Solar version, so they cannot be pinned. +min="0.2.0" + +versions=$( + git ls-remote --tags --refs "https://github.com/$repo" 'v*' | + sed -E 's|.*refs/tags/v||' | + grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | + sort -t. -k1,1n -k2,2n -k3,3n | + awk -v min="$min" '{ + split($0, v, "."); split(min, m, "."); + if (v[1] > m[1] || (v[1] == m[1] && (v[2] > m[2] || (v[2] == m[2] && v[3] >= m[3])))) print + }' +) + +{ + echo "//! Solar releases with the SHA-256 checksum of each release archive." + echo "//!" + echo "//! Generated by \`scripts/update-solar-releases.sh\`. Do not edit." + echo + echo "use super::solar_release::SolarRelease;" + echo "use alloy_primitives::hex;" + echo "use semver::Version;" + echo + echo "/// Solar releases, oldest first." + echo "pub(super) const SOLAR_RELEASES: &[SolarRelease] = &[" + for version in $versions; do + IFS=. read -r major minor patch <<<"$version" + sums=$(curl -fsSL "https://github.com/$repo/releases/download/v$version/sha256.sum") + echo " SolarRelease {" + echo " version: Version::new($major, $minor, $patch)," + echo " archives: &[" + echo "$sums" | sed -E 's/^([0-9a-f]{64}) [ *]?(.*)$/\2 \1/' | sort | while read -r file sha; do + case "$file" in + solar-*.tar.gz | solar-*.zip) + echo " (\"$file\", hex!(\"$sha\"))," + ;; + esac + done + echo " ]," + echo " }," + done + echo "];" +} >"$out" +rustfmt +nightly --edition 2024 "$out" +echo "wrote $out" From ea201facc7ba235632d0e6c9ee2c9bb5f857d31a Mon Sep 17 00:00:00 2001 From: Gustavo Figueiredo Date: Wed, 25 Nov 2026 11:49:41 +0100 Subject: [PATCH 2/3] chore(compilers): pin Solar 0.3.1 --- .../src/compilers/solc/solar_releases.rs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs b/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs index 0054de67..51be73ae 100644 --- a/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs +++ b/crates/compilers/crates/compilers/src/compilers/solc/solar_releases.rs @@ -66,4 +66,33 @@ pub(super) const SOLAR_RELEASES: &[SolarRelease] = &[ ), ], }, + SolarRelease { + version: Version::new(0, 3, 1), + archives: &[ + ( + "solar-aarch64-apple-darwin.tar.gz", + hex!("820a253db5d65b873bce89191c57f44fcdbfca254c9510e293c14505925db678"), + ), + ( + "solar-aarch64-unknown-linux-gnu.tar.gz", + hex!("e4c86ecc9ad5f27540835ac7ac70a32372fa7d91bfaedb3d6e71470ec2de5f67"), + ), + ( + "solar-x86_64-apple-darwin.tar.gz", + hex!("da8273c3a220599b574b2344d32f3343d30432bea038e7928819da3e62f1a292"), + ), + ( + "solar-x86_64-pc-windows-msvc.zip", + hex!("e83a1143e2a42f4da48a947eac155076797b732f822a039ec631167d129933ed"), + ), + ( + "solar-x86_64-unknown-linux-gnu.tar.gz", + hex!("0a620c9da5918c773a8630a8a25da83ee2b6101dcf53b7eea304af96b41e8b4a"), + ), + ( + "solar-x86_64-unknown-linux-musl.tar.gz", + hex!("7de1b3c9fd7e20b8b2383bc5f4f369a262f09a74164c9698f45c28b397ff2bb2"), + ), + ], + }, ]; From 0afcfe392003c2a7178cc9438e020660039a1ac9 Mon Sep 17 00:00:00 2001 From: Gustavo Figueiredo Date: Wed, 25 Nov 2026 12:12:51 +0100 Subject: [PATCH 3/3] fix(scripts): replace the Solar release table atomically --- scripts/update-solar-releases.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scripts/update-solar-releases.sh b/scripts/update-solar-releases.sh index ef393281..3e4e036e 100755 --- a/scripts/update-solar-releases.sh +++ b/scripts/update-solar-releases.sh @@ -23,6 +23,11 @@ versions=$( }' ) +# Writes to a temporary file next to the table and moves it into place only after generation and +# formatting succeed, so a failed download leaves the checked-in table untouched. +tmp=$(mktemp "$(dirname "$out")/.solar_releases.XXXXXX") +trap 'rm -f "$tmp"' EXIT + { echo "//! Solar releases with the SHA-256 checksum of each release archive." echo "//!" @@ -51,6 +56,8 @@ versions=$( echo " }," done echo "];" -} >"$out" -rustfmt +nightly --edition 2024 "$out" +} >"$tmp" +rustfmt +nightly --edition 2024 "$tmp" +chmod 644 "$tmp" +mv "$tmp" "$out" echo "wrote $out"