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
+32 -3
View File
@@ -18,10 +18,35 @@ export function playwright() {
return createRequire(import.meta.url)(process.env.PLAYWRIGHT_MODULE || "playwright");
}
/** Start headless Chromium. CHROME_PATH selects an installed browser. */
export function launchBrowser(options = {}) {
/** True in CSP mode (`pnpm smoke --csp`). The dev server then sends the app's content security policy. */
export const cspMode = process.env.RUNESTONE_CSP === "1";
/** The content security policy violations of all pages in CSP mode. `runSmoke` fails when this list is not empty. */
export const cspViolations = [];
const CSP_PREFIX = "[csp-violation] ";
// This function runs in each page and frame. Violations inside workers do not reach it.
function reportCspViolations(prefix) {
addEventListener("securitypolicyviolation", event => {
console.error(prefix + JSON.stringify({ directive: event.effectiveDirective, blocked: event.blockedURI, source: event.sourceFile, line: event.lineNumber, sample: event.sample, page: location.href }));
}, true);
}
/** Record the violations of a browser context or a page. */
async function watchCsp(target) {
await target.addInitScript(reportCspViolations, CSP_PREFIX);
target.on("console", message => {
if (message.text().startsWith(CSP_PREFIX)) cspViolations.push(message.text().slice(CSP_PREFIX.length));
});
}
/** Start headless Chromium. CHROME_PATH selects an installed browser. In CSP mode, each new context and page records violations. */
export async function launchBrowser(options = {}) {
const executablePath = process.env.CHROME_PATH;
return playwright().chromium.launch({ headless: true, ...(executablePath ? { executablePath } : {}), ...options });
const browser = await playwright().chromium.launch({ headless: true, ...(executablePath ? { executablePath } : {}), ...options });
if (cspMode) {
const { newContext, newPage } = { newContext: browser.newContext.bind(browser), newPage: browser.newPage.bind(browser) };
browser.newContext = async (...args) => { const context = await newContext(...args); await watchCsp(context); return context; };
browser.newPage = async (...args) => { const page = await newPage(...args); await watchCsp(page); return page; };
}
return browser;
}
/**
@@ -267,10 +292,14 @@ export async function runSmoke(name, body, { viewport, timeout, allowPageErrors
page = await openPage(browser, { viewport, timeout });
await body({ browser, page });
if (!allowPageErrors && page.errors.length) throw new Error(`Page errors:\n${page.errors.join("\n")}`);
if (cspViolations.length) throw new Error(`Content security policy violations:\n${[...new Set(cspViolations)].join("\n")}`);
console.log(`PASS ${name}`);
} catch (error) {
console.error(`FAIL ${name}`);
console.error(error);
if (cspViolations.length && !String(error?.message).startsWith("Content security policy violations")) {
console.error(`Content security policy violations:\n${[...new Set(cspViolations)].join("\n")}`);
}
if (page) {
if (page.console.length) console.error(`Console:\n${page.console.join("\n")}`);
const file = path.join(await outputDir(name), "failure.png");