Skip to content

Commit cf54dcf

Browse files
committed
feat: improve CSP security with nonces and hashes, add devCsp [TRI-004] (#8)
1 parent dd83217 commit cf54dcf

File tree

35 files changed

+7555
-188
lines changed

35 files changed

+7555
-188
lines changed

.changes/csp-nonces.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch
3+
"tauri-codegen": patch
4+
"tauri-utils": patch
5+
---
6+
7+
Apply `nonce` to `script` and `style` tags and set them on the `CSP` (`script-src` and `style-src` fetch directives).

.changes/dev-csp.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-utils": patch
3+
"cli.rs": patch
4+
---
5+
6+
Added `dev_csp` to the `security` configuration object.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ members = [
1212
exclude = [
1313
# examples that can be compiled with the tauri CLI
1414
"examples/api/src-tauri",
15-
"examples/updater/src-tauri"
15+
"examples/updater/src-tauri",
16+
"examples/resources/src-tauri",
17+
"examples/sidecar/src-tauri"
1618
]
1719

1820
# default to small, optimized workspace release binaries

core/tauri-build/src/lib.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,13 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
131131
}
132132
}
133133
}
134-
} else if let Some(tauri) = tauri_item.as_value_mut() {
135-
match tauri {
136-
Value::InlineTable(table) => {
137-
if let Some(Value::Array(f)) = table.get("features") {
138-
for feat in f.iter() {
139-
if let Value::String(feature) = feat {
140-
features.push(feature.value().to_string());
141-
}
142-
}
134+
} else if let Some(Value::InlineTable(table)) = tauri_item.as_value_mut() {
135+
if let Some(Value::Array(f)) = table.get("features") {
136+
for feat in f.iter() {
137+
if let Value::String(feature) = feat {
138+
features.push(feature.value().to_string());
143139
}
144140
}
145-
_ => {}
146141
}
147142
}
148143

core/tauri-codegen/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ exclude = [ ".license_template", "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16+
sha2 = "0.9"
17+
base64 = "0.13"
1618
blake3 = { version = "1.2", features = [ "rayon" ] }
1719
proc-macro2 = "1"
1820
quote = "1"

core/tauri-codegen/src/context.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
2626
} = data;
2727

2828
let mut options = AssetOptions::new();
29-
if let Some(csp) = &config.tauri.security.csp {
30-
options = options.csp(csp.clone());
29+
let csp = if dev {
30+
config
31+
.tauri
32+
.security
33+
.dev_csp
34+
.clone()
35+
.or_else(|| config.tauri.security.csp.clone())
36+
} else {
37+
config.tauri.security.csp.clone()
38+
};
39+
if csp.is_some() {
40+
options = options.with_csp();
3141
}
3242

3343
let app_url = if dev {
@@ -54,12 +64,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
5464
path
5565
)
5666
}
57-
EmbeddedAssets::new(&assets_path, options)?
67+
EmbeddedAssets::new(assets_path, options)?
5868
}
5969
_ => unimplemented!(),
6070
},
61-
AppUrl::Files(files) => EmbeddedAssets::load_paths(
62-
files.iter().map(|p| config_parent.join(p)).collect(),
71+
AppUrl::Files(files) => EmbeddedAssets::new(
72+
files
73+
.iter()
74+
.map(|p| config_parent.join(p))
75+
.collect::<Vec<_>>(),
6376
options,
6477
)?,
6578
_ => unimplemented!(),
@@ -121,9 +134,11 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
121134
Some(
122135
#root::Icon::File(
123136
#root::api::path::resolve_path(
124-
&#config, &#package_info,
125-
#system_tray_icon_file_path,
126-
Some(#root::api::path::BaseDirectory::Resource)
137+
&#config,
138+
&#package_info,
139+
&Default::default(),
140+
#system_tray_icon_file_path,
141+
Some(#root::api::path::BaseDirectory::Resource)
127142
).expect("failed to resolve resource dir")
128143
)
129144
)

0 commit comments

Comments
 (0)