Skip to content

Commit b42064d

Browse files
committed
fix(fs): Escape paths in Scope methods
1 parent ff05a59 commit b42064d

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fs: patch
3+
---
4+
5+
Paths given to the `Scope` methods are now correctly escaped, preventing issues with paths containing `[]`.

plugins/fs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ rustc-args = ["--cfg", "docsrs"]
1414
rustdoc-args = ["--cfg", "docsrs"]
1515

1616
[package.metadata.platforms.support]
17-
windows = { level = "full", notes = "" }
17+
windows = { level = "full", notes = "No write access to `$RESOURCES` folder with MSI installer and NSIS installers in `perMachine` or `both` mode" }
1818
linux = { level = "full", notes = "No write access to `$RESOURCES` folder" }
1919
macos = { level = "full", notes = "No write access to `$RESOURCES` folder" }
2020
android = { level = "partial", notes = "Access is restricted to Application folder by default" }

plugins/fs/src/scope.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use std::{
66
collections::HashMap,
7-
path::{Path, PathBuf},
7+
path::{Path, PathBuf, MAIN_SEPARATOR},
88
sync::{
99
atomic::{AtomicU32, Ordering},
1010
Mutex,
1111
},
1212
};
1313

14+
use glob::Pattern;
1415
use serde::Deserialize;
1516

1617
#[derive(Deserialize)]
@@ -56,8 +57,9 @@ impl Scope {
5657

5758
{
5859
let mut allowed = self.allowed.lock().unwrap();
59-
allowed.push(path.to_path_buf());
60-
allowed.push(path.join(if recursive { "**" } else { "*" }));
60+
let p = path.to_string_lossy();
61+
allowed.push(escaped_pattern(&p));
62+
allowed.push(escaped_pattern_with(&p, if recursive { "**" } else { "*" }));
6163
}
6264

6365
self.emit(Event::PathAllowed(path.to_path_buf()));
@@ -69,7 +71,10 @@ impl Scope {
6971
pub fn allow_file<P: AsRef<Path>>(&self, path: P) {
7072
let path = path.as_ref();
7173

72-
self.allowed.lock().unwrap().push(path.to_path_buf());
74+
self.allowed
75+
.lock()
76+
.unwrap()
77+
.push(escaped_pattern(&path.to_string_lossy()));
7378

7479
self.emit(Event::PathAllowed(path.to_path_buf()));
7580
}
@@ -82,8 +87,9 @@ impl Scope {
8287

8388
{
8489
let mut denied = self.denied.lock().unwrap();
85-
denied.push(path.to_path_buf());
86-
denied.push(path.join(if recursive { "**" } else { "*" }));
90+
let p = path.to_string_lossy();
91+
denied.push(escaped_pattern(&p));
92+
denied.push(escaped_pattern_with(&p, if recursive { "**" } else { "*" }));
8793
}
8894

8995
self.emit(Event::PathForbidden(path.to_path_buf()));
@@ -95,7 +101,10 @@ impl Scope {
95101
pub fn forbid_file<P: AsRef<Path>>(&self, path: P) {
96102
let path = path.as_ref();
97103

98-
self.denied.lock().unwrap().push(path.to_path_buf());
104+
self.denied
105+
.lock()
106+
.unwrap()
107+
.push(escaped_pattern(&path.to_string_lossy()));
99108

100109
self.emit(Event::PathForbidden(path.to_path_buf()));
101110
}
@@ -129,3 +138,15 @@ impl Scope {
129138
id
130139
}
131140
}
141+
142+
fn escaped_pattern(p: &str) -> Result<Pattern, glob::PatternError> {
143+
Pattern::new(&Pattern::escape(p))
144+
}
145+
146+
fn escaped_pattern_with(p: &str, append: &str) -> Result<Pattern, glob::PatternError> {
147+
if p.ends_with(MAIN_SEPARATOR) {
148+
Pattern::new(&format!("{}{append}", Pattern::escape(p)))
149+
} else {
150+
Pattern::new(&format!("{}{}{append}", Pattern::escape(p), MAIN_SEPARATOR))
151+
}
152+
}

0 commit comments

Comments
 (0)