Skip to content

Commit b8be924

Browse files
authored
fix(cli): escape user values in the tauri init config template (#16106)
1 parent cada1cd commit b8be924

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
`tauri init` no longer panics when the app name, window title, frontend dist, dev URL or before dev/build commands contain quotes or backslashes (e.g. `-D ..\dist` or `vite --host "0.0.0.0"`); the values are now JSON-escaped in the generated `tauri.conf.json`.

‎crates/tauri-cli/src/init.rs‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,7 @@ pub fn command(mut options: Options) -> Result<()> {
228228
to_json(options.before_build_command),
229229
);
230230

231-
let mut config = serde_json::from_str(
232-
&handlebars
233-
.render_template(TAURI_CONF_TEMPLATE, &data)
234-
.expect("Failed to render tauri.conf.json template"),
235-
)
236-
.unwrap();
231+
let mut config = render_tauri_config(&data)?;
237232
if option_env!("TARGET") == Some("node") {
238233
let mut dir = current_dir().expect("failed to read cwd");
239234
let mut count = 0;
@@ -290,6 +285,25 @@ pub fn command(mut options: Options) -> Result<()> {
290285
Ok(())
291286
}
292287

288+
/// Renders the `tauri.conf.json` template.
289+
///
290+
/// Every value in the template is interpolated inside a JSON string literal,
291+
/// so values are JSON-escaped to keep quotes and backslashes in user input
292+
/// (e.g. `vite --host "0.0.0.0"` or `..\dist`) from breaking the document.
293+
fn render_tauri_config(
294+
data: &BTreeMap<&'static str, serde_json::Value>,
295+
) -> Result<serde_json::Value> {
296+
let mut handlebars = Handlebars::new();
297+
handlebars.register_escape_fn(|value| {
298+
let quoted = serde_json::Value::String(value.into()).to_string();
299+
quoted[1..quoted.len() - 1].to_string()
300+
});
301+
let config = handlebars
302+
.render_template(TAURI_CONF_TEMPLATE, data)
303+
.context("failed to render tauri.conf.json template")?;
304+
serde_json::from_str(&config).context("failed to parse rendered tauri.conf.json template")
305+
}
306+
293307
/// Builds the template variables for the Tauri crate dependencies of the generated `Cargo.toml`.
294308
///
295309
/// `tauri_dep` and `tauri_build_dep` are always set.
@@ -332,6 +346,34 @@ fn tauri_dependencies_data(
332346
mod tests {
333347
use super::*;
334348

349+
#[test]
350+
fn tauri_config_escapes_values() {
351+
let mut data = BTreeMap::new();
352+
data.insert("app_name", to_json(r#"My "quoted" App"#));
353+
data.insert("window_title", to_json("Tauri\tApp"));
354+
data.insert("frontend_dist", to_json(r"..\dist"));
355+
data.insert("dev_url", to_json("http://localhost:1420"));
356+
data.insert("before_dev_command", to_json(r#"vite --host "0.0.0.0""#));
357+
data.insert(
358+
"before_build_command",
359+
to_json(r"npm run build -- -D ..\dist"),
360+
);
361+
362+
let config = render_tauri_config(&data).unwrap();
363+
assert_eq!(config["productName"], r#"My "quoted" App"#);
364+
assert_eq!(config["app"]["windows"][0]["title"], "Tauri\tApp");
365+
assert_eq!(config["build"]["frontendDist"], r"..\dist");
366+
assert_eq!(config["build"]["devUrl"], "http://localhost:1420");
367+
assert_eq!(
368+
config["build"]["beforeDevCommand"],
369+
r#"vite --host "0.0.0.0""#
370+
);
371+
assert_eq!(
372+
config["build"]["beforeBuildCommand"],
373+
r"npm run build -- -D ..\dist"
374+
);
375+
}
376+
335377
/// The version metadata shipped with the CLI, the same source `command` uses.
336378
fn metadata() -> VersionMetadata {
337379
serde_json::from_str(include_str!("../metadata-v2.json"))

0 commit comments

Comments
 (0)