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

feat: allow disabling SRI and minification #635

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ offline = false
frozen = false
# Require Cargo.lock is up to date
locked = false
# Allow disabling minification
no_minification = false
# Allow disabling sub-resource integrity (SRI)
no_sri = false

[watch]
# Paths to watch. The `build.target`'s parent folder is watched by default.
Expand Down
26 changes: 24 additions & 2 deletions src/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,23 @@ pub struct ConfigOptsBuild {
#[serde(default)]
#[arg(long)]
pub root_certificate: Option<String>,

/// Allows request to ignore certificate validation errors.
///
/// Can be useful when behind a corporate proxy.
#[serde(default)]
#[arg(long)]
pub accept_invalid_certs: Option<bool>,

/// Allows disabling minification
#[serde(default)]
#[arg(long)]
pub no_minification: bool,

/// Allows disabling sub-resource integrity (SRI)
#[serde(default)]
#[arg(long)]
pub no_sri: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -181,7 +192,7 @@ pub enum WsProtocol {
}

impl Display for WsProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
Expand Down Expand Up @@ -318,7 +329,7 @@ pub struct ConfigOptsHook {
fn deserialize_uri<'de, D, T>(data: D) -> std::result::Result<T, D::Error>
where
D: Deserializer<'de>,
T: std::convert::From<Uri>,
T: From<Uri>,
{
let val = String::deserialize(data)?;
Uri::from_str(val.as_str())
Expand Down Expand Up @@ -430,6 +441,8 @@ impl ConfigOpts {
locked: cli.locked,
root_certificate: cli.root_certificate,
accept_invalid_certs: cli.accept_invalid_certs,
no_minification: cli.no_minification,
no_sri: cli.no_sri,
};
let cfg_build = ConfigOpts {
build: Some(opts),
Expand Down Expand Up @@ -650,6 +663,15 @@ impl ConfigOpts {
g.pattern_preload = g.pattern_preload.or(l.pattern_preload);
g.pattern_script = g.pattern_script.or(l.pattern_script);
g.pattern_params = g.pattern_params.or(l.pattern_params);
// NOTE: this can not be disabled in the cascade.
if l.no_minification {
g.no_minification = true;
}
// NOTE: this can not be disabled in the cascade.
if l.no_sri {
g.no_sri = true;
}

Some(g)
}
};
Expand Down
8 changes: 8 additions & 0 deletions src/config/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub struct RtcBuild {
///
/// **WARNING**: Setting this to true can make you vulnerable to man-in-the-middle attacks. Sometimes this is necessary when working behind corporate proxies.
pub accept_invalid_certs: Option<bool>,
/// Allow disabling minification
pub no_minification: bool,
/// Allow disabling SRI
pub no_sri: bool,
}

impl RtcBuild {
Expand Down Expand Up @@ -153,6 +157,8 @@ impl RtcBuild {
locked: opts.locked,
root_certificate: opts.root_certificate.map(PathBuf::from),
accept_invalid_certs: opts.accept_invalid_certs,
no_minification: opts.no_minification,
no_sri: opts.no_sri,
})
}

Expand Down Expand Up @@ -192,6 +198,8 @@ impl RtcBuild {
locked: false,
root_certificate: None,
accept_invalid_certs: None,
no_minification: false,
no_sri: false,
})
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/pipelines/css.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! CSS asset pipeline.

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INTEGRITY};
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
use crate::{
config::RtcBuild,
pipelines::AssetFileType,
Expand All @@ -9,7 +9,6 @@ use crate::{
use anyhow::{Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use tokio::task::JoinHandle;

Expand Down Expand Up @@ -44,11 +43,7 @@ impl Css {
path.extend(href_attr.split('/'));
let asset = AssetFile::new(&html_dir, path).await?;

let integrity = attrs
.get(ATTR_INTEGRITY)
.map(|value| IntegrityType::from_str(value))
.transpose()?
.unwrap_or_default();
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

Ok(Self {
id,
Expand All @@ -75,7 +70,7 @@ impl Css {
.copy(
&self.cfg.staging_dist,
self.cfg.filehash,
self.cfg.release,
self.cfg.release && !self.cfg.no_minification,
AssetFileType::Css,
)
.await?;
Expand Down
4 changes: 2 additions & 2 deletions src/pipelines/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct HtmlPipeline {
/// An optional channel to be used to communicate ignore paths to the watcher.
ignore_chan: Option<mpsc::Sender<PathBuf>>,
/// Protocol used for autoreload WebSockets connection.
pub ws_protocol: Option<WsProtocol>,
ws_protocol: Option<WsProtocol>,
}

impl HtmlPipeline {
Expand Down Expand Up @@ -179,7 +179,7 @@ impl HtmlPipeline {
self.finalize_html(&mut target_html);

// Assemble a new output index.html file.
let output_html = match self.cfg.release {
let output_html = match self.cfg.release && !self.cfg.no_minification {
true => {
let mut minify_cfg = minify_html::Cfg::spec_compliant();
minify_cfg.minify_css = true;
Expand Down
23 changes: 8 additions & 15 deletions src/pipelines/icon.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
//! Icon asset pipeline.

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
use crate::config::RtcBuild;
use crate::pipelines::{AssetFileType, ImageType};
use crate::processing::integrity::{IntegrityType, OutputDigest};
use anyhow::{Context, Result};
use nipper::Document;
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{Context, Result};
use nipper::Document;
use tokio::task::JoinHandle;

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INTEGRITY};
use crate::config::RtcBuild;
use crate::pipelines::{AssetFileType, ImageType};
use crate::processing::integrity::{IntegrityType, OutputDigest};

/// An Icon asset pipeline.
pub struct Icon {
/// The ID of this pipeline's source HTML element.
Expand Down Expand Up @@ -43,11 +40,7 @@ impl Icon {
path.extend(href_attr.split('/'));
let asset = AssetFile::new(&html_dir, path).await?;

let integrity = attrs
.get(ATTR_INTEGRITY)
.map(|value| IntegrityType::from_str(value))
.transpose()?
.unwrap_or_default();
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

Ok(Self {
id,
Expand Down Expand Up @@ -78,7 +71,7 @@ impl Icon {
.copy(
&self.cfg.staging_dist,
self.cfg.filehash,
self.cfg.release,
self.cfg.release && !self.cfg.no_minification,
AssetFileType::Icon(image_type),
)
.await?;
Expand Down
11 changes: 3 additions & 8 deletions src/pipelines/js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! JS asset pipeline.

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_INTEGRITY, ATTR_SRC};
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_SRC};
use crate::{
config::RtcBuild,
pipelines::AssetFileType,
Expand All @@ -9,7 +9,6 @@ use crate::{
use anyhow::{Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use tokio::task::JoinHandle;

Expand Down Expand Up @@ -42,11 +41,7 @@ impl Js {
path.extend(src_attr.split('/'));
let asset = AssetFile::new(&html_dir, path).await?;

let integrity = attrs
.get(ATTR_INTEGRITY)
.map(|value| IntegrityType::from_str(value))
.transpose()?
.unwrap_or_default();
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

// Remove src and data-trunk from attributes.
let attrs = attrs
Expand Down Expand Up @@ -79,7 +74,7 @@ impl Js {
.copy(
&self.cfg.staging_dist,
self.cfg.filehash,
self.cfg.release,
self.cfg.release && !self.cfg.no_minification,
AssetFileType::Js,
)
.await?;
Expand Down
1 change: 0 additions & 1 deletion src/pipelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const ATTR_HREF: &str = "href";
const ATTR_SRC: &str = "src";
const ATTR_TYPE: &str = "type";
const ATTR_REL: &str = "rel";
const ATTR_INTEGRITY: &str = "data-integrity";
const SNIPPETS_DIR: &str = "snippets";
const TRUNK_ID: &str = "data-trunk-id";
const PNG_OPTIMIZATION_LEVEL: u8 = 6;
Expand Down
11 changes: 4 additions & 7 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ impl RustApp {
.map(|val| CrossOrigin::from_str(val))
.transpose()?
.unwrap_or_default();
let integrity = attrs
.get("data-integrity")
.map(|val| IntegrityType::from_str(val))
.transpose()?
.unwrap_or_default();
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

let manifest = CargoMetadata::new(&manifest_href).await?;
let id = Some(id);
Expand Down Expand Up @@ -238,6 +234,7 @@ impl RustApp {

let manifest = CargoMetadata::new(&path).await?;
let name = manifest.package.name.clone();
let integrity = IntegrityType::default_unless(cfg.no_sri);

Ok(Some(Self {
id: None,
Expand All @@ -256,7 +253,7 @@ impl RustApp {
name,
loader_shim: false,
cross_origin: Default::default(),
integrity: Default::default(),
integrity,
import_bindings: true,
import_bindings_name: None,
}))
Expand Down Expand Up @@ -619,7 +616,7 @@ impl RustApp {
.await
.context("error reading JS loader file")?;

let write_bytes = match self.cfg.release {
let write_bytes = match self.cfg.release && !self.cfg.no_minification {
true => {
let mut output: Vec<u8> = vec![];
let bytes_clone = bytes.clone();
Expand Down
13 changes: 3 additions & 10 deletions src/pipelines/sass.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Sass/Scss asset pipeline.

use super::{
AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INLINE, ATTR_INTEGRITY,
};
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INLINE};
use crate::{
common,
config::RtcBuild,
Expand All @@ -12,7 +10,6 @@ use crate::{
use anyhow::{ensure, Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use tokio::fs;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -52,11 +49,7 @@ impl Sass {
let asset = AssetFile::new(&html_dir, path).await?;
let use_inline = attrs.get(ATTR_INLINE).is_some();

let integrity = attrs
.get(ATTR_INTEGRITY)
.map(|value| IntegrityType::from_str(value))
.transpose()?
.unwrap_or_default();
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

Ok(Self {
id,
Expand Down Expand Up @@ -105,7 +98,7 @@ impl Sass {
let args = &[
"--no-source-map",
"--style",
match &self.cfg.release {
match self.cfg.release && !self.cfg.no_minification {
true => "compressed",
false => "expanded",
},
Expand Down
25 changes: 8 additions & 17 deletions src/pipelines/tailwind_css.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
//! Tailwind CSS asset pipeline.

use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{Context, Result};
use nipper::Document;
use tokio::fs;
use tokio::task::JoinHandle;

use super::{
AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INLINE, ATTR_INTEGRITY,
};
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INLINE};
use crate::common;
use crate::config::RtcBuild;
use crate::processing::integrity::{IntegrityType, OutputDigest};
use crate::tools::{self, Application};
use anyhow::{Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::fs;
use tokio::task::JoinHandle;

/// A tailwind css asset pipeline.
pub struct TailwindCss {
Expand Down Expand Up @@ -51,11 +46,7 @@ impl TailwindCss {
let asset = AssetFile::new(&html_dir, path).await?;
let use_inline = attrs.get(ATTR_INLINE).is_some();

let integrity = attrs
.get(ATTR_INTEGRITY)
.map(|value| IntegrityType::from_str(value))
.transpose()?
.unwrap_or_default();
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

Ok(Self {
id,
Expand Down
Loading