Skip to content

Commit

Permalink
feat: improve CSP security with nonces and hashes, add devCsp [TR…
Browse files Browse the repository at this point in the history
…I-004] (#8)
  • Loading branch information
lucasfernog committed Jan 9, 2022
1 parent dd83217 commit cf54dcf
Show file tree
Hide file tree
Showing 35 changed files with 7,555 additions and 188 deletions.
7 changes: 7 additions & 0 deletions .changes/csp-nonces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": patch
"tauri-codegen": patch
"tauri-utils": patch
---

Apply `nonce` to `script` and `style` tags and set them on the `CSP` (`script-src` and `style-src` fetch directives).
6 changes: 6 additions & 0 deletions .changes/dev-csp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-utils": patch
"cli.rs": patch
---

Added `dev_csp` to the `security` configuration object.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ members = [
exclude = [
# examples that can be compiled with the tauri CLI
"examples/api/src-tauri",
"examples/updater/src-tauri"
"examples/updater/src-tauri",
"examples/resources/src-tauri",
"examples/sidecar/src-tauri"
]

# default to small, optimized workspace release binaries
Expand Down
15 changes: 5 additions & 10 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,13 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
}
}
}
} else if let Some(tauri) = tauri_item.as_value_mut() {
match tauri {
Value::InlineTable(table) => {
if let Some(Value::Array(f)) = table.get("features") {
for feat in f.iter() {
if let Value::String(feature) = feat {
features.push(feature.value().to_string());
}
}
} else if let Some(Value::InlineTable(table)) = tauri_item.as_value_mut() {
if let Some(Value::Array(f)) = table.get("features") {
for feat in f.iter() {
if let Value::String(feature) = feat {
features.push(feature.value().to_string());
}
}
_ => {}
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/tauri-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ exclude = [ ".license_template", "CHANGELOG.md", "/target" ]
readme = "README.md"

[dependencies]
sha2 = "0.9"
base64 = "0.13"
blake3 = { version = "1.2", features = [ "rayon" ] }
proc-macro2 = "1"
quote = "1"
Expand Down
31 changes: 23 additions & 8 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
} = data;

let mut options = AssetOptions::new();
if let Some(csp) = &config.tauri.security.csp {
options = options.csp(csp.clone());
let csp = if dev {
config
.tauri
.security
.dev_csp
.clone()
.or_else(|| config.tauri.security.csp.clone())
} else {
config.tauri.security.csp.clone()
};
if csp.is_some() {
options = options.with_csp();
}

let app_url = if dev {
Expand All @@ -54,12 +64,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
path
)
}
EmbeddedAssets::new(&assets_path, options)?
EmbeddedAssets::new(assets_path, options)?
}
_ => unimplemented!(),
},
AppUrl::Files(files) => EmbeddedAssets::load_paths(
files.iter().map(|p| config_parent.join(p)).collect(),
AppUrl::Files(files) => EmbeddedAssets::new(
files
.iter()
.map(|p| config_parent.join(p))
.collect::<Vec<_>>(),
options,
)?,
_ => unimplemented!(),
Expand Down Expand Up @@ -121,9 +134,11 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
Some(
#root::Icon::File(
#root::api::path::resolve_path(
&#config, &#package_info,
#system_tray_icon_file_path,
Some(#root::api::path::BaseDirectory::Resource)
&#config,
&#package_info,
&Default::default(),
#system_tray_icon_file_path,
Some(#root::api::path::BaseDirectory::Resource)
).expect("failed to resolve resource dir")
)
)
Expand Down
Loading

0 comments on commit cf54dcf

Please sign in to comment.