new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Tauri · Plugins and security · all subjects

fs

31 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

File System plugin setup - automatic

Use your project's package manager to add the fs plugin dependency with: npm run tauri add fs, yarn run tauri add fs, pnpm tauri add fs, deno task tauri add fs, bun tauri add fs, or cargo tauri add fs.

File System plugin setup - manual Rust

To manually add the fs plugin to Rust, run 'cargo add tauri-plugin-fs' in the src-tauri folder, then modify lib.rs to add .plugin(tauri_plugin_fs::init()) to the tauri::Builder::default() chain before .run(tauri::generate_context!()).

File System plugin setup - manual JavaScript

Install JavaScript Guest bindings using: npm install @tauri-apps/plugin-fs, yarn add @tauri-apps/plugin-fs, pnpm add @tauri-apps/plugin-fs, deno add npm:@tauri-apps/plugin-fs, or bun add @tauri-apps/plugin-fs.

File System plugin iOS privacy configuration

On iOS, create a PrivacyInfo.xcprivacy file in src-tauri/gen/apple with NSPrivacyAccessedAPICategoryFileTimestamp key and C617.1 recommended reason to specify approved API usage reasons for user privacy compliance.

File System plugin on Rust backend

On the Rust backend, the fs plugin offers only methods to change permissions of resources. For file manipulation, use traditional Rust libraries: std::fs, tokio::fs, or others.

File System plugin security - path traversal prevention

The fs plugin prevents path traversal by not allowing parent directory accessors. Paths like '/usr/path/to/../file' or '../path/to/file' are not allowed. Paths must be either relative to a base directory or created with the path API.

File System API - exists function

The exists() function checks if a file or directory exists. Example: await exists('avatar.png', { baseDir: BaseDirectory.AppData }). Returns boolean.

File System API - writeTextFile function

The writeTextFile() function writes text to a file. Example: const contents = JSON.stringify({ notifications: true }); await writeTextFile('config.json', contents, { baseDir: BaseDirectory.AppConfig });

File System API - writeFile function

The writeFile() function writes binary data to a file. Example: const contents = new Uint8Array(); await writeFile('config', contents, { baseDir: BaseDirectory.AppConfig });

File System API - readTextFileLines function

The readTextFileLines() function streams lines from a large text file. Example: const lines = await readTextFileLines('app.logs', { baseDir: BaseDirectory.AppLog }); for await (const line of lines) { console.log(line); }

File System API - readFile function

The readFile() function reads a binary file. Example: const icon = await readFile('icon.png', { baseDir: BaseDirectory.Resources });

File System API - stat function

The stat() function retrieves file or directory metadata. It follows symlinks and returns an error if the actual file it points to is not allowed by the scope. Example: const metadata = await stat('app.db', { baseDir: BaseDirectory.AppLocalData });

File System API - lstat function

The lstat() function retrieves file or directory metadata without following symlinks, returning information of the symlink itself.

File System API - watch function

The watch() function watches a directory or file for changes with debouncing that only emits events after a delay. Example: await watch('app.log', (event) => { console.log('app.log event', event); }, { baseDir: BaseDirectory.AppLog, delayMs: 500 });

File System API - watchImmediate function

The watchImmediate() function immediately notifies listeners of file system events without debouncing. Example: await watchImmediate('logs', (event) => { console.log('logs directory event', event); }, { baseDir: BaseDirectory.AppLog, recursive: true });

File System watch non-recursive by default

By default watch operations on a directory are not recursive. Set the recursive option to true to recursively watch for changes on all sub-directories.

File System plugin base directories

Every fs plugin API has an options argument with a baseDir parameter that acts as the working directory. This is one of two ways to manipulate paths, along with the path API. Example: await readFile('avatars/tauri.png', { baseDir: BaseDirectory.Home });

File System plugin path API

Alternatively to baseDir, you can use the @tauri-apps/api/path module to manipulate paths. Example: import * as path from '@tauri-apps/api/path'; const home = await path.homeDir(); const contents = await readFile(await path.join(home, 'avatars/tauri.png'));

File System scope deny takes precedence

In fs plugin scopes, deny takes precedence over allow. If a path is denied by a scope, it will be blocked at runtime even if it is allowed by another scope.

File System Rust backend using FsExt

To use fs plugin on the Rust backend, import FsExt trait and use app.fs_scope() to manage file system access. Example: use tauri_plugin_fs::FsExt; let scope = app.fs_scope(); scope.allow_directory("/path/to/directory", false); dbg!(scope.allowed());

Example file system read-files permission

The fs plugin includes a read-files permission that enables all file read related commands: read_file, read, open, read_text_file, read_text_file_lines, and read_text_file_lines_next. This permission has no pre-configured accessible paths.

Example file system scope-home permission

The fs plugin includes a scope-home permission that permits access to all files and listing content of top-level directories in the $HOME folder via the path scope $HOME/*.

Tauri fs plugin default permissions structure

The fs plugin's default permissions are defined in a TOML file with a '[default]' section containing a description and permissions array. The default fs permissions include: permissions = ["read-all", "scope-app-recursive", "deny-default"]. This enables all read-related commands and allows access to the $APP folder and subdirectories. On Windows, webview data folder access is denied by default.

Customizing fs plugin write-text-file scope

The fs plugin has only autogenerated scopes for accessing entire folders like $HOME. To restrict write-text-file command to only 'test.txt' in the home directory, create a custom scope in capabilities/default.json with identifier 'fs:allow-write-text-file' and allow array containing { "path": "$HOME/test.txt" }.

Adding fs plugin to Tauri app

To add the official fs plugin to a Tauri app, run 'pnpm tauri add fs' for automated setup. Alternatively, manually add 'cargo add tauri-plugin-fs' and initialize in lib.rs by adding '.plugin(tauri_plugin_fs::init())' to the Builder chain before '.run(tauri::generate_context!())'.

Writing text files with fs plugin from JavaScript

To write a text file using the Tauri fs plugin, import writeTextFile and BaseDirectory from '@tauri-apps/plugin-fs', then call: await writeTextFile('test.txt', message, { baseDir: BaseDirectory.Home }). This writes to the home directory.

Permission denied error for fs.write_text_file

If executing fs.write_text_file results in error 'fs.write_text_file not allowed' with listed permissions like fs:allow-app-write, fs:allow-app-write-recursive, fs:allow-appcache-write, this indicates the required permission and corresponding scope were not correctly added to the capability file.

fs plugin default permissions include read-all and scope-app-recursive

The fs plugin default permission set includes "read-all", "scope-app-recursive", and "deny-default". This enables all read-related commands, allows access to the $APP folder and its subdirectories, and denies access to critical components like the webview data folder on Windows by default.

Example: write text file to home directory with permission

To write a text file to the home directory, add the permission fs:allow-write-text-file to the capabilities, then create a custom scope for fs:allow-write-text-file with {"identifier": "fs:allow-write-text-file", "allow": [{"path": "$HOME/test.txt"}]}. Use the API: await writeTextFile('test.txt', message, { baseDir: BaseDirectory.Home }); from @tauri-apps/plugin-fs.

Path traversal protection in File System plugin

The File System module prevents path traversal attacks and does not allow accessor methods to access parent directories. Paths like /usr/path/to/../file or ../path/to/file are not permitted. All accessed paths must be related to one of the base directories or created using the path API.

Fs plugin uses glob-compatible paths for file access scoping

The Fs plugin uses string-type scopes containing glob-compatible paths to allow or deny access to specific directories and files.

Give your agent this brain