262 lines
13 KiB
JavaScript
262 lines
13 KiB
JavaScript
"use strict";
|
|
|
|
// Tests for template alias-id resolution (audit finding: "Template alias-id
|
|
// normalization can edit/delete the WRONG template").
|
|
//
|
|
// normalizeTemplateId maps alias ids to canonical ones ("py" -> "python",
|
|
// "mc" -> "minecraft", "bot"/"discord" -> "discord-bot", "node"/"node.js" ->
|
|
// "nodejs"). Legacy data can hold BOTH spellings ("py" + "python") because
|
|
// the create routes only compared exact ids. Against such a pair:
|
|
// - POST /api/settings/templates and /store-install let a new alias member
|
|
// be created next to its canonical twin (exact-only duplicate check).
|
|
// - PUT/DELETE /api/settings/templates/:id located their target by FIRST
|
|
// alias-normalized match, so a mutation of the "py" card overwrote or
|
|
// removed the real "python" entry.
|
|
// - The client keyed templateExportMap by alias-normalized id, so the pair
|
|
// collided and edit pre-fill/export resolved to the wrong template.
|
|
// The fixes under test (pure helpers sliced out of index.js / settings.js,
|
|
// same technique as template-store-install-review.test.js):
|
|
// 1. findTemplateIdConflict — normalized ids on BOTH sides reserve alias
|
|
// ids at create time.
|
|
// 2. findTemplateIndexForMutation — raw (exact, case-insensitive) id match
|
|
// first, normalized fallback for legacy alias URLs.
|
|
// 3. findTemplateRenameCollision — exact-id rename collision guard.
|
|
// 4. Client: templateExportMap keyed/looked up by the raw id only.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const INDEX_JS = path.join(__dirname, "..", "index.js");
|
|
const SETTINGS_JS = path.join(__dirname, "..", "public", "js", "settings.js");
|
|
|
|
function endIndexOfFunction(source, startMarker) {
|
|
const start = source.indexOf(startMarker);
|
|
assert.ok(start >= 0, `marker ${startMarker} not found`);
|
|
const bodyStart = source.indexOf("{", start);
|
|
let depth = 0;
|
|
for (let i = bodyStart; i < source.length; i += 1) {
|
|
if (source[i] === "{") depth += 1;
|
|
else if (source[i] === "}") {
|
|
depth -= 1;
|
|
if (depth === 0) return i + 1;
|
|
}
|
|
}
|
|
assert.fail(`unbalanced braces after marker ${startMarker}`);
|
|
}
|
|
|
|
function sliceFunction(source, startMarker) {
|
|
const end = endIndexOfFunction(source, startMarker);
|
|
return source.slice(source.indexOf(startMarker), end);
|
|
}
|
|
|
|
// --- Server helpers (index.js) ------------------------------------------------
|
|
|
|
const {
|
|
normalizeTemplateId,
|
|
findTemplateIdConflict,
|
|
findTemplateIndexForMutation,
|
|
findTemplateRenameCollision,
|
|
} = (() => {
|
|
const source = fs.readFileSync(INDEX_JS, "utf8");
|
|
const slice = [
|
|
sliceFunction(source, "function normalizeTemplateId(tpl)"),
|
|
sliceFunction(source, "function templateIdKey(tpl)"),
|
|
sliceFunction(source, "function templateIdsEqual(a, b)"),
|
|
sliceFunction(source, "function findTemplateIdConflict(list, newId)"),
|
|
sliceFunction(source, "function findTemplateIndexForMutation(list, rawParamId)"),
|
|
sliceFunction(source, "function findTemplateRenameCollision(list, updateIdx, newId)"),
|
|
].join("\n");
|
|
return new Function(
|
|
`${slice}\nreturn { normalizeTemplateId, findTemplateIdConflict, findTemplateIndexForMutation, findTemplateRenameCollision };`
|
|
)();
|
|
})();
|
|
|
|
test("sanity: the sliced alias normalizer maps every documented alias", () => {
|
|
assert.strictEqual(normalizeTemplateId("py"), "python");
|
|
assert.strictEqual(normalizeTemplateId("mc"), "minecraft");
|
|
assert.strictEqual(normalizeTemplateId("bot"), "discord-bot");
|
|
assert.strictEqual(normalizeTemplateId("node.js"), "nodejs");
|
|
assert.strictEqual(normalizeTemplateId("python"), "python");
|
|
assert.strictEqual(normalizeTemplateId("other"), "other");
|
|
});
|
|
|
|
test("create duplicate rule rejects the alias ids of an existing template", () => {
|
|
const canonical = [{ id: "python" }, { id: "minecraft" }, { id: "discord-bot" }, { id: "nodejs" }];
|
|
// Every alias spelling of a present template must be refused...
|
|
assert.ok(findTemplateIdConflict(canonical, "py"), "py vs python");
|
|
assert.ok(findTemplateIdConflict(canonical, "Py"), "case-insensitive");
|
|
assert.ok(findTemplateIdConflict(canonical, " python "), "whitespace-tolerant");
|
|
assert.ok(findTemplateIdConflict(canonical, "mc"), "mc vs minecraft");
|
|
assert.ok(findTemplateIdConflict(canonical, "minecraft"), "exact id");
|
|
assert.ok(findTemplateIdConflict(canonical, "discord"), "discord vs discord-bot");
|
|
assert.ok(findTemplateIdConflict(canonical, "bot"), "bot vs discord-bot");
|
|
assert.ok(findTemplateIdConflict(canonical, "node"), "node vs nodejs");
|
|
assert.ok(findTemplateIdConflict(canonical, "node.js"), "node.js vs nodejs");
|
|
// ...and the reverse direction: the canonical id is refused when the ALIAS
|
|
// is the one already stored (legacy data).
|
|
const aliases = [{ id: "py" }, { id: "mc" }, { id: "bot" }, { id: "node" }];
|
|
assert.ok(findTemplateIdConflict(aliases, "python"), "python vs stored py");
|
|
assert.ok(findTemplateIdConflict(aliases, "minecraft"), "minecraft vs stored mc");
|
|
assert.ok(findTemplateIdConflict(aliases, "discord-bot"), "discord-bot vs stored bot");
|
|
assert.ok(findTemplateIdConflict(aliases, "nodejs"), "nodejs vs stored node");
|
|
});
|
|
|
|
test("create duplicate rule still admits genuinely distinct ids", () => {
|
|
const list = [{ id: "python" }, { id: "minecraft" }];
|
|
assert.strictEqual(findTemplateIdConflict(list, "python2"), null);
|
|
assert.strictEqual(findTemplateIdConflict(list, "mc-server"), null);
|
|
assert.strictEqual(findTemplateIdConflict(list, "pymc"), null);
|
|
assert.strictEqual(findTemplateIdConflict(list, "discord-bot-xl"), null);
|
|
assert.strictEqual(findTemplateIdConflict([], "python"), null);
|
|
assert.strictEqual(findTemplateIdConflict(list, ""), null);
|
|
assert.strictEqual(findTemplateIdConflict(list, null), null);
|
|
});
|
|
|
|
// The reporter's exact simulation: list [{id:"minecraft"},{id:"python"},{id:"py"}]
|
|
// — with first-normalized-match targeting, a PUT or DELETE of the "py" card
|
|
// resolved to "python" and hit the REAL python entry.
|
|
test("mutation of a legacy alias pair hits the exact card the admin clicked", () => {
|
|
const list = [{ id: "minecraft", tag: 0 }, { id: "python", tag: 1 }, { id: "py", tag: 2 }];
|
|
|
|
// PUT simulation: overwriting the "py" target leaves python untouched.
|
|
const updated = list.map(t => ({ ...t }));
|
|
const putIdx = findTemplateIndexForMutation(updated, "py");
|
|
assert.strictEqual(putIdx, 2);
|
|
updated[putIdx] = { id: "py", tag: "edited" };
|
|
assert.strictEqual(updated[1].tag, 1, "python entry was modified");
|
|
assert.strictEqual(updated[2].tag, "edited");
|
|
|
|
// ...and targeting "python" leaves py untouched.
|
|
const updatedCanon = list.map(t => ({ ...t }));
|
|
const putCanonIdx = findTemplateIndexForMutation(updatedCanon, "python");
|
|
assert.strictEqual(putCanonIdx, 1);
|
|
updatedCanon[putCanonIdx] = { id: "python", tag: "edited" };
|
|
assert.strictEqual(updatedCanon[1].tag, "edited");
|
|
assert.strictEqual(updatedCanon[2].tag, 2, "py entry was modified");
|
|
|
|
// DELETE simulation: removing "py" leaves exactly ["minecraft", "python"].
|
|
const removed = list.map(t => ({ ...t }));
|
|
const delIdx = findTemplateIndexForMutation(removed, "py");
|
|
assert.strictEqual(delIdx, 2);
|
|
removed.splice(delIdx, 1);
|
|
assert.deepStrictEqual(removed.map(t => t.id), ["minecraft", "python"]);
|
|
|
|
// DELETE the canonical twin instead: py survives.
|
|
const removedCanon = list.map(t => ({ ...t }));
|
|
const delCanonIdx = findTemplateIndexForMutation(removedCanon, "python");
|
|
assert.strictEqual(delCanonIdx, 1);
|
|
removedCanon.splice(delCanonIdx, 1);
|
|
assert.deepStrictEqual(removedCanon.map(t => t.id), ["minecraft", "py"]);
|
|
});
|
|
|
|
test("legacy alias URLs still resolve through the normalized fallback", () => {
|
|
const list = [{ id: "minecraft" }, { id: "python" }, { id: "discord-bot" }];
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "mc"), 0);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "MC"), 0);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "py"), 1);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "bot"), 2);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "discord bot"), 2);
|
|
// No nodejs template stored: alias or not, nothing to mutate.
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "node"), -1);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "does-not-exist"), -1);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, ""), -1);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, null), -1);
|
|
assert.strictEqual(findTemplateIndexForMutation(null, "python"), -1);
|
|
});
|
|
|
|
test("exact raw match wins over the normalized fallback (case-insensitive)", () => {
|
|
// Legacy file with mixed-case ids: the named entry must be picked, not the
|
|
// first alias-class member.
|
|
const list = [{ id: "Minecraft" }, { id: "Python" }, { id: "py" }];
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "python"), 1);
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "MINECRAFT"), 0);
|
|
// "py" exists verbatim — it must win over normalizing to "python".
|
|
assert.strictEqual(findTemplateIndexForMutation(list, "py"), 2);
|
|
});
|
|
|
|
test("PUT rename collision guard rejects ids another entry already owns", () => {
|
|
const list = [{ id: "minecraft" }, { id: "python" }, { id: "py" }];
|
|
// renaming py -> python while python exists: collision (the route 400s)
|
|
assert.ok(findTemplateRenameCollision(list, 2, "python") >= 0);
|
|
// ...and the reverse rename
|
|
assert.ok(findTemplateRenameCollision(list, 1, "py") >= 0);
|
|
// the issue's case: renaming "discord" -> "discord-bot" while discord-bot exists
|
|
const discord = [{ id: "discord" }, { id: "discord-bot" }];
|
|
assert.ok(findTemplateRenameCollision(discord, 0, "discord-bot") >= 0);
|
|
// exact match is case-insensitive on the stored side too
|
|
assert.ok(findTemplateRenameCollision([{ id: "Python" }, { id: "py" }], 1, "PYTHON") >= 0);
|
|
});
|
|
|
|
test("in-place edits of a legacy pair entry are not rename collisions", () => {
|
|
const list = [{ id: "minecraft" }, { id: "python" }, { id: "py" }];
|
|
// Saving the "py" card with its id unchanged must NOT be flagged merely
|
|
// because "python" normalizes to the same alias class — the guard is exact.
|
|
assert.strictEqual(findTemplateRenameCollision(list, 2, "py"), -1);
|
|
assert.strictEqual(findTemplateRenameCollision(list, 1, "python"), -1);
|
|
assert.strictEqual(findTemplateRenameCollision(list, 2, "PY"), -1);
|
|
// A changed id no other entry owns passes the guard (the route's own
|
|
// alias-class guard decides whether the rename is even allowed).
|
|
assert.strictEqual(findTemplateRenameCollision(list, 2, "py2"), -1);
|
|
// Empty id never collides (the route's id regex rejects it separately).
|
|
assert.strictEqual(findTemplateRenameCollision(list, 2, ""), -1);
|
|
assert.strictEqual(findTemplateRenameCollision(list, 2, null), -1);
|
|
});
|
|
|
|
// --- Client (public/js/settings.js) --------------------------------------------
|
|
|
|
test("client export/edit map resolves each card of a legacy alias pair to itself", () => {
|
|
const source = fs.readFileSync(SETTINGS_JS, "utf8");
|
|
// Evaluate the sliced findTemplateForEditor with templateExportMap in scope:
|
|
// invoking this wrapper binds the map, the returned function takes the id.
|
|
const bindMap = new Function(
|
|
"templateExportMap",
|
|
`${sliceFunction(source, "function findTemplateForEditor")}\nreturn findTemplateForEditor;`
|
|
);
|
|
|
|
// Key the map exactly the way renderTemplates does: RAW trimmed/lowercased
|
|
// id (never the alias-normalized id, which made "py" and "python" cards
|
|
// overwrite each other).
|
|
const templateExportMap = new Map();
|
|
const templates = [{ id: "minecraft" }, { id: "python" }, { id: "py" }];
|
|
templates.forEach(tpl => {
|
|
const rawTemplateId = String(tpl?.id || '').trim().toLowerCase();
|
|
if (rawTemplateId) templateExportMap.set(rawTemplateId, tpl);
|
|
});
|
|
const findTemplateForEditor = bindMap(templateExportMap);
|
|
|
|
// The click handlers pass the card's raw data-id — the lookups below mirror
|
|
// openTemplateEditModal(...) and exportTemplate(...).
|
|
assert.strictEqual(findTemplateForEditor("py").id, "py");
|
|
assert.strictEqual(findTemplateForEditor("python").id, "python");
|
|
assert.strictEqual(findTemplateForEditor("PY").id, "py");
|
|
assert.strictEqual(findTemplateForEditor("minecraft").id, "minecraft");
|
|
assert.strictEqual(findTemplateForEditor("does-not-exist"), null);
|
|
assert.strictEqual(findTemplateForEditor(""), null);
|
|
assert.strictEqual(findTemplateForEditor(null), null);
|
|
});
|
|
|
|
test("client map keying and lookups stay raw-id based (regression pin)", () => {
|
|
const source = fs.readFileSync(SETTINGS_JS, "utf8");
|
|
const renderSlice = sliceFunction(source, "function renderTemplates");
|
|
assert.ok(
|
|
renderSlice.includes("templateExportMap.set(rawTemplateId"),
|
|
"renderTemplates must key templateExportMap by the raw template id"
|
|
);
|
|
assert.ok(
|
|
!/\btemplateExportMap\.set\(\s*normalized/.test(renderSlice),
|
|
"renderTemplates must not key templateExportMap by an alias-normalized id"
|
|
);
|
|
|
|
const exportSlice = sliceFunction(source, "function exportTemplate");
|
|
assert.ok(
|
|
exportSlice.includes("templateExportMap.get(rawId)"),
|
|
"exportTemplate must look the template up by the raw id key"
|
|
);
|
|
assert.ok(
|
|
!/templateExportMap\.get\(\s*normalized/.test(exportSlice),
|
|
"exportTemplate must not look the template up by an alias-normalized id"
|
|
);
|
|
});
|