diff --git a/scripts/build-frontend.ts b/scripts/build-frontend.ts index d3f670e9..4c891059 100644 --- a/scripts/build-frontend.ts +++ b/scripts/build-frontend.ts @@ -3,6 +3,7 @@ import { rm } from "node:fs/promises"; import process from "node:process"; import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts"; +import { makeDistFilesPublic } from "./lib/dist-permissions.ts"; /** * ยง17.2.2. Deterministic two-pass build. @@ -17,6 +18,7 @@ import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtim * 5. scan app dist and generate the static asset source * 6. ACTIVE only: Vite Service Worker build (emptyOutDir = false) * 7. generate Release Manifest V2 and the build manifest + * 8. make every dist file readable by other users (13-02) * * Steps 5 and 6 are skipped for `REMOVE_REGISTRATION`, `PURGE_OWNED_RESOURCES` * and `null`: those modes never run an active worker build. @@ -68,3 +70,6 @@ if (buildsActiveWorker) { // 6. release + build manifest run("node", ["scripts/generate-build-manifest.ts"]); + +// 8. publishable permissions: the validated writer creates 0600 files +await makeDistFilesPublic("dist"); diff --git a/scripts/lib/dist-permissions.ts b/scripts/lib/dist-permissions.ts new file mode 100644 index 00000000..003f2951 --- /dev/null +++ b/scripts/lib/dist-permissions.ts @@ -0,0 +1,18 @@ +import { chmod, readdir, stat } from "node:fs/promises"; +import path from "node:path"; + +/** + * 13-02. dist is published as-is to a web server that may run as another OS + * user, so every file in it must be readable by others. The validated JSON + * writer creates files 0600 on purpose (evidence under artifacts/ stays + * private); the build adds read bits to what it publishes instead of loosening + * that writer. + */ +export async function makeDistFilesPublic(directory: string): Promise { + for (const entry of await readdir(directory, { recursive: true, withFileTypes: true })) { + if (!entry.isFile()) continue; + const file = path.join(entry.parentPath, entry.name); + const { mode } = await stat(file); + if ((mode & 0o004) === 0) await chmod(file, (mode & 0o7777) | 0o444); + } +} diff --git a/tests/unit/dist-permissions.test.ts b/tests/unit/dist-permissions.test.ts new file mode 100644 index 00000000..8d01937d --- /dev/null +++ b/tests/unit/dist-permissions.test.ts @@ -0,0 +1,38 @@ +import { chmod, mkdir, mkdtemp, rm, stat, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { afterEach, describe, expect, it } from "vitest"; + +import { makeDistFilesPublic } from "../../scripts/lib/dist-permissions.ts"; + +// 13-02. The validated JSON writer publishes with mode 0600, so dist/config.json +// and dist/release-manifest.json reached the release archive unreadable to a web +// server running as another user, and every boot failed with 403. +const roots: string[] = []; +afterEach(async () => { + await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }))); +}); + +describe("dist file permissions", () => { + it("makes every dist file readable by other users and keeps other bits", async () => { + const root = await mkdtemp(path.join(tmpdir(), "dist-permissions-")); + roots.push(root); + await mkdir(path.join(root, ".vite"), { recursive: true }); + const files = { + "config.json": 0o600, + ".vite/manifest.json": 0o600, + "index.html": 0o664, + } as const; + for (const [file, mode] of Object.entries(files)) { + await writeFile(path.join(root, file), "{}"); + await chmod(path.join(root, file), mode); + } + + await makeDistFilesPublic(root); + + expect((await stat(path.join(root, "config.json"))).mode & 0o777).toBe(0o644); + expect((await stat(path.join(root, ".vite/manifest.json"))).mode & 0o777).toBe(0o644); + expect((await stat(path.join(root, "index.html"))).mode & 0o777).toBe(0o664); + }); +});