Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wasix): Prevent blocking package hash validations after downloads #5032

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/wasix/src/runtime/package_loader/builtin_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,20 @@ impl BuiltinPackageLoader {
}

/// Validate image contents with the specified validation mode.
fn validate_hash(
async fn validate_hash(
image: &bytes::Bytes,
mode: HashIntegrityValidationMode,
info: &DistributionInfo,
) -> Result<(), anyhow::Error> {
let info = info.clone();
let image = image.clone();
crate::spawn_blocking(move || Self::validate_hash_sync(&image, mode, &info))
.await
.context("tokio runtime failed")?
}

/// Validate image contents with the specified validation mode.
fn validate_hash_sync(
image: &[u8],
mode: HashIntegrityValidationMode,
info: &DistributionInfo,
Expand Down Expand Up @@ -217,9 +230,11 @@ impl BuiltinPackageLoader {
.await?
.with_context(|| format!("Unable to read \"{}\"", path.display()))?;

Self::validate_hash(&bytes, self.hash_validation, dist)?;
let bytes = bytes::Bytes::from(bytes);

return Ok(bytes.into());
Self::validate_hash(&bytes, self.hash_validation, dist).await?;

return Ok(bytes);
}
Err(e) => {
tracing::debug!(
Expand Down Expand Up @@ -265,9 +280,11 @@ impl BuiltinPackageLoader {
let body = response.body.context("package download failed")?;
tracing::debug!(%url, "package_download_succeeded");

Self::validate_hash(&body, self.hash_validation, dist)?;
let body = bytes::Bytes::from(body);

Self::validate_hash(&body, self.hash_validation, dist).await?;

Ok(body.into())
Ok(body)
}

fn headers(&self, url: &Url) -> HeaderMap {
Expand Down
Loading