From 02d86e5664dd8925adfd248a72aed07674d49219 Mon Sep 17 00:00:00 2001 From: Yizhou Jiang Date: Sun, 22 Nov 2026 11:35:22 +0800 Subject: [PATCH] fix(updates): let the What's new panel read the release it asks for --- src-tauri/tauri.conf.json | 2 +- src/lib/updates/csp.test.ts | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/lib/updates/csp.test.ts diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 3fb386f9..77c044f4 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -23,7 +23,7 @@ } ], "security": { - "csp": "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: asset: http://asset.localhost; font-src 'self' data:; connect-src 'self' ipc: http://ipc.localhost" + "csp": "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: asset: http://asset.localhost; font-src 'self' data:; connect-src 'self' ipc: http://ipc.localhost https://api.github.com" } }, "bundle": { diff --git a/src/lib/updates/csp.test.ts b/src/lib/updates/csp.test.ts new file mode 100644 index 00000000..459f2e2f --- /dev/null +++ b/src/lib/updates/csp.test.ts @@ -0,0 +1,55 @@ +import { describe, expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { fetchReleaseNotes } from "@/lib/updates/notes"; + +const REPO_ROOT = join(import.meta.dir, "../../.."); +const CONFIG = "src-tauri/tauri.conf.json"; + +type Config = { + app?: { security?: { csp?: string | null } }; + version?: string; +}; + +function config(): Config { + try { + return JSON.parse(readFileSync(join(REPO_ROOT, CONFIG), "utf8")) as Config; + } catch (error) { + throw new Error(`${CONFIG} cannot be read: ${String(error)}`); + } +} + +function connectSources(csp: string): string[] { + const directive = /(?:^|;)\s*connect-src([^;]*)/.exec(csp)?.[1] ?? ""; + return directive.trim().split(/\s+/).filter(Boolean); +} + +/** + * The origin comes from the URL the module actually builds, not from a literal kept in this test: + * a change to the notes endpoint must fail here until the shipped policy allows the new origin. + */ +async function notesOrigin(version: string): Promise { + let requested = ""; + await fetchReleaseNotes(version, (url) => { + requested = url; + return Promise.resolve({ ok: true, json: async () => ({ body: "- one" }) }); + }); + expect(requested).not.toBe(""); + return new URL(requested).origin; +} + +const shipped = config(); +const sources = connectSources(shipped.app?.security?.csp ?? ""); + +describe("release notes against the shipped CSP", () => { + test("the webview may request the origin the notes URL uses", async () => { + const origin = await notesOrigin(shipped.version ?? ""); + expect(sources).toContain(origin); + }); + + test("opening that origin did not open up all of https", async () => { + const origin = await notesOrigin(shipped.version ?? ""); + const remote = sources.filter((source) => source.startsWith("https:")); + expect(remote).toEqual([origin]); + }); +});