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:
@@ -1,4 +1,4 @@
|
||||
import { atomicNotebookWrite, notebookDestination, notebookRoot, notebookManagement } from "./notebooks";
|
||||
import { atomicNotebookWrite, notebookRoot, notebookManagement } from "./notebooks";
|
||||
import { createHash } from "node:crypto";
|
||||
import { readFile, readdir, realpath, lstat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
@@ -35,7 +35,8 @@ export async function safeFile(root: string, name: string): Promise<string> {
|
||||
}
|
||||
|
||||
export async function readNotebook(config: ServerConfig, name: string) {
|
||||
await notebookDestination(config, name);
|
||||
// Name rules apply to creating notebooks only; existing files (git, Claude) stay readable.
|
||||
await notebookRoot(config);
|
||||
if (!name.endsWith(".md")) throw httpError(400, "Only Markdown notebooks are allowed");
|
||||
const file = await safeFile(path.join(config.workspaceDir, "notebooks"), name);
|
||||
return { file, source: await readFile(file, "utf8") };
|
||||
|
||||
@@ -110,8 +110,9 @@ export async function atomicNotebookWrite(
|
||||
}
|
||||
}
|
||||
async function prune(config: ServerConfig, file: string) {
|
||||
const root = path.resolve(config.workspaceDir, "notebooks");
|
||||
for (let dir = path.dirname(file); dir !== root; dir = path.dirname(dir)) {
|
||||
// Same resolved root as the file path; stop at it, never walk above it.
|
||||
const root = await notebookRoot(config);
|
||||
for (let dir = path.dirname(file); dir.startsWith(root + path.sep); dir = path.dirname(dir)) {
|
||||
try {
|
||||
await rmdir(dir);
|
||||
} catch (e) {
|
||||
@@ -146,8 +147,8 @@ export function notebookManagement(
|
||||
queue.run("notebooks", async () => {
|
||||
if (!req.body || typeof req.body.baseVersion !== "string")
|
||||
throw httpError(400, "Missing baseVersion");
|
||||
const original = await notebookDestination(config, req.params.path);
|
||||
const { source } = await readNotebook(config, req.params.path);
|
||||
// Only the new path must satisfy the create rules; the source may predate them.
|
||||
const { file: original, source } = await readNotebook(config, req.params.path);
|
||||
if (version(source) !== req.body.baseVersion)
|
||||
throw httpError(409, "Notebook changed");
|
||||
const target = await notebookDestination(config, req.body.path);
|
||||
@@ -171,8 +172,7 @@ export function notebookManagement(
|
||||
queue.run("notebooks", async () => {
|
||||
if (!req.body || typeof req.body.baseVersion !== "string")
|
||||
throw httpError(400, "Missing baseVersion");
|
||||
const file = await notebookDestination(config, req.params.path);
|
||||
const { source } = await readNotebook(config, req.params.path);
|
||||
const { file, source } = await readNotebook(config, req.params.path);
|
||||
if (version(source) !== req.body.baseVersion)
|
||||
throw httpError(409, "Notebook changed");
|
||||
await unlink(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();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user