87 lines
3.4 KiB
JavaScript
87 lines
3.4 KiB
JavaScript
"use strict";
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const crypto = require("crypto");
|
|
const { atomicWriteFileSync } = require("../utils/atomic-file");
|
|
const { createCrossProcessOperationMutex } = require("../utils/cross-process-mutex");
|
|
|
|
function policyWindowMs(policy) {
|
|
const seconds = parseInt(policy?.window_seconds, 10);
|
|
return (Number.isFinite(seconds) && seconds > 0 ? seconds : 120) * 1000;
|
|
}
|
|
|
|
function policyRevision(policy) {
|
|
const revision = policy?.rate_limit_revision;
|
|
if (typeof revision === "string" && /^[a-f0-9]{32}$/.test(revision) && Number(policy.rate_limit_revision_window_ms) === policyWindowMs(policy)) return revision;
|
|
return null;
|
|
}
|
|
|
|
// adpanel:security-policy-commit - disk commit precedes publication; a shared
|
|
// window revision survives worker restarts and missed intermediate edits.
|
|
function createSecurityPolicyStore({ filePath, defaults, parse = JSON.parse, write = atomicWriteFileSync, mutex, publish = () => {} }) {
|
|
const lock = mutex || createCrossProcessOperationMutex({ label: "[security-policy]", lockFilePath: `${filePath}.lock` });
|
|
const baseline = { ...defaults };
|
|
delete baseline.rate_limit_revision;
|
|
delete baseline.rate_limit_revision_window_ms;
|
|
let snapshot = { ...baseline };
|
|
const read = () => {
|
|
const parsed = parse(fs.readFileSync(filePath, "utf8"));
|
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("Invalid security policy object");
|
|
return { ...baseline, ...parsed };
|
|
};
|
|
const reconcile = (policy) => {
|
|
if (policyRevision(policy)) return policy;
|
|
return { ...policy, rate_limit_revision: crypto.randomBytes(16).toString("hex"), rate_limit_revision_window_ms: policyWindowMs(policy) };
|
|
};
|
|
const persist = (policy) => {
|
|
const result = write(filePath, JSON.stringify(policy, null, 2), { mode: 0o600 });
|
|
if (result === false) throw new Error("Security policy persistence failed");
|
|
snapshot = policy;
|
|
publish(policy);
|
|
return policy;
|
|
};
|
|
return {
|
|
get: () => snapshot,
|
|
async initialize() {
|
|
return lock.runExclusive(() => {
|
|
const policy = fs.existsSync(filePath) ? read() : { ...baseline };
|
|
const reconciled = reconcile(policy);
|
|
if (reconciled !== policy || !fs.existsSync(filePath)) return persist(reconciled);
|
|
snapshot = reconciled;
|
|
publish(snapshot);
|
|
return snapshot;
|
|
});
|
|
},
|
|
async reload() {
|
|
return lock.runExclusive(() => {
|
|
const policy = read();
|
|
const reconciled = reconcile(policy);
|
|
if (reconciled !== policy) return persist(reconciled);
|
|
snapshot = policy;
|
|
publish(policy);
|
|
return policy;
|
|
});
|
|
},
|
|
async save(changes) {
|
|
return lock.runExclusive(() => {
|
|
const previous = read();
|
|
const candidate = { ...previous, ...changes };
|
|
if (policyWindowMs(candidate) !== policyWindowMs(previous)) {
|
|
delete candidate.rate_limit_revision;
|
|
}
|
|
return persist(reconcile(candidate));
|
|
});
|
|
},
|
|
watch(logger = console) {
|
|
const watcher = fs.watch(path.dirname(filePath), (_event, name) => {
|
|
if (name && String(name) !== path.basename(filePath)) return;
|
|
this.reload().catch((error) => logger.warn("[security-policy] Reload failed; retaining last committed policy:", error.message));
|
|
});
|
|
watcher.unref();
|
|
return watcher;
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = { createSecurityPolicyStore, policyWindowMs, policyRevision }; |