fix(build): make every dist file readable by other users (13-02)

The validated JSON writer creates files 0600, so dist/config.json,
release-manifest.json and runtime-config.schema.json shipped unreadable to a
web server running as another user. The build now adds read bits to dist files;
evidence under artifacts/ stays private.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:52:33 +09:00
co-authored by Claude Opus 5.5
parent b0f7944b41
commit 1a1cfe6605
3 changed files with 61 additions and 0 deletions
+5
View File
@@ -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");
+18
View File
@@ -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<void> {
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);
}
}
+38
View File
@@ -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);
});
});