Skip to content

Commit 9bad06b

Browse files
authored
fix(cli): validate input of capability new and permission new (#16096)
1 parent cc9d522 commit 9bad06b

4 files changed

Lines changed: 123 additions & 20 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 capability new` and `tauri permission new` now accept an `--out` path to a file that does not exist yet, trim comma-separated prompt answers (so `fs:default, core:default` works), report invalid permissions as errors instead of panicking, and reject identifiers that are not valid file names (such as `../../x`), which previously let them write outside of the capabilities or permissions directory.

‎crates/tauri-cli/src/acl/capability/new.rs‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use std::{collections::HashSet, path::PathBuf};
77
use clap::Parser;
88
use tauri_utils::acl::capability::{Capability, PermissionEntry};
99

10-
use crate::{Result, acl::FileFormat, error::ErrorExt, helpers::prompts};
10+
use crate::{
11+
Result,
12+
acl::{FileFormat, split_comma_separated, validate_capability_identifier},
13+
error::ErrorExt,
14+
helpers::prompts,
15+
};
1116

1217
#[derive(Debug, Parser)]
1318
#[clap(about = "Create a new capability file")]
@@ -38,6 +43,7 @@ pub fn command(options: Options) -> Result<()> {
3843
Some(i) => i,
3944
None => prompts::input("What's the capability identifier?", None, false, false)?.unwrap(),
4045
};
46+
validate_capability_identifier(&identifier)?;
4147

4248
let description = match options.description {
4349
Some(d) => Some(d),
@@ -57,7 +63,7 @@ pub fn command(options: Options) -> Result<()> {
5763
if d.is_empty() {
5864
None
5965
} else {
60-
Some(d.split(',').map(ToString::to_string).collect())
66+
Some(split_comma_separated(&d).collect())
6167
}
6268
})
6369
.unwrap_or_default(),
@@ -75,36 +81,34 @@ pub fn command(options: Options) -> Result<()> {
7581
if p.is_empty() {
7682
None
7783
} else {
78-
Some(p.split(',').map(ToString::to_string).collect())
84+
Some(split_comma_separated(&p).collect())
7985
}
8086
})
8187
.unwrap_or_default(),
8288
};
8389

90+
let permissions = permissions
91+
.into_iter()
92+
.map(|p| match p.clone().try_into() {
93+
Ok(identifier) => Ok(PermissionEntry::PermissionRef(identifier)),
94+
Err(e) => crate::error::bail!("invalid permission `{}`: {}", p, e),
95+
})
96+
.collect::<Result<Vec<_>>>()?;
97+
8498
let capability = Capability {
8599
identifier,
86100
description: description.unwrap_or_default(),
87101
remote: None,
88102
local: true,
89103
windows,
90104
webviews: Vec::new(),
91-
permissions: permissions
92-
.into_iter()
93-
.map(|p| {
94-
PermissionEntry::PermissionRef(
95-
p.clone()
96-
.try_into()
97-
.unwrap_or_else(|_| panic!("invalid permission {p}")),
98-
)
99-
})
100-
.collect(),
105+
permissions,
101106
platforms: None,
102107
};
103108

104109
let path = match options.out {
105-
Some(o) => o
106-
.canonicalize()
107-
.fs_context("failed to canonicalize capability file path", o.clone())?,
110+
// the file may not exist yet, so it cannot be canonicalized
111+
Some(o) => o,
108112
None => {
109113
let capabilities_dir = dirs.tauri.join("capabilities");
110114
capabilities_dir.join(format!(

‎crates/tauri-cli/src/acl/mod.rs‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,96 @@ impl FileFormat {
4040
Ok(contents)
4141
}
4242
}
43+
44+
/// Splits a comma separated prompt answer, trimming each entry and skipping empty ones.
45+
fn split_comma_separated(input: &str) -> impl Iterator<Item = String> + '_ {
46+
input
47+
.split(',')
48+
.map(str::trim)
49+
.filter(|s| !s.is_empty())
50+
.map(ToString::to_string)
51+
}
52+
53+
/// Validates a capability identifier, which is also used as its file name.
54+
///
55+
/// Only ASCII letters, digits, hyphens and underscores are allowed,
56+
/// so the identifier cannot be used to write outside of the capabilities directory.
57+
fn validate_capability_identifier(identifier: &str) -> crate::Result<()> {
58+
if identifier.is_empty() {
59+
crate::error::bail!("capability identifier cannot be empty");
60+
}
61+
if !identifier
62+
.bytes()
63+
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
64+
{
65+
crate::error::bail!(
66+
"invalid capability identifier `{}`: only ASCII letters, digits, hyphens and underscores are allowed",
67+
identifier
68+
);
69+
}
70+
Ok(())
71+
}
72+
73+
/// Validates an app permission identifier, which is also used as its file name.
74+
fn validate_permission_identifier(identifier: &str) -> crate::Result<()> {
75+
let parsed = match tauri_utils::acl::identifier::Identifier::try_from(identifier.to_string()) {
76+
Ok(parsed) => parsed,
77+
Err(e) => crate::error::bail!("invalid permission identifier `{}`: {}", identifier, e),
78+
};
79+
// app permissions are referenced without a prefix, the prefix is reserved for plugins
80+
if parsed.get_prefix().is_some() {
81+
crate::error::bail!(
82+
"invalid permission identifier `{}`: app permissions cannot have a `:` prefix",
83+
identifier
84+
);
85+
}
86+
Ok(())
87+
}
88+
89+
#[cfg(test)]
90+
mod tests {
91+
use super::{
92+
split_comma_separated, validate_capability_identifier, validate_permission_identifier,
93+
};
94+
95+
#[test]
96+
fn splits_comma_separated_input() {
97+
assert_eq!(
98+
split_comma_separated("fs:default, core:default,, ").collect::<Vec<_>>(),
99+
vec!["fs:default", "core:default"]
100+
);
101+
assert_eq!(split_comma_separated(" ").count(), 0);
102+
}
103+
104+
#[test]
105+
fn capability_identifier_validation() {
106+
assert!(validate_capability_identifier("main-capability").is_ok());
107+
assert!(validate_capability_identifier("main_window2").is_ok());
108+
for invalid in ["", "../../x", "a/b", "a\\b", "..", "a.b", "a:b", "a b"] {
109+
assert!(
110+
validate_capability_identifier(invalid).is_err(),
111+
"{invalid} should be rejected"
112+
);
113+
}
114+
}
115+
116+
#[test]
117+
fn permission_identifier_validation() {
118+
assert!(validate_permission_identifier("allow-greet").is_ok());
119+
for invalid in [
120+
"",
121+
"../../x",
122+
"a/b",
123+
"a\\b",
124+
"..",
125+
"a.b",
126+
"fs:allow-read",
127+
"-a",
128+
] {
129+
assert!(
130+
validate_permission_identifier(invalid).is_err(),
131+
"{invalid} should be rejected"
132+
);
133+
}
134+
}
135+
}

‎crates/tauri-cli/src/acl/permission/new.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clap::Parser;
88

99
use crate::{
1010
Result,
11-
acl::FileFormat,
11+
acl::{FileFormat, validate_permission_identifier},
1212
error::{Context, ErrorExt},
1313
helpers::{app_paths::resolve_tauri_dir, prompts},
1414
};
@@ -42,6 +42,7 @@ pub fn command(options: Options) -> Result<()> {
4242
Some(i) => i,
4343
None => prompts::input("What's the permission identifier?", None, false, false)?.unwrap(),
4444
};
45+
validate_permission_identifier(&identifier)?;
4546

4647
let description = match options.description {
4748
Some(d) => Some(d),
@@ -68,9 +69,8 @@ pub fn command(options: Options) -> Result<()> {
6869
};
6970

7071
let path = match options.out {
71-
Some(o) => o
72-
.canonicalize()
73-
.fs_context("failed to canonicalize permission file path", o.clone())?,
72+
// the file may not exist yet, so it cannot be canonicalized
73+
Some(o) => o,
7474
None => {
7575
let dir = match resolve_tauri_dir() {
7676
Some(t) => t,

0 commit comments

Comments
 (0)