From e83bf46268875e9d2a6df3d9fe192c58461e3aea Mon Sep 17 00:00:00 2001 From: Knotty Wyvern Date: Wed, 28 Oct 2026 02:01:16 -0700 Subject: [PATCH] Cache plugin package hashes, add a content security policy, and limit browser windows Plugin runtime cost: - Remove the timed plugin scan. Windows scan at start, on focus, on Refresh, and on plugins-changed. - Keep one package check for each plugin folder. Read and hash the files again only when the entry names, sizes, or modification times change. Enable and load always read all files. An asset read checks the SHA-256 hash of its file. - A changed package still stops and needs a new enable decision. Add tests for the cache, for changes that keep the metadata, and for the enable decision. - Replace the 50 ms wait loops of update and removal with a condition variable. The limit stays at 10 seconds. Command names, arguments, and results do not change. Content security policy: - Set app.security.csp in tauri.conf.json. Keep Tauri from adding nonces to style-src. - Add CSP mode to the smoke tests (pnpm smoke --csp). The dev server sends the same policy with each HTML page, and the harness fails a test on a policy violation. - Decode data URLs without fetch in the background frame test. Capabilities: - Give browser windows their own capability. They load no installed plugin. Add a unit test and a browser window smoke test for the command set. Document the scans, the hash cache, the policy, and the window capabilities. --- docs/development.md | 19 +- docs/plugin-host.md | 78 +++- scripts/smoke.mjs | 24 +- src-tauri/capabilities/browser.json | 27 ++ src-tauri/capabilities/default.json | 3 +- src-tauri/src/plugins.rs | 410 +++++++++++++----- src-tauri/tauri.conf.json | 15 +- src/lib/plugins/external.ts | 5 +- tests/smoke/_harness/index.mjs | 35 +- .../appearance/backgroundFrames.smoke.mjs | 4 +- tests/smoke/browser/window.smoke.mjs | 53 +++ tests/unit/plugins/windowCapabilities.spec.ts | 24 + vite.config.js | 69 ++- 13 files changed, 639 insertions(+), 127 deletions(-) create mode 100644 src-tauri/capabilities/browser.json create mode 100644 tests/smoke/browser/window.smoke.mjs create mode 100644 tests/unit/plugins/windowCapabilities.spec.ts diff --git a/docs/development.md b/docs/development.md index 8cc1b05..6346dbe 100644 --- a/docs/development.md +++ b/docs/development.md @@ -36,7 +36,7 @@ Do not start a separate `pnpm dev` for this workflow. | Rust code in `src-tauri/src/` | Cargo rebuilds the changed code, and Tauri restarts the app. | | Rust dependencies or build settings | Cargo compiles the affected crates. This can take several minutes. | | JavaScript dependencies, after a pull | Stop the session, run `pnpm install --frozen-lockfile`, and start it again. | -| Tauri configuration, capabilities, or Vite configuration | Restart `pnpm tauri dev`. | +| Tauri configuration, capabilities, or Vite configuration | Restart `pnpm tauri dev`. The content security policy applies only to a built app, not to `pnpm tauri dev`. See [Content security policy](plugin-host.md#content-security-policy). | | A plugin in a sibling repository | Build and package that plugin, then install the new ZIP through Settings. See [Build examples](plugin-development.md#build-examples). | | Installer or release behavior | Use the [build guide](builds.md). | @@ -74,6 +74,7 @@ Windows is the primary platform. macOS and Linux builds are experimental. The interface calls Rust commands through Tauri `invoke`. Most calls go through the wrappers in `src/lib/workspace/api.ts`. A new command needs an entry in `generate_handler!` in `lib.rs`, in the command list in `src-tauri/build.rs`, and in the capability files in `src-tauri/capabilities/`. +`default.json` must list every command. `settings.json` and `browser.json` list subsets. See [Window capabilities](plugin-host.md#window-capabilities). ### Interface: `src/lib/` @@ -193,6 +194,7 @@ They do not replace tests in the native app. Real folder access, file writes, an | `pnpm smoke` | All smoke tests, four at a time | | `pnpm smoke tabs tables/drag` | The tests whose path contains one of the filters | | `pnpm smoke --list` | Nothing. It lists each test, its requirements, and why a test would skip. | +| `pnpm smoke --csp` | All smoke tests under the content security policy of the app. A test fails on any policy violation. | The runner starts the Vite server when nothing serves `SMOKE_URL`, and stops it at the end. Use `--no-serve` to require a running server, `--jobs ` to change the number of parallel tests, and `--timeout ` to change the limit for each test (default 180). @@ -216,6 +218,7 @@ A test states what it needs near its top. The runner skips the test, with the re | `SMOKE_URL` | The frontend address. The default is `http://127.0.0.1:1420`. When the runner starts Vite, it uses the port of this address, so two checkouts can run tests at the same time. | | `RUNESTONE_PLUGIN_REPOS` | The folder of the sibling plugin checkouts. The default is the parent folder of this repository. | | `RUNESTONE_TERMINAL_DIST` | The compiled Terminal plugin. The default is `RuneStone_Terminal/dist` in that folder. | +| `RUNESTONE_CSP` | `1` sends and checks the content security policy. `--csp` sets it. | | `CHROME_PATH` | A Chromium browser for Playwright | | `PLAYWRIGHT_MODULE` | A different Playwright package | @@ -227,6 +230,20 @@ To read or change a section later, use `window.__TEST__.settings(section)` and ` A failed test saves `failure.png` in its output folder and prints the page console errors and warnings. You can also run one test file directly while `pnpm dev` runs, for example `node tests/smoke/tabs/order.smoke.mjs`. +### CSP mode + +`--csp` sets `RUNESTONE_CSP=1`. With this variable, `vite.config.js` sends the policy from `src-tauri/tauri.conf.json` as a `Content-Security-Policy` header with each HTML page, as the installed app does. +It adds only what the dev server needs: + +| Directive | Addition | Reason | +| --- | --- | --- | +| `script-src` | The SHA-256 hash of each inline script in the page | Tauri adds the same hashes in the installed app. The SvelteKit start script is inline. | +| `connect-src` | `ws://
` | The Vite hot-reload WebSocket | + +The harness records the `securitypolicyviolation` events of each page and frame, and fails the test when there is one. Violations inside workers do not reach the harness. +The runner refuses to use a server at `SMOKE_URL` that does not send the policy. Stop that server, or set `SMOKE_URL` to a free port. +The smoke tests do not replace a check in the installed app. The page there is the Tauri web view, and some plugins are not in the smoke tests. See [Content security policy](plugin-host.md#content-security-policy). + The tests in `tests/native/` attach to a running desktop app through the WebView2 remote-debugging port, and `pnpm smoke` does not run them. Set `NATIVE_CDP_URL` to the debugging address, for example `http://127.0.0.1:9224`. Before you start the app, set `WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--remote-debugging-port=`. Each file states its port and variables at the top. diff --git a/docs/plugin-host.md b/docs/plugin-host.md index 802effa..c8da4b0 100644 --- a/docs/plugin-host.md +++ b/docs/plugin-host.md @@ -6,6 +6,8 @@ This reference describes how RuneStone stores, installs, downloads, and removes - [Storage and recovery](#storage-and-recovery) - [Gitea downloads](#gitea-downloads) +- [Content security policy](#content-security-policy) +- [Window capabilities](#window-capabilities) - [Modules and checks](#modules-and-checks) ## Storage and recovery @@ -37,17 +39,37 @@ Each window asks for a new snapshot at these times: | --- | --- | | Window start | `startExternalPlugins` in `src/lib/plugins/external.ts` | | Window focus | The same function | -| Every 5 seconds | The same function. It finds changes from a file manager. | | **Refresh** in Settings → Plug-ins | `PluginManager.svelte` | | `plugins-changed` event | Any change from another window | -A scan reads files and hashes only. It does not run code, install dependencies, or run scripts. +There is no timed scan. A change from a file manager appears at the next focus or **Refresh**. RuneStone does not support packages that users copy into the folder. + +A scan reads file metadata, and reads and hashes files only when necessary. It does not run code, install dependencies, or run scripts. - A package with a changed hash gets an error and becomes disabled. The user must review it and enable it again. - When a package folder is missing, the service removes its catalog entry. It never restores a missing package. - A valid package that someone copies directly into `plugins/` appears as disabled, with an unknown install time. - The service never scans `plugin-staging/`. +### Package hashes + +The package hash is a SHA-256 hash of all file names and contents. To avoid a full read of each package at each scan, the service keeps a cache in memory: + +| Step | Reads | +| --- | --- | +| Scan (`plugins_snapshot`), `plugins_data`, and the row checks of other commands | The metadata of the package folder: entry names, file sizes, and modification times. The service reads and hashes the files again only when this metadata changed. | +| Enable (`plugins_set_enabled`) and load (`plugins_load`) | All files. The hash must agree with the hash that the user saw (enable) or the installed hash (load). The cache cannot approve code. | +| Asset (`plugins_asset`) | The metadata, then the one file. The file must have the SHA-256 hash from the last full read. | +| Install, update, and removal | All files, as before. These steps also clear the cache entry of the package. | + +The cache keeps the metadata from before each read. A file that changes during a read thus causes a new read at the next check. +An edit that keeps the file size and the modification time stays hidden from a scan. The next load or asset read finds it, and the plugin then becomes disabled at the next scan. + +### Waits for other windows + +An update and a removal disable the plugin, then wait until no window holds a lease, at most 10 seconds. +The wait uses a condition variable that `plugins_release` and window close signal. It does not keep the plugin lock while it waits. + ### Install and update 1. The ZIP picker accepts at most 100 `.zip` files. Folders and loose manifests are not installation inputs. @@ -124,6 +146,55 @@ The review shows the repository, the tag, and the attachment link without its qu A change to the remote release after review does not change the pending installation. The same ID, enable, update, and removal rules apply to local and remote packages. +## Content security policy + +`app.security.csp` in `src-tauri/tauri.conf.json` holds the policy. It limits what a page can load. It does not isolate plugins: a plugin module runs in the app window with the same access as the app. +The policy stops a script that comes in through rendered content, for example a `