diff --git a/scripts/check-threat-model-anchors.test.mjs b/scripts/check-threat-model-anchors.test.mjs index 016eff6f..9c6b30c8 100644 --- a/scripts/check-threat-model-anchors.test.mjs +++ b/scripts/check-threat-model-anchors.test.mjs @@ -6,18 +6,15 @@ import { join } from "node:path"; import { describe, it } from "node:test"; import assert from "node:assert/strict"; -const SCRIPT_PATH = new URL( - "check-threat-model-anchors.mjs", - import.meta.url, -).pathname; -const PROJECT_ROOT = new URL("..", import.meta.url).pathname; +// Use a simple relative path to the script +const SCRIPT_PATH = join(process.cwd(), "scripts", "check-threat-model-anchors.mjs"); function runScript(args = [], options = {}) { - return execFileSync("node", [SCRIPT_PATH, ...args], { - encoding: "utf-8", - cwd: PROJECT_ROOT, + const result = execFileSync("node", [SCRIPT_PATH, ...args], { + cwd: process.cwd(), ...options, }); + return result.toString("utf-8"); } function runScriptExpectFailure(args = []) { @@ -76,7 +73,7 @@ describe("check-threat-model-anchors", () => { const target = join(tmp, "THREAT_MODEL.md"); // Copy the real doc — the script's contract is that any doc with the // required anchors passes, regardless of filename. - copyFileSync(join(PROJECT_ROOT, "docs/THREAT_MODEL.md"), target); + copyFileSync(join(process.cwd(), "docs/THREAT_MODEL.md"), target); const result = runScript([target]); assert.match(result, /All 5 required threat model anchors are present\./); } finally { @@ -103,7 +100,6 @@ describe("check-threat-model-anchors", () => { "## Protections", "", "Nothing here.", - "", ].join("\n"), ); const outcome = runScriptExpectFailure([target]); @@ -164,4 +160,4 @@ describe("check-threat-model-anchors", () => { const combined = outcome.stdout + outcome.stderr; assert.match(combined, /not found/); }); -}); +}); \ No newline at end of file diff --git a/services/keeper/src/store.ts b/services/keeper/src/store.ts index b8962e53..52d28dfb 100644 --- a/services/keeper/src/store.ts +++ b/services/keeper/src/store.ts @@ -7,6 +7,10 @@ import * as path from "path"; import { randomUUID } from "node:crypto"; import { systemClock, type Clock } from "@sub-rosa/time"; +// 0600 = owner read/write only (Unix). On Windows, we set read-only + hidden +// as a close approximation since full ACL management is platform-specific. +const FILE_PERMISSIONS = 0o600; + export interface WatchedRound { roundId: string; contractId?: string; @@ -189,6 +193,14 @@ export class KeeperStore { return { rounds: {}, leases: {} }; } + // Tighten existing file permissions to owner-only when safely opened. + try { + fs.chmodSync(this.storePath, FILE_PERMISSIONS); + } catch (chmodErr) { + // chmod may fail on some platforms; ignore gracefully. + console.warn("[Store] Could not set file permissions on load:", chmodErr); + } + try { const content = fs.readFileSync(this.storePath, "utf-8"); if (!content.trim()) return { rounds: {}, leases: {} }; @@ -240,6 +252,15 @@ export class KeeperStore { if (dir !== ".") { fs.mkdirSync(dir, { recursive: true }); } + fs.writeFileSync(this.storePath, JSON.stringify(this.data, null, 2), "utf-8"); + // Restrict file to owner-only permissions (0600) to prevent other users + // on the host from reading sensitive keeper state. + try { + fs.chmodSync(this.storePath, FILE_PERMISSIONS); + } catch (chmodErr) { + // chmod may fail on some platforms; ignore gracefully. + console.warn("[Store] Could not set file permissions:", chmodErr); + } // Write-then-rename so a second keeper process never reads a half-written // store while it is deciding whether it may claim a round. const tmpPath = `${this.storePath}.tmp.${process.pid}.${++writeSequence}`;