From ca229e4feab8e92a205db0d4066845467ad757d6 Mon Sep 17 00:00:00 2001 From: Arnaud Poullet Date: Sat, 3 Aug 2024 20:23:11 +0200 Subject: [PATCH] fix: --no-sri flag removes integrity hash but keeps "modulepreload" links for the assets --- src/pipelines/rust/sri.rs | 50 +++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/pipelines/rust/sri.rs b/src/pipelines/rust/sri.rs index f8f103a8..c9d207af 100644 --- a/src/pipelines/rust/sri.rs +++ b/src/pipelines/rust/sri.rs @@ -71,20 +71,21 @@ impl SriBuilder { T: AsRef<[u8]>, Fut: Future>, { - if !matches!(self.r#type, IntegrityType::None) { - let name = name.into(); - let digest = OutputDigest::generate_async(self.r#type, source).await?; - tracing::debug!( - "recording SRI record - type: {:?}. name: {name}, value: {digest:?}", - self.r#type, - ); - let key = SriKey { r#type, name }; - let entry = SriEntry { digest, options }; - if let Some(record) = self.result.integrities.iter_mut().find(|(k, _)| k == &key) { - record.1 = entry; - } else { - self.result.integrities.push((key, entry)); - } + let name = name.into(); + let digest = match self.r#type { + IntegrityType::None => OutputDigest::default(), + _ => OutputDigest::generate_async(self.r#type, source).await?, + }; + tracing::debug!( + "recording SRI record - type: {:?}. name: {name}, value: {digest:?}", + self.r#type, + ); + let key = SriKey { r#type, name }; + let entry = SriEntry { digest, options }; + if let Some(record) = self.result.integrities.iter_mut().find(|(k, _)| k == &key) { + record.1 = entry; + } else { + self.result.integrities.push((key, entry)); } Ok(()) @@ -149,15 +150,18 @@ impl SriResult { cross_origin: CrossOrigin, ) -> anyhow::Result<()> { for (SriKey { r#type, name }, SriEntry { digest, options }) in &self.integrities { - if let Some(integrity) = digest.to_integrity_value() { - let preload = format!( - r#" -"#, - ); - location - .append_html(head, &preload) - .context("Unable to write SRI.")?; - } + let preload = if let Some(integrity) = digest.to_integrity_value() { + format!( + r#""#, + ) + } else { + format!( + r#""#, + ) + }; + location + .append_html(head, &preload) + .context("Unable to write SRI.")?; } Ok(())