Skip to content
Closed
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
17 changes: 17 additions & 0 deletions .github/workflows/desktop-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,20 @@ jobs:
run: >-
scripts/ci/desktop-release.sh github-release release
"${{ needs.prepare.outputs.tag }}" "${{ needs.prepare.outputs.version }}"
# The China update feed: the signed manifest and each package's parts
# as npm packages, which npmmirror serves. Without NPM_TOKEN or a
# manifest nothing goes to npm; apps in China, finding nothing newer
# there, then take this version from the GitHub release.
- name: Publish the npm update feed
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
VERSION: ${{ needs.prepare.outputs.version }}
shell: bash
run: |
set -euo pipefail
if [ -z "$NPM_TOKEN" ] || [ ! -f release/manifest.json ]; then
echo "::warning::No NPM_TOKEN or no update manifest: $VERSION is not published to npm (docs/desktop-release.md#china-update-feed)."
exit 0
fi
scripts/ci/desktop-release.sh npm-pack release "$VERSION" "$RUNNER_TEMP/npm"
scripts/ci/desktop-release.sh npm-publish "$RUNNER_TEMP/npm" "$VERSION" --publish
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@linger/local-installation": "workspace:*",
"@linger/service-kit": "workspace:*",
"@linger/wire-contracts": "workspace:*",
"yazl": "3.3.1",
"zod": "4.4.3"
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ async function main(): Promise<void> {
? updateInstallFor(process.platform, process.execPath, existsSync)
: undefined,
downloadDirectory: path.join(userData, "updates"),
home,
announce: (state) => {
if (!Notification.isSupported()) return;
const notice = new Notification(updateNotice(state));
Expand Down
36 changes: 36 additions & 0 deletions apps/desktop/src/main/test-support.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdtempSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { gzipSync } from "node:zlib";

import { ConfigStore, type DesktopConfig } from "./config.js";
import type { Logger } from "./log.js";
Expand Down Expand Up @@ -35,3 +36,38 @@ export const stubModule = (name: string, dependsOn: string[] = []): Module => ({
start: async () => {},
stop: async () => {},
});

// One tar member as `npm pack` writes it: a ustar header, the body and the
// padding to the next 512-byte block. `type` "0" is a regular file.
export const tarEntry = (name: string, body: Buffer, type = "0"): Buffer => {
const header = Buffer.alloc(512);
header.write(name, 0, 100, "utf8");
header.write("0000644\0", 100);
header.write("0000000\0", 108);
header.write("0000000\0", 116);
header.write(`${body.length.toString(8).padStart(11, "0")}\0`, 124);
header.write("00000000000\0", 136);
header.write(" ", 148);
header.write(type, 156);
header.write("ustar\0", 257);
header.write("00", 263);
let sum = 0;
for (const byte of header) sum += byte;
header.write(`${sum.toString(8).padStart(6, "0")}\0 `, 148);
return Buffer.concat([
header,
body,
Buffer.alloc((512 - (body.length % 512)) % 512),
]);
};

/** A gzipped tarball of `members` (name, body[, type]) and its end blocks. */
export const npmTarball = (
members: readonly (readonly [string, Buffer, string?])[],
): Buffer =>
gzipSync(
Buffer.concat([
...members.map(([name, body, type]) => tarEntry(name, body, type)),
Buffer.alloc(1024),
]),
);
34 changes: 34 additions & 0 deletions apps/desktop/src/main/update-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ describe("update manifest", () => {
);
});

it("reads the npm parts of a package", () => {
const { privateKey, publicPem } = keys();
const parts = [
{ sha256: "b".repeat(64), size: 6 },
{ sha256: "c".repeat(64), size: 4 },
];
const withParts = structuredClone(manifest);
Object.assign(withParts.packages["darwin-arm64"], { parts });
const { bytes, signature } = signed(withParts, privateKey);
expect(
packageFor(verifyManifest(bytes, signature, publicPem), "darwin", "arm64")
?.parts,
).toEqual(parts);

const bad = structuredClone(manifest);
Object.assign(bad.packages["darwin-arm64"], {
parts: [{ sha256: "nope", size: 6 }],
});
const refused = signed(bad, privateKey);
expect(() =>
verifyManifest(refused.bytes, refused.signature, publicPem),
).toThrow("Update manifest is invalid");
});

// Apps read manifests of releases newer than themselves.
it("ignores keys it does not know", () => {
const { privateKey, publicPem } = keys();
const newer = structuredClone(manifest);
Object.assign(newer, { channel: "stable" });
Object.assign(newer.packages["darwin-arm64"], { mirrors: ["x"] });
const { bytes, signature } = signed(newer, privateKey);
expect(verifyManifest(bytes, signature, publicPem)).toEqual(manifest);
});

it("compares versions numerically", () => {
expect(compareVersions("0.16.10", "0.16.9")).toBeGreaterThan(0);
expect(compareVersions("0.16.0", "0.16.0")).toBe(0);
Expand Down
23 changes: 22 additions & 1 deletion apps/desktop/src/main/update-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,35 @@ MCowBQYDK2VwAyEA985QJ8UAvzK0ZLPW6YPaIkiCS2A+qPbEaZvi0aAccR4=

const VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z][0-9A-Za-z.-]*)?$/;

const SHA256 = /^[0-9a-f]{64}$/;

// Not strict: an app ignores keys a newer release adds, as apps from before
// `parts` ignore that one.
const updatePackage = z.object({
// A bare file name: the package always comes from the manifest's release.
file: z.string().regex(/^[A-Za-z0-9._-]+$/),
sha256: z.string().regex(/^[0-9a-f]{64}$/),
sha256: z.string().regex(SHA256),
size: z.number().int().positive(),
// The package cut into consecutive pieces, each its own npm package
// (update-npm.ts), so the China mirror can serve it. The pieces join into
// the whole file above.
parts: z
.array(
z.object({
sha256: z.string().regex(SHA256),
size: z.number().int().positive(),
}),
)
.min(1)
.optional(),
});
export type UpdatePackage = z.infer<typeof updatePackage>;

/** Whether `value` is a version a manifest may name. */
export function isReleaseVersion(value: unknown): value is string {
return typeof value === "string" && VERSION.test(value);
}

const updateManifest = z.object({
schema: z.literal(2),
version: z.string().regex(VERSION),
Expand Down
154 changes: 154 additions & 0 deletions apps/desktop/src/main/update-npm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { gzipSync } from "node:zlib";

import { describe, expect, it } from "vitest";

import { npmTarball, tarEntry } from "./test-support.js";
import {
NPM_PART_MEMBER,
NpmTarError,
distTagsUrl,
extractNpmTarball,
npmTarballUrl,
partNpmPackage,
} from "./update-npm.js";

const PACKAGE_JSON = Buffer.from('{"name":"x","version":"1.0.0"}');

async function* chunked(bytes: Buffer, size = 7): AsyncGenerator<Buffer> {
for (let i = 0; i < bytes.length; i += size)
yield bytes.subarray(i, i + size);
}

const extract = async (
tarball: Buffer,
wanted: Record<string, number> = { [NPM_PART_MEMBER]: 4096 },
chunk = 7,
) => {
const files: Record<string, Buffer[]> = {};
await extractNpmTarball(chunked(tarball, chunk), wanted, (member, chunk) => {
(files[member] ??= []).push(Buffer.from(chunk));
});
return Object.fromEntries(
Object.entries(files).map(([name, chunks]) => [
name,
Buffer.concat(chunks).toString(),
]),
);
};

describe("npm update packages", () => {
it("names the packages and where each registry serves them", () => {
expect(partNpmPackage("darwin-arm64", 0)).toBe(
"@simplexai/linger-desktop-darwin-arm64-part1",
);
const name = "@simplexai/linger-desktop-update";
expect(
distTagsUrl("https://registry.npmmirror.com/", name).toString(),
).toBe(
"https://registry.npmmirror.com/-/package/@simplexai/linger-desktop-update/dist-tags",
);
expect(
npmTarballUrl(
"https://registry.npmmirror.com/",
name,
"1.2.3",
).toString(),
).toBe(
"https://cdn.npmmirror.com/packages/@simplexai/linger-desktop-update/1.2.3/linger-desktop-update-1.2.3.tgz",
);
expect(
npmTarballUrl("https://registry.npmjs.org/", name, "1.2.3").toString(),
).toBe(
"https://registry.npmjs.org/@simplexai/linger-desktop-update/-/linger-desktop-update-1.2.3.tgz",
);
});
});

describe("extractNpmTarball", () => {
it("hands over the wanted member and reads past package.json", async () => {
const body = Buffer.alloc(1300, "p");
const files = await extract(
npmTarball([
["package/package.json", PACKAGE_JSON],
[NPM_PART_MEMBER, body],
]),
);
expect(files).toEqual({ [NPM_PART_MEMBER]: body.toString() });
});

// Bytes after the end blocks, however many they inflate to, are not read.
it("stops at the end of the archive", async () => {
const tar = Buffer.concat([
tarEntry(NPM_PART_MEMBER, Buffer.from("part")),
Buffer.alloc(1024),
tarEntry("package/evil", Buffer.from("never read")),
Buffer.alloc(64 * 1024 * 1024, 1),
]);
expect(await extract(gzipSync(tar), undefined, 64 * 1024)).toEqual({
[NPM_PART_MEMBER]: "part",
});
});

it.each([
["another file", [["package/other", Buffer.from("x")]]],
["a path outside package/", [["part", Buffer.from("x")]]],
["a traversal", [["package/../part", Buffer.from("x")]]],
["a directory", [[NPM_PART_MEMBER, Buffer.alloc(0), "5"]]],
["a symlink", [[NPM_PART_MEMBER, Buffer.alloc(0), "2"]]],
["a pax header", [[NPM_PART_MEMBER, Buffer.from("x"), "x"]]],
] as const)("rejects %s", async (_label, members) => {
await expect(
extract(npmTarball([...members, [NPM_PART_MEMBER, Buffer.from("p")]])),
).rejects.toThrow(NpmTarError);
});

it("rejects a member over its size before reading it", async () => {
const written: Buffer[] = [];
await expect(
extractNpmTarball(
chunked(npmTarball([[NPM_PART_MEMBER, Buffer.alloc(5000)]])),
{ [NPM_PART_MEMBER]: 4096 },
(_member, chunk) => {
written.push(chunk);
},
),
).rejects.toThrow("larger than expected");
expect(written).toEqual([]);
});

it("rejects an oversized package.json", async () => {
await expect(
extract(
npmTarball([
["package/package.json", Buffer.alloc(70 * 1024)],
[NPM_PART_MEMBER, Buffer.from("p")],
]),
),
).rejects.toThrow(NpmTarError);
});

it("rejects a repeated, missing or damaged member", async () => {
await expect(
extract(
npmTarball([
[NPM_PART_MEMBER, Buffer.from("a")],
[NPM_PART_MEMBER, Buffer.from("b")],
]),
),
).rejects.toThrow("repeats");
await expect(
extract(npmTarball([["package/package.json", PACKAGE_JSON]])),
).rejects.toThrow("missing");
const entry = tarEntry(NPM_PART_MEMBER, Buffer.from("p"));
entry[0] = "q".charCodeAt(0);
await expect(
extract(gzipSync(Buffer.concat([entry, Buffer.alloc(1024)]))),
).rejects.toThrow("malformed");
const cut = tarEntry(NPM_PART_MEMBER, Buffer.alloc(600)).subarray(0, 700);
await expect(extract(gzipSync(cut))).rejects.toThrow("ends early");
});

it("rejects bytes that are not gzip", async () => {
await expect(extract(Buffer.from("not gzip at all"))).rejects.toThrow();
});
});
Loading
Loading