Skip to content

Commit aef81ce

Browse files
committed
webrtc-sys-build: verify downloaded prebuilt library with SHA256
1 parent 66d81e1 commit aef81ce

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Cargo.lock

Lines changed: 15 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ scratch = "1.0"
1414
fs2 = "0.4"
1515
semver = "1.0"
1616
anyhow = "1.0"
17+
sha2 = "0.10.9"
18+
hex-literal = "1.1.0"
19+
hex = "0.4.3"

webrtc-sys/build/src/lib.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ use std::{
2424

2525
use anyhow::{anyhow, Context, Result};
2626
use fs2::FileExt;
27+
use hex_literal::hex;
2728
use regex::Regex;
2829
use reqwest::StatusCode;
30+
use sha2::{Digest, Sha256};
2931

3032
pub const SCRATH_PATH: &str = "livekit_webrtc";
33+
// Update the hash values in download_webrtc function when updating the tag.
3134
pub const WEBRTC_TAG: &str = "webrtc-0001d84-2";
3235
pub const IGNORE_DEFINES: [&str; 2] = ["CR_CLANG_REVISION", "CR_XCODE_VERSION"];
3336

@@ -224,6 +227,62 @@ pub fn download_webrtc() -> Result<()> {
224227
.open(&tmp_path)
225228
.context("Failed to create temporary file for WebRTC download")?;
226229
resp.copy_to(&mut file).context("Failed to write WebRTC download to temporary file")?;
230+
drop(file);
231+
232+
let mut file = fs::File::open(&tmp_path)?;
233+
let mut hasher = Sha256::new();
234+
io::copy(&mut file, &mut hasher)?;
235+
let hash = hasher.finalize();
236+
// GitHub generates the SHA256 hashes of all artifacts attached to releases.
237+
// Copy and paste those here when updating WEBRTC_TAG.
238+
let expected_hash = match webrtc_triple().as_str() {
239+
"android-arm-release" => {
240+
hex!("9e0f49584e8fa2ed7b15e4921a8d630c1b88d891d150d7335fea91bb01844899")
241+
}
242+
"android-arm64-release" => {
243+
hex!("76007fdd92f2eee53f68990890cc52cbd98a2f33df8569dc03a3fe57aba93908")
244+
}
245+
"android-x64-release" => {
246+
hex!("fc551ce022fa69bfdb207b0878bb6d148cc5eebf283ffbf28600d25d6f97de38")
247+
}
248+
"ios-device-arm64-release" => {
249+
hex!("4fcd722678c2ceed448ffbaeba8bb1ce2063ed8ca3d5f2318d6c371f20c43851")
250+
}
251+
"ios-simulator-arm64-release" => {
252+
hex!("b9191da03c89ff39b23ee806d768f139a1b2ca0845597dd6dec2ac8500fa599b")
253+
}
254+
"linux-arm64-release" => {
255+
hex!("d3181bd42900f9b3b15bec4669187861a226d6e8657734f2f51649f71c974bc0")
256+
}
257+
"linux-x64-release" => {
258+
hex!("6a41ae5cdf27ea8fdfb7e2ae3d1abda6b74d8917b77beac5c63ee2b048e28ffd")
259+
}
260+
"mac-arm64-release" => {
261+
hex!("9d7254202cf9b242f648421369d3f053091844e13a32690db5fd5c5b507253be")
262+
}
263+
"mac-x64-release" => {
264+
hex!("d7612ca5626d3e4fc07cb7b6f2b07a9fd5184ffe3f0bba13b119b1116b4ddd9e")
265+
}
266+
"win-arm64-release" => {
267+
hex!("d28480035dc8b83aef2e40ca49bd457bf58b781ebbf274ca90e224a1b29e37c7")
268+
}
269+
"win-x64-release" => {
270+
hex!("16ebb2f7dc15db943313bd80b81bbd9689fa4cbf4bf65f3c407a93cc33d8afe8")
271+
}
272+
_ => panic!("Unsupported triple"),
273+
};
274+
// RustCrypto crypto-common traits are using an old version of generic-array
275+
// https://github.com/fizyk20/generic-array/issues/158
276+
#[allow(deprecated)]
277+
if hash.as_slice() != expected_hash {
278+
panic!(
279+
"SHA256 hash of downloaded prebuilt libwebrtc C++ library did not match the expected value.
280+
Got: {}
281+
Expected: {}",
282+
hex::encode(hash.as_slice()),
283+
hex::encode(expected_hash)
284+
);
285+
}
227286

228287
let mut archive = zip::ZipArchive::new(file).context("Failed to open WebRTC zip archive")?;
229288
archive.extract(webrtc_dir.parent().unwrap()).context("Failed to extract WebRTC archive")?;

0 commit comments

Comments
 (0)