# Capabilities and permissions

> What a page may reach and who decides it.

A page starts with nothing beyond the web platform. Your app grants each thing it may reach:

| What the page reaches | Who decides |
|---|---|
| Your commands, events and streams | the origin rules of [IPC](https://kurogane-rs.org/docs/concepts/ipc.md#who-may-call) |
| Files on disk | the filesystem capability's grants |
| Camera, microphone, location and Chromium's other permissions | [`App::on_permission`](https://kurogane-rs.org/docs/api/app/app.md#method.on_permission) |
| New windows, navigation, downloads and other requests | a hook for each request |

Every grant binds pages only. Your Rust code isn't restricted by any of them.

## The filesystem capability

A page reaches files only through `window.kurogane.fs` and only within the grants your app gives its origin. [`App::filesystem`](https://kurogane-rs.org/docs/api/app/app.md#method.filesystem) takes them:

```rust
use kurogane::capability::{Filesystem, FsAccess};
use kurogane::{App, Origin};

fn main() {
    let root = std::env::temp_dir().join("notes");

    let mut fs = Filesystem::builder();
    let notes = fs.scope("notes", |scope| {
        scope.allow_directory_recursive(&root);
        scope.deny_path(root.join("private"));
    });
    fs.grant(
        Origin::parse("app://app").unwrap(),
        notes,
        FsAccess::READ | FsAccess::LIST | FsAccess::WRITE | FsAccess::CREATE,
    );

    App::new("content").filesystem(fs).run_or_exit();
}
```

In the page:

```javascript
const bytes = await window.kurogane.fs.readFile("today.md");
const text = new TextDecoder().decode(bytes);
await window.kurogane.fs.writeFile("today.md", text + "\n- done");
```

### Scopes and grants

A scope names the directories a page may reach and the paths it may not. A grant gives an origin a set of capabilities over a scope. Several grants to one origin combine.

| [`ScopeBuilder`](https://kurogane-rs.org/docs/api/capabilities/scopebuilder.md) method | What it does |
|---|---|
| `allow_directory` | allows a directory and its immediate children |
| `allow_directory_recursive` | allows a directory and its whole subtree |
| `deny_path` | denies a literal path and everything beneath it |
| `deny_glob` | denies what a pattern such as `**/.git` matches beneath every allowed root |

Deny rules always win over allow rules. Every allowed root must exist when the app starts.

### Operations

Each `fs` method needs one [`FsAccess`](https://kurogane-rs.org/docs/api/capabilities/fsaccess.md) capability:

| Method | Needs |
|---|---|
| `readFile(path)` | `READ` |
| `writeFile(path, data)` | `WRITE` over an existing file, `CREATE` for a new one |
| `exists(path)`, `size(path)` | `METADATA` |
| `readDir(path)` | `LIST` |
| `createDir(path)` | `CREATE` |
| `removeFile(path)`, `removeDir(path)` | `DELETE` |
| `renameFile(src, dst)` | `RENAME` over both paths |
| `copyFile(src, dst)` | `READ` over `src` and `CREATE` over `dst` |

`FsAccess::ALL` holds every capability. Paths are absolute or relative to the origin's one allowed root.

### What holds

* Links, junctions and other reparse points are never followed, listed or opened.
* The real location of an opened file is checked again before use.
* A denied request never reveals whether the path exists.
* `readFile` and `writeFile` move whole files up to a cap. [`FilesystemBuilder::max_file_size`](https://kurogane-rs.org/docs/api/capabilities/filesystembuilder.md#method.max_file_size) changes it. `copyFile` stays in the browser process and has no cap.
* A file with other names (hard links) is refused unless [`FilesystemBuilder::allow_hard_links`](https://kurogane-rs.org/docs/api/capabilities/filesystembuilder.md#method.allow_hard_links) permits it.

A refused call rejects with an `Error` whose `code` says why:

| Code | Meaning |
|---|---|
| `-5` | no grant of the origin carries the capability |
| `-6` | the path is outside the granted roots, denied or a link |
| `-7` | the path is malformed |
| `-8` | the file exceeds the transfer cap |
| `0` | the operating system refused; `message` holds its error |

[`Filesystem::authorize`](https://kurogane-rs.org/docs/api/capabilities/filesystem.md#method.authorize) gives the exact authority an origin holds. Use it to test your grants without a window.

## Chromium's permissions

A page asks before it uses a camera, a microphone, the screen, the location, notifications or another [`Permission`](https://kurogane-rs.org/docs/api/requests/permission.md). Without a hook every request is denied and Chromium's own prompt never shows. `App::on_permission` decides instead:

```rust
use kurogane::{App, Origin, Permission, PermissionDecision};

fn main() {
    let own = Origin::parse("app://app").unwrap();

    App::new("content")
        .on_permission(move |request, _app| {
            let calls = request
                .permissions()
                .iter()
                .all(|kind| matches!(kind, Permission::Camera | Permission::Microphone));
            if request.origin() == &own && calls {
                PermissionDecision::Allow
            } else {
                PermissionDecision::Deny
            }
        })
        .run_or_exit();
}
```

* A request is granted or refused whole. A page asking for a camera and a microphone together gets both or neither.
* `Later` waits for an answer from the request's [`PermissionResponder`](https://kurogane-rs.org/docs/api/requests/permissionresponder.md), from any thread. Ask the user that way. A responder dropped unanswered denies.
* `Default` gives Kurogane's answer. That's `Deny`.
* Requests from DevTools never reach the hook and are denied.

> [!WARNING]
> Allowing `Permission::ScreenVideo` lets the page record the whole screen with no picker.

### What Chromium remembers

Chromium remembers its answer to an `http` or `https` site in the profile. The site's later requests get that answer without reaching the hook. [`AppHandle::forget_permissions`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.forget_permissions) makes it ask again after your policy changed.

A camera, a microphone and the screen are never remembered. Nothing is remembered for your app's own pages either. A grant that has to outlast the request doesn't hold there. Such a page is told yes for notifications and the location but can't show a notification or read the location.

## Other requests

Kurogane asks your app before Chromium acts on a page's other requests. Each hook takes a typed request and answers with a decision whose `Default` leaves the choice to Kurogane:

| Hook | Request |
|---|---|
| [`on_new_window`](https://kurogane-rs.org/docs/api/app/app.md#method.on_new_window) | a page opens a window |
| [`on_navigation`](https://kurogane-rs.org/docs/api/app/app.md#method.on_navigation) | a page leaves for another origin |
| [`on_download`](https://kurogane-rs.org/docs/api/app/app.md#method.on_download) | a page starts a download |
| [`on_file_dialog`](https://kurogane-rs.org/docs/api/app/app.md#method.on_file_dialog) | a page opens a file chooser |
| [`on_context_menu`](https://kurogane-rs.org/docs/api/app/app.md#method.on_context_menu) | the user right-clicks |
| [`on_key`](https://kurogane-rs.org/docs/api/app/app.md#method.on_key) | the user presses a key |
| [`on_chrome_command`](https://kurogane-rs.org/docs/api/app/app.md#method.on_chrome_command) | Chromium runs one of its commands |
| [`on_drag_enter`](https://kurogane-rs.org/docs/api/app/app.md#method.on_drag_enter) | something is dragged into an embedded browser |

Every hook runs on the UI thread and must not block. A hook that panics gets the safe answer. The [Requests and decisions](https://kurogane-rs.org/docs/api/requests.md) reference covers each request and its decisions.
