Skip to content

Commit 34df132

Browse files
authored
feat(shell): enhance regex validators to match on entire string (tauri-apps#1603)
1 parent b1e5cae commit 34df132

File tree

8 files changed

+45
-9
lines changed

8 files changed

+45
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shell": patch
3+
---
4+
5+
Change the `open` scope validator regex to match on the entire string.

.changes/shell-regex-match-string.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"shell": patch
3+
---
4+
5+
Change the `execute` scope argument validator regex to match on the entire string by default.
6+
If this behavior is not desired check the `raw` boolean configuration option that is available along the `validator` string.

examples/api/src-tauri/capabilities/base.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
"dialog:allow-confirm",
3737
"dialog:allow-message",
3838
{
39-
"identifier": "shell:allow-execute",
39+
"identifier": "shell:allow-spawn",
4040
"allow": [
4141
{
4242
"name": "sh",
4343
"cmd": "sh",
4444
"args": [
4545
"-c",
4646
{
47-
"validator": "\\S+"
47+
"validator": ".+"
4848
}
4949
]
5050
},
@@ -54,7 +54,7 @@
5454
"args": [
5555
"/C",
5656
{
57-
"validator": "\\S+"
57+
"validator": ".+"
5858
}
5959
]
6060
}

examples/api/src-tauri/gen/schemas/desktop-schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7410,8 +7410,13 @@
74107410
"validator"
74117411
],
74127412
"properties": {
7413+
"raw": {
7414+
"description": "Marks the validator as a raw regex, meaning the plugin should not make any modification at runtime.\n\nThis means the regex will not match on the entire string by default, which might be exploited if your regex allow unexpected input to be considered valid. When using this option, make sure your regex is correct.",
7415+
"default": false,
7416+
"type": "boolean"
7417+
},
74137418
"validator": {
7414-
"description": "[regex] validator to require passed values to conform to an expected input.\n\nThis will require the argument value passed to this variable to match the `validator` regex before it will be executed.\n\n[regex]: https://docs.rs/regex/latest/regex/#syntax",
7419+
"description": "[regex] validator to require passed values to conform to an expected input.\n\nThis will require the argument value passed to this variable to match the `validator` regex before it will be executed.\n\nThe regex string is by default surrounded by `^...$` to match the full string. For example the `https?://\\w+` regex would be registered as `^https?://\\w+$`.\n\n[regex]: <https://docs.rs/regex/latest/regex/#syntax>",
74157420
"type": "string"
74167421
}
74177422
},

plugins/shell/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub enum ShellAllowlistOpen {
2525

2626
/// Enable the shell open API, with a custom regex that the opened path must match against.
2727
///
28+
/// The regex string is automatically surrounded by `^...$` to match the full string.
29+
/// For example the `https?://\w+` regex would be registered as `^https?://\w+$`.
30+
///
2831
/// If using a custom regex to support a non-http(s) schema, care should be used to prevent values
2932
/// that allow flag-like strings to pass validation. e.g. `--enable-debugging`, `-i`, `/R`.
3033
Validate(String),

plugins/shell/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ fn open_scope(open: &config::ShellAllowlistOpen) -> scope::OpenScope {
148148
Some(Regex::new(r"^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+").unwrap())
149149
}
150150
config::ShellAllowlistOpen::Validate(validator) => {
151+
let regex = format!("^{validator}$");
151152
let validator =
152-
Regex::new(validator).unwrap_or_else(|e| panic!("invalid regex {validator}: {e}"));
153+
Regex::new(&regex).unwrap_or_else(|e| panic!("invalid regex {regex}: {e}"));
153154
Some(validator)
154155
}
155156
};

plugins/shell/src/scope.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ impl ScopeObject for ScopeAllowedCommand {
8888
crate::scope_entry::ShellAllowedArg::Fixed(fixed) => {
8989
crate::scope::ScopeAllowedArg::Fixed(fixed)
9090
}
91-
crate::scope_entry::ShellAllowedArg::Var { validator } => {
92-
let validator = Regex::new(&validator)
93-
.unwrap_or_else(|e| panic!("invalid regex {validator}: {e}"));
91+
crate::scope_entry::ShellAllowedArg::Var { validator, raw } => {
92+
let regex = if raw {
93+
validator
94+
} else {
95+
format!("^{validator}$")
96+
};
97+
let validator = Regex::new(&regex)
98+
.unwrap_or_else(|e| panic!("invalid regex {regex}: {e}"));
9499
crate::scope::ScopeAllowedArg::Var { validator }
95100
}
96101
});

plugins/shell/src/scope_entry.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,18 @@ pub enum ShellAllowedArg {
103103
/// This will require the argument value passed to this variable to match the `validator` regex
104104
/// before it will be executed.
105105
///
106-
/// [regex]: https://docs.rs/regex/latest/regex/#syntax
106+
/// The regex string is by default surrounded by `^...$` to match the full string.
107+
/// For example the `https?://\w+` regex would be registered as `^https?://\w+$`.
108+
///
109+
/// [regex]: <https://docs.rs/regex/latest/regex/#syntax>
107110
validator: String,
111+
112+
/// Marks the validator as a raw regex, meaning the plugin should not make any modification at runtime.
113+
///
114+
/// This means the regex will not match on the entire string by default, which might
115+
/// be exploited if your regex allow unexpected input to be considered valid.
116+
/// When using this option, make sure your regex is correct.
117+
#[serde(default)]
118+
raw: bool,
108119
},
109120
}

0 commit comments

Comments
 (0)