#Forbidden Path: Permissions, Capabilities and where to put Files?

34 messages · Page 1 of 1 (latest)

dull plover
#

Hi there, i have a question:
I want to put some html templates in the src/templates folder and load them dynamicly.
I have them listed in the tauri.conf.json

        "resources": [
            "../src/templates/*",

and i have a fs-read-capabilities.tml

identifier = "fs-read-templates"
description = "Erlaubt das Lesen von HTML-Templates"
windows = ["main"]
permissions = [
  "fs:allow-exists",
  "fs:allow-read-text-file",
  "fs:allow-resource-read-recursive",
]

[scope.fs.allow]
path = ["$RESOURCE/../../src/templates/*"]

i tried it so many times with all kinds of paths, even having it under src_tauri/resources/templates
but i always get the forbidden path warning.

my call looks like this:

        const templatePath = "templates";
        const resourcePath = `${templatePath}/${name}`;
        const fileExists = await exists(resourcePath);
        if (!fileExists) {
            console.warn("⚠️ Datei existiert nicht:", resourcePath);
        } else {
            console.warn("⚠️ Datei existiert:", resourcePath);
        }

        const template = await readTextFile(templatePath + name, {
            //baseDir: BaseDirectory.AppData,
            baseDir: BaseDirectory.Resource,
            // baseDir: (await isDev)
            //     ? BaseDirectory.AppData
            //     : BaseDirectory.Resource,
        });

How can i get this working?
Where do i get it wrong?
Where should i best put my Templates?

full torrent
#

When using the bundled resources feature, you must use the same path or provide a key-value in the config.

dull plover
full torrent
#

Not normally.

dull plover
#

What excactly do you mean with: use the same path or provide a key-value in the config ?

full torrent
#

If you configure it as ../src/templates/name then you can find it at await resolveResource("../src/templates/name").

dull plover
#

this looks good! i try this!

full torrent
#

You won't need the path = ["$RESOURCE/../../src/templates/*"] if you have specified "fs:allow-resource-read-recursive".

#

If I remember correctly, the path does change between dev and production builds but it shouldn't change the behaviour when using that API.

dull plover
# full torrent You won't need the `path = ["$RESOURCE/../../src/templates/*"]` if you have spec...

I changed it now to

[scope.fs.allow]
path = ["../../src/templates/*"]

and added the resoveResource again:

export async function loadTemplate(name: string): Promise<string> {
    const templatePath = "templates";
    try {
        const resourcePath = await resolveResource("../src/templates/" + name);
        // const resourcePath = `${templatePath}/${name}`;
        console.log("📁 Recource path:", resourcePath);

        const fileExists = await exists(resourcePath);
        if (!fileExists) {
            console.warn("⚠️ Datei existiert nicht:", resourcePath);
        } else {
            console.warn("⚠️ Datei existiert:", resourcePath);
        }

        const template = await readTextFile(templatePath + name, {
            // baseDir: BaseDirectory.AppData,
            baseDir: BaseDirectory.Resource,
            // baseDir: (await isDev)
            //     ? BaseDirectory.AppData
            //     : BaseDirectory.Resource,
        });

        console.log("✅ Template geladen:", template);
        return template;

but now i get the sam problem again like yesterday, that wy i threw out the resolve function:
the path ist now: asset://localhost/_up_/src/templates/menu_foto_upload.html
i do not understand it

full torrent
#

You should still be able to pass it to readTextFile.

dull plover
#

it throws forbidden path still

#

asset://localhost/up/src/templates/menu_foto_upload.html
is the path

full torrent
#

Are you using templatePath + name or resourcePath for the readTextFile?

dull plover
#

Hey icb i am very thankfull for your help!

dull plover
#

thats the code now

        const resourcePath = await resolveResource("../src/templates/" + name);
        console.log("📁 Recource path:", resourcePath);
        const template = await readTextFile(resourcePath, {
            baseDir: BaseDirectory.Resource,
        });

        console.log("✅ Template geladen:", template);
        return template;
full torrent
#

That should work. I'll try to figure out what the problem is in Rust.

dull plover
#

i am asking my self from what root are all these paths are starting?
there is:
tauri config json:

"resources": [
  "../src/templates/*",

the capabilites.toml:

[scope.fs.allow]
path = ["../src/templates/*"]

and the call in type script:

const resourcePath = await resolveResource("../src/templates/" + name);

they are all in different folders and i only know that the tauri.config.json starts from its folder acording to the docs

full torrent
#

resolveResource will start from the $RESOURCE directory, which is different for each platform.

#

Do you still have "fs:allow-resource-read-recursive" in the capabilities.toml?

dull plover
#

yes!

full torrent
#

I'll try with just that path allowed.

#

Sorry, not path. I meant permission.

dull plover
#

Ok, now it states: faild to open file
maybe the path is wrong, but allowed now

#

but when i go ***../../templates... *** in the cconfig file it throws an compile error

#

is there a way to find out where the resource folder is located on android?
this is the only plattform that matters for now

full torrent
#

If this is for Android, I will have to look into the problem in a few hours as I'm currently working on a system that only has the desktop dependencies installed.

dull plover
#

Hey @full torrent i thank you very much for your help ❣️
If i find the solution i post it here

dull plover
#

The Solution for now:

Since the resources paths are expected to be relative to the src-tauri directory, not the project root directory, the path ../src/templates/* lies** outside the src-tauri context, which causes issues.**

On Android, resources are embedded in the app bundle, and access does not occur through the regular filesystem. The BaseDirectory.Resource in the readTextFile call is correct, but the path resolution with ../src/templates/ results in the file not being found.

The resources must be moved to the src-tauri directory and referenced in the configuration.

tauri_app/
├── src/
│   ├── gui_elements.ts
│   └── index.html
├── src-tauri/
│   ├── tauri.conf.json
│   ├── src/
│   │   └── main.rs
│   └── **templates/**
│       ├── menu_foto_upload.html
│       └── andere_templates.html
"resources": [
    "templates/*",
[scope.fs.allow]

Is empty, because it does not need any path scope permissions for that operation.
I dont know if that is right so far but it works.

full torrent
#

Sorry I took so long to get back to you. It seems that using a map for the paths works for Android, as shown in the 2nd code block of https://tauri.app/develop/resources/. For your case, that would be the following in tauri.conf.json:

    "resources": {
      "../src/templates/*": "templates/"
    }

You can then put the templates directory back inside the project root src directory rather than inside src-tauri.

#

Keep in mind that putting them in the project root src directory will cause those files to be bundled twice if you're not using a framework, the first time as internally stored assets for your frontend and the second time as externally stored resources.