Files
openclaw/src/plugin-state/plugin-state-store.permissions.test.ts
Alex Knight bbf985d50a feat(plugins): add SQLite plugin state store (#74190)
* feat(plugins): add experimental sqlite plugin state store
2026-04-29 23:02:14 +10:00

57 lines
1.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
afterEach(() => {
vi.doUnmock("node:fs");
vi.resetModules();
});
describe("plugin state permission hardening", () => {
it("does not reject a committed write when post-commit chmod fails", async () => {
let chmodCalls = 0;
let throwAfter = Number.POSITIVE_INFINITY;
vi.doMock("node:fs", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs")>();
return {
...actual,
chmodSync: (target: Parameters<typeof actual.chmodSync>[0], mode: number) => {
chmodCalls += 1;
if (chmodCalls > throwAfter) {
throw Object.assign(new Error("chmod denied"), { code: "EACCES" });
}
return actual.chmodSync(target, mode);
},
existsSync: (target: Parameters<typeof actual.existsSync>[0]) => {
const pathname = String(target);
if (pathname.endsWith("-shm") || pathname.endsWith("-wal")) {
return false;
}
return actual.existsSync(target);
},
};
});
const { createPluginStateKeyedStore, resetPluginStateStoreForTests } =
await import("./plugin-state-store.js");
try {
await withOpenClawTestState({ label: "plugin-state-post-commit-chmod" }, async () => {
const store = createPluginStateKeyedStore<{ value: number }>("fixture-plugin", {
namespace: "post-commit",
maxEntries: 10,
});
await store.register("first", { value: 1 });
chmodCalls = 0;
throwAfter = 2;
await expect(store.register("second", { value: 2 })).resolves.toBeUndefined();
await expect(store.lookup("second")).resolves.toEqual({ value: 2 });
});
} finally {
resetPluginStateStoreForTests();
}
});
});