Audit the project and secure toolbox data

This commit is contained in:
Shinuwa 2026-10-14 16:59:09 +02:00
parent 96af62edf0
commit e87edda5c7
40 changed files with 2324 additions and 125 deletions

View file

@ -4,6 +4,8 @@ import assert from "node:assert/strict";
import { createPersistenceQueue } from "../website/src/features/toolboxes/storage/persistenceQueue.js";
import { useToolboxActions } from "../website/src/features/toolboxes/useToolboxActions.js";
import { applyModuleDataValue, removeModuleDataKeys } from "../website/src/features/toolboxes/storage/optimisticModuleData.js";
import { DEFERRED_MODULE_DATA } from "../website/src/features/toolboxes/storage/deferredModuleData.js";
import { createStorageMutation } from "../website/src/features/toolboxes/storage/storageMutation.js";
function deferred() {
let resolve, reject;
@ -81,7 +83,15 @@ test("imports merge after file reading and preserve edits queued during the tran
});
const imported = actions.importToolbox({ text: () => fileRead.promise }, "game");
await queue.enqueue((s) => ({ ...s, moduleData: { note: "B" } }), async (next) => { disk = next; });
fileRead.resolve(JSON.stringify({ toolbox: { id: "source", name: "Import", modules: [] }, modules: {} }));
fileRead.resolve(JSON.stringify({
version: 2,
toolbox: {
id: "source", name: "Import", modules: [],
spaces: [{ id: "default", name: "Défaut", moduleOrder: { one: [], two: [[], []] } }],
defaultSpaceId: "default", activeSpaceId: "default"
},
modules: {}
}));
await Promise.resolve();
assert.equal(notices.length, 0);
const edit = queue.enqueue((s) => ({ ...s, moduleData: { ...s.moduleData, note: "C" } }), async (next) => { disk = next; });
@ -124,9 +134,15 @@ test("global imports started from the same render both survive", async () => {
setConfirmModal: (value) => assert.fail(value.message)
});
const file = (id) => ({ text: async () => JSON.stringify({
toolboxes: [{ id, name: id, modules: [{ id: "note", type: "notepad" }] }],
version: 2,
exportedAt: "2026-10-14T12:00:00.000Z",
toolboxes: [{
id, name: id, modules: [{ id: "note", type: "notepad" }],
spaces: [{ id: "default", name: "Défaut", moduleOrder: { one: ["note"], two: [["note"], []] } }],
defaultSpaceId: "default", activeSpaceId: "default"
}],
links: { [id]: id },
modules: { [`${id}:note`]: { text: id } }
modules: { [`${id}:note`]: { pages: [{ id: "page", title: "Page 1", html: `<p>${id}</p>`, text: id }] } }
}) });
assert.deepEqual(await Promise.all([
actions.importAllToolboxes(file("one")), actions.importAllToolboxes(file("two"))
@ -160,7 +176,7 @@ test("v2 imports reject more than five spaces before writing", async () => {
}) });
assert.equal(result, null);
assert.equal(writes, 0);
assert.match(errorMessage, /plus de 5 espaces/);
assert.match(errorMessage, /entre 1 et 5 espaces/);
});
test("failed toolbox deletion preserves registry links and modules together", async () => {
@ -182,3 +198,60 @@ test("failed toolbox deletion preserves registry links and modules together", as
assert.equal(await deleted, false);
assert.deepEqual(queue.getCurrent(), initial);
});
test("storage mutations target independent records and preserve deferred media deletions", () => {
const expected = {
toolboxes: [{ id: "a", modules: [{ id: "media", type: "images" }] }],
links: { game: "a" },
moduleData: { "a:media": DEFERRED_MODULE_DATA, "a:note": { text: "A" } }
};
const next = {
toolboxes: [
{ id: "b", modules: [{ id: "note", type: "notepad" }] },
{ id: "a", modules: [] }
],
links: { game: "a", other: "b" },
moduleData: { "a:note": { text: "A" }, "b:note": { text: "B" } }
};
const mutation = createStorageMutation(expected, next);
assert.deepEqual(mutation.toolboxes.changes.map((change) => change.key).sort(), ["a", "b"]);
assert.deepEqual(mutation.links.map((change) => change.key), ["other"]);
assert.deepEqual(mutation.modules.map((change) => [change.key, change.expected, change.nextExists]), [
["a:media", "any", false],
["b:note", "missing", true]
]);
});
test("the persistence queue adopts the canonical stored state returned by a write", async () => {
const initial = { toolboxes: ["local"], moduleData: {} };
const canonical = { toolboxes: ["remote", "local"], moduleData: {} };
const published = [];
const queue = createPersistenceQueue(initial, (state) => published.push(state));
const success = await queue.enqueue(
(current) => ({ ...current, toolboxes: ["new", ...current.toolboxes] }),
async () => canonical
);
assert.equal(success, true);
assert.deepEqual(queue.getCurrent(), canonical);
assert.deepEqual(published.at(-1), canonical);
});
test("a storage conflict rebases pending operations on the confirmed remote state", async () => {
const initial = { value: "initial", independent: 0 };
const remote = { value: "remote", independent: 0 };
const queue = createPersistenceQueue(initial, () => {});
const conflict = queue.enqueue(
(current) => ({ ...current, value: "local" }),
async () => {
const error = new Error("conflict");
error.confirmedState = remote;
throw error;
}
);
const independent = queue.enqueue(
(current) => ({ ...current, independent: current.independent + 1 }),
async (next) => next
);
assert.deepEqual(await Promise.all([conflict, independent]), [false, true]);
assert.deepEqual(queue.getCurrent(), { value: "remote", independent: 1 });
});