Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions .github/scripts/latest-json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { describe, expect, test } from "bun:test";
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { build, installerName, type Inputs } from "./latest-json";

// A real minisign signature made with `tauri signer sign --app-version 0.4.0` and a throwaway key, over PAYLOAD.
const SIGNATURE =
"dW50cnVzdGVkIGNvbW1lbnQ6IHNpZ25hdHVyZSBmcm9tIHRhdXJpIHNlY3JldCBrZXkKUlVRbVl0WTluQkE2MHVvZGtISU5WQXQrS2QwbUZiSWwwRElGc0dWTVE4OTF6RHVUdm9JNTlRV3hnallFY2ZRWUVhT1BuVjlZUElpdVdFT29zWncxN2RZOEZyTXJWMGpHbmdVPQp0cnVzdGVkIGNvbW1lbnQ6IHRpbWVzdGFtcDoxNzk1NDcwMDcwCWZpbGU6cGF5bG9hZC5iaW4JdmVyc2lvbjowLjQuMApDWUVjbVFHRHhtV3cvbUoxMVNNc1ArRmZic0JWcCs2dDQxcE1ZRVR3RWR1UEtYSFY0eE1UOEVoQ0hxcHRwc3A2YkVFU1NyMXhSaTFoVnArQTRsTnJEZz09Cg==";

// The bytes that were signed, and the public key (the .pub file, base64) of the throwaway key that signed them.
const PAYLOAD = Buffer.from("Not a real installer: bytes for the updater signature test.");
const PUBKEY =
"dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEQyM0ExMDlDM0RENjYyMjYKUldRbVl0WTluQkE2MHBHSWZiK0lwY3gzRVpkQVVPNzkyU3ZKMXBvV0dUY0QwSDFoQUVOV1V2S1oK";
// The public key of a second throwaway key, as if the owner had run `signer generate` twice.
const OTHER_PUBKEY =
"dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDk1RjZBNUFCRERCNjAxRDIKUldUU0FiYmRxNlgybFNnS3VldWU0NXhhRmxZQ1Rodjk3K1JQVytBdm1HcEVkN0ZRcW5CMXd4K2gK";

const ok: Inputs = {
version: "0.4.0",
installer: installerName("0.4.0"),
installerBytes: PAYLOAD,
signature: SIGNATURE,
pubkey: PUBKEY,
repo: "Page011/LocalTugboat",
date: "2026-11-24T10:00:00Z",
};

const b64 = (s: string) => Buffer.from(s, "utf8").toString("base64");

describe("latest.json", () => {
test("has the shape the updater reads, with the signature of that exact file and the download URL of the release", () => {
const m = JSON.parse(build(ok));
expect(m).toEqual({
version: "0.4.0",
notes: "Local Tugboat 0.4.0 - https://github.com/Page011/LocalTugboat/releases/tag/v0.4.0",
pub_date: "2026-11-24T10:00:00Z",
platforms: {
"windows-x86_64": {
signature: SIGNATURE,
url: "https://github.com/Page011/LocalTugboat/releases/download/v0.4.0/LocalTugboat_0.4.0_x64-setup.exe",
},
},
});
expect(m.notes).not.toContain("\n");
});

test("a .sig file's trailing newline is dropped, never written into the signature", () => {
expect(JSON.parse(build({ ...ok, signature: `${SIGNATURE}\r\n` })).platforms["windows-x86_64"].signature).toBe(SIGNATURE);
});

test("a signature made for another version is refused", () => {
expect(() => build({ ...ok, version: "0.4.1", installer: installerName("0.4.1") })).toThrow("made for version 0.4.0, not 0.4.1");
});

test("a signature that does not name its version, or is not a signature, is refused", () => {
const unversioned = b64("untrusted comment: signature from tauri secret key\nRUQabc\ntrusted comment: timestamp:1\tfile:x\nabc\n");
expect(() => build({ ...ok, signature: unversioned })).toThrow("does not say which version");
expect(() => build({ ...ok, signature: b64("hello") })).toThrow("not a minisign signature");
expect(() => build({ ...ok, signature: "" })).toThrow("empty");
expect(() => build({ ...ok, signature: "two\nlines" })).toThrow("one line");
});

test("a signature made with another key than plugins.updater.pubkey is refused", () => {
expect(() => build({ ...ok, pubkey: OTHER_PUBKEY })).toThrow("different key than plugins.updater.pubkey");
});

test("a signature that does not cover the installer bytes is refused", () => {
expect(() => build({ ...ok, installerBytes: Buffer.from("Not a real installer: other bytes.") })).toThrow("does not match the installer");
expect(() => build({ ...ok, installerBytes: Buffer.alloc(0) })).toThrow("does not match the installer");
});

test("a signature with the right key id but forged bytes is refused", () => {
const lines = Buffer.from(SIGNATURE, "base64").toString("utf8").split("\n");
const sig = Buffer.from(lines[1]!, "base64");
sig[40] = sig[40]! ^ 1;
lines[1] = sig.toString("base64");
expect(() => build({ ...ok, signature: b64(lines.join("\n")) })).toThrow("does not match the installer");
});

test("an altered trusted comment is refused (the signed comment carries the version)", () => {
const text = Buffer.from(SIGNATURE, "base64").toString("utf8").replace("file:payload.bin", "file:payload.exe");
expect(() => build({ ...ok, signature: b64(text) })).toThrow("trusted comment was altered");
});

test("a public key that is not a minisign public key is refused", () => {
expect(() => build({ ...ok, pubkey: "UPDATER_PUBKEY_MISSING" })).toThrow("public key");
expect(() => build({ ...ok, pubkey: b64("untrusted comment: x\nnot-a-key\n") })).toThrow("public key");
expect(() => build({ ...ok, pubkey: "" })).toThrow("public key");
});

test("the installer name, version, repository and date are checked", () => {
expect(() => build({ ...ok, installer: "Local Tugboat_0.4.0_x64-setup.exe" })).toThrow("must be named LocalTugboat_0.4.0_x64-setup.exe");
expect(() => build({ ...ok, version: "v0.4.0" })).toThrow("bad version");
expect(() => build({ ...ok, version: "null" })).toThrow("bad version");
expect(() => build({ ...ok, repo: "Page011" })).toThrow("bad repository");
expect(() => build({ ...ok, date: "yesterday" })).toThrow("bad date");
expect(() => build({ ...ok, date: "2026-13-45T10:00:00Z" })).toThrow("bad date");
});

test("the script runs on files: writes the manifest, and on bad input writes nothing and exits 1", () => {
const dir = mkdtempSync(join(tmpdir(), "latest-json-"));
try {
const sig = join(dir, "setup.exe.sig");
writeFileSync(sig, `${SIGNATURE}\n`);
const exe = join(dir, installerName("0.4.0"));
writeFileSync(exe, PAYLOAD);
const script = join(import.meta.dir, "latest-json.ts");
const run = (extra: string[], out: string) =>
Bun.spawnSync([process.execPath, script, ...extra, "--version", "0.4.0", "--installer", installerName("0.4.0"), "--sig", sig, "--installer-file", exe, "--pubkey", PUBKEY, "--repo", "Page011/LocalTugboat", "--out", out]);

const out = join(dir, "latest.json");
const good = run(["--date", "2026-11-24T10:00:00Z"], out);
expect(good.exitCode).toBe(0);
expect(JSON.parse(readFileSync(out, "utf8")).version).toBe("0.4.0");

const withDefaultDate = join(dir, "dated.json");
expect(run([], withDefaultDate).exitCode).toBe(0);
expect(JSON.parse(readFileSync(withDefaultDate, "utf8")).pub_date).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/);

// A key from another `signer generate`: the script fails and writes nothing.
const mismatched = join(dir, "mismatched.json");
const wrongKey = run(["--pubkey", OTHER_PUBKEY], mismatched);
expect(wrongKey.exitCode).toBe(1);
expect(wrongKey.stderr.toString()).toContain("different key than plugins.updater.pubkey");
expect(existsSync(mismatched)).toBe(false);

const bad = join(dir, "bad.json");
const failed = run(["--date", "never"], bad);
expect(failed.exitCode).toBe(1);
expect(failed.stderr.toString()).toContain("bad date");
expect(existsSync(bad)).toBe(false);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
139 changes: 139 additions & 0 deletions .github/scripts/latest-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Writes latest.json, the file the desktop app's update check reads from the latest GitHub release.
//
// bun .github/scripts/latest-json.ts --version 0.3.1 --installer LocalTugboat_0.3.1_x64-setup.exe \
// --installer-file out/LocalTugboat_0.3.1_x64-setup.exe --pubkey "<plugins.updater.pubkey>" \
// --sig "Local Tugboat_0.3.1_x64-setup.exe.sig" --repo Page011/LocalTugboat --out out/latest.json
//
// --sig is the .sig that `tauri signer sign` wrote for the installer. A signature covers the file's bytes, not its
// name, so the installer can be renamed after the build (the release workflow does) and keep working.
// --installer is the published file name; --installer-file is the local file the signature is checked against.
// --pubkey is plugins.updater.pubkey from tauri.conf.json: the signature must verify against it, because an
// installed app would reject any other and never update. --date is optional (RFC 3339, default now).
// Any problem with the inputs is an error and writes nothing.

import { createHash, createPublicKey, verify } from "node:crypto";
import { readFileSync, writeFileSync } from "node:fs";

const SEMVER = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
const REPO = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
const BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
const TRUSTED_PREFIX = "trusted comment: ";

export interface Inputs {
version: string;
/** File name of the installer as published (no folder). */
installer: string;
/** The installer's bytes: what the signature must verify against. */
installerBytes: Uint8Array;
/** Contents of the .sig file. */
signature: string;
/** plugins.updater.pubkey: the base64 of the .pub file. */
pubkey: string;
/** owner/name of the GitHub repository. */
repo: string;
/** RFC 3339. */
date: string;
}

export function installerName(version: string): string {
return `LocalTugboat_${version}_x64-setup.exe`;
}

function decode(text: string, what: string): Buffer {
if (!BASE64.test(text)) throw new Error(`${what} is not base64`);
return Buffer.from(text, "base64");
}

/** A minisign key line: 2 algorithm bytes, an 8 byte key id, then the 32 byte Ed25519 public key. */
function parsePublicKey(pubkey: string): { keyId: Buffer; raw: Buffer } {
const lines = decode(pubkey.trim(), "the public key").toString("utf8").split(/\r?\n/);
if (!lines[0]?.startsWith("untrusted comment:") || !lines[1]) throw new Error("plugins.updater.pubkey is not a minisign public key");
const bytes = decode(lines[1].trim(), "the public key line");
if (bytes.length !== 42 || bytes.subarray(0, 2).toString("latin1") !== "Ed") throw new Error("plugins.updater.pubkey is not a minisign public key");
return { keyId: bytes.subarray(2, 10), raw: bytes.subarray(10) };
}

/**
* The signature must be a minisign signature made for exactly this version, with the key in plugins.updater.pubkey,
* over exactly these installer bytes. Minisign "prehashed" form (what tauri writes): Ed25519 over the BLAKE2b-512 of
* the file, then a second Ed25519 signature over the first one plus the trusted comment.
*/
function checkSignature(signature: string, version: string, pubkey: string, installerBytes: Uint8Array): void {
if (!signature) throw new Error("the signature is empty");
if (/\s/.test(signature)) throw new Error("the signature must be one line (the contents of the .sig file)");
if (!BASE64.test(signature)) throw new Error("the signature is not base64");
const lines = Buffer.from(signature, "base64").toString("utf8").split(/\r?\n/);
if (!lines[0]?.startsWith("untrusted comment:")) throw new Error("the signature is not a minisign signature");
const trusted = lines.find((l) => l.startsWith(TRUSTED_PREFIX));
if (!trusted) throw new Error("the signature has no trusted comment");
const signed = trusted.split("\t").find((f) => f.startsWith("version:"))?.slice("version:".length);
if (signed === undefined) throw new Error("the signature does not say which version it was made for (sign with --app-version)");
if (signed !== version) throw new Error(`the signature was made for version ${signed}, not ${version}`);

const key = parsePublicKey(pubkey);
const sig = decode((lines[1] ?? "").trim(), "the signature line");
const global = decode((lines[3] ?? "").trim(), "the trusted comment signature");
if (sig.length !== 74 || sig.subarray(0, 2).toString("latin1") !== "ED" || global.length !== 64) throw new Error("the signature is not a prehashed minisign signature");
if (!sig.subarray(2, 10).equals(key.keyId)) throw new Error("the signature was made with a different key than plugins.updater.pubkey");
const publicKey = createPublicKey({ key: { kty: "OKP", crv: "Ed25519", x: key.raw.toString("base64url") }, format: "jwk" });
const digest = createHash("blake2b512").update(installerBytes).digest();
if (!verify(null, digest, publicKey, sig.subarray(10))) throw new Error("the signature does not match the installer (other bytes, or a different key than plugins.updater.pubkey)");
const signedComment = Buffer.concat([sig.subarray(10), Buffer.from(trusted.slice(TRUSTED_PREFIX.length), "utf8")]);
if (!verify(null, signedComment, publicKey, global)) throw new Error("the signature's trusted comment was altered");
}

export function build(i: Inputs): string {
if (!SEMVER.test(i.version)) throw new Error(`bad version '${i.version}'`);
if (i.installer !== installerName(i.version)) throw new Error(`the installer must be named ${installerName(i.version)}, got '${i.installer}'`);
if (!REPO.test(i.repo)) throw new Error(`bad repository '${i.repo}' (want owner/name)`);
if (!RFC3339.test(i.date) || Number.isNaN(Date.parse(i.date))) throw new Error(`bad date '${i.date}' (want RFC 3339)`);
const signature = i.signature.trim();
checkSignature(signature, i.version, i.pubkey, i.installerBytes);
const manifest = {
version: i.version,
notes: `Local Tugboat ${i.version} - https://github.com/${i.repo}/releases/tag/v${i.version}`,
pub_date: i.date,
platforms: {
"windows-x86_64": {
signature,
url: `https://github.com/${i.repo}/releases/download/v${i.version}/${i.installer}`,
},
},
};
return `${JSON.stringify(manifest, null, 2)}\n`;
}

function arg(args: string[], name: string): string | undefined {
const at = args.indexOf(`--${name}`);
return at >= 0 ? args[at + 1] : undefined;
}

function main(args: string[]): void {
const need = (name: string): string => {
const v = arg(args, name);
if (!v) throw new Error(`missing --${name}`);
return v;
};
const out = need("out");
const json = build({
version: need("version"),
installer: need("installer"),
installerBytes: readFileSync(need("installer-file")),
signature: readFileSync(need("sig"), "utf8"),
pubkey: need("pubkey"),
repo: need("repo"),
date: arg(args, "date") ?? new Date().toISOString().replace(/\.\d{3}Z$/, "Z"),
});
writeFileSync(out, json);
console.log(`wrote ${out}`);
}

if (import.meta.main) {
try {
main(process.argv.slice(2));
} catch (e) {
console.error(`latest.json: ${e instanceof Error ? e.message : String(e)}`);
process.exit(1);
}
}
55 changes: 55 additions & 0 deletions .github/scripts/release-workflow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";

interface Step {
name?: string;
if?: string;
env?: Record<string, string>;
run?: string;
}

const text = readFileSync(join(import.meta.dir, "..", "workflows", "release.yml"), "utf8");
const wf = Bun.YAML.parse(text) as { jobs: Record<string, { steps: Step[] }> };
const steps = wf.jobs.build!.steps;
const step = (name: string): Step => {
const s = steps.find((x) => x.name === name);
if (!s) throw new Error(`no step named ${name}`);
return s;
};
const TAG_ONLY = "startsWith(github.ref, 'refs/tags/v')";

describe("release workflow", () => {
test("the signing key and its password are given to tag runs only, and only to the signing step", () => {
const withSecrets = steps.filter((s) => JSON.stringify(s.env ?? {}).includes("TAURI_SIGNING_PRIVATE_KEY"));
expect(withSecrets.map((s) => s.name)).toEqual(["Update signing is set up", "Sign installer"]);
for (const s of withSecrets) expect(s.if).toBe(TAG_ONLY);
// Nothing else in the build mentions the secrets, not even through a job or workflow level env.
expect(text.match(/secrets\.TAURI_SIGNING_PRIVATE_KEY(_PASSWORD)?/g)).toHaveLength(4);
});

test("the build runs without the key and without making updater artifacts", () => {
const build = step("Build installer");
expect(build.env).toBeUndefined();
expect(build.run).toContain(`--config '{"bundle":{"createUpdaterArtifacts":false}}'`);
});

test("signing and latest.json happen on tag runs only, so other runs upload an unsigned installer and no manifest", () => {
expect(step("Sign installer").if).toBe(TAG_ONLY);
expect(step("Write latest.json").if).toBe(TAG_ONLY);
expect(step("Sign installer").run).toContain("signer sign --app-version");
});

test("latest.json is checked against the configured key and the installer's bytes", () => {
const run = step("Write latest.json").run ?? "";
expect(run).toContain("--pubkey");
expect(run).toContain("plugins.updater.pubkey");
expect(run).toContain("--installer-file");
});

test("a tag with a suffix is published as a pre-release", () => {
const release = wf.jobs.release!.steps.find((s) => s.name === "Create draft release");
expect(release?.run).toContain("*-*) pre=(--prerelease)");
expect(release?.run).toMatch(/gh release create .*\$\{pre\[@\]\+"\$\{pre\[@\]\}"\}/);
});
});
Loading
Loading