Skip to content

Commit 383e537

Browse files
authored
Check integrity of webrtc download in build script (#6)
We see periodic failures of zed-industries/zed CI when the `cc` builder in the `webrtc-sys` build script fails to find some webrtc header files that are supposed to be downloaded using this function: https://github.com/zed-industries/livekit-rust-sdks/blob/d2eade7a6b15d6dbdb38ba12a1ff7bf07fcebba4/webrtc-sys/build/src/lib.rs#L179 This downloads a zip from the assets attached to a specific livekit/rust-sdks release, and unpacks it under OUT_DIR. My guess is that sometimes the download step fails and produces a corrupt downloaded zip, without signaling the failure in a way that would stop the build. This PR tries to fix that by having the download step check the SHA-256 digest of the downloaded zip (using a manually-computed list of good digests; I'd like to use digests from the GitHub API but the particular artifacts we download don't have them). If we confirm that this is the source of the problem, I'll follow up with a change to retry the download in the build script.
1 parent d2eade7 commit 383e537

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webrtc-sys/build/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ regex = "1.0"
1313
scratch = "1.0"
1414
fs2 = "0.4"
1515
semver = "1.0"
16+
sha2 = "0.10"
17+
hex-literal = "1.0.0"

webrtc-sys/build/src/lib.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::{
16+
collections::HashMap,
1617
env,
1718
error::Error,
1819
fs::{self, File},
@@ -22,8 +23,10 @@ use std::{
2223
};
2324

2425
use fs2::FileExt;
26+
use hex_literal::hex;
2527
use regex::Regex;
2628
use reqwest::StatusCode;
29+
use sha2::{Digest as _, Sha256};
2730

2831
pub const SCRATH_PATH: &str = "livekit_webrtc";
2932
pub const WEBRTC_TAG: &str = "webrtc-b99fd2c-6";
@@ -191,16 +194,56 @@ pub fn download_webrtc() -> Result<(), Box<dyn Error>> {
191194
return Err(format!("failed to download webrtc: {}", resp.status()).into());
192195
}
193196

197+
let digests = [
198+
(
199+
"linux-arm64-release",
200+
hex!("363d90ed91ffc8fc70c94ad86759876a3255d526ac0792fbf9e9462cac964b81"),
201+
),
202+
(
203+
"linux-x64-release",
204+
hex!("130b37ae8d3ffcdff8ed9347cb482a493075b586cbfaa9ebe1f0079ddb734ff1"),
205+
),
206+
(
207+
"mac-arm64-release",
208+
hex!("b29fed70cfa8282fce1e987b3abf9a471b5d223ddcf03f64282f8d5f0cc89542"),
209+
),
210+
(
211+
"mac-x64-release",
212+
hex!("3f24dc470977d976aa3b72add36dec6822f1da65031b493a67645af1e7aca792"),
213+
),
214+
(
215+
"win-arm64-release",
216+
hex!("69bafa396c4032b06468c396da6a05a431761631f47a1e5d1e8a2cc6c3dd6fe9"),
217+
),
218+
(
219+
"win-x64-release",
220+
hex!("eb7dcc30bc7c3f331acd8648495b420e7f2445f5d8d49ddb755026b2b572495d"),
221+
),
222+
]
223+
.into_iter()
224+
.collect::<HashMap<_, _>>();
225+
194226
let tmp_path = env::var("OUT_DIR").unwrap() + "/webrtc.zip";
195227
let tmp_path = path::Path::new(&tmp_path);
196-
let mut file = fs::File::options().write(true).read(true).create(true).open(tmp_path)?;
228+
let mut file =
229+
fs::File::options().write(true).read(true).create(true).truncate(true).open(tmp_path)?;
197230
resp.copy_to(&mut file)?;
231+
let content = std::fs::read(tmp_path)?;
232+
let digest = Sha256::digest(&content);
233+
if digest.as_slice() != digests[webrtc_triple().as_str()].as_slice() {
234+
return Err("corrupt webrtc download".to_owned().into());
235+
}
198236

199-
let mut archive = zip::ZipArchive::new(file)?;
200-
archive.extract(webrtc_dir.parent().unwrap())?;
201-
drop(archive);
237+
let result = (|| {
238+
let mut archive = zip::ZipArchive::new(file)?;
239+
archive.extract(webrtc_dir.parent().unwrap())?;
240+
Ok::<_, Box<dyn Error>>(())
241+
})();
242+
if result.is_err() {
243+
std::fs::remove_dir_all(webrtc_dir).ok();
244+
}
245+
result?;
202246

203-
fs::remove_file(tmp_path)?;
204247
Ok(())
205248
}
206249

0 commit comments

Comments
 (0)