Skip to content

Commit

Permalink
refactor(fs): use scope from tauri core (#825)
Browse files Browse the repository at this point in the history
* fix(fs): scope checks on Android

On Android, when we call canonicalize() on "/data/user/0/appid" (which is the data dir), the result is a "/data/data/appid" path, so we need to adjust our scope for that.

* use scope from core

* update persisted-scope

* fix build

* dev branch
  • Loading branch information
lucasfernog authored Dec 20, 2023
1 parent 1eaf640 commit 10b8039
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 454 deletions.
6 changes: 6 additions & 0 deletions .changes/fs-scope-tauri.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fs": patch
"persisted-scope": patch
---

Use `tauri::scope::fs::Scope` instead of local copy of its implementation.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[workspace]
members = ["plugins/*", "plugins/*/tests/*", "plugins/*/examples/*/src-tauri", "examples/*/src-tauri"]
members = [
"plugins/*",
"plugins/*/tests/*",
"plugins/*/examples/*/src-tauri",
"examples/*/src-tauri",
]
resolver = "2"

[workspace.dependencies]
Expand All @@ -13,7 +18,7 @@ url = "2"

[workspace.package]
edition = "2021"
authors = [ "Tauri Programme within The Commons Conservancy" ]
authors = ["Tauri Programme within The Commons Conservancy"]
license = "Apache-2.0 OR MIT"
rust-version = "1.70"

Expand Down
52 changes: 1 addition & 51 deletions plugins/fs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::path::PathBuf;

use serde::Deserialize;
use tauri::utils::config::FsScope;

#[derive(Debug, Deserialize)]
pub struct Config {
pub scope: FsScope,
}

/// Protocol scope definition.
/// It is a list of glob patterns that restrict the API access from the webview.
///
/// Each pattern can start with a variable that resolves to a system base directory.
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`, `$APPCONFIG`, `$APPDATA`,
/// `$APPLOCALDATA`, `$APPCACHE`, `$APPLOG`.
#[derive(Debug, PartialEq, Eq, Clone, Deserialize)]
#[serde(untagged)]
pub enum FsScope {
/// A list of paths that are allowed by this scope.
AllowedPaths(Vec<PathBuf>),
/// A complete scope configuration.
Scope {
/// A list of paths that are allowed by this scope.
#[serde(default)]
allow: Vec<PathBuf>,
/// A list of paths that are not allowed by this scope.
/// This gets precedence over the [`Self::Scope::allow`] list.
#[serde(default)]
deny: Vec<PathBuf>,
},
}

impl Default for FsScope {
fn default() -> Self {
Self::AllowedPaths(Vec::new())
}
}

impl FsScope {
/// The list of allowed paths.
pub fn allowed_paths(&self) -> &Vec<PathBuf> {
match self {
Self::AllowedPaths(p) => p,
Self::Scope { allow, .. } => allow,
}
}

/// The list of forbidden paths.
pub fn forbidden_paths(&self) -> Option<&Vec<PathBuf>> {
match self {
Self::AllowedPaths(_) => None,
Self::Scope { deny, .. } => Some(deny),
}
}
}
5 changes: 2 additions & 3 deletions plugins/fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
)]

use config::FsScope;
use tauri::{
plugin::{Builder as PluginBuilder, TauriPlugin},
scope::fs::Scope,
utils::config::FsScope,
FileDropEvent, Manager, RunEvent, Runtime, WindowEvent,
};

mod commands;
mod config;
mod error;
mod scope;
#[cfg(feature = "watch")]
mod watcher;

pub use config::Config;
pub use error::Error;
pub use scope::{Event as ScopeEvent, Scope};

type Result<T> = std::result::Result<T, Error>;

Expand Down
Loading

0 comments on commit 10b8039

Please sign in to comment.