tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
926 stars 257 forks source link

[persisted-scope] v2.0.0-alpha - Not persisted after restart ? #849

Open Vexcited opened 10 months ago

Vexcited commented 10 months ago

Hi!

I have an issue where the scope isn't getting restored after restarting the application. Wonder if that plugin supports it. If it doesn't, I'd have to have all the * variations in my fs allow scope but I don't want this for security reasons.

Here's what happening in my main.rs file, nothing special here.

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_persisted_scope::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_dialog::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Then, in my app, I open a directory using the following piece of code :

import { open } from '@tauri-apps/plugin-dialog';
import { documentDir } from '@tauri-apps/api/path';

const selectedFolderPath = await open({
  multiple: false,
  directory: true,
  recursive: true,
  defaultPath: await documentDir()
});

Then I save the path inside localStorage. When the app gets restarted, it reads that value from localStorage and tries to open back the folder with the following function (also used when opening for the first time the folder through open) :

import { readDir } from "@tauri-apps/plugin-fs";

// I kinda liked the API of FileEntry in 1.x, so I tried to reimplement it.
export interface FileEntry {
  name: string;
  path: string;
  children?: Array<FileEntry>;
}

// Since I'm on Windows, I hardwritten this until I find another better way.
const sep = "\\";

export const listRecursivelyFilesInside = async (absolutePath: string): Promise<Array<FileEntry>> => {
  const entries = await readDir(absolutePath);
  const output: Array<FileEntry> = [];

  for (const entry of entries) {
    if (entry.isDirectory) {
      const dir = absolutePath + sep + entry.name;
      const entries = await listRecursivelyFilesInside(dir);

      output.push({
        name: entry.name,
        path: dir,
        children: entries
      });
    }
    else {
      output.push({
        name: entry.name,
        path: absolutePath + sep + entry.name
      });
    }
  }

  return output;
}

But whenever I restart the app, I get hit by a Error: "forbidden path: C:\\...". Happens on development and production.

Any idea ? Thanks !

FabianLars commented 2 months ago

Is this still an issue in rc? If so, could you upload a minimal reproduction repo for us to try? I'm a bit confused as to why we only got a single report for this issue so i assume this is something app specific.

lsegurado commented 1 month ago

@FabianLars I have the same issue when opening a file asociated. Can be reproduced by sending the file path to the FE without using dialogs and executing readTextFile / readFile

FabianLars commented 1 month ago

Hmm, yeah the File Association feature does not extend the scope as far as i know. Or did you use the FsScope methods yourself and that's not saved?

lsegurado commented 1 month ago

I have no idea about those methods 😂. How are associated files expected to be handled? In this example there is no file manipulation

FabianLars commented 1 month ago

Hmmm, okay we either need to make it behave like the dialog api (auto adding the paths to the scope) or add the scope stuff in the example here https://github.com/tauri-apps/tauri/blob/dev/examples/file-associations/src-tauri/src/main.rs (scopes must be modified in rust).

Edit: I'll update the example in a few. Makes more sense than typing it out twice.

FabianLars commented 1 month ago

Okay, this is a bit messed up but here it is: https://github.com/tauri-apps/tauri/pull/10864/files - for you the interesting parts are the lines that are commented out :/ (the fs_scope() stuff)

lsegurado commented 1 month ago

Works perfectly that way. Thanks!