Files
photoreport/apps/api/test/desktop-updates.test.ts
spark 985188812a
ci / rust (pull_request) Successful in 4m4s
ci / typescript (pull_request) Successful in 8m25s
fix(desktop): reject update manifests whose signature version disagrees
A retagged desktop-v* on the wrong commit announced 0.3.25 while the
binary was still signed as 0.3.24. Prefer the newest asset, skip mismatched
releases, and refuse to write such a manifest at build time.
2026-11-02 22:37:35 +00:00

132 lines
7.8 KiB
TypeScript

import Fastify from "fastify";
import { describe, expect, it } from "vitest";
import { DesktopReleases, newer, registerDesktopRoutes, signedVersion } from "../src/desktop/updates.js";
const API = "https://git.example/api/v1/repos/o/r";
function fakeGitea(releases: unknown[], files: Record<string, unknown>) {
const calls: { url: string; auth: string | null }[] = [];
const impl: typeof fetch = async (input, init) => {
const url = String(input);
calls.push({ url, auth: new Headers(init?.headers).get("authorization") });
if (url.startsWith(`${API}/releases`)) return Response.json(releases);
const name = url.split("/").pop()!;
if (name in files) return typeof files[name] === "string" ? new Response(files[name] as string) : Response.json(files[name]);
return new Response("missing", { status: 404 });
};
return { impl, calls };
}
const asset = (id: number, name: string) => ({ id, name, browser_download_url: `https://git.example/o/r/releases/download/x/${name}` });
describe("desktop updates", () => {
it("compares versions", () => {
expect(newer("0.3.0", "0.2.9")).toBe(true);
expect(newer("0.10.0", "0.9.0")).toBe(true);
expect(newer("0.2.0", "0.2.0")).toBe(false);
expect(newer("0.2.0", "0.3.0")).toBe(false);
expect(newer("1.0.0", "1.0.0-beta.2")).toBe(true);
});
it("reads the version Tauri embeds in a signature trusted comment", () => {
const sig = Buffer.from("trusted comment: timestamp:1\tfile:PhotoReport.app.tar.gz\tversion:0.3.24\n").toString("base64");
expect(signedVersion(sig)).toBe("0.3.24");
expect(signedVersion("dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==")).toBeNull();
});
it("skips a release whose manifest version disagrees with the signature (re-tagged build)", async () => {
const badSig = Buffer.from("trusted comment: timestamp:1\tfile:PhotoReport.app.tar.gz\tversion:0.3.24\n").toString("base64");
const goodSig = Buffer.from("trusted comment: timestamp:2\tfile:PhotoReport.app.tar.gz\tversion:0.3.25\n").toString("base64");
const releases = [
{
id: 20,
tag_name: "desktop-v0.3.25",
draft: false,
prerelease: false,
body: "",
published_at: null,
assets: [asset(1, "update-darwin-universal.json"), asset(2, "PhotoReport.app.tar.gz"), asset(3, "PhotoReport_0.3.24_universal.dmg")],
},
{
id: 19,
tag_name: "desktop-v0.3.24",
draft: false,
prerelease: false,
body: "ok",
published_at: null,
assets: [asset(10, "update-darwin-universal.json"), asset(11, "PhotoReport.app.tar.gz"), asset(12, "PhotoReport_0.3.24_universal.dmg")],
},
];
const files: Record<string, unknown> = {
// First download is the bad 0.3.25 manifest (same filename across releases; fake fetch keys by basename).
"update-darwin-universal.json": { version: "0.3.25", file: "PhotoReport.app.tar.gz", signature: badSig },
"PhotoReport.app.tar.gz": "bytes",
};
const r = new DesktopReleases({ api: API, token: "t" }, fakeGitea(releases, files).impl);
// Bad newest release is skipped; nothing older matches 0.3.25 either with this stub, so null — then swap to good.
expect(await r.updateFor("darwin", "aarch64", "0.3.24")).toBeNull();
files["update-darwin-universal.json"] = { version: "0.3.25", file: "PhotoReport.app.tar.gz", signature: goodSig };
const r2 = new DesktopReleases({ api: API, token: "t" }, fakeGitea(releases, files).impl);
expect(await r2.updateFor("darwin", "aarch64", "0.3.24")).toMatchObject({
version: "0.3.25",
installer: "/v1/desktop/download/20/PhotoReport_0.3.24_universal.dmg",
});
});
it("answers with the newest desktop release for the platform, through the API, and nothing when up to date", async () => {
const releases = [
{ id: 7, tag_name: "desktop-v0.3.0", draft: false, prerelease: false, body: "Catálogo local", published_at: "2026-10-13T09:00:00Z", assets: [asset(1, "update-darwin-universal.json"), asset(2, "PhotoReport_universal.app.tar.gz"), asset(4, "PhotoReport_0.3.0_universal.dmg")] },
{ id: 9, tag_name: "desktop-v0.4.0", draft: true, prerelease: false, body: "", published_at: null, assets: [] },
{ id: 5, tag_name: "v1.2.0", draft: false, prerelease: false, body: "", published_at: null, assets: [] },
{ id: 3, tag_name: "desktop-v0.2.0", draft: false, prerelease: false, body: "", published_at: null, assets: [] },
];
const files = { "update-darwin-universal.json": { version: "0.3.0", file: "PhotoReport_universal.app.tar.gz", signature: "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==" }, "PhotoReport_universal.app.tar.gz": "installer bytes" };
const { impl, calls } = fakeGitea(releases, files);
const r = new DesktopReleases({ api: API, token: "read-token-123" }, impl);
const update = await r.updateFor("darwin", "aarch64", "0.2.0");
expect(update).toMatchObject({ version: "0.3.0", notes: "Catálogo local", url: "/v1/desktop/download/7/PhotoReport_universal.app.tar.gz", installer: "/v1/desktop/download/7/PhotoReport_0.3.0_universal.dmg" });
expect(await r.updateFor("darwin", "x86_64", "0.3.0")).toBeNull();
expect(await r.updateFor("windows", "x86_64", "0.2.0")).toBeNull();
expect(calls.every((c) => c.auth === "token read-token-123")).toBe(true);
expect(calls.filter((c) => c.url.includes("/releases?")).length).toBe(1); // cached
const file = await r.file(7, "PhotoReport_universal.app.tar.gz");
const chunks: Buffer[] = [];
for await (const c of file!.body) chunks.push(Buffer.from(c));
expect(Buffer.concat(chunks).toString()).toBe("installer bytes");
// Only installers of the current release; never the manifests or other releases.
expect(await r.file(3, "PhotoReport_universal.app.tar.gz")).toBeNull();
expect(await r.file(7, "update-darwin-universal.json")).toBeNull();
});
it("keeps offering the last release built for a platform while a newer one is still building", async () => {
const releases = [
// 0.3.5: Linux done, the Mac still building (or switched off).
{ id: 12, tag_name: "desktop-v0.3.5", draft: false, prerelease: false, body: "", published_at: null, assets: [asset(20, "update-linux-x86_64.json"), asset(21, "PhotoReport_0.3.5_amd64.AppImage")] },
{ id: 11, tag_name: "desktop-v0.3.4", draft: false, prerelease: false, body: "Mac", published_at: null, assets: [asset(10, "update-darwin-universal.json"), asset(11, "PhotoReport.app.tar.gz")] },
];
const files = {
"update-linux-x86_64.json": { version: "0.3.5", file: "PhotoReport_0.3.5_amd64.AppImage", signature: "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==" },
"update-darwin-universal.json": { version: "0.3.4", file: "PhotoReport.app.tar.gz", signature: "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==" },
"PhotoReport.app.tar.gz": "mac bytes",
};
const r = new DesktopReleases({ api: API, token: "t" }, fakeGitea(releases, files).impl);
expect(await r.updateFor("linux", "x86_64", "0.3.2")).toMatchObject({ version: "0.3.5", url: "/v1/desktop/download/12/PhotoReport_0.3.5_amd64.AppImage" });
expect(await r.updateFor("darwin", "aarch64", "0.3.2")).toMatchObject({ version: "0.3.4", url: "/v1/desktop/download/11/PhotoReport.app.tar.gz" });
expect(await r.file(11, "PhotoReport.app.tar.gz")).not.toBeNull();
});
it("answers 503 instead of crashing the route when Gitea is unreachable (BIT-31961)", async () => {
const releases = new DesktopReleases({ api: API, token: "t" }, async () => {
throw new TypeError("fetch failed");
});
const app = Fastify({ logger: false });
await registerDesktopRoutes(app, { releases, publicApiUrl: "https://app.example" });
const res = await app.inject({ method: "GET", url: "/v1/desktop/download/7/PhotoReport_universal.app.tar.gz" });
expect(res.statusCode).toBe(503);
expect(res.json()).toEqual({ error: "download_unavailable" });
});
});