|
@@ -1,6 +1,7 @@
|
|
|
import fs from "node:fs";
|
|
import fs from "node:fs";
|
|
|
import os from "node:os";
|
|
import os from "node:os";
|
|
|
import { join } from "node:path";
|
|
import { join } from "node:path";
|
|
|
|
|
+import { spawnSync } from "node:child_process";
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
import {
|
|
import {
|
|
|
getConfig,
|
|
getConfig,
|
|
@@ -130,6 +131,51 @@ describe.skipIf(process.platform === "win32")(
|
|
|
expect(fs.readFileSync(configPath, "utf8")).toBe(document);
|
|
expect(fs.readFileSync(configPath, "utf8")).toBe(document);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ it.each([0o400, 0o600])("loads an already-private %i config without chmod", (mode) => {
|
|
|
|
|
+ fs.writeFileSync(configPath, document, { mode });
|
|
|
|
|
+ const chmod = vi.spyOn(fs, "chmodSync").mockImplementation(() => {
|
|
|
|
|
+ throw new Error("synthetic read-only filesystem");
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(() => getConfig()).not.toThrow();
|
|
|
|
|
+ expect(chmod).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(fs.statSync(configPath).mode & 0o777).toBe(mode);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("removes non-owner access without granting owner write permission", () => {
|
|
|
|
|
+ fs.writeFileSync(configPath, document);
|
|
|
|
|
+ fs.chmodSync(configPath, 0o440);
|
|
|
|
|
+ getConfig();
|
|
|
|
|
+ expect(fs.statSync(configPath).mode & 0o777).toBe(0o400);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("still rejects loading an unsafe main config when chmod fails", () => {
|
|
|
|
|
+ fs.writeFileSync(configPath, document);
|
|
|
|
|
+ fs.chmodSync(configPath, 0o644);
|
|
|
|
|
+ vi.spyOn(fs, "chmodSync").mockImplementation(() => {
|
|
|
|
|
+ throw new Error("synthetic chmod failure");
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(() => getConfig()).toThrow("synthetic chmod failure");
|
|
|
|
|
+ expect(fs.readFileSync(configPath, "utf8")).toBe(document);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it.skipIf(process.platform !== "darwin")("loads immutable private config and backup files", () => {
|
|
|
|
|
+ const backup = join(dataDir, "config.yaml.bak.byok-legacy-provider.immutable");
|
|
|
|
|
+ const files = [configPath, backup];
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (const file of files) {
|
|
|
|
|
+ fs.writeFileSync(file, document, { mode: 0o400 });
|
|
|
|
|
+ const result = spawnSync("chflags", ["uchg", file], { encoding: "utf8" });
|
|
|
|
|
+ expect(result.status, result.stderr).toBe(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
|
|
|
+ expect(() => getConfig()).not.toThrow();
|
|
|
|
|
+ expect(warn).not.toHaveBeenCalled();
|
|
|
|
|
+ for (const file of files) expect(fs.statSync(file).mode & 0o777).toBe(0o400);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ for (const file of files) spawnSync("chflags", ["nouchg", file]);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
it("creates managed defaults privately", () => {
|
|
it("creates managed defaults privately", () => {
|
|
|
vi.stubEnv("__MAVIS_RUNTIME_MANAGED", "1");
|
|
vi.stubEnv("__MAVIS_RUNTIME_MANAGED", "1");
|
|
|
getConfig();
|
|
getConfig();
|
|
@@ -157,6 +203,41 @@ describe.skipIf(process.platform === "win32")(
|
|
|
expect(fs.readFileSync(backup, "utf8")).toBe(document);
|
|
expect(fs.readFileSync(backup, "utf8")).toBe(document);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ it("warns on backup repair failure, loads the config and repairs remaining backups", () => {
|
|
|
|
|
+ fs.writeFileSync(configPath, document, { mode: 0o600 });
|
|
|
|
|
+ const failed = join(dataDir, "config.yaml.bak.byok-legacy-provider.1");
|
|
|
|
|
+ const repaired = join(dataDir, "config.yaml.bak.byok-legacy-provider.2");
|
|
|
|
|
+ for (const backup of [failed, repaired]) {
|
|
|
|
|
+ fs.writeFileSync(backup, document);
|
|
|
|
|
+ fs.chmodSync(backup, 0o644);
|
|
|
|
|
+ }
|
|
|
|
|
+ const chmod = fs.chmodSync;
|
|
|
|
|
+ vi.spyOn(fs, "chmodSync").mockImplementation((file, mode) => {
|
|
|
|
|
+ if (file === failed) throw new Error(secret);
|
|
|
|
|
+ chmod(file, mode);
|
|
|
|
|
+ });
|
|
|
|
|
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
|
|
|
+ expect(() => getConfig()).not.toThrow();
|
|
|
|
|
+ expect(fs.statSync(failed).mode & 0o777).toBe(0o644);
|
|
|
|
|
+ expect(fs.statSync(repaired).mode & 0o777).toBe(0o600);
|
|
|
|
|
+ expect(warn).toHaveBeenCalledTimes(1);
|
|
|
|
|
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining(JSON.stringify(failed)));
|
|
|
|
|
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("restrict its permissions manually"));
|
|
|
|
|
+ expect(JSON.stringify(warn.mock.calls)).not.toContain(secret);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("warns when backup inspection fails without blocking the main config", () => {
|
|
|
|
|
+ fs.writeFileSync(configPath, document, { mode: 0o600 });
|
|
|
|
|
+ vi.spyOn(fs, "readdirSync").mockImplementation(() => {
|
|
|
|
|
+ throw new Error(secret);
|
|
|
|
|
+ });
|
|
|
|
|
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
|
|
|
+ expect(() => getConfig()).not.toThrow();
|
|
|
|
|
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining(JSON.stringify(dataDir)));
|
|
|
|
|
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("Backup permissions were not verified"));
|
|
|
|
|
+ expect(JSON.stringify(warn.mock.calls)).not.toContain(secret);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
it("keeps a symlinked status-line config and its credentials private", async () => {
|
|
it("keeps a symlinked status-line config and its credentials private", async () => {
|
|
|
const target = join(root, "linked.yaml");
|
|
const target = join(root, "linked.yaml");
|
|
|
fs.writeFileSync(target, document);
|
|
fs.writeFileSync(target, document);
|