fix(server): keep existing notebooks manageable and pruning inside notebooks/ (#9)

Reading, saving, renaming and deleting applied the create-time name rules
to existing files, so notebooks named outside those rules (umlauts,
brackets; written by git or Claude) were listed but returned 400. Name rules
now apply to new paths only; existing paths keep the traversal/symlink checks.

Empty-folder pruning compared a realpath against an unresolved root. With a
symlinked workspace path it walked past notebooks/ and removed the
notebooks folder itself. It now uses the resolved root and stops at it.
This commit is contained in:
Florian Schmidt
2026-11-16 10:19:02 +01:00
parent 911387264b
commit 2e907b75f2
3 changed files with 47 additions and 8 deletions
+38
View File
@@ -264,3 +264,41 @@ it("does not overwrite concurrent creates", async () => {
await app.close();
}
});
it("still opens and saves existing notebooks whose names the create rules would reject", async () => {
const { app, dir } = await setup();
try {
const { writeFile } = await import("node:fs/promises");
// Written by git or Claude, not through the create API.
await writeFile(join(dir, "notebooks", "Übersicht (alt).md"), "# Old\n");
const name = encodeURIComponent("Übersicht (alt).md");
const read = await app.inject(`/api/notebooks/${name}`);
expect(read.statusCode).toBe(200);
const save = await app.inject({ method: "PUT", url: `/api/notebooks/${name}`, payload: { source: "# New\n", baseVersion: read.json().version } });
expect(save.statusCode).toBe(200);
const moved = await app.inject({ method: "POST", url: `/api/notebooks/${name}/rename`, payload: { path: "uebersicht.md", baseVersion: save.json().version } });
expect(moved.statusCode).toBe(200);
expect((await app.inject("/api/notebooks/uebersicht.md")).statusCode).toBe(200);
} finally {
await app.close();
}
});
it("never removes folders outside notebooks/ when the workspace path is a symlink", async () => {
const real = await mkdtemp(join(tmpdir(), "shellbook-real-"));
const linkParent = await mkdtemp(join(tmpdir(), "shellbook-link-"));
dirs.push(real, linkParent);
await mkdir(join(real, "notebooks"));
const workspace = join(linkParent, "ws");
await symlink(real, workspace, "junction");
const app = await buildApp({ config: { workspaceDir: workspace, dataDir: real }, auth: false });
try {
const created = await app.inject({ method: "POST", url: "/api/notebooks", payload: { path: "a/x.md", source: "# X\n" } });
expect(created.statusCode).toBe(200);
const deleted = await app.inject({ method: "DELETE", url: `/api/notebooks/${encodeURIComponent("a/x.md")}`, payload: { baseVersion: created.json().version } });
expect(deleted.statusCode).toBe(200);
expect((await stat(join(real, "notebooks"))).isDirectory()).toBe(true);
} finally {
await app.close();
}
});