Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,71 @@
|
||||
"use strict";
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { createSecurityPolicyStore } = require("../services/security-policy");
|
||||
const { atomicWriteFileSync } = require("../utils/atomic-file");
|
||||
|
||||
test("failed security saves do not publish changes or alter the file", async (context) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "security-policy-"));
|
||||
context.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const filePath = path.join(dir, "security.json");
|
||||
const defaults = { rate_limiting: true, limit: 300, window_seconds: 120 };
|
||||
const initial = createSecurityPolicyStore({ filePath, defaults });
|
||||
await initial.initialize();
|
||||
const before = fs.readFileSync(filePath, "utf8");
|
||||
const publications = [];
|
||||
const store = createSecurityPolicyStore({ filePath, defaults, publish: (policy) => publications.push(policy), write: () => false });
|
||||
await store.initialize();
|
||||
await assert.rejects(store.save({ rate_limiting: false, window_seconds: 600 }), /persistence failed/);
|
||||
assert.equal(store.get().rate_limiting, true);
|
||||
assert.equal(publications.length, 1);
|
||||
assert.equal(fs.readFileSync(filePath, "utf8"), before);
|
||||
});
|
||||
|
||||
test("workers converge on persisted revisions after restart and manual window edits", async (context) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "security-policy-"));
|
||||
context.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const filePath = path.join(dir, "security.json");
|
||||
const defaults = { limit: 2, window_seconds: 120 };
|
||||
const first = createSecurityPolicyStore({ filePath, defaults });
|
||||
const second = createSecurityPolicyStore({ filePath, defaults });
|
||||
await Promise.all([first.initialize(), second.initialize()]);
|
||||
assert.equal(first.get().rate_limit_revision, second.get().rate_limit_revision);
|
||||
const oldRevision = first.get().rate_limit_revision;
|
||||
await first.save({ window_seconds: 600 });
|
||||
await first.save({ window_seconds: 120 });
|
||||
await second.reload();
|
||||
assert.notEqual(second.get().rate_limit_revision, oldRevision);
|
||||
assert.equal(first.get().rate_limit_revision, second.get().rate_limit_revision);
|
||||
atomicWriteFileSync(filePath, JSON.stringify({ ...second.get(), window_seconds: 900 }));
|
||||
await first.reload();
|
||||
await second.reload();
|
||||
assert.equal(first.get().rate_limit_revision, second.get().rate_limit_revision);
|
||||
assert.equal(first.get().rate_limit_revision_window_ms, 900000);
|
||||
});
|
||||
|
||||
test("directory watcher adopts successive atomic replacements without losing its inode", async (context) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "security-policy-watch-"));
|
||||
context.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const filePath = path.join(dir, "security.json");
|
||||
const store = createSecurityPolicyStore({ filePath, defaults: { window_seconds: 120 } });
|
||||
await store.initialize();
|
||||
const watcher = store.watch();
|
||||
context.after(() => watcher.close());
|
||||
const adopt = (seconds) => new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => { clearInterval(check); reject(new Error("policy watcher did not adopt replacement")); }, 2000);
|
||||
const check = setInterval(() => {
|
||||
if (store.get().window_seconds !== seconds || store.get().rate_limit_revision_window_ms !== seconds * 1000) return;
|
||||
clearInterval(check); clearTimeout(timeout); resolve();
|
||||
}, 10);
|
||||
atomicWriteFileSync(filePath, JSON.stringify({ ...store.get(), window_seconds: seconds }));
|
||||
});
|
||||
const firstRevision = store.get().rate_limit_revision;
|
||||
await adopt(600);
|
||||
assert.notEqual(store.get().rate_limit_revision, firstRevision);
|
||||
await adopt(120);
|
||||
assert.notEqual(store.get().rate_limit_revision, firstRevision);
|
||||
});
|
||||
Reference in new issue
Block a user