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.
This commit is contained in:
Knotty Wyvern
2026-10-28 02:01:50 -07:00
parent a6d6e96d6d
commit e83bf46268
13 changed files with 639 additions and 127 deletions
+75 -3
View File
@@ -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 `<script>` element or an event attribute in a Markdown document.
| Directive | Sources | Reason |
| --- | --- | --- |
| `default-src` | `'self'` | All other types come only from the app. |
| `script-src` | `'self' blob: 'wasm-unsafe-eval'` | `blob:`: plugin entry modules, which `external.ts` loads from Blob URLs. `'wasm-unsafe-eval'`: the PDF Viewer decodes JPEG 2000 and JBIG2 images and color profiles with WebAssembly in its worker. It does not permit `eval`. |
| `style-src` | `'self' 'unsafe-inline'` | Svelte `style` attributes, the `style` attribute in `src/app.html`, Mermaid SVG styles, and the `<style>` elements of Monaco, the Vite dev server, and plugins (Terminal, CSV Viewer, PDF Viewer, Background Slideshow). |
| `img-src` | `'self' data: blob: https: http:` | `data:`: workspace images, browser frames, tab and site icons, and icons in CSS. `blob:`: background images. `https:` and `http:`: remote images in Markdown previews. |
| `font-src` | `'self' data:` | The Monaco icon font, and fonts that the PDF Viewer embeds. |
| `connect-src` | `'self' ipc: http://ipc.localhost blob:` | `ipc:` and `http://ipc.localhost`: Tauri commands. `blob:`: the Background Slideshow worker reads its image from a Blob URL. |
| `worker-src` | `'self' blob:` | `'self'`: Monaco workers. `blob:`: the PDF Viewer and Background Slideshow workers. |
| `object-src` | `'none'` | RuneStone uses no plugins of the web view. |
| `base-uri` | `'self'` | |
| `form-action` | `'none'` | No form sends data to an address. |
| `frame-ancestors` | `'none'` | |
No source permits `'unsafe-eval'`. The app, Monaco, Mermaid, and the current plugins do not use `eval` or `new Function`.
`style-src` needs `'unsafe-inline'`, because style attributes and generated `<style>` elements have no nonce.
Tauri sends the policy as a header with each HTML page of the installed app. It adds the SHA-256 hash of the inline start script of SvelteKit to `script-src`.
`dangerousDisableAssetCspModification: ["style-src"]` stops Tauri from adding nonces to `style-src`, because a nonce disables `'unsafe-inline'`. Tauri still adds the script hashes.
`pnpm tauri dev` loads the page from the Vite server. Tauri does not apply the policy there. Use the [CSP mode](development.md#csp-mode) of the smoke tests, or an installed build.
Workers that the app loads from its own files get no policy. Workers from Blob URLs get the policy of the page.
## Window capabilities
`src-tauri/capabilities/` gives commands to window labels. A plugin can call every command that its window allows.
| Capability | Windows | Plugins | Commands |
| --- | --- | --- | --- |
| `default.json` | `main`, `workspace-*`, `terminal-*` | All enabled plugins | All app commands |
| `settings.json` | `settings-main`, `settings-workspace-*` | All enabled plugins | Settings, plugins, backgrounds, completion, browser |
| `browser.json` | `browser-*` | Only the built-in browser (`src/routes/+page.svelte`) | Browser, `plugins_snapshot`, settings, spelling, and window controls |
A browser window loads no installed plugin, so its capability has no workspace, file, Git, terminal, or plugin package commands. `tests/unit/plugins/windowCapabilities.spec.ts` and `tests/smoke/browser/window.smoke.mjs` check this.
The other windows load every enabled plugin. A smaller command set for them would limit only the app code, not the plugins. That split belongs to the plugin isolation work. Findings for that work:
- Terminal windows use the plugin host commands, `pty_*` through the Terminal plugin, and `core:window:allow-destroy`. Other plugins in the same window expect the full host API.
- Settings windows use no `workspace_*`, `git_*`, or `pty_*` commands, but plugin settings views run there.
- Commands that need a workspace (`workspace_*`, `git_*`, `pty_open`, `pty_new_window`) fail in a window without a bound workspace.
- `pty_write`, `pty_resize`, and `pty_close` do not check the window that opened the terminal.
- `backgrounds_*`, and `browser_request` without a browser session token, need a plugin lease of the calling window.
- A new command for plugins goes in `default.json` and, if settings views need it, in `settings.json`. Add it to `browser.json` only when the built-in browser needs it.
## Modules and checks
| Module | Owns |
@@ -154,7 +225,7 @@ To make it without a release ZIP, run `node scripts/build.mjs --no-package` in t
Unit tests:
```powershell
node --experimental-strip-types --test tests/unit/plugins/plugins.spec.ts tests/unit/plugins/externalPlugins.spec.ts tests/unit/plugins/pluginPackaging.spec.ts tests/unit/plugins/pluginHostExtensions.spec.ts tests/unit/plugins/pluginDocuments.spec.ts tests/unit/plugins/pluginAccess.spec.ts tests/unit/plugins/completion.spec.ts tests/unit/theme/backgrounds.spec.ts
node --experimental-strip-types --test tests/unit/plugins/plugins.spec.ts tests/unit/plugins/externalPlugins.spec.ts tests/unit/plugins/pluginPackaging.spec.ts tests/unit/plugins/pluginHostExtensions.spec.ts tests/unit/plugins/pluginDocuments.spec.ts tests/unit/plugins/pluginAccess.spec.ts tests/unit/plugins/completion.spec.ts tests/unit/plugins/windowCapabilities.spec.ts tests/unit/theme/backgrounds.spec.ts
```
Native tests use real temporary files and archives:
@@ -173,6 +244,7 @@ node tests/smoke/plugins/choiceMenus.smoke.mjs
node tests/smoke/find/pluginViews.smoke.mjs
node tests/smoke/tabs/lastTabAndSiteIcons.smoke.mjs
node tests/smoke/settings/general.smoke.mjs
node tests/smoke/browser/window.smoke.mjs
```
`pluginInstall.smoke.mjs` covers the link form, download errors, token clearing, cancel, focus, and disabled installation.