Skip to content

Commit

Permalink
fix: --no-sri flag removes integrity hash but keeps "modulepreload" l…
Browse files Browse the repository at this point in the history
…inks for the assets
  • Loading branch information
arnaudpoullet-dkt authored and ctron committed Aug 5, 2024
1 parent ad6c488 commit ca229e4
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/pipelines/rust/sri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,21 @@ impl SriBuilder {
T: AsRef<[u8]>,
Fut: Future<Output = Result<T, E>>,
{
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(())
Expand Down Expand Up @@ -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#"
<link rel="{type}" href="{base}{name}" crossorigin={cross_origin} integrity="{integrity}"{options}>"#,
);
location
.append_html(head, &preload)
.context("Unable to write SRI.")?;
}
let preload = if let Some(integrity) = digest.to_integrity_value() {
format!(
r#"<link rel="{type}" href="{base}{name}" crossorigin={cross_origin} integrity="{integrity}"{options}>"#,
)
} else {
format!(
r#"<link rel="{type}" href="{base}{name}" crossorigin={cross_origin}{options}>"#,
)
};
location
.append_html(head, &preload)
.context("Unable to write SRI.")?;
}

Ok(())
Expand Down

0 comments on commit ca229e4

Please sign in to comment.