Skip to content

Commit 4de285c

Browse files
committed
feat(core): validate Cargo features matching allowlist [TRI-023]
1 parent 46f2eae commit 4de285c

File tree

17 files changed

+1706
-1418
lines changed

17 files changed

+1706
-1418
lines changed

.changes/cli.rs-use-tauri-utils.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Use `tauri-utils` to get the `Config` types.

.changes/validate-allowlist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": patch
3+
---
4+
5+
Validate `tauri` dependency `features` under `Cargo.toml` matching `tauri.conf.json`'s `allowlist`.

core/tauri-build/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ rustdoc-args = [ "--cfg", "doc_cfg" ]
2020
anyhow = "1"
2121
quote = { version = "1", optional = true }
2222
tauri-codegen = { version = "1.0.0-beta.4", path = "../tauri-codegen", optional = true }
23+
serde_json = "1.0"
24+
tauri-utils = { version = "1.0.0-beta.0", path = "../tauri-utils", features = [ "build" ] }
25+
toml_edit = "0.5"
2326

2427
[target."cfg(windows)".dependencies]
2528
winres = "0.1"
26-
serde_json = "1.0"
27-
tauri-utils = { version = "1.0.0-beta.0", path = "../tauri-utils", features = [ "build" ] }
2829

2930
[features]
3031
codegen = [ "tauri-codegen", "quote" ]

core/tauri-build/src/lib.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,62 @@ pub fn build() {
104104
/// Non-panicking [`build()`].
105105
#[allow(unused_variables)]
106106
pub fn try_build(attributes: Attributes) -> Result<()> {
107+
use anyhow::anyhow;
108+
use std::fs::read_to_string;
109+
use tauri_utils::config::Config;
110+
use toml_edit::{Document, Item, Table, Value};
111+
112+
println!("cargo:rerun-if-changed=tauri.conf.json");
113+
println!("cargo:rerun-if-changed=src/Cargo.toml");
114+
115+
let config: Config = serde_json::from_str(&read_to_string("tauri.conf.json")?)?;
116+
117+
let mut features = Vec::new();
118+
let mut manifest: Document = read_to_string("Cargo.toml")?.parse::<Document>()?;
119+
let dependencies = manifest
120+
.as_table_mut()
121+
.entry("dependencies")
122+
.or_insert(Item::Table(Table::new()))
123+
.as_table_mut()
124+
.expect("manifest dependencies isn't a table");
125+
let tauri_item = dependencies.entry("tauri").or_insert(Item::None);
126+
if let Some(tauri) = tauri_item.as_table_mut() {
127+
if let Item::Value(Value::Array(f)) = tauri.entry("features").or_insert(Item::None) {
128+
for feat in f.iter() {
129+
if let Value::String(feature) = feat {
130+
features.push(feature.value().to_string());
131+
}
132+
}
133+
}
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+
}
143+
}
144+
}
145+
_ => {}
146+
}
147+
}
148+
149+
features.sort();
150+
let expected_features = config.tauri.features();
151+
if features != expected_features {
152+
return Err(anyhow!("
153+
The `tauri` dependency features on the `Cargo.toml` file does not match the allowlist defined under `tauri.conf.json`.
154+
Please run `tauri dev` or `tauri build` or set it to {:?}.
155+
", expected_features));
156+
}
157+
107158
#[cfg(windows)]
108159
{
109-
use anyhow::{anyhow, Context};
110-
use std::fs::read_to_string;
111-
use tauri_utils::config::Config;
160+
use anyhow::Context;
112161
use winres::WindowsResource;
113162

114-
let config: Config = serde_json::from_str(
115-
&read_to_string("tauri.conf.json").expect("failed to read tauri.conf.json"),
116-
)
117-
.expect("failed to parse tauri.conf.json");
118-
119163
let icon_path_string = attributes
120164
.windows_attributes
121165
.window_icon_path

core/tauri-utils/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ kuchiki = "0.8"
2222
html5ever = "0.25"
2323
proc-macro2 = { version = "1.0", optional = true }
2424
quote = { version = "1.0", optional = true }
25+
schemars = { version = "0.8", features = ["url"], optional = true }
26+
serde_with = "1.10"
2527

2628
[target."cfg(target_os = \"linux\")".dependencies]
2729
heck = "0.4"
2830

2931
[features]
3032
build = [ "proc-macro2", "quote" ]
3133
compression = [ "zstd" ]
34+
schema = ["schemars"]

0 commit comments

Comments
 (0)