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
@@ -0,0 +1,24 @@
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
type Capability = { windows: string[]; permissions: string[] };
const read = (file: string): string => readFileSync(new URL(`../../../${file}`, import.meta.url), "utf8");
const capability = (name: string): Capability => JSON.parse(read(`src-tauri/capabilities/${name}.json`));
test("browser windows have their own capability, a subset of the main window commands", () => {
const browser = capability("browser"), main = capability("default"), settings = capability("settings");
assert.deepEqual(browser.windows, ["browser-*"]);
for (const other of [main, settings]) assert(!other.windows.some(pattern => pattern.startsWith("browser")), "Only browser.json matches browser windows.");
const extra = browser.permissions.filter(permission => !main.permissions.includes(permission));
assert.deepEqual(extra, [], "browser.json grants only permissions that default.json also grants.");
// Browser windows run no installed plugin, so they need no workspace, file, plugin package, terminal, or Git access.
const denied = browser.permissions.filter(permission => /^allow-(workspace|git|pty|backgrounds|tab-transfer|completion|theme|settings-open)-|^allow-plugins-(?!snapshot$)|^(opener|dialog):/.test(permission));
assert.deepEqual(denied, []);
});
test("browser windows start only the built-in browser", () => {
// The capability split depends on this: a browser window never loads an installed plugin.
assert.match(read("src/routes/+page.svelte"), /startExternalPlugins\(browserWindow \? \["runestone\.browser"\] : undefined\)/);
assert.match(read("src-tauri/src/browser.rs"), /let label = format!\("browser-\{\}", uuid::Uuid::new_v4\(\)\);/);
});