Keep every owner's font guard for a folder and require all of them

The font table is shared by the process, so a folder added without a guard (or with another owner's)
dropped the guard of a confined session and its font links were opened unchecked. A folder now keeps
the guard of each owner that gave one, and a font is opened only when all of them allow it.
This commit is contained in:
Adithya Shankaran
2026-11-14 19:03:04 +05:30
parent 99f442d40e
commit 4efecc2f0e
3 changed files with 50 additions and 33 deletions
+5 -4
View File
@@ -346,10 +346,11 @@ fn read_bytes(bytes: &[u8], name: &str, path: Option<String>) -> Result<ReadFile
None => cadcraft_fonts::shx::add_drawing_dir(&dir),
// The folder holds no record of the roots, and a font's name there can be a link leading
// out of them: the search asks the roots about each file before it opens one.
Some(roots) => cadcraft_fonts::shx::add_drawing_dir_guarded(
&dir,
std::sync::Arc::new(move |font| roots.check(font, crate::file_access::Access::Read).is_ok()),
),
Some(roots) => {
let owner = format!("{:?}|{:?}", roots.read_root(), roots.write_root());
let guard = std::sync::Arc::new(move |font: &std::path::Path| roots.check(font, crate::file_access::Access::Read).is_ok());
cadcraft_fonts::shx::add_drawing_dir_guarded(&dir, &owner, guard);
}
}
}
Ok(ReadFile { drawing, binary_dxf: craft_dxf::is_binary(bytes), name: name.to_string(), path })
+28 -26
View File
@@ -43,8 +43,8 @@ struct Table {
loaded_bytes: usize,
provider: Option<Arc<dyn FontProvider>>,
drawing_dirs: Vec<PathBuf>,
/// The guards of the drawing folders that have one.
drawing_guards: HashMap<PathBuf, OpenGuard>,
/// The guards of the drawing folders that have any, by the owner that gave each one.
drawing_guards: HashMap<PathBuf, Vec<(String, OpenGuard)>>,
font_dirs: Vec<PathBuf>,
search_path: Vec<PathBuf>,
map: Vec<(String, String)>,
@@ -129,33 +129,34 @@ pub fn add_drawing_dir(dir: &Path) {
}
/// Like [`add_drawing_dir`], with every file the search would open in `dir` put to `guard` first,
/// whatever its name and however it is found in the folder: a font the guard refuses is skipped. The
/// guard stays with the folder until the folder is added again.
pub fn add_drawing_dir_guarded(dir: &Path, guard: OpenGuard) {
add_dir(dir, Some(guard));
/// whatever its name and however it is found in the folder: a font the guard refuses is skipped.
///
/// The table is shared by the whole process, so a folder keeps the guard of every `owner` that gave
/// one (an owner giving it again replaces its own) and a font is opened only when all of them allow
/// it. Adding the folder without a guard never removes one. Fonts found are kept for every caller,
/// so owners with different rules shouldn't share a process.
pub fn add_drawing_dir_guarded(dir: &Path, owner: &str, guard: OpenGuard) {
add_dir(dir, Some((owner, guard)));
}
/// Remember `dir`, most recent first, with the guard it is searched under (none: unrestricted).
fn add_dir(dir: &Path, guard: Option<OpenGuard>) {
/// Remember `dir`, most recent first, with the guard it is searched under, if any.
fn add_dir(dir: &Path, guard: Option<(&str, OpenGuard)>) {
if dir.as_os_str().is_empty() {
return;
}
let mut t = table();
if guard.is_none() && t.drawing_dirs.first().is_some_and(|d| d == dir) && !t.drawing_guards.contains_key(dir) {
if guard.is_none() && t.drawing_dirs.first().is_some_and(|d| d == dir) {
return;
}
t.drawing_dirs.retain(|d| d != dir);
t.drawing_dirs.insert(0, dir.to_path_buf());
t.drawing_dirs.truncate(MAX_DIRS);
match guard {
Some(g) => {
t.drawing_guards.insert(dir.to_path_buf(), g);
}
None => {
t.drawing_guards.remove(dir);
}
if let Some((owner, guard)) = guard {
let guards = t.drawing_guards.entry(dir.to_path_buf()).or_default();
guards.retain(|(o, _)| o != owner);
guards.push((owner.to_string(), guard));
}
// A folder dropped from the list takes its guard with it.
// A folder dropped from the list takes its guards with it.
let Table { drawing_dirs, drawing_guards, .. } = &mut *t;
drawing_guards.retain(|d, _| drawing_dirs.contains(d));
t.changed();
@@ -236,9 +237,10 @@ pub fn find(name: &str) -> Option<Arc<ShxFont>> {
if let Some(r) = t.resolved.get(&k) {
return r.clone();
}
let drawing = t.drawing_dirs.iter().map(|d| (d.clone(), t.drawing_guards.get(d).cloned()));
let dirs: Vec<(PathBuf, Option<OpenGuard>)> =
drawing.chain(t.font_dirs.iter().chain(t.search_path.iter()).map(|d| (d.clone(), None))).collect();
let guards = |d: &PathBuf| t.drawing_guards.get(d).map(|g| g.iter().map(|(_, g)| g.clone()).collect()).unwrap_or_default();
let drawing = t.drawing_dirs.iter().map(|d| (d.clone(), guards(d)));
let dirs: Vec<(PathBuf, Vec<OpenGuard>)> =
drawing.chain(t.font_dirs.iter().chain(t.search_path.iter()).map(|d| (d.clone(), Vec::new()))).collect();
(t.provider.clone(), dirs, t.generation)
};
let font = provider
@@ -298,13 +300,13 @@ pub fn installed() -> Vec<String> {
/// The file `name` (a lower-case key) in the first of `dirs` that has it, whatever the case of
/// its name: lower-case, upper-case, else any other case found in the directory (`Simplex.shx`
/// on a case-sensitive file system). A folder's guard is asked about each candidate before anything
/// opens it, so a link is judged by where it leads.
/// on a case-sensitive file system). Each of a folder's guards is asked about each candidate before
/// anything opens it, so a link is judged by where it leads.
#[cfg(not(target_arch = "wasm32"))]
fn read_from(dirs: &[(PathBuf, Option<OpenGuard>)], name: &str) -> Option<Vec<u8>> {
for (d, guard) in dirs {
fn read_from(dirs: &[(PathBuf, Vec<OpenGuard>)], name: &str) -> Option<Vec<u8>> {
for (d, guards) in dirs {
let read = |file: &Path| -> Option<Vec<u8>> {
if guard.as_ref().is_some_and(|g| !g(file)) {
if !guards.iter().all(|g| g(file)) {
return None;
}
read_file(file)
@@ -341,6 +343,6 @@ fn read_file(file: &Path) -> Option<Vec<u8>> {
/// On the web there are no folders to search.
#[cfg(target_arch = "wasm32")]
fn read_from(_dirs: &[(PathBuf, Option<OpenGuard>)], _name: &str) -> Option<Vec<u8>> {
fn read_from(_dirs: &[(PathBuf, Vec<OpenGuard>)], _name: &str) -> Option<Vec<u8>> {
None
}
+17 -3
View File
@@ -17,6 +17,11 @@ fn fresh_dir() -> PathBuf {
dir
}
/// The lower-case file name of `file`, so a guard never reads the test folder's own name.
fn name(file: &Path) -> String {
file.file_name().map(|n| n.to_string_lossy().to_ascii_lowercase()).unwrap_or_default()
}
/// Every candidate the search opens in a guarded folder goes to the guard first, whatever its name
/// or how the search finds it, and a font the guard refuses is skipped while the others are used.
#[test]
@@ -32,9 +37,10 @@ fn every_file_the_search_opens_goes_to_the_guard_first() {
let log = asked.clone();
shx::add_drawing_dir_guarded(
&dir,
"first",
Arc::new(move |file: &Path| {
log.lock().unwrap().push(file.to_path_buf());
!file.to_string_lossy().to_ascii_lowercase().contains("bad")
!name(file).contains("bad")
}),
);
assert!(shx::find("cadcraft-guard-ok.shx").is_some());
@@ -48,8 +54,16 @@ fn every_file_the_search_opens_goes_to_the_guard_first() {
let want = dir.join(file).to_string_lossy().to_ascii_lowercase();
assert!(asked.iter().any(|a| a.to_string_lossy().to_ascii_lowercase() == want), "the guard was asked about {file}: {asked:?}");
}
// The guard goes with the folder: added again without one, the folder is searched freely.
// The folder added again without a guard (another session in the process) keeps its guard.
shx::add_drawing_dir(&dir);
assert!(shx::find("cadcraft-guard-bad.shx").is_some());
assert!(shx::find("cadcraft-guard-bad.shx").is_none());
// Another owner's guard is required as well: a font is opened only when every owner allows it.
shx::add_drawing_dir_guarded(&dir, "second", Arc::new(|file: &Path| !name(file).contains("ok")));
assert!(shx::find("cadcraft-guard-ok.shx").is_none(), "refused by the second owner");
assert!(shx::find("cadcraft-guard-mixed.shx").is_some(), "allowed by both");
// An owner giving its guard again replaces its own, and the other's stays.
shx::add_drawing_dir_guarded(&dir, "second", Arc::new(|_: &Path| true));
assert!(shx::find("cadcraft-guard-ok.shx").is_some());
assert!(shx::find("cadcraft-guard-bad.shx").is_none(), "the first owner's guard still holds");
std::fs::remove_dir_all(&dir).unwrap();
}