Skip to content
Open
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
18 changes: 7 additions & 11 deletions scripts/check-threat-model-anchors.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []) {
Expand Down Expand Up @@ -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 {
Expand All @@ -103,7 +100,6 @@ describe("check-threat-model-anchors", () => {
"## Protections",
"",
"Nothing here.",
"",
].join("\n"),
);
const outcome = runScriptExpectFailure([target]);
Expand Down Expand Up @@ -164,4 +160,4 @@ describe("check-threat-model-anchors", () => {
const combined = outcome.stdout + outcome.stderr;
assert.match(combined, /not found/);
});
});
});
21 changes: 21 additions & 0 deletions services/keeper/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: {} };
Expand Down Expand Up @@ -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}`;
Expand Down
Loading