Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(endpoints/fs/readDir): don't read symlinks that are not allowed b… #5123

Merged
merged 5 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next
fix(endpoints/fs/readDir): don't read symlinks that are not allowed b…
…y the scope, closes #4882
  • Loading branch information
amrbashir committed Sep 1, 2022
commit 1f9b9e8d26a2c915390323e161020bcb36d44678
5 changes: 5 additions & 0 deletions .changes/fix-readir-symlink-scope.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Fix `fs.readDir` reading symlinks that isn't allowed by the scope.
35 changes: 29 additions & 6 deletions core/tauri/src/api/dir.rs
Expand Up @@ -6,7 +6,7 @@

use serde::Serialize;
use std::{
fs::{self, metadata},
fs::{self, metadata, symlink_metadata},
path::{Path, PathBuf},
};
use tempfile::{self, tempdir};
Expand All @@ -31,8 +31,26 @@ pub fn is_dir<P: AsRef<Path>>(path: P) -> crate::api::Result<bool> {
metadata(path).map(|md| md.is_dir()).map_err(Into::into)
}

fn is_symlink<P: AsRef<Path>>(path: P) -> crate::api::Result<bool> {
symlink_metadata(path)
.map(|md| md.is_symlink())
.map_err(Into::into)
}

/// Reads a directory. Can perform recursive operations.
pub fn read_dir<P: AsRef<Path>>(path: P, recursive: bool) -> crate::api::Result<Vec<DiskEntry>> {
read_dir_with_options(path, recursive, ReadDirOptions { scope: None })
}

pub(crate) struct ReadDirOptions<'a> {
pub scope: Option<&'a crate::FsScope>,
}

pub(crate) fn read_dir_with_options<P: AsRef<Path>>(
path: P,
recursive: bool,
options: ReadDirOptions<'_>,
) -> crate::api::Result<Vec<DiskEntry>> {
let mut files_and_dirs: Vec<DiskEntry> = vec![];
for entry in fs::read_dir(path)? {
let path = entry?.path();
Expand All @@ -42,11 +60,16 @@ pub fn read_dir<P: AsRef<Path>>(path: P, recursive: bool) -> crate::api::Result<
files_and_dirs.push(DiskEntry {
path: path.clone(),
children: if flag {
Some(if recursive {
read_dir(&path_as_string, true)?
} else {
vec![]
})
Some(
if recursive
&& (!is_symlink(&path_as_string)?
|| options.scope.map(|s| s.is_allowed(&path)).unwrap_or(true))
{
read_dir(&path_as_string, true)?
} else {
vec![]
},
)
} else {
None
},
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/api/file.rs
Expand Up @@ -74,6 +74,7 @@ pub fn read_binary<P: AsRef<Path>>(file: P) -> crate::api::Result<Vec<u8>> {
#[cfg(test)]
mod test {
use super::*;
#[cfg(not(windows))]
use crate::api::Error;
use quickcheck::{Arbitrary, Gen};

Expand Down
12 changes: 9 additions & 3 deletions core/tauri/src/endpoints/file_system.rs
Expand Up @@ -191,9 +191,15 @@ impl Cmd {
path,
dir,
)?;
dir::read_dir(&resolved_path, recursive)
.with_context(|| format!("path: {}", resolved_path.display()))
.map_err(Into::into)
dir::read_dir_with_options(
&resolved_path,
recursive,
dir::ReadDirOptions {
scope: Some(&context.window.state::<Scopes>().fs),
},
)
.with_context(|| format!("path: {}", resolved_path.display()))
.map_err(Into::into)
}

#[module_command_handler(fs_copy_file)]
Expand Down