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);
|
||||
|
||||
Reference in New Issue
Block a user