feat(fs): enhance scope config to accept string as well (#1053)

* feat(fs): enhance scope config to accept string as well

* clippy

* Update protocol.rs

* regression resolving path variables

* add change file

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
pull/1118/head
Amr Bashir 1 year ago committed by GitHub
parent fe4ad4dd5c
commit f39d362add
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"fs": patch
---
Enhance the scope type to also allow a plain string representing the path to allow or deny.

62
Cargo.lock generated

@ -230,7 +230,7 @@ checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1"
[[package]]
name = "api"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"log",
"serde",
@ -4062,9 +4062,9 @@ checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
[[package]]
name = "open"
version = "5.1.1"
version = "5.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b3fbb0d52bf0cbb5225ba3d2c303aa136031d43abff98284332a9981ecddec"
checksum = "449f0ff855d85ddbf1edd5b646d65249ead3f5e422aaa86b7d2d0b049b103e32"
dependencies = [
"is-wsl",
"libc",
@ -6409,7 +6409,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-authenticator"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"authenticator",
"base64 0.21.7",
@ -6431,7 +6431,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-autostart"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"auto-launch",
"log",
@ -6444,7 +6444,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-barcode-scanner"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"log",
"serde",
@ -6456,7 +6456,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-biometric"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"log",
"serde",
@ -6469,7 +6469,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-cli"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"clap",
"log",
@ -6482,7 +6482,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-clipboard-manager"
version = "2.0.0-beta.2"
version = "2.1.0-beta.0"
dependencies = [
"arboard",
"image",
@ -6496,7 +6496,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-deep-link"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"log",
"serde",
@ -6509,7 +6509,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-dialog"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"glib 0.16.9",
"log",
@ -6525,7 +6525,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-fs"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"anyhow",
"glob",
@ -6544,7 +6544,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-global-shortcut"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"global-hotkey",
"log",
@ -6557,7 +6557,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-http"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"data-url",
"http 0.2.12",
@ -6576,7 +6576,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-localhost"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"http 1.1.0",
"log",
@ -6589,7 +6589,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-log"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"android_logger",
"byte-unit",
@ -6608,7 +6608,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-nfc"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"log",
"serde",
@ -6621,7 +6621,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-notification"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"chrono",
"color-backtrace",
@ -6648,7 +6648,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-os"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"gethostname",
"log",
@ -6664,7 +6664,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-persisted-scope"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"aho-corasick",
"bincode",
@ -6678,7 +6678,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-positioner"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"log",
"serde",
@ -6691,7 +6691,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-process"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"tauri",
"tauri-plugin",
@ -6699,7 +6699,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-shell"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"encoding_rs",
"log",
@ -6717,7 +6717,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-single-instance"
version = "2.0.0-beta.3"
version = "2.0.0-beta.4"
dependencies = [
"log",
"serde",
@ -6730,7 +6730,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-sql"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"futures-core",
"log",
@ -6746,7 +6746,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-store"
version = "2.0.0-beta.3"
version = "2.0.0-beta.4"
dependencies = [
"log",
"serde",
@ -6758,7 +6758,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-stronghold"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"hex",
"iota-crypto 0.23.1",
@ -6779,7 +6779,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-updater"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"base64 0.21.7",
"dirs-next",
@ -6807,7 +6807,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-upload"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"futures-util",
"log",
@ -6824,7 +6824,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-websocket"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"futures-util",
"http 1.1.0",
@ -6841,7 +6841,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-window-state"
version = "2.0.0-beta.2"
version = "2.0.0-beta.3"
dependencies = [
"bincode",
"bitflags 2.4.2",

@ -8,6 +8,7 @@
{
"identifier": "http:default",
"allow": [
"https://tauri.app",
{
"url": "http://localhost:3003"
}
@ -72,11 +73,7 @@
"path": "$APPDATA/db/**"
}
],
"deny": [
{
"path": "$APPDATA/db/*.stronghold"
}
]
"deny": ["$APPDATA/db/*.stronghold"]
}
]
}

@ -2133,30 +2133,50 @@
},
"allow": {
"items": {
"title": "Entry",
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"title": "FsScopeEntry",
"description": "FS scope entry.",
"anyOf": [
{
"description": "FS scope path.",
"type": "string"
},
{
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"description": "FS scope path.",
"type": "string"
}
}
}
}
]
}
},
"deny": {
"items": {
"title": "Entry",
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"title": "FsScopeEntry",
"description": "FS scope entry.",
"anyOf": [
{
"description": "FS scope path.",
"type": "string"
},
{
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"description": "FS scope path.",
"type": "string"
}
}
}
}
]
}
}
}
@ -2236,8 +2256,8 @@
},
"allow": {
"items": {
"title": "ScopeEntry",
"description": "HTTP scope entry object definition.",
"title": "HttpScopeEntry",
"description": "HTTP scope entry.",
"anyOf": [
{
"description": "A URL that can be accessed by the webview when using the HTTP APIs. Wildcards can be used following the URL pattern standard.\n\nSee [the URL Pattern spec](https://urlpattern.spec.whatwg.org/) for more information.\n\nExamples:\n\n- \"https://*\" : allows all HTTPS origin on port 443\n\n- \"https://*:*\" : allows all HTTPS origin on any port\n\n- \"https://*.github.com/tauri-apps/tauri\": allows any subdomain of \"github.com\" with the \"tauri-apps/api\" path\n\n- \"https://myapi.service.com/users/*\": allows access to any URLs that begins with \"https://myapi.service.com/users/\"",
@ -2260,8 +2280,8 @@
},
"deny": {
"items": {
"title": "ScopeEntry",
"description": "HTTP scope entry object definition.",
"title": "HttpScopeEntry",
"description": "HTTP scope entry.",
"anyOf": [
{
"description": "A URL that can be accessed by the webview when using the HTTP APIs. Wildcards can be used following the URL pattern standard.\n\nSee [the URL Pattern spec](https://urlpattern.spec.whatwg.org/) for more information.\n\nExamples:\n\n- \"https://*\" : allows all HTTPS origin on port 443\n\n- \"https://*:*\" : allows all HTTPS origin on any port\n\n- \"https://*.github.com/tauri-apps/tauri\": allows any subdomain of \"github.com\" with the \"tauri-apps/api\" path\n\n- \"https://myapi.service.com/users/*\": allows access to any URLs that begins with \"https://myapi.service.com/users/\"",

@ -2133,30 +2133,50 @@
},
"allow": {
"items": {
"title": "Entry",
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"title": "FsScopeEntry",
"description": "FS scope entry.",
"anyOf": [
{
"description": "FS scope path.",
"type": "string"
},
{
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"description": "FS scope path.",
"type": "string"
}
}
}
}
]
}
},
"deny": {
"items": {
"title": "Entry",
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"title": "FsScopeEntry",
"description": "FS scope entry.",
"anyOf": [
{
"description": "FS scope path.",
"type": "string"
},
{
"type": "object",
"required": [
"path"
],
"properties": {
"path": {
"description": "FS scope path.",
"type": "string"
}
}
}
}
]
}
}
}
@ -2236,8 +2256,8 @@
},
"allow": {
"items": {
"title": "ScopeEntry",
"description": "HTTP scope entry object definition.",
"title": "HttpScopeEntry",
"description": "HTTP scope entry.",
"anyOf": [
{
"description": "A URL that can be accessed by the webview when using the HTTP APIs. Wildcards can be used following the URL pattern standard.\n\nSee [the URL Pattern spec](https://urlpattern.spec.whatwg.org/) for more information.\n\nExamples:\n\n- \"https://*\" : allows all HTTPS origin on port 443\n\n- \"https://*:*\" : allows all HTTPS origin on any port\n\n- \"https://*.github.com/tauri-apps/tauri\": allows any subdomain of \"github.com\" with the \"tauri-apps/api\" path\n\n- \"https://myapi.service.com/users/*\": allows access to any URLs that begins with \"https://myapi.service.com/users/\"",
@ -2260,8 +2280,8 @@
},
"deny": {
"items": {
"title": "ScopeEntry",
"description": "HTTP scope entry object definition.",
"title": "HttpScopeEntry",
"description": "HTTP scope entry.",
"anyOf": [
{
"description": "A URL that can be accessed by the webview when using the HTTP APIs. Wildcards can be used following the URL pattern standard.\n\nSee [the URL Pattern spec](https://urlpattern.spec.whatwg.org/) for more information.\n\nExamples:\n\n- \"https://*\" : allows all HTTPS origin on port 443\n\n- \"https://*:*\" : allows all HTTPS origin on any port\n\n- \"https://*.github.com/tauri-apps/tauri\": allows any subdomain of \"github.com\" with the \"tauri-apps/api\" path\n\n- \"https://myapi.service.com/users/*\": allows access to any URLs that begins with \"https://myapi.service.com/users/\"",

@ -15,11 +15,11 @@ rustdoc-args = [ "--cfg", "docsrs" ]
[build-dependencies]
tauri-plugin = { workspace = true, features = [ "build" ] }
schemars = { workspace = true }
serde = { workspace = true }
[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
schemars = { workspace = true }
serde_repr = "0.1"
tauri = { workspace = true }
thiserror = { workspace = true }

@ -2,12 +2,38 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{fs::create_dir_all, path::Path};
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
};
#[path = "src/scope.rs"]
#[allow(dead_code)]
mod scope;
/// FS scope entry.
#[derive(schemars::JsonSchema)]
#[serde(untagged)]
#[allow(unused)]
enum FsScopeEntry {
/// FS scope path.
Value(PathBuf),
Object {
/// FS scope path.
path: PathBuf,
},
}
// Ensure scope entry is kept up to date
impl From<FsScopeEntry> for scope::EntryRaw {
fn from(value: FsScopeEntry) -> Self {
match value {
FsScopeEntry::Value(path) => scope::EntryRaw::Value(path),
FsScopeEntry::Object { path } => scope::EntryRaw::Object { path },
}
}
}
const BASE_DIR_VARS: &[&str] = &[
"AUDIO",
"CACHE",
@ -163,6 +189,6 @@ permissions = [
tauri_plugin::Builder::new(COMMANDS)
.global_api_script_path("./api-iife.js")
.global_scope_schema(schemars::schema_for!(scope::Entry))
.global_scope_schema(schemars::schema_for!(FsScopeEntry))
.build();
}

@ -11,9 +11,6 @@
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
)]
use std::path::PathBuf;
use serde::Deserialize;
use tauri::{
ipc::ScopeObject,
plugin::{Builder as PluginBuilder, TauriPlugin},
@ -33,6 +30,28 @@ pub use scope::{Event as ScopeEvent, Scope};
type Result<T> = std::result::Result<T, Error>;
// implement ScopeObject here instead of in the scope module because it is also used on the build script
// and we don't want to add tauri as a build dependency
impl ScopeObject for scope::Entry {
type Error = Error;
fn deserialize<R: Runtime>(
app: &AppHandle<R>,
raw: Value,
) -> std::result::Result<Self, Self::Error> {
let entry = serde_json::from_value(raw.into()).map(|raw| {
let path = match raw {
scope::EntryRaw::Value(path) => path,
scope::EntryRaw::Object { path } => path,
};
Self { path }
})?;
Ok(Self {
path: app.path().parse(entry.path)?,
})
}
}
pub trait FsExt<R: Runtime> {
fn fs_scope(&self) -> &Scope;
fn try_fs_scope(&self) -> Option<&Scope>;
@ -48,25 +67,6 @@ impl<R: Runtime, T: Manager<R>> FsExt<R> for T {
}
}
impl ScopeObject for scope::Entry {
type Error = Error;
fn deserialize<R: Runtime>(
app: &AppHandle<R>,
raw: Value,
) -> std::result::Result<Self, Self::Error> {
#[derive(Deserialize)]
struct EntryRaw {
path: PathBuf,
}
let entry = serde_json::from_value::<EntryRaw>(raw.into())?;
Ok(Self {
path: app.path().parse(entry.path)?,
})
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
PluginBuilder::<R, Option<config::Config>>::new("fs")
.invoke_handler(tauri::generate_handler![

@ -11,7 +11,17 @@ use std::{
},
};
#[derive(Debug, schemars::JsonSchema)]
use serde::Deserialize;
#[doc(hidden)]
#[derive(Deserialize)]
#[serde(untagged)]
pub enum EntryRaw {
Value(PathBuf),
Object { path: PathBuf },
}
#[derive(Debug)]
pub struct Entry {
pub path: PathBuf,
}

@ -8,11 +8,11 @@ mod scope;
const COMMANDS: &[&str] = &["fetch", "fetch_cancel", "fetch_send", "fetch_read_body"];
/// HTTP scope entry object definition.
#[allow(unused)]
/// HTTP scope entry.
#[derive(schemars::JsonSchema)]
#[serde(untagged)]
enum ScopeEntry {
#[allow(unused)]
enum HttpScopeEntry {
/// A URL that can be accessed by the webview when using the HTTP APIs.
/// Wildcards can be used following the URL pattern standard.
///
@ -28,7 +28,6 @@ enum ScopeEntry {
///
/// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/"
Value(String),
Object {
/// A URL that can be accessed by the webview when using the HTTP APIs.
/// Wildcards can be used following the URL pattern standard.
@ -48,12 +47,12 @@ enum ScopeEntry {
},
}
// ensure scope entry is up to date
impl From<ScopeEntry> for scope::Entry {
fn from(value: ScopeEntry) -> Self {
// Ensure scope entry is kept up to date
impl From<HttpScopeEntry> for scope::Entry {
fn from(value: HttpScopeEntry) -> Self {
let url = match value {
ScopeEntry::Value(url) => url,
ScopeEntry::Object { url } => url,
HttpScopeEntry::Value(url) => url,
HttpScopeEntry::Object { url } => url,
};
scope::Entry {
@ -69,6 +68,6 @@ impl From<ScopeEntry> for scope::Entry {
fn main() {
tauri_plugin::Builder::new(COMMANDS)
.global_api_script_path("./api-iife.js")
.global_scope_schema(schemars::schema_for!(ScopeEntry))
.global_scope_schema(schemars::schema_for!(HttpScopeEntry))
.build();
}

Loading…
Cancel
Save